os

package
v0.0.0-...-90c9d3a Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2010 License: BSD-3-Clause, GooglePatentClause Imports: 4 Imported by: 0

Documentation

Overview

The os package provides a platform-independent interface to operating system functionality. The design is Unix-like.

Index

Constants

View Source
const (
	WNOHANG   = syscall.WNOHANG  // Don't wait if no process has exited.
	WSTOPPED  = syscall.WSTOPPED // If set, status of stopped subprocesses is also reported.
	WUNTRACED = WSTOPPED
	WRUSAGE   = 1 << 20 // Record resource usage.
)

Options for Wait.

View Source
const (
	O_RDONLY   = syscall.O_RDONLY   // open the file read-only.
	O_WRONLY   = syscall.O_WRONLY   // open the file write-only.
	O_RDWR     = syscall.O_RDWR     // open the file read-write.
	O_APPEND   = syscall.O_APPEND   // open the file append-only.
	O_ASYNC    = syscall.O_ASYNC    // generate a signal when I/O is available.
	O_CREAT    = syscall.O_CREAT    // create a new file if none exists.
	O_EXCL     = syscall.O_EXCL     // used with O_CREAT, file must not exist
	O_NOCTTY   = syscall.O_NOCTTY   // do not make file the controlling tty.
	O_NONBLOCK = syscall.O_NONBLOCK // open in non-blocking mode.
	O_NDELAY   = O_NONBLOCK         // synonym for O_NONBLOCK
	O_SYNC     = syscall.O_SYNC     // open for synchronous I/O.
	O_TRUNC    = syscall.O_TRUNC    // if possible, truncate file when opened.
	O_CREATE   = O_CREAT            // create a new file if none exists.
)

Flags to Open wrapping those of the underlying system. Not all flags may be implemented on a given system.

Variables

View Source
var (
	Stdin  = NewFile(0, "/dev/stdin")
	Stdout = NewFile(1, "/dev/stdout")
	Stderr = NewFile(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 // provided by runtime
View Source
var ENOENV = NewError("no such environment variable")

ENOENV is the Error indicating that an environment variable does not exist.

View Source
var Envs []string // provided by runtime

Functions

func Clearenv

func Clearenv()

Clearenv deletes all environment variables.

func Environ

func Environ() []string

Environ returns an array of strings representing the environment, in the form "key=value".

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.

func Getegid

func Getegid() int

Getegid returns the numeric effective group id of the caller.

func Getenv

func Getenv(key string) string

Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.

func Geteuid

func Geteuid() int

Geteuid returns the numeric effective user id of the caller.

func Getgid

func Getgid() int

Getgid returns the numeric group id of the caller.

func Getpagesize

func Getpagesize() int

Getpagesize returns the underlying system's memory page size.

func Getpid

func Getpid() int

Getpid returns the process id of the caller.

func Getppid

func Getppid() int

Getppid returns the process id of the caller's parent.

func Getuid

func Getuid() int

Getuid returns the numeric user id of the caller.

func Lstat

func Lstat(name string) (dir *Dir, err Error)

Lstat returns the Dir structure describing the named file and an error, if any. If the file is a symbolic link, the returned Dir describes the symbolic link. Lstat makes no attempt to follow the link.

func Open

func Open(name string, flag int, perm int) (file *File, err Error)

Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. It returns the File and an Error, if any.

func Pipe

func Pipe() (r *File, w *File, err Error)

Pipe returns a connected pair of Files; reads from r return bytes written to w. It returns the files and an Error, if any.

func Stat

func Stat(name string) (dir *Dir, err Error)

Stat returns a Dir structure describing the named file and an error, if any. If name names a valid symbolic link, the returned Dir describes the file pointed at by the link and has dir.FollowedSymlink set to true. If name names an invalid symbolic link, the returned Dir describes the link itself and has dir.FollowedSymlink set to false.

func Wait

func Wait(pid int, options int) (w *Waitmsg, err Error)

Wait waits for process pid to exit or stop, and then returns a Waitmsg describing its status and an Error, if any. The options (WNOHANG etc.) affect the behavior of the Wait call.

Types

type Dir

type Dir struct {
	Dev             uint64 // device number of file system holding file.
	Ino             uint64 // inode number.
	Nlink           uint64 // number of hard links.
	Mode            uint32 // permission and mode bits.
	Uid             uint32 // user id of owner.
	Gid             uint32 // group id of owner.
	Rdev            uint64 // device type for special file.
	Size            uint64 // length in bytes.
	Blksize         uint64 // size of blocks, in bytes.
	Blocks          uint64 // number of blocks allocated for file.
	Atime_ns        uint64 // access time; nanoseconds since epoch.
	Mtime_ns        uint64 // modified time; nanoseconds since epoch.
	Ctime_ns        uint64 // status change time; nanoseconds since epoch.
	Name            string // name of file as presented to Open.
	FollowedSymlink bool   // followed a symlink to get this information
}

A Dir describes a file and is returned by Stat, Fstat, and Lstat

func (*Dir) IsBlock

func (dir *Dir) IsBlock() bool

IsBlock reports whether the Dir describes a block special file.

func (*Dir) IsChar

func (dir *Dir) IsChar() bool

IsChar reports whether the Dir describes a character special file.

func (*Dir) IsDirectory

func (dir *Dir) IsDirectory() bool

IsDirectory reports whether the Dir describes a directory.

func (*Dir) IsFifo

func (dir *Dir) IsFifo() bool

IsFifo reports whether the Dir describes a FIFO file.

func (*Dir) IsRegular

func (dir *Dir) IsRegular() bool

IsRegular reports whether the Dir describes a regular file.

func (*Dir) IsSocket

func (dir *Dir) IsSocket() bool

IsSocket reports whether the Dir describes a socket.

func (dir *Dir) IsSymlink() bool

IsSymlink reports whether the Dir describes a symbolic link.

func (*Dir) Permission

func (dir *Dir) Permission() int

Permission returns the file permission bits.

type Errno

type Errno int64

Errno is the Unix error number. Names such as EINVAL are simple wrappers to convert the error number into an Error.

func (Errno) String

func (e Errno) String() string

type Error

type Error interface {
	String() string
}

An Error can represent any printable error condition.

var EOF Error = eofError(0)

EOF is the Error returned by Read when no more input is available. Functions should return EOF only to signal a graceful end of input. If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either io.ErrUnexpectedEOF or some other error giving more detail.

func Chdir

func Chdir(dir string) Error

Chdir changes the current working directory to the named directory.

func Chmod

func Chmod(name string, mode int) Error

Chmod changes the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target.

func Chown

func Chown(name string, uid, gid int) Error

Chown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target.

func Exec

func Exec(argv0 string, argv []string, envv []string) Error

Exec replaces the current process with an execution of the program named by argv0, with arguments argv and environment envv. If successful, Exec never returns. If it fails, it returns an Error. ForkExec is almost always a better way to execute a program.

func ForkExec

func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File) (pid int, err Error)

ForkExec forks the current process and invokes Exec with the file, arguments, and environment specified by argv0, argv, and envv. It returns the process id of the forked process and an Error, if any. The fd array specifies the file descriptors to be set up in the new process: fd[0] will be Unix file descriptor 0 (standard input), fd[1] descriptor 1, and so on. A nil entry will cause the child to have no open file descriptor with that index. If dir is not empty, the child chdirs into the directory before execing the program.

func Getenverror

func Getenverror(key string) (value string, err Error)

Getenverror retrieves the value of the environment variable named by the key. It returns the value and an error, if any.

func Getgroups

func Getgroups() ([]int, Error)

Getgroups returns a list of the numeric ids of groups that the caller belongs to.

func Getwd

func Getwd() (string, Error)

Getwd returns a rooted path name corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them.

func Hostname

func Hostname() (name string, err Error)

Hostname returns the host name reported by the kernel.

func Lchown

func Lchown(name string, uid, gid int) Error

Lchown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link itself.

func Link(oldname, newname string) Error

Link creates a hard link.

func Mkdir

func Mkdir(name string, perm int) Error

Mkdir creates a new directory with the specified name and permission bits. It returns an error, if any.

func MkdirAll

func MkdirAll(path string, perm int) Error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.

func NewError

func NewError(s string) Error

NewError converts s to an ErrorString, which satisfies the Error interface.

func NewSyscallError

func NewSyscallError(syscall string, errno int) Error

NewSyscallError returns, as an Error, a new SyscallError with the given system call name and error number. As a convenience, if errno is 0, NewSyscallError returns nil.

func Readlink(name string) (string, Error)

Readlink reads the contents of a symbolic link: the destination of the link. It returns the contents and an Error, if any.

func Remove

func Remove(name string) Error

Remove removes the named file or directory.

func RemoveAll

func RemoveAll(path string) Error

RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).

func Rename

func Rename(oldname, newname string) Error

Rename renames a file.

func Setenv

func Setenv(key, value string) Error

Setenv sets the value of the environment variable named by the key. It returns an Error, if any.

func Symlink(oldname, newname string) Error

Symlink creates a symbolic link.

func Time

func Time() (sec int64, nsec int64, err Error)

Time returns the current time, in whole seconds and fractional nanoseconds, plus an Error if any. The current time is thus 1e9*sec+nsec, in nanoseconds. The zero of time is the Unix epoch.

func Truncate

func Truncate(name string, size int64) Error

Truncate changes the size of the named file. If the file is a symbolic link, it changes the size of the link's target.

type ErrorString

type ErrorString string

A helper type that can be embedded or wrapped to simplify satisfying Error.

func (ErrorString) String

func (e ErrorString) String() string

type File

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

File represents an open file descriptor.

func NewFile

func NewFile(fd int, name string) *File

NewFile returns a new File with the given file descriptor and name.

func (*File) Chdir

func (f *File) Chdir() Error

Chdir changes the current working directory to the file, which must be a directory.

func (*File) Chmod

func (f *File) Chmod(mode int) Error

Chmod changes the mode of the file to mode.

func (*File) Chown

func (f *File) Chown(uid, gid int) Error

Chown changes the numeric uid and gid of the named file.

func (*File) Close

func (file *File) Close() Error

Close closes the File, rendering it unusable for I/O. It returns an Error, if any.

func (*File) Fd

func (file *File) Fd() int

Fd returns the integer Unix file descriptor referencing the open file.

func (*File) Name

func (file *File) Name() string

Name returns the name of the file as presented to Open.

func (*File) Read

func (file *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 an Error, if any. EOF is signaled by a zero count with err set to EOF.

func (*File) ReadAt

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

ReadAt reads len(b) bytes from the File starting at byte offset off. It returns the number of bytes read and the Error, if any. EOF is signaled by a zero count with err set to EOF. ReadAt always returns a non-nil Error when n != len(b).

func (*File) Readdir

func (file *File) Readdir(count int) (dirs []Dir, err Error)

Readdir reads the contents of the directory associated with file and returns an array of up to count Dir structures, as would be returned by Stat, in directory order. Subsequent calls on the same file will yield further Dirs. A negative count means to read until EOF. Readdir returns the array and an Error, if any.

func (*File) Readdirnames

func (file *File) Readdirnames(count int) (names []string, err Error)

func (*File) Seek

func (file *File) Seek(offset int64, whence int) (ret int64, err Error)

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an Error, if any.

func (*File) Stat

func (file *File) Stat() (dir *Dir, err Error)

Stat returns the Dir structure describing file. It returns the Dir and an error, if any.

func (*File) Truncate

func (f *File) Truncate(size int64) Error

Truncate changes the size of the file. It does not change the I/O offset.

func (*File) Write

func (file *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).

func (*File) WriteAt

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

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an Error, if any. WriteAt returns a non-nil Error when n != len(b).

func (*File) WriteString

func (file *File) WriteString(s string) (ret int, err Error)

WriteString is like Write, but writes the contents of string s rather than an array of bytes.

type LinkError

type LinkError struct {
	Op    string
	Old   string
	New   string
	Error Error
}

LinkError records an error during a link or symlink or rename system call and the paths that caused it.

func (*LinkError) String

func (e *LinkError) String() string

type PathError

type PathError struct {
	Op    string
	Path  string
	Error Error
}

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

func (*PathError) String

func (e *PathError) String() string

type SyscallError

type SyscallError struct {
	Syscall string
	Errno   Errno
}

SyscallError records an error from a specific system call.

func (*SyscallError) String

func (e *SyscallError) String() string

type Waitmsg

type Waitmsg struct {
	Pid                int             // The process's id.
	syscall.WaitStatus                 // System-dependent status info.
	Rusage             *syscall.Rusage // System-dependent resource usage info.
}

Waitmsg stores the information about an exited process as reported by Wait.

func (Waitmsg) String

func (w Waitmsg) String() string

Directories

Path Synopsis
Package signal implements operating system-independent signal handling.
Package signal implements operating system-independent signal handling.

Jump to

Keyboard shortcuts

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