Skip to content

std/os

import "github.com/eugenioenko/goalchemy/std/os"

Package os reads and writes whole files on the host file system. Errors are *PathError values whose Err matches ErrNotExist, ErrExist, ErrPermission or errors.ErrUnsupported through errors.Is, as in Go. Hosts without a file system, such as browsers, fail every call with an error matching errors.ErrUnsupported.

const (
O_RDONLY int = os.RDONLY
O_WRONLY int = os.WRONLY
O_RDWR int = os.RDWR
O_APPEND int = os.APPEND
O_CREATE int = os.CREATE
O_EXCL int = os.EXCL
O_TRUNC int = os.TRUNC
)

Flags to OpenFile, combined with bitwise or. Exactly one of O_RDONLY, O_WRONLY and O_RDWR selects the access mode.

const MaxFileBytes = os.MaxFileBytes

MaxFileBytes is the largest file ReadFile returns and WriteFile accepts.

var (
ErrInvalid = errors.New("invalid argument")
ErrPermission = errors.New("permission denied")
ErrExist = errors.New("file already exists")
ErrNotExist = errors.New("file does not exist")
)

Portable analogs of the io/fs errors.

var ErrClosed = errors.New("file already closed")

ErrClosed is the error of an operation on a closed File, as in Go.

func ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents. A directory fails with op “read”, as on Linux; other failures use op “open”.

func Remove(name string) error

Remove removes the named file or empty directory.

func Rename(oldpath, newpath string) error

Rename renames oldpath to newpath, replacing a file at newpath. Failures are *LinkError values.

func WriteFile(name string, data []byte, perm FileMode) error

WriteFile writes data to the named file, creating it if necessary. If the file does not exist, WriteFile creates it with permissions perm (before the umask); otherwise WriteFile truncates it before writing, without changing permissions.

type File struct {
}

File is an open host file. Its methods implement io.Reader, io.ReaderAt, io.Writer, io.Seeker, io.StringWriter and io.Closer. A nil *File fails every method with ErrInvalid; a closed one with ErrClosed.

func Create(name string) (*File, error)

Create creates or truncates the named file for reading and writing with mode 0o666 before the umask.

func CreateTemp(dir, pattern string) (*File, error)

CreateTemp creates a new file in dir, or in the host’s temporary directory when dir is empty, opened for reading and writing with mode 0o600. Its base name is pattern with the last “*” replaced by a random string, or pattern followed by one. The caller removes the file.

func Open(name string) (*File, error)

Open opens the named file for reading.

func OpenFile(name string, flag int, perm FileMode) (*File, error)

OpenFile opens the named file with the given flags and, when it creates the file, perm before the umask. Failures are *PathError values with op “open”.

func (f *File) Close() error

Close closes the file. A second Close fails with ErrClosed.

func (f *File) Name() string

Name returns the name the file was opened with.

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

Read reads up to len(b) bytes into b and returns the count. At the end of the file it returns 0, io.EOF.

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

ReadAt reads len(b) bytes starting at offset off, without moving the file’s offset. It returns io.EOF with the count when the file ends first.

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write relative to the start (io.SeekStart), the current offset (io.SeekCurrent) or the end (io.SeekEnd), and returns the new offset.

func (f *File) Stat() (FileInfo, error)

Stat describes the file by its name.

func (f *File) Sync() error

Sync commits the file’s written data to the storage device.

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

Write writes b at the file’s offset, or at its end when it was opened with O_APPEND, and returns the bytes written. A short write without a host failure returns io.ErrShortWrite.

func (f *File) WriteString(s string) (int, error)

WriteString writes s.

type FileInfo interface {
Name() string
Size() int64
Mode() FileMode
IsDir() bool
}

A FileInfo describes a file, as returned by Stat. Modification times are not reported.

func Stat(name string) (FileInfo, error)

Stat describes the named file. Failures are *PathError values with op “stat”.

type FileMode uint32

A FileMode holds permission bits; only the low nine bits are applied.

const (
ModeDir FileMode = 1 << 31
ModePerm FileMode = 0o777
)

ModeDir marks a directory in a FileMode; ModePerm selects its permission bits. Other Go mode bits are not reported.

func (m FileMode) IsDir() bool

IsDir reports whether m describes a directory.

func (m FileMode) IsRegular() bool

IsRegular reports whether m describes a regular file.

func (m FileMode) Perm() FileMode

Perm returns the permission bits of m.

type LinkError struct {
Op string
Old string
New string
Err error
}

LinkError records an error during a Rename and the paths that caused it.

func (e *LinkError) Error() string
func (e *LinkError) Unwrap() error
type PathError struct {
Op string
Path string
Err error
}

PathError records an error and the operation and file path that caused it.

func (e *PathError) Error() string
func (e *PathError) Unwrap() error