basicfile

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NormalMode os.FileMode = 0644
	DirMode    os.FileMode = 0755
)

Variables

View Source
var (
	ErrNoAlloc          = NewGoFileError("memory allocation failure", "", ErrInvalid)
	ErrNotImplemented   = NewGoFileError("feature not implemented", "", ErrInvalid)
	ErrFileLocked       = NewGoFileError("file locked", "", ErrClosed)
	ErrExist            = NewGoFileError("", "", fs.ErrExist)
	ErrNotExist         = NewGoFileError("", "", fs.ErrNotExist)
	ErrPermission       = NewGoFileError("", "", fs.ErrPermission)
	ErrClosed           = NewGoFileError("", "", fs.ErrClosed)
	ErrInvalid          = NewGoFileError("", "", fs.ErrInvalid)
	ErrNoDeadline       = NewGoFileError("", "", os.ErrNoDeadline)
	ErrDeadlineExceeded = NewGoFileError("", "", os.ErrDeadlineExceeded)
	ErrProcessDone      = NewGoFileError("", "", os.ErrProcessDone)
	ErrClosedPipe       = NewGoFileError("", "", io.ErrClosedPipe)
	ErrNoProgress       = NewGoFileError("", "", io.ErrNoProgress)
	ErrShortBuffer      = NewGoFileError("", "", io.ErrShortBuffer)
	ErrShortWrite       = NewGoFileError("", "", io.ErrShortWrite)
	ErrUnexpectedEOF    = NewGoFileError("", "", io.ErrUnexpectedEOF)
	ErrBadPattern       = NewGoFileError("", "", filepath.ErrBadPattern)
)

Portable analogs of some common errors.

Errors returned from this package may be tested against these errors with errors.Is.

View Source
var (
	Err = log.Err
)
View Source
var NewSyscallError = os.NewSyscallError

NewSyscallError returns, as an error, a new SyscallError with the given system call name and error details.

As a convenience, if err is nil, NewSyscallError returns nil.

View Source
var SameFile = os.SameFile

SameFile reports whether fi1 and fi2 describe the same file. For example, on Unix this means that the device and inode fields of the two underlying structures are identical; on other systems the decision may be based on the path names. SameFile only applies to results returned by this package gofile It returns false in other cases.

Functions

func CreateSafe

func CreateSafe(name string) (io.ReadWriteCloser, error)

CreateSafe creates the named file and returns an opened file as io.ReadWriteCloser.

If the file already exists, an error is returned. If the file does not exist, it is created with mode 0644 (before umask). If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.

If there is an error, it will be of type *PathError.

func Exists

func Exists(filename string) bool

func FileMode

func FileMode(file string) os.FileMode

FileMode returns the filemode of file. If an error occurs, it is logged and 0 is returned.

func NotExists

func NotExists(filename string) bool

func PWD

func PWD() string

PWD returns the current working directory.

If an error is encountered, "" is returned and the error is logged.

func RegularFileInfo

func RegularFileInfo(filename string) os.FileInfo

RegularFileInfo returns file information (after symlink evaluation and path cleaning) using os.Stat().

If the file does not exist, is a directory, is not a regular file, or if the user lacks adequate permissions, an error is logged and nil is returned.

It is a convenience wrapper for os.Stat that traps and processes errors that may occur using the the ErrorLogger package.

Errors are logged if Err is active.

func Stat

func Stat(filename string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

It is a convenience wrapper for os.Stat that traps and processes errors that may occur using the the ErrorLogger package.

Errors are logged if Err is active.

Types

type BasicFile

type BasicFile interface {

	// Dirty sets isDirty to true, forcing any
	// cached values to be recalculated.
	Dirty()
	// contains filtered or unexported methods
}

A BasicFile provides access to a single file as an in memory buffer.

The BasicFile interface is the minimum implementation required of the file and may be extended to specific file types. (e.g. CSV, JSON, Esri Shapefile, config files, etc.)

It may also be implemented as an abstract "file" interface that provides access to a single file that is too large to fit in memory at once.

An implementation for large files should include a way to cache one section at a time, perhaps using a maxAlloc value or a mutex of file sections.

Caching write requests will likely be the bottleneck and collecting multiple write requests and then writing the results of the most recent or most active areas of the file may be effective. However, performance profiling and some research into whether a database is more efficient is warranted.

It could also be implemented as a way to access a database, API, buffer, or other storage.

A file may implement additional interfaces, such as ReadDirFile, ReaderAt, or Seeker, to provide additional or optimized functionality.

Reference: standard library fs.go

func Create

func Create(name string) (BasicFile, error)

Create creates or truncates the named file and returns an opened file as io.ReadWriteCloser.

If the file already exists, it is truncated. If the file does not exist, it is created with mode 0644 (before umask). If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.

If there is an error, it will be of type *PathError.

func NewBasicFile

func NewBasicFile(filename string) (BasicFile, error)

func Open

func Open(name string) (BasicFile, error)

Open opens the named file for reading as an in memory object. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *os.PathError.

type Closer

type Closer interface {
	Close() error
}

Reference types copied from io package

type DataFile

type DataFile = BasicFile // todo - finish DataFile implementation

type DirEntry

type DirEntry = fs.DirEntry

A DirEntry is an entry read from a directory (using the ReadDir function or a ReadDirFile's ReadDir method).

 type DirEntry interface {
 	// Name returns the name of the file (or
 	// subdirectory) described by the entry.
 	// This name is only the final element of
 	// the path (the base name), not the entire
 	// path.
 	// For example, Name would return "hello.go"
 	// not "home/gopher/hello.go".
 	Name() string

	// IsDir reports whether the entry describes
 	// a directory.
 	IsDir() bool

 	// Type returns the type bits for the entry.
 	// The type bits are a subset of the usual
 	// FileMode bits, those returned by the
 	// FileMode.Type method.
 	Type() fs.FileMode

 	// Info returns the FileInfo for the file or
	// subdirectory described by the entry.
 	// The returned FileInfo may be from the
 	// time of the original directory read
 	// or from the time of the call to Info.
 	// If the file has been removed or renamed
 	// since the directory read, Info may return
 	// an error satisfying errors.Is(err, ErrNotExist).
 	// If the entry denotes a symbolic link, Info
 	// reports the information about the link itself,
 	// not the link's target.
 	Info() (FileInfo, error)
 }

type Errer

type Errer interface {
	Error() string
	Unwrap() error
	Timeout() bool
}

Errer implements the common error methods associated with file and system errors.

func NewPathError

func NewPathError(op, path string, err error) Errer

NewPathError returns, as an error, a new PathError with the given operation and file path that caused it. The error will be of type *os.PathError As a convenience, if err is nil, NewPathError returns nil.

type FS

type FS = fs.FS

An FS provides access to a hierarchical file system.

The FS interface is the minimum implementation required of the file system. A file system may implement additional interfaces, such as ReadFileFS, to provide additional or optimized functionality.

type FS interface {
	// Open opens the named file.
	//
	// When Open returns an error, it should be of type *PathError
	// with the Op field set to "open", the Path field set to name,
	// and the Err field describing the problem.
	//
	// Open should reject attempts to open names that do not satisfy
	// ValidPath(name), returning a *PathError with Err set to
	// ErrInvalid or ErrNotExist.
	Open(name string) (File, error)
}

Reference: standard library fs.go

type FileInfo

type FileInfo = fs.FileInfo

A FileInfo describes a file and is returned by Stat.

type FileInfo interface {
	Name() string       // base name of the file
	Size() int64        // length in bytes for regular files; system-dependent for others
	Mode() FileMode     // file mode bits
	ModTime() time.Time // modification time
	IsDir() bool        // abbreviation for Mode().IsDir()
	Sys() interface{}   // underlying data source (can return nil)
}

Reference: standard library fs.go

type FileOps

type FileOps interface {
	Abs() string
	Base() string
	Dir() string
	Ext() string
	Split() (dir, file string)

	Chmod(mode os.FileMode) error
	Chown(uid int, gid int) error
	Move(newpath string) error
	Sync() error

	SetDeadline(t time.Time) error
	SetReadDeadline(t time.Time) error
	SetWriteDeadline(t time.Time) error

	SyscallConn() (syscall.RawConn, error)
}

FileOps implements common file operations of *os.File.

type FileUnix

type FileUnix interface {
	Fd() uintptr
	Link(newname string) error
	Readlink() (string, error)
	Remove() error
	Symlink(newname string) error
	Truncate(size int64) error
}

FileUnix implements additional common file operations (complementing FileOps) for Unix files.

type GoDir

type GoDir interface {

	// Readdir(count int) ([]os.FileInfo, error)
	ReadDir(n int) ([]fs.DirEntry, error)

	Chdir() error

	Readdirnames(dir string) (n int, err error)
	// contains filtered or unexported methods
}

Reference types copied from io package

type GoFile

type GoFile interface {
	fs.DirEntry
	fs.FileInfo
	fs.FileMode

	// OsFile returns the file descriptor, *os.File.
	OsFile() *os.File

	// Handle returns a buffered io.ReadWriteCloser
	// (with io.WriteString, io.WriterTo, and io.ReaderFrom)
	Handle() Handle

	// A DirEntry is an entry read from a directory
	// (using the ReadDir function or a ReadDirFile's
	// ReadDir method).
	DirEntry() DirEntry

	// FileOps provides access to common file
	// operations of *os.File.
	FileOps() FileOps

	// FileUnix provides access to Unix file operations.
	FileUnix() FileUnix
	// contains filtered or unexported methods
}

type GoFileError

type GoFileError struct {
	Op   string
	Path string
	Err  error
}

GoFileError records an error from a specific GoFile function call. If there is a relevant path, the path will be set and the error will be of type *os.PathError

func NewGoFileError

func NewGoFileError(op, path string, err error) *GoFileError

NewGoFileError returns, as an error, a new GoFileError which implements the gofile.Errer interface.

If there is a relevant path given, the path will be set and the error will wrap *os.PathError.

As a convenience, if err is nil, NewGoFileError returns nil.

func SetError

func SetError(op, path string, err GoFileError) GoFileError

func (*GoFileError) As

func (e *GoFileError) As(target any) bool

As finds the first error in err's chain that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method

As(interface{}) bool

such that As(target) returns true. In the latter case, the As method is responsible for setting target.

An error type might provide an As method so it can be treated as if it were a different errors type.

As panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.

func (*GoFileError) Error

func (e *GoFileError) Error() string

func (*GoFileError) Is

func (e *GoFileError) Is(target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func (*GoFileError) SetError

func (e *GoFileError) SetError(op, path string) *GoFileError

SetError sets the details of an error, Op and Path. As a convenience, a pointer to the error is returned.

func (*GoFileError) Timeout

func (e *GoFileError) Timeout() bool

Timeout reports whether this error represents a timeout.

func (*GoFileError) Unwrap

func (e *GoFileError) Unwrap() error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method. Otherwise, Unwrap returns nil.

func (*GoFileError) WithMessage

func (e *GoFileError) WithMessage(msg string) *GoFileError

WithMessage annotates err with a new message. If err is nil, WithMessage returns nil.

func (*GoFileError) Wrap

func (e *GoFileError) Wrap(message string) *GoFileError

Wrap replaces the underlying error (err) with a wrapper annotating it with a stack trace at the point Wrap is called, and the supplied message.

A pointer to the new, wrapped error is returned. If err is nil, Wrap returns nil and performs no other operations.

func (*GoFileError) Wrapf

func (e *GoFileError) Wrapf(format string, args ...interface{}) *GoFileError

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied, formatted message. If err is nil, Wrap returns nil.

type Handle

type Handle interface {
	io.ReadWriteCloser
	io.StringWriter
	RWToFrom
}

Handle returns the underlying io.ReadWriteCloser. For convenience, the following are implemented:

io.ReadWriteCloser
io.StringWriter
io.ReaderFrom
io.WriterTo

not included:

io.ReaderAt
io.WriterAt

type RWAt

type RWAt interface {
	io.ReaderAt
	io.WriterAt
}

RWAt implements io.ReaderAt and io.WriterAt

type RWToFrom

type RWToFrom interface {
	io.ReaderFrom
	io.WriterTo
}

RWToFrom implements io.ReaderFrom and io.WriterTo

type ReadDirFile

type ReadDirFile = fs.ReadDirFile

A ReadDirFile is a directory file whose entries can be read with the ReadDir method. Every directory file should implement this interface. (It is permissible for any file to implement this interface, but if so ReadDir should return an error for non-directories.)

  type ReadDirFile interface {
  	File
		// ReadDir reads the contents of the directory and returns
		// a slice of up to n DirEntry values in directory order.
		// Subsequent calls on the same file will yield further DirEntry values.
		//
		// If n > 0, ReadDir returns at most n DirEntry structures.
		// In this case, if ReadDir returns an empty slice, it will return
		// a non-nil error explaining why.
		// At the end of a directory, the error is io.EOF.
		//
		// If n <= 0, ReadDir returns all the DirEntry values from the directory
		// in a single slice. In this case, if ReadDir succeeds (reads all the way
		// to the end of the directory), it returns the slice and a nil error.
		// If it encounters an error before the end of the directory,
		// ReadDir returns the DirEntry list read until that point and a non-nil error.
  	ReadDir(n int) ([]DirEntry, error)
  }

Reference: standard library fs.go

type SyscallError

type SyscallError = os.SyscallError

SyscallError records an error from a specific system call.

type TextFile

type TextFile interface {
	BasicFile
	Text() string
	Lines() (retval []string, err error)
	Sep(c byte)
}

Jump to

Keyboard shortcuts

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