cfile

package module
v0.0.0-...-285d4ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2017 License: MIT Imports: 5 Imported by: 2

README

cfile GoDoc Build Status Coverage

Concurrent File Reader/Appender/Writer over a single fd.

Features

  • Concurrent multiple readers.

  • Append while concurrently reading.

  • Only 1 system fd per file.

Example

f, err := cfile.New("something.file", 0644)
handle(err)
defer f.Close()

for i := 0; i < 10; i++ {
	go func() {
		r := f.Reader()
		defer r.Close()
		// use r
	}()
}

a := f.Appender()
defer a.Close()

a.Write([]byte("hello world"))

License

This project is released under the MIT. See LICENCE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Appender

type Appender struct {
	// contains filtered or unexported fields
}

func (*Appender) Close

func (a *Appender) Close() (err error)

func (*Appender) Sync

func (a *Appender) Sync() (err error)

func (*Appender) Write

func (a *Appender) Write(b []byte) (n int, err error)

type File

type File struct {
	SyncAfterWriterClose bool // if set to true, calling `Writer.Close()`, will call `*os.File.Sync()`.
	// contains filtered or unexported fields
}

File is an `*os.File` wrapper that allows multiple readers or one writer or appender on a single file descriptor.

func FromFile

func FromFile(in *os.File) (f *File, err error)

FromFile returns a `*File` from an `*os.File`. Using `f.Writer` requires that the file to *not* be opened with os.O_APPEND. On success, the returned `*File` will handle closing the `*os.File`.

func New

func New(fp string, perm os.FileMode) (*File, error)

New opens `fp“ with `os.O_CREATE|os.O_RDWR“ and the given permissions.

func TempFile

func TempFile(dir, prefix string) (*File, error)

TempFile is a convenience wrapper for `ioutil.TempFile`. Remember to clean up by calling `os.Remove(f.Name())` when you're done.

func (*File) Appender

func (f *File) Appender() *Appender

Appender returns an `*Appender` that can be used with any active Readers.

func (*File) Close

func (f *File) Close() error

Close waits for all the active readers/writer to finish before closing the underlying `*os.File`.

func (*File) Fd

func (f *File) Fd() int

Fd returns the underlying `*os.File`'s file descriptor.

func (*File) ForceClose

func (f *File) ForceClose() error

ForceClose will close the underlying `*os.File` without waiting for any active readers/writer.

func (*File) Name

func (f *File) Name() string

Name returns the name of the file.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

ReadAt implements `io.Read`. Note that it will always start at offset 0.

func (*File) ReadAt

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements `io.ReaderAt`.

func (*File) ReadFrom

func (f *File) ReadFrom(rd io.Reader) (n int64, err error)

ReadFrom implements `io.ReaderFrom`. Appends to the end of the file.

func (*File) Reader

func (f *File) Reader() *Reader

Reader returns f.SectionReader(0, -1).

func (*File) ReaderAt

func (f *File) ReaderAt(off int64) *Reader

ReaderAt returns f.SectionReader(off, -1).

func (*File) SectionReader

func (f *File) SectionReader(off, n int64) *Reader

SectionReader returns a SectionReader that reads from f starting at offset off and stops with EOF after n bytes. if n - 1, it will read the entire file.

func (*File) Size

func (f *File) Size() int64

Size returns the current file size. the size is cached after each writer is closed, so it doesn't call Stat().

func (*File) Stat

func (f *File) Stat() (fi os.FileInfo, err error)

Stat calls the underlying `*os.File.Stat()`. Will block if there are any active appenders or writers.

func (*File) Truncate

func (f *File) Truncate(sz int64) (err error)

Truncate truncates the underlying `*os.File` to the specific size.

func (*File) With

func (f *File) With(fn func(*os.File) error) (err error)

With acquires a write lock and calls fn with the underlying `*os.File` and returns any errors it returns.

func (*File) Write

func (f *File) Write(b []byte) (n int, err error)

Write wraps `f.Appender().Write` for convenience. If expecting multiple `Write` calls, use `f.Appender()“ directly.

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

WriteAt implements `io.WriterAt`.

func (*File) WriteTo

func (f *File) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements `io.WriterTo`.

func (*File) Writer

func (f *File) Writer() *Writer

Writer returns f.WriteAt(-1).

func (*File) WriterAt

func (f *File) WriterAt(off int64) *Writer

WriterAt acquires a write-lock, seeks to the given offset and returns a writer. if off is < 0, it seeks to the end of the file, otherwise it seeks to the off value.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader implements `io.Reader`, `io.ReaderAt`, `io.Seeker` and `io.Closer`.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the parent's read-lock.

func (*Reader) Read

func (r *Reader) Read(b []byte) (int, error)

Read implements `io.Read`.

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, off int64) (int, error)

ReadAt implements `io.ReaderAt`.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek implements `io.Seeker`.

func (*Reader) Size

func (r *Reader) Size() int64

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements `io.Writer`, `io.WriterAt`, `io.Seeker“ and `io.Closer`.

func (*Writer) Close

func (w *Writer) Close() (err error)

Close releases the parent's write-lock.

func (*Writer) Seek

func (w *Writer) Seek(offset int64, whence int) (n int64, err error)

Seek implements `io.Seeker`.

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write implements `io.Writer`.

func (*Writer) WriteAt

func (w *Writer) WriteAt(b []byte, off int64) (n int, err error)

WriteAt implements `io.WriterAt`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL