Skip to content
Snippets Groups Projects
file.go 8.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • Rob Pike's avatar
    Rob Pike committed
    // Copyright 2009 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    
    Rob Pike's avatar
    Rob Pike committed
    // The os package provides a platform-independent interface to operating
    // system functionality.  The design is Unix-like.
    
    Rob Pike's avatar
    Rob Pike committed
    package os
    
    
    import (
    	"os";
    	"syscall";
    )
    
    Russ Cox's avatar
    Russ Cox committed
    // Auxiliary information if the File describes a directory
    
    type dirInfo struct {	// TODO(r): 6g bug means this can't be private
    
    	buf	[]byte;	// buffer for directory I/O
    	nbuf	int64;	// length of buf; return value from Getdirentries
    	bufp	int64;	// location of next record in buf.
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // File represents an open file descriptor.
    type File struct {
    
    Rob Pike's avatar
    Rob Pike committed
    	fd int64;
    	name	string;
    
    	dirinfo	*dirInfo;	// nil unless directory being read
    
    Rob Pike's avatar
    Rob Pike committed
    // Fd returns the integer Unix file descriptor referencing the open file.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Fd() int64 {
    	return file.fd
    
    Rob Pike's avatar
    Rob Pike committed
    }
    
    
    Rob Pike's avatar
    Rob Pike committed
    // Name returns the name of the file as presented to Open.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Name() string {
    	return file.name
    
    Rob Pike's avatar
    Rob Pike committed
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // NewFile returns a new File with the given file descriptor and name.
    func NewFile(file int64, name string) *File {
    	if file < 0 {
    
    Rob Pike's avatar
    Rob Pike committed
    		return nil
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    	return &File{file, name, nil}
    
    Russ Cox's avatar
    Russ Cox committed
    // Stdin, Stdout, and Stderr are open Files pointing to the standard input,
    
    Rob Pike's avatar
    Rob Pike committed
    // standard output, and standard error file descriptors.
    
    Russ Cox's avatar
    Russ Cox committed
    var (
    
    Russ Cox's avatar
    Russ Cox committed
    	Stdin  = NewFile(0, "/dev/stdin");
    	Stdout = NewFile(1, "/dev/stdout");
    	Stderr = NewFile(2, "/dev/stderr");
    
    Rob Pike's avatar
    Rob Pike committed
    // Flags to Open wrapping those of the underlying system. Not all flags
    // may be implemented on a given system.
    
    Russ Cox's avatar
    Russ Cox committed
    const (
    
    Rob Pike's avatar
    Rob Pike committed
    	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_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.
    
    Rob Pike's avatar
    Rob Pike committed
    // Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.)
    
    Russ Cox's avatar
    Russ Cox committed
    // 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 Open(name string, flag int, perm int) (file *File, err *Error) {
    
    Rob Pike's avatar
    Rob Pike committed
    	r, e := syscall.Open(name, int64(flag | syscall.O_CLOEXEC), int64(perm));
    
    	if e != 0 {
    		return nil, ErrnoToError(e);
    	}
    
    	// There's a race here with fork/exec, which we are
    	// content to live with.  See ../syscall/exec.go
    	if syscall.O_CLOEXEC == 0 {	// O_CLOEXEC not supported
    		syscall.CloseOnExec(r);
    	}
    
    
    Russ Cox's avatar
    Russ Cox committed
    	return NewFile(r, name), ErrnoToError(e)
    
    Russ Cox's avatar
    Russ Cox committed
    // Close closes the File, rendering it unusable for I/O.
    
    Rob Pike's avatar
    Rob Pike committed
    // It returns an Error, if any.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Close() *Error {
    	if file == nil {
    
    Rob Pike's avatar
    Rob Pike committed
    		return EINVAL
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    	r, e := syscall.Close(file.fd);
    	file.fd = -1;  // so it can't be closed again
    
    Rob Pike's avatar
    Rob Pike committed
    	return ErrnoToError(e)
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // Read reads up to len(b) bytes from the File.
    
    Rob Pike's avatar
    Rob Pike committed
    // It returns the number of bytes read and an Error, if any.
    // EOF is signaled by a zero count with a nil Error.
    // TODO(r): Add Pread, Pwrite (maybe ReadAt, WriteAt).
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Read(b []byte) (ret int, err *Error) {
    	if file == nil {
    
    	var r, e int64;
    	if len(b) > 0 {  // because we access b[0]
    
    Russ Cox's avatar
    Russ Cox committed
    		r, e = syscall.Read(file.fd, &b[0], int64(len(b)));
    
    Russ Cox's avatar
    Russ Cox committed
    	return int(r), ErrnoToError(e)
    
    Russ Cox's avatar
    Russ Cox committed
    // Write writes len(b) bytes to the File.
    
    Rob Pike's avatar
    Rob Pike committed
    // It returns the number of bytes written and an Error, if any.
    // If the byte count differs from len(b), it usually implies an error occurred.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Write(b []byte) (ret int, err *Error) {
    	if file == nil {
    
    	var r, e int64;
    	if len(b) > 0 {  // because we access b[0]
    
    Russ Cox's avatar
    Russ Cox committed
    		r, e = syscall.Write(file.fd, &b[0], int64(len(b)));
    
    Russ Cox's avatar
    Russ Cox committed
    	return int(r), ErrnoToError(e)
    
    Russ Cox's avatar
    Russ Cox committed
    // Seek sets the offset for the next Read or Write on file to offset, interpreted
    
    Rob Pike's avatar
    Rob Pike committed
    // 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.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Seek(offset int64, whence int) (ret int64, err *Error) {
    	r, e := syscall.Seek(file.fd, offset, int64(whence));
    
    Russ Cox's avatar
    Russ Cox committed
    	if file.dirinfo != nil && r != 0 {
    
    Rob Pike's avatar
    Rob Pike committed
    // WriteString is like Write, but writes the contents of string s rather than
    // an array of bytes.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) WriteString(s string) (ret int, err *Error) {
    	if file == nil {
    
    Russ Cox's avatar
    Russ Cox committed
    	r, e := syscall.Write(file.fd, syscall.StringBytePtr(s), int64(len(s)));
    
    Russ Cox's avatar
    Russ Cox committed
    	return int(r), ErrnoToError(e)
    
    Russ Cox's avatar
    Russ Cox committed
    
    
    Russ Cox's avatar
    Russ Cox committed
    // 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 Pipe() (r *File, w *File, err *Error) {
    
    	var p [2]int64;
    
    
    	// See ../syscall/exec.go for description of lock.
    	syscall.ForkLock.RLock();
    
    Rob Pike's avatar
    Rob Pike committed
    	ret, e := syscall.Pipe(&p);
    
    Russ Cox's avatar
    Russ Cox committed
    	if e != 0 {
    
    		syscall.ForkLock.RUnlock();
    
    Russ Cox's avatar
    Russ Cox committed
    		return nil, nil, ErrnoToError(e)
    	}
    
    	syscall.CloseOnExec(p[0]);
    	syscall.CloseOnExec(p[1]);
    	syscall.ForkLock.RUnlock();
    
    
    Russ Cox's avatar
    Russ Cox committed
    	return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
    
    Russ Cox's avatar
    Russ Cox committed
    }
    
    Cary Hull's avatar
    Cary Hull committed
    
    
    Rob Pike's avatar
    Rob Pike committed
    // Mkdir creates a new directory with the specified name and permission bits.
    // It returns an error, if any.
    
    Russ Cox's avatar
    Russ Cox committed
    func Mkdir(name string, perm int) *Error {
    
    Rob Pike's avatar
    Rob Pike committed
    	r, e := syscall.Mkdir(name, int64(perm));
    
    Cary Hull's avatar
    Cary Hull committed
    	return ErrnoToError(e)
    }
    
    Rob Pike's avatar
    Rob Pike committed
    
    
    Rob Pike's avatar
    Rob Pike committed
    // Stat returns the Dir structure describing the named file. If the file
    // is a symbolic link, it returns information about the file the link
    // references.
    // It returns the Dir and an error, if any.
    
    Rob Pike's avatar
    Rob Pike committed
    func Stat(name string) (dir *Dir, err *Error) {
    	stat := new(syscall.Stat_t);
    	r, e := syscall.Stat(name, stat);
    	if e != 0 {
    		return nil, ErrnoToError(e)
    	}
    	return dirFromStat(name, new(Dir), stat), nil
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    // Stat returns the Dir structure describing file.
    
    Rob Pike's avatar
    Rob Pike committed
    // It returns the Dir and an error, if any.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Stat() (dir *Dir, err *Error) {
    
    Rob Pike's avatar
    Rob Pike committed
    	stat := new(syscall.Stat_t);
    
    Russ Cox's avatar
    Russ Cox committed
    	r, e := syscall.Fstat(file.fd, stat);
    
    Rob Pike's avatar
    Rob Pike committed
    	if e != 0 {
    		return nil, ErrnoToError(e)
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    	return dirFromStat(file.name, new(Dir), stat), nil
    
    Rob Pike's avatar
    Rob Pike committed
    }
    
    
    Rob Pike's avatar
    Rob Pike committed
    // Lstat returns the Dir structure describing the named file. If the file
    // is a symbolic link, it returns information about the link itself.
    // It returns the Dir and an error, if any.
    
    Rob Pike's avatar
    Rob Pike committed
    func Lstat(name string) (dir *Dir, err *Error) {
    	stat := new(syscall.Stat_t);
    	r, e := syscall.Lstat(name, stat);
    	if e != 0 {
    		return nil, ErrnoToError(e)
    	}
    	return dirFromStat(name, new(Dir), stat), nil
    }
    
    Rob Pike's avatar
    Rob Pike committed
    // Readdirnames has a non-portable implemenation so its code is separated into an
    // operating-system-dependent file.
    
    Russ Cox's avatar
    Russ Cox committed
    func readdirnames(file *File, count int) (names []string, err *os.Error)
    
    Rob Pike's avatar
    Rob Pike committed
    
    
    Russ Cox's avatar
    Russ Cox committed
    // Readdirnames reads the contents of the directory associated with file and
    
    Rob Pike's avatar
    Rob Pike committed
    // returns an array of up to count names, in directory order.  Subsequent
    
    Russ Cox's avatar
    Russ Cox committed
    // calls on the same file will yield further names.
    
    Rob Pike's avatar
    Rob Pike committed
    // A negative count means to read until EOF.
    // It returns the array and an Error, if any.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Readdirnames(count int) (names []string, err *os.Error) {
    	return readdirnames(file, count);
    }
    
    Russ Cox's avatar
    Russ Cox committed
    // Readdir reads the contents of the directory associated with file and
    
    Rob Pike's avatar
    Rob Pike committed
    // returns an array of up to count Dir structures, in directory order.  Subsequent
    
    Russ Cox's avatar
    Russ Cox committed
    // calls on the same file will yield further Dirs.
    
    Rob Pike's avatar
    Rob Pike committed
    // A negative count means to read until EOF.
    // It returns the array and an Error, if any.
    
    Russ Cox's avatar
    Russ Cox committed
    func (file *File) Readdir(count int) (dirs []Dir, err *os.Error) {
    	dirname := file.name;
    
    	if dirname == "" {
    		dirname = ".";
    	}
    	dirname += "/";
    
    Russ Cox's avatar
    Russ Cox committed
    	names, err1 := file.Readdirnames(count);
    
    	if err1 != nil {
    		return nil, err1
    	}
    	dirs = make([]Dir, len(names));
    	for i, filename := range names {
    		dirp, err := Stat(dirname + filename);
    		if dirp ==  nil || err != nil {
    			dirs[i].Name = filename	// rest is already zeroed out
    		} else {
    			dirs[i] = *dirp
    		}
    	}
    	return
    }