os

package
Version: v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2021 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package os implements a subset of the Go "os" package. See https://godoc.org/os for details.

Note that the current implementation is blocking. This limitation should be removed in a future version.

Package os implements a subset of the Go "os" package. See https://godoc.org/os for details.

Note that the current implementation is blocking. This limitation should be removed in a future version.

Index

Constants

View Source
const (
	PathSeparator     = '/' // OS-specific path separator
	PathListSeparator = ':' // OS-specific path list separator
)
View Source
const (
	O_RDONLY int = syscall.O_RDONLY
	O_WRONLY int = syscall.O_WRONLY
	O_RDWR   int = syscall.O_RDWR
	O_APPEND int = syscall.O_APPEND
	O_CREATE int = syscall.O_CREAT
	O_EXCL   int = syscall.O_EXCL
	O_SYNC   int = syscall.O_SYNC
	O_TRUNC  int = syscall.O_TRUNC
)
View Source
const (
	// The single letters are the abbreviations
	// used by the String method's formatting.
	ModeDir        = fs.ModeDir        // d: is a directory
	ModeAppend     = fs.ModeAppend     // a: append-only
	ModeExclusive  = fs.ModeExclusive  // l: exclusive use
	ModeTemporary  = fs.ModeTemporary  // T: temporary file; Plan 9 only
	ModeSymlink    = fs.ModeSymlink    // L: symbolic link
	ModeDevice     = fs.ModeDevice     // D: device file
	ModeNamedPipe  = fs.ModeNamedPipe  // p: named pipe (FIFO)
	ModeSocket     = fs.ModeSocket     // S: Unix domain socket
	ModeSetuid     = fs.ModeSetuid     // u: setuid
	ModeSetgid     = fs.ModeSetgid     // g: setgid
	ModeCharDevice = fs.ModeCharDevice // c: Unix character device, when ModeDevice is set
	ModeSticky     = fs.ModeSticky     // t: sticky
	ModeIrregular  = fs.ModeIrregular  // ?: non-regular file; nothing else is known about this file

	// Mask for the type bits. For regular files, none will be set.
	ModeType = fs.ModeType

	ModePerm = fs.ModePerm // Unix permission bits, 0o777
)

The defined file mode bits are the most significant bits of the FileMode. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.

Variables

View Source
var (
	ErrInvalid    = errors.New("invalid argument")
	ErrPermission = errors.New("permission denied")
	ErrClosed     = errors.New("file already closed")

	// Portable analogs of some common system call errors.
	// Note that these are exported for use in the Filesystem interface.
	ErrUnsupported    = errors.New("operation not supported")
	ErrNotImplemented = errors.New("operation not implemented")
	ErrNotExist       = errors.New("file not found")
	ErrExist          = errors.New("file exists")
)
View Source
var (
	Stdin  = &File{unixFileHandle(0), "/dev/stdin"}
	Stdout = &File{unixFileHandle(1), "/dev/stdout"}
	Stderr = &File{unixFileHandle(2), "/dev/stderr"}
)

Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.

View Source
var Args []string

Args hold the command-line arguments, starting with the program name.

Functions

func Exit

func Exit(code int)

Exit causes the current program to exit with the given status code. Conventionally, code zero indicates success, non-zero an error. The program terminates immediately; deferred functions are not run.

func Getegid

func Getegid() int

Getegid returns the numeric effective group id of the caller.

On non-POSIX systems, it returns -1.

func Getenv

func Getenv(key string) string

func Geteuid

func Geteuid() int

Geteuid returns the numeric effective user id of the caller.

On non-POSIX systems, it returns -1.

func Getgid

func Getgid() int

Getgid returns the numeric group id of the caller.

On non-POSIX systems, it returns -1.

func Getpid

func Getpid() int

Getpid returns the process id of the caller, or -1 if unavailable.

func Getppid

func Getppid() int

Getppid returns the process id of the caller's parent, or -1 if unavailable.

func Getuid

func Getuid() int

Getuid returns the numeric user id of the caller.

On non-POSIX systems, it returns -1.

func Getwd

func Getwd() (string, error)

Getwd is a stub (for now), always returning an empty string

func Hostname

func Hostname() (name string, err error)

func IsExist

func IsExist(err error) bool

func IsNotExist

func IsNotExist(err error) bool

func IsPathSeparator

func IsPathSeparator(c uint8) bool

IsPathSeparator reports whether c is a directory separator character.

func IsPermission

func IsPermission(err error) bool

func LookupEnv

func LookupEnv(key string) (string, bool)

func Mkdir

func Mkdir(path string, perm FileMode) error

Mkdir creates a directory. If the operation fails, it will return an error of type *PathError.

func Mount

func Mount(prefix string, filesystem Filesystem)

Mount mounts the given filesystem in the filesystem abstraction layer of the os package. It is not possible to unmount filesystems. Filesystems added later will override earlier filesystems.

The provided prefix must start and end with a forward slash. This is true for the root directory ("/") for example.

func NewSyscallError

func NewSyscallError(syscall string, err error) error

func ReadFile

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

ReadFile reads the named file and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

func Readlink(name string) (string, error)

Readlink is a stub (for now), always returning the string it was given

func Remove

func Remove(path string) error

Remove removes a file or (empty) directory. If the operation fails, it will return an error of type *PathError.

func TempDir

func TempDir() string

TempDir is a stub (for now), always returning the string "/tmp"

func WriteFile

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 umask); otherwise WriteFile truncates it before writing, without changing permissions.

Types

type DirEntry

type DirEntry = fs.DirEntry

func ReadDir

func ReadDir(name string) ([]DirEntry, error)

type File

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

File represents an open file descriptor.

func Create

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

Create creates the named file, overwriting it if it already exists.

func CreateTemp

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

func Open

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

Open opens the file named for reading.

func OpenFile

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

OpenFile opens the named file. If the operation fails, the returned error will be of type *PathError.

func (*File) Close

func (f *File) Close() (err error)

Close closes the File, rendering it unusable for I/O.

func (*File) Fd

func (f *File) Fd() uintptr

Fd returns the file handle referencing the open file.

func (*File) Name

func (f *File) Name() string

Name returns the name of the file with which it was opened.

func (*File) Read

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

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

func (*File) ReadAt

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

func (*File) ReadDir

func (f *File) ReadDir(n int) ([]DirEntry, error)

func (*File) Readdir

func (f *File) Readdir(n int) ([]FileInfo, error)

Readdir is a stub, not yet implemented

func (*File) Readdirnames

func (f *File) Readdirnames(n int) (names []string, err error)

Readdirnames is a stub, not yet implemented

func (*File) Seek

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

Seek is a stub, not yet implemented

func (*File) Stat

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

Stat is a stub, not yet implemented

func (*File) Sync

func (f *File) Sync() error

Sync is a stub, not yet implemented

func (*File) SyscallConn

func (f *File) SyscallConn() (syscall.RawConn, error)

func (*File) Write

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

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

type FileHandle

type FileHandle interface {
	// Read reads up to len(b) bytes from the file.
	Read(b []byte) (n int, err error)

	// Write writes up to len(b) bytes to the file.
	Write(b []byte) (n int, err error)

	// Close closes the file, making it unusable for further writes.
	Close() (err error)
}

FileHandle is an interface that should be implemented by filesystems implementing the Filesystem interface.

WARNING: this interface is not finalized and may change in a future version.

type FileInfo

type FileInfo = fs.FileInfo

func Lstat

func Lstat(name string) (FileInfo, error)

Lstat is a stub, not yet implemented

func Stat

func Stat(name string) (FileInfo, error)

Stat is a stub, not yet implemented

type FileMode

type FileMode = fs.FileMode

type Filesystem

type Filesystem interface {
	// OpenFile opens the named file.
	OpenFile(name string, flag int, perm FileMode) (FileHandle, error)

	// Mkdir creates a new directoy with the specified permission (before
	// umask). Some filesystems may not support directories or permissions.
	Mkdir(name string, perm FileMode) error

	// Remove removes the named file or (empty) directory.
	Remove(name string) error
}

Filesystem provides an interface for generic filesystem drivers mounted in the os package. The errors returned must be one of the os.Err* errors, or a custom error if one doesn't exist. It should not be a *PathError because errors will be wrapped with a *PathError by the filesystem abstraction.

WARNING: this interface is not finalized and may change in a future version.

type PathError

type PathError struct {
	Op   string
	Path string
	Err  error
}

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

func (*PathError) Error

func (e *PathError) Error() string

type Signal

type Signal interface {
	String() string
	Signal() // to distinguish from other Stringers
}

type SyscallError

type SyscallError struct {
	Syscall string
	Err     error
}

SyscallError records an error from a specific system call.

func (*SyscallError) Error

func (e *SyscallError) Error() string

func (*SyscallError) Unwrap

func (e *SyscallError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to