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 // Link creates newname as a hard link to the oldname file.
17 // If there is an error, it will be of type *LinkError.
18 func Link(oldname, newname string) error {
19 e := syscall.Link(oldname, newname)
21 return &LinkError{"link", oldname, newname, e}
26 // Symlink creates newname as a symbolic link to oldname.
27 // If there is an error, it will be of type *LinkError.
28 func Symlink(oldname, newname string) error {
29 e := syscall.Symlink(oldname, newname)
31 return &LinkError{"symlink", oldname, newname, e}
36 // Readlink returns the destination of the named symbolic link.
37 // If there is an error, it will be of type *PathError.
38 func Readlink(name string) (string, error) {
39 for len := 128; ; len *= 2 {
40 b := make([]byte, len)
41 n, e := syscall.Readlink(name, b)
43 return "", &PathError{"readlink", name, e}
46 return string(b[0:n]), nil
51 // Rename renames a file.
52 func Rename(oldname, newname string) error {
53 e := syscall.Rename(oldname, newname)
55 return &LinkError{"rename", oldname, newname, e}
60 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
61 func syscallMode(i FileMode) (o uint32) {
63 if i&ModeSetuid != 0 {
66 if i&ModeSetgid != 0 {
69 if i&ModeSticky != 0 {
72 // No mapping for Go's ModeTemporary (plan9 only).
76 // Chmod changes the mode of the named file to mode.
77 // If the file is a symbolic link, it changes the mode of the link's target.
78 // If there is an error, it will be of type *PathError.
79 func Chmod(name string, mode FileMode) error {
80 if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
81 return &PathError{"chmod", name, e}
86 // Chmod changes the mode of the file to mode.
87 // If there is an error, it will be of type *PathError.
88 func (f *File) Chmod(mode FileMode) error {
89 if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
90 return &PathError{"chmod", f.name, e}
95 // Chown changes the numeric uid and gid of the named file.
96 // If the file is a symbolic link, it changes the uid and gid of the link's target.
97 // If there is an error, it will be of type *PathError.
98 func Chown(name string, uid, gid int) error {
99 if e := syscall.Chown(name, uid, gid); e != nil {
100 return &PathError{"chown", name, e}
105 // Lchown changes the numeric uid and gid of the named file.
106 // If the file is a symbolic link, it changes the uid and gid of the link itself.
107 // If there is an error, it will be of type *PathError.
108 func Lchown(name string, uid, gid int) error {
109 if e := syscall.Lchown(name, uid, gid); e != nil {
110 return &PathError{"lchown", name, e}
115 // Chown changes the numeric uid and gid of the named file.
116 // If there is an error, it will be of type *PathError.
117 func (f *File) Chown(uid, gid int) error {
118 if e := syscall.Fchown(f.fd, uid, gid); e != nil {
119 return &PathError{"chown", f.name, e}
124 // Truncate changes the size of the file.
125 // It does not change the I/O offset.
126 // If there is an error, it will be of type *PathError.
127 func (f *File) Truncate(size int64) error {
128 if e := syscall.Ftruncate(f.fd, size); e != nil {
129 return &PathError{"truncate", f.name, e}
134 // Sync commits the current contents of the file to stable storage.
135 // Typically, this means flushing the file system's in-memory copy
136 // of recently written data to disk.
137 func (f *File) Sync() (err error) {
139 return syscall.EINVAL
141 if e := syscall.Fsync(f.fd); e != nil {
142 return NewSyscallError("fsync", e)
147 // Chtimes changes the access and modification times of the named
148 // file, similar to the Unix utime() or utimes() functions.
150 // The underlying filesystem may truncate or round the values to a
151 // less precise time unit.
152 // If there is an error, it will be of type *PathError.
153 func Chtimes(name string, atime time.Time, mtime time.Time) error {
154 var utimes [2]syscall.Timespec
155 utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
156 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
157 if e := syscall.UtimesNano(name, utimes[0:]); e != nil {
158 return &PathError{"chtimes", name, e}