github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/os/file_posix.go (about) 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. 4 5 // +build darwin dragonfly freebsd linux netbsd openbsd windows 6 7 package os 8 9 import ( 10 "syscall" 11 "time" 12 ) 13 14 func sigpipe() // implemented in package runtime 15 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) 20 if e != nil { 21 return &LinkError{"link", oldname, newname, e} 22 } 23 return nil 24 } 25 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) 30 if e != nil { 31 return &LinkError{"symlink", oldname, newname, e} 32 } 33 return nil 34 } 35 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) 42 if e != nil { 43 return "", &PathError{"readlink", name, e} 44 } 45 if n < len { 46 return string(b[0:n]), nil 47 } 48 } 49 } 50 51 // Rename renames a file. 52 func Rename(oldname, newname string) error { 53 e := syscall.Rename(oldname, newname) 54 if e != nil { 55 return &LinkError{"rename", oldname, newname, e} 56 } 57 return nil 58 } 59 60 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits. 61 func syscallMode(i FileMode) (o uint32) { 62 o |= uint32(i.Perm()) 63 if i&ModeSetuid != 0 { 64 o |= syscall.S_ISUID 65 } 66 if i&ModeSetgid != 0 { 67 o |= syscall.S_ISGID 68 } 69 if i&ModeSticky != 0 { 70 o |= syscall.S_ISVTX 71 } 72 // No mapping for Go's ModeTemporary (plan9 only). 73 return 74 } 75 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} 82 } 83 return nil 84 } 85 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 f == nil { 90 return ErrInvalid 91 } 92 if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil { 93 return &PathError{"chmod", f.name, e} 94 } 95 return nil 96 } 97 98 // Chown changes the numeric uid and gid of the named file. 99 // If the file is a symbolic link, it changes the uid and gid of the link's target. 100 // If there is an error, it will be of type *PathError. 101 func Chown(name string, uid, gid int) error { 102 if e := syscall.Chown(name, uid, gid); e != nil { 103 return &PathError{"chown", name, e} 104 } 105 return nil 106 } 107 108 // Lchown changes the numeric uid and gid of the named file. 109 // If the file is a symbolic link, it changes the uid and gid of the link itself. 110 // If there is an error, it will be of type *PathError. 111 func Lchown(name string, uid, gid int) error { 112 if e := syscall.Lchown(name, uid, gid); e != nil { 113 return &PathError{"lchown", name, e} 114 } 115 return nil 116 } 117 118 // Chown changes the numeric uid and gid of the named file. 119 // If there is an error, it will be of type *PathError. 120 func (f *File) Chown(uid, gid int) error { 121 if f == nil { 122 return ErrInvalid 123 } 124 if e := syscall.Fchown(f.fd, uid, gid); e != nil { 125 return &PathError{"chown", f.name, e} 126 } 127 return nil 128 } 129 130 // Truncate changes the size of the file. 131 // It does not change the I/O offset. 132 // If there is an error, it will be of type *PathError. 133 func (f *File) Truncate(size int64) error { 134 if f == nil { 135 return ErrInvalid 136 } 137 if e := syscall.Ftruncate(f.fd, size); e != nil { 138 return &PathError{"truncate", f.name, e} 139 } 140 return nil 141 } 142 143 // Sync commits the current contents of the file to stable storage. 144 // Typically, this means flushing the file system's in-memory copy 145 // of recently written data to disk. 146 func (f *File) Sync() (err error) { 147 if f == nil { 148 return syscall.EINVAL 149 } 150 if e := syscall.Fsync(f.fd); e != nil { 151 return NewSyscallError("fsync", e) 152 } 153 return nil 154 } 155 156 // Chtimes changes the access and modification times of the named 157 // file, similar to the Unix utime() or utimes() functions. 158 // 159 // The underlying filesystem may truncate or round the values to a 160 // less precise time unit. 161 // If there is an error, it will be of type *PathError. 162 func Chtimes(name string, atime time.Time, mtime time.Time) error { 163 var utimes [2]syscall.Timespec 164 utimes[0] = syscall.NsecToTimespec(atime.UnixNano()) 165 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano()) 166 if e := syscall.UtimesNano(name, utimes[0:]); e != nil { 167 return &PathError{"chtimes", name, e} 168 } 169 return nil 170 }