1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin freebsd linux openbsd windows
13 func sigpipe() // implemented in package runtime
15 func epipecheck(file *File, e int) {
16 if e == syscall.EPIPE {
18 if file.nepipe >= 10 {
26 // Remove removes the named file or directory.
27 func Remove(name string) error {
28 // System call interface forces us to know
29 // whether name is a file or directory.
30 // Try both: it is cheaper on average than
31 // doing a Stat plus the right one.
32 e := syscall.Unlink(name)
36 e1 := syscall.Rmdir(name)
41 // Both failed: figure out which error to return.
42 // OS X and Linux differ on whether unlink(dir)
43 // returns EISDIR, so can't use that. However,
44 // both agree that rmdir(file) returns ENOTDIR,
45 // so we can use that to decide which error is real.
46 // Rmdir might also return ENOTDIR if given a bad
47 // file path, like /etc/passwd/foo, but in that case,
48 // both errors will be ENOTDIR, so it's okay to
49 // use the error from unlink.
50 // For windows syscall.ENOTDIR is set
51 // to syscall.ERROR_PATH_NOT_FOUND, hopefully it should
53 if e1 != syscall.ENOTDIR {
56 return &PathError{"remove", name, Errno(e)}
59 // LinkError records an error during a link or symlink or rename
60 // system call and the paths that caused it.
61 type LinkError struct {
68 func (e *LinkError) Error() string {
69 return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
72 // Link creates a hard link.
73 func Link(oldname, newname string) error {
74 e := syscall.Link(oldname, newname)
76 return &LinkError{"link", oldname, newname, Errno(e)}
81 // Symlink creates a symbolic link.
82 func Symlink(oldname, newname string) error {
83 e := syscall.Symlink(oldname, newname)
85 return &LinkError{"symlink", oldname, newname, Errno(e)}
90 // Readlink reads the contents of a symbolic link: the destination of
91 // the link. It returns the contents and an error, if any.
92 func Readlink(name string) (string, error) {
93 for len := 128; ; len *= 2 {
94 b := make([]byte, len)
95 n, e := syscall.Readlink(name, b)
97 return "", &PathError{"readlink", name, Errno(e)}
100 return string(b[0:n]), nil
107 // Rename renames a file.
108 func Rename(oldname, newname string) error {
109 e := syscall.Rename(oldname, newname)
111 return &LinkError{"rename", oldname, newname, Errno(e)}
116 // Chmod changes the mode of the named file to mode.
117 // If the file is a symbolic link, it changes the mode of the link's target.
118 func Chmod(name string, mode uint32) error {
119 if e := syscall.Chmod(name, mode); iserror(e) {
120 return &PathError{"chmod", name, Errno(e)}
125 // Chmod changes the mode of the file to mode.
126 func (f *File) Chmod(mode uint32) error {
127 if e := syscall.Fchmod(f.fd, mode); iserror(e) {
128 return &PathError{"chmod", f.name, Errno(e)}
133 // Chown changes the numeric uid and gid of the named file.
134 // If the file is a symbolic link, it changes the uid and gid of the link's target.
135 func Chown(name string, uid, gid int) error {
136 if e := syscall.Chown(name, uid, gid); iserror(e) {
137 return &PathError{"chown", name, Errno(e)}
142 // Lchown changes the numeric uid and gid of the named file.
143 // If the file is a symbolic link, it changes the uid and gid of the link itself.
144 func Lchown(name string, uid, gid int) error {
145 if e := syscall.Lchown(name, uid, gid); iserror(e) {
146 return &PathError{"lchown", name, Errno(e)}
151 // Chown changes the numeric uid and gid of the named file.
152 func (f *File) Chown(uid, gid int) error {
153 if e := syscall.Fchown(f.fd, uid, gid); iserror(e) {
154 return &PathError{"chown", f.name, Errno(e)}
159 // Truncate changes the size of the file.
160 // It does not change the I/O offset.
161 func (f *File) Truncate(size int64) error {
162 if e := syscall.Ftruncate(f.fd, size); iserror(e) {
163 return &PathError{"truncate", f.name, Errno(e)}
168 // Sync commits the current contents of the file to stable storage.
169 // Typically, this means flushing the file system's in-memory copy
170 // of recently written data to disk.
171 func (file *File) Sync() (err error) {
175 if e := syscall.Fsync(file.fd); iserror(e) {
176 return NewSyscallError("fsync", e)
181 // Chtimes changes the access and modification times of the named
182 // file, similar to the Unix utime() or utimes() functions.
184 // The argument times are in nanoseconds, although the underlying
185 // filesystem may truncate or round the values to a more
187 func Chtimes(name string, atime_ns int64, mtime_ns int64) error {
188 var utimes [2]syscall.Timeval
189 utimes[0] = syscall.NsecToTimeval(atime_ns)
190 utimes[1] = syscall.NsecToTimeval(mtime_ns)
191 if e := syscall.Utimes(name, utimes[0:]); iserror(e) {
192 return &PathError{"chtimes", name, Errno(e)}