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 netbsd openbsd windows
14 func sigpipe() // implemented in package runtime
16 func epipecheck(file *File, e error) {
17 if e == syscall.EPIPE {
19 if file.nepipe >= 10 {
27 // LinkError records an error during a link or symlink or rename
28 // system call and the paths that caused it.
29 type LinkError struct {
36 func (e *LinkError) Error() string {
37 return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
40 // Link creates a hard link.
41 func Link(oldname, newname string) error {
42 e := syscall.Link(oldname, newname)
44 return &LinkError{"link", oldname, newname, e}
49 // Symlink creates a symbolic link.
50 func Symlink(oldname, newname string) error {
51 e := syscall.Symlink(oldname, newname)
53 return &LinkError{"symlink", oldname, newname, e}
58 // Readlink reads the contents of a symbolic link: the destination of
59 // the link. It returns the contents and an error, if any.
60 func Readlink(name string) (string, error) {
61 for len := 128; ; len *= 2 {
62 b := make([]byte, len)
63 n, e := syscall.Readlink(name, b)
65 return "", &PathError{"readlink", name, e}
68 return string(b[0:n]), nil
75 // Rename renames a file.
76 func Rename(oldname, newname string) error {
77 e := syscall.Rename(oldname, newname)
79 return &LinkError{"rename", oldname, newname, e}
84 // Chmod changes the mode of the named file to mode.
85 // If the file is a symbolic link, it changes the mode of the link's target.
86 func Chmod(name string, mode uint32) error {
87 if e := syscall.Chmod(name, mode); e != nil {
88 return &PathError{"chmod", name, e}
93 // Chmod changes the mode of the file to mode.
94 func (f *File) Chmod(mode uint32) error {
95 if e := syscall.Fchmod(f.fd, mode); e != nil {
96 return &PathError{"chmod", f.name, e}
101 // Chown changes the numeric uid and gid of the named file.
102 // If the file is a symbolic link, it changes the uid and gid of the link's target.
103 func Chown(name string, uid, gid int) error {
104 if e := syscall.Chown(name, uid, gid); e != nil {
105 return &PathError{"chown", name, e}
110 // Lchown changes the numeric uid and gid of the named file.
111 // If the file is a symbolic link, it changes the uid and gid of the link itself.
112 func Lchown(name string, uid, gid int) error {
113 if e := syscall.Lchown(name, uid, gid); e != nil {
114 return &PathError{"lchown", name, e}
119 // Chown changes the numeric uid and gid of the named file.
120 func (f *File) Chown(uid, gid int) error {
121 if e := syscall.Fchown(f.fd, uid, gid); e != nil {
122 return &PathError{"chown", f.name, e}
127 // Truncate changes the size of the file.
128 // It does not change the I/O offset.
129 func (f *File) Truncate(size int64) error {
130 if e := syscall.Ftruncate(f.fd, size); e != nil {
131 return &PathError{"truncate", f.name, e}
136 // Sync commits the current contents of the file to stable storage.
137 // Typically, this means flushing the file system's in-memory copy
138 // of recently written data to disk.
139 func (f *File) Sync() (err error) {
143 if e := syscall.Fsync(f.fd); e != nil {
144 return NewSyscallError("fsync", e)
149 // Chtimes changes the access and modification times of the named
150 // file, similar to the Unix utime() or utimes() functions.
152 // The underlying filesystem may truncate or round the values to a
153 // less precise time unit.
154 func Chtimes(name string, atime time.Time, mtime time.Time) error {
155 var utimes [2]syscall.Timeval
156 atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())
157 mtime_ns := mtime.Unix()*1e9 + int64(mtime.Nanosecond())
158 utimes[0] = syscall.NsecToTimeval(atime_ns)
159 utimes[1] = syscall.NsecToTimeval(mtime_ns)
160 if e := syscall.Utimes(name, utimes[0:]); e != nil {
161 return &PathError{"chtimes", name, e}