Skip to content
Snippets Groups Projects
file_posix.go 5.13 KiB
Newer Older
  • Learn to ignore specific revisions
  • // 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.
    
    
    // +build darwin freebsd linux openbsd windows
    
    
    package os
    
    import (
    	"syscall"
    
    Russ Cox's avatar
    Russ Cox committed
    	"time"
    
    func sigpipe() // implemented in package runtime
    
    
    Russ Cox's avatar
    Russ Cox committed
    func epipecheck(file *File, e error) {
    
    	if e == syscall.EPIPE {
    		file.nepipe++
    		if file.nepipe >= 10 {
    
    		}
    	} else {
    		file.nepipe = 0
    	}
    }
    
    // Remove removes the named file or directory.
    
    Russ Cox's avatar
    Russ Cox committed
    func Remove(name string) error {
    
    	// System call interface forces us to know
    	// whether name is a file or directory.
    	// Try both: it is cheaper on average than
    	// doing a Stat plus the right one.
    	e := syscall.Unlink(name)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e == nil {
    
    		return nil
    	}
    	e1 := syscall.Rmdir(name)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e1 == nil {
    
    		return nil
    	}
    
    	// Both failed: figure out which error to return.
    	// OS X and Linux differ on whether unlink(dir)
    	// returns EISDIR, so can't use that.  However,
    	// both agree that rmdir(file) returns ENOTDIR,
    	// so we can use that to decide which error is real.
    	// Rmdir might also return ENOTDIR if given a bad
    	// file path, like /etc/passwd/foo, but in that case,
    	// both errors will be ENOTDIR, so it's okay to
    	// use the error from unlink.
    	// For windows syscall.ENOTDIR is set
    
    	// to syscall.ERROR_PATH_NOT_FOUND, hopefully it should
    
    	// do the trick.
    	if e1 != syscall.ENOTDIR {
    		e = e1
    	}
    
    Russ Cox's avatar
    Russ Cox committed
    	return &PathError{"remove", name, e}
    
    }
    
    // LinkError records an error during a link or symlink or rename
    // system call and the paths that caused it.
    type LinkError struct {
    
    Russ Cox's avatar
    Russ Cox committed
    	Op  string
    	Old string
    	New string
    	Err error
    
    Russ Cox's avatar
    Russ Cox committed
    func (e *LinkError) Error() string {
    	return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
    
    }
    
    // Link creates a hard link.
    
    Russ Cox's avatar
    Russ Cox committed
    func Link(oldname, newname string) error {
    
    	e := syscall.Link(oldname, newname)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e != nil {
    		return &LinkError{"link", oldname, newname, e}
    
    	}
    	return nil
    }
    
    // Symlink creates a symbolic link.
    
    Russ Cox's avatar
    Russ Cox committed
    func Symlink(oldname, newname string) error {
    
    	e := syscall.Symlink(oldname, newname)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e != nil {
    		return &LinkError{"symlink", oldname, newname, e}
    
    	}
    	return nil
    }
    
    // Readlink reads the contents of a symbolic link: the destination of
    
    Russ Cox's avatar
    Russ Cox committed
    // the link.  It returns the contents and an error, if any.
    func Readlink(name string) (string, error) {
    
    	for len := 128; ; len *= 2 {
    		b := make([]byte, len)
    		n, e := syscall.Readlink(name, b)
    
    Russ Cox's avatar
    Russ Cox committed
    		if e != nil {
    			return "", &PathError{"readlink", name, e}
    
    		}
    		if n < len {
    			return string(b[0:n]), nil
    		}
    	}
    	// Silence 6g.
    	return "", nil
    }
    
    // Rename renames a file.
    
    Russ Cox's avatar
    Russ Cox committed
    func Rename(oldname, newname string) error {
    
    	e := syscall.Rename(oldname, newname)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e != nil {
    		return &LinkError{"rename", oldname, newname, e}
    
    	}
    	return nil
    }
    
    // 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.
    
    Russ Cox's avatar
    Russ Cox committed
    func Chmod(name string, mode uint32) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Chmod(name, mode); e != nil {
    		return &PathError{"chmod", name, e}
    
    	}
    	return nil
    }
    
    // Chmod changes the mode of the file to mode.
    
    Russ Cox's avatar
    Russ Cox committed
    func (f *File) Chmod(mode uint32) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Fchmod(f.fd, mode); e != nil {
    		return &PathError{"chmod", f.name, e}
    
    	}
    	return nil
    }
    
    // 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.
    
    Russ Cox's avatar
    Russ Cox committed
    func Chown(name string, uid, gid int) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Chown(name, uid, gid); e != nil {
    		return &PathError{"chown", name, e}
    
    	}
    	return nil
    }
    
    // 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.
    
    Russ Cox's avatar
    Russ Cox committed
    func Lchown(name string, uid, gid int) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Lchown(name, uid, gid); e != nil {
    		return &PathError{"lchown", name, e}
    
    	}
    	return nil
    }
    
    // Chown changes the numeric uid and gid of the named file.
    
    Russ Cox's avatar
    Russ Cox committed
    func (f *File) Chown(uid, gid int) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Fchown(f.fd, uid, gid); e != nil {
    		return &PathError{"chown", f.name, e}
    
    	}
    	return nil
    }
    
    // Truncate changes the size of the file.
    // It does not change the I/O offset.
    
    Russ Cox's avatar
    Russ Cox committed
    func (f *File) Truncate(size int64) error {
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Ftruncate(f.fd, size); e != nil {
    		return &PathError{"truncate", f.name, e}
    
    	}
    	return nil
    }
    
    // Sync commits the current contents of the file to stable storage.
    // Typically, this means flushing the file system's in-memory copy
    // of recently written data to disk.
    
    func (f *File) Sync() (err error) {
    	if f == nil {
    
    		return EINVAL
    	}
    
    	if e := syscall.Fsync(f.fd); e != nil {
    
    		return NewSyscallError("fsync", e)
    	}
    	return nil
    }
    
    // Chtimes changes the access and modification times of the named
    // file, similar to the Unix utime() or utimes() functions.
    //
    
    Russ Cox's avatar
    Russ Cox committed
    // The underlying filesystem may truncate or round the values to a
    // less precise time unit.
    func Chtimes(name string, atime time.Time, mtime time.Time) error {
    
    	var utimes [2]syscall.Timeval
    
    Russ Cox's avatar
    Russ Cox committed
    	atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())
    	mtime_ns := mtime.Unix()*1e9 + int64(mtime.Nanosecond())
    
    	utimes[0] = syscall.NsecToTimeval(atime_ns)
    	utimes[1] = syscall.NsecToTimeval(mtime_ns)
    
    Russ Cox's avatar
    Russ Cox committed
    	if e := syscall.Utimes(name, utimes[0:]); e != nil {
    		return &PathError{"chtimes", name, e}
    
    	}
    	return nil
    }