github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/os/file_unix.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 nacl netbsd openbsd solaris 6 7 package os 8 9 import ( 10 "internal/poll" 11 "runtime" 12 "syscall" 13 ) 14 15 // fixLongPath is a noop on non-Windows platforms. 16 func fixLongPath(path string) string { 17 return path 18 } 19 20 func rename(oldname, newname string) error { 21 fi, err := Lstat(newname) 22 if err == nil && fi.IsDir() { 23 // There are two independent errors this function can return: 24 // one for a bad oldname, and one for a bad newname. 25 // At this point we've determined the newname is bad. 26 // But just in case oldname is also bad, prioritize returning 27 // the oldname error because that's what we did historically. 28 if _, err := Lstat(oldname); err != nil { 29 if pe, ok := err.(*PathError); ok { 30 err = pe.Err 31 } 32 return &LinkError{"rename", oldname, newname, err} 33 } 34 return &LinkError{"rename", oldname, newname, syscall.EEXIST} 35 } 36 err = syscall.Rename(oldname, newname) 37 if err != nil { 38 return &LinkError{"rename", oldname, newname, err} 39 } 40 return nil 41 } 42 43 // file is the real representation of *File. 44 // The extra level of indirection ensures that no clients of os 45 // can overwrite this data, which could cause the finalizer 46 // to close the wrong file descriptor. 47 type file struct { 48 pfd poll.FD 49 name string 50 dirinfo *dirInfo // nil unless directory being read 51 nonblock bool // whether we set nonblocking mode 52 stdoutOrErr bool // whether this is stdout or stderr 53 } 54 55 // Fd returns the integer Unix file descriptor referencing the open file. 56 // The file descriptor is valid only until f.Close is called or f is garbage collected. 57 // On Unix systems this will cause the SetDeadline methods to stop working. 58 func (f *File) Fd() uintptr { 59 if f == nil { 60 return ^(uintptr(0)) 61 } 62 63 // If we put the file descriptor into nonblocking mode, 64 // then set it to blocking mode before we return it, 65 // because historically we have always returned a descriptor 66 // opened in blocking mode. The File will continue to work, 67 // but any blocking operation will tie up a thread. 68 if f.nonblock { 69 f.pfd.SetBlocking() 70 } 71 72 return uintptr(f.pfd.Sysfd) 73 } 74 75 // NewFile returns a new File with the given file descriptor and 76 // name. The returned value will be nil if fd is not a valid file 77 // descriptor. 78 func NewFile(fd uintptr, name string) *File { 79 return newFile(fd, name, kindNewFile) 80 } 81 82 // newFileKind describes the kind of file to newFile. 83 type newFileKind int 84 85 const ( 86 kindNewFile newFileKind = iota 87 kindOpenFile 88 kindPipe 89 ) 90 91 // newFile is like NewFile, but if called from OpenFile or Pipe 92 // (as passed in the kind parameter) it tries to add the file to 93 // the runtime poller. 94 func newFile(fd uintptr, name string, kind newFileKind) *File { 95 fdi := int(fd) 96 if fdi < 0 { 97 return nil 98 } 99 f := &File{&file{ 100 pfd: poll.FD{ 101 Sysfd: fdi, 102 IsStream: true, 103 ZeroReadIsEOF: true, 104 }, 105 name: name, 106 stdoutOrErr: fdi == 1 || fdi == 2, 107 }} 108 109 // Don't try to use kqueue with regular files on FreeBSD. 110 // It crashes the system unpredictably while running all.bash. 111 // Issue 19093. 112 if runtime.GOOS == "freebsd" && kind == kindOpenFile { 113 kind = kindNewFile 114 } 115 116 pollable := kind == kindOpenFile || kind == kindPipe 117 if err := f.pfd.Init("file", pollable); err != nil { 118 // An error here indicates a failure to register 119 // with the netpoll system. That can happen for 120 // a file descriptor that is not supported by 121 // epoll/kqueue; for example, disk files on 122 // GNU/Linux systems. We assume that any real error 123 // will show up in later I/O. 124 } else if pollable { 125 // We successfully registered with netpoll, so put 126 // the file into nonblocking mode. 127 if err := syscall.SetNonblock(fdi, true); err == nil { 128 f.nonblock = true 129 } 130 } 131 132 runtime.SetFinalizer(f.file, (*file).close) 133 return f 134 } 135 136 // Auxiliary information if the File describes a directory 137 type dirInfo struct { 138 buf []byte // buffer for directory I/O 139 nbuf int // length of buf; return value from Getdirentries 140 bufp int // location of next record in buf. 141 } 142 143 // epipecheck raises SIGPIPE if we get an EPIPE error on standard 144 // output or standard error. See the SIGPIPE docs in os/signal, and 145 // issue 11845. 146 func epipecheck(file *File, e error) { 147 if e == syscall.EPIPE && file.stdoutOrErr { 148 sigpipe() 149 } 150 } 151 152 // DevNull is the name of the operating system's ``null device.'' 153 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL". 154 const DevNull = "/dev/null" 155 156 // openFileNolog is the Unix implementation of OpenFile. 157 func openFileNolog(name string, flag int, perm FileMode) (*File, error) { 158 chmod := false 159 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 { 160 if _, err := Stat(name); IsNotExist(err) { 161 chmod = true 162 } 163 } 164 165 var r int 166 for { 167 var e error 168 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm)) 169 if e == nil { 170 break 171 } 172 173 // On OS X, sigaction(2) doesn't guarantee that SA_RESTART will cause 174 // open(2) to be restarted for regular files. This is easy to reproduce on 175 // fuse file systems (see http://golang.org/issue/11180). 176 if runtime.GOOS == "darwin" && e == syscall.EINTR { 177 continue 178 } 179 180 return nil, &PathError{"open", name, e} 181 } 182 183 // open(2) itself won't handle the sticky bit on *BSD and Solaris 184 if chmod { 185 Chmod(name, perm) 186 } 187 188 // There's a race here with fork/exec, which we are 189 // content to live with. See ../syscall/exec_unix.go. 190 if !supportsCloseOnExec { 191 syscall.CloseOnExec(r) 192 } 193 194 return newFile(uintptr(r), name, kindOpenFile), nil 195 } 196 197 // Close closes the File, rendering it unusable for I/O. 198 // It returns an error, if any. 199 func (f *File) Close() error { 200 if f == nil { 201 return ErrInvalid 202 } 203 return f.file.close() 204 } 205 206 func (file *file) close() error { 207 if file == nil { 208 return syscall.EINVAL 209 } 210 var err error 211 if e := file.pfd.Close(); e != nil { 212 if e == poll.ErrFileClosing { 213 e = ErrClosed 214 } 215 err = &PathError{"close", file.name, e} 216 } 217 218 // no need for a finalizer anymore 219 runtime.SetFinalizer(file, nil) 220 return err 221 } 222 223 // read reads up to len(b) bytes from the File. 224 // It returns the number of bytes read and an error, if any. 225 func (f *File) read(b []byte) (n int, err error) { 226 n, err = f.pfd.Read(b) 227 runtime.KeepAlive(f) 228 return n, err 229 } 230 231 // pread reads len(b) bytes from the File starting at byte offset off. 232 // It returns the number of bytes read and the error, if any. 233 // EOF is signaled by a zero count with err set to nil. 234 func (f *File) pread(b []byte, off int64) (n int, err error) { 235 n, err = f.pfd.Pread(b, off) 236 runtime.KeepAlive(f) 237 return n, err 238 } 239 240 // write writes len(b) bytes to the File. 241 // It returns the number of bytes written and an error, if any. 242 func (f *File) write(b []byte) (n int, err error) { 243 n, err = f.pfd.Write(b) 244 runtime.KeepAlive(f) 245 return n, err 246 } 247 248 // pwrite writes len(b) bytes to the File starting at byte offset off. 249 // It returns the number of bytes written and an error, if any. 250 func (f *File) pwrite(b []byte, off int64) (n int, err error) { 251 n, err = f.pfd.Pwrite(b, off) 252 runtime.KeepAlive(f) 253 return n, err 254 } 255 256 // seek sets the offset for the next Read or Write on file to offset, interpreted 257 // according to whence: 0 means relative to the origin of the file, 1 means 258 // relative to the current offset, and 2 means relative to the end. 259 // It returns the new offset and an error, if any. 260 func (f *File) seek(offset int64, whence int) (ret int64, err error) { 261 ret, err = f.pfd.Seek(offset, whence) 262 runtime.KeepAlive(f) 263 return ret, err 264 } 265 266 // Truncate changes the size of the named file. 267 // If the file is a symbolic link, it changes the size of the link's target. 268 // If there is an error, it will be of type *PathError. 269 func Truncate(name string, size int64) error { 270 if e := syscall.Truncate(name, size); e != nil { 271 return &PathError{"truncate", name, e} 272 } 273 return nil 274 } 275 276 // Remove removes the named file or directory. 277 // If there is an error, it will be of type *PathError. 278 func Remove(name string) error { 279 // System call interface forces us to know 280 // whether name is a file or directory. 281 // Try both: it is cheaper on average than 282 // doing a Stat plus the right one. 283 e := syscall.Unlink(name) 284 if e == nil { 285 return nil 286 } 287 e1 := syscall.Rmdir(name) 288 if e1 == nil { 289 return nil 290 } 291 292 // Both failed: figure out which error to return. 293 // OS X and Linux differ on whether unlink(dir) 294 // returns EISDIR, so can't use that. However, 295 // both agree that rmdir(file) returns ENOTDIR, 296 // so we can use that to decide which error is real. 297 // Rmdir might also return ENOTDIR if given a bad 298 // file path, like /etc/passwd/foo, but in that case, 299 // both errors will be ENOTDIR, so it's okay to 300 // use the error from unlink. 301 if e1 != syscall.ENOTDIR { 302 e = e1 303 } 304 return &PathError{"remove", name, e} 305 } 306 307 func tempDir() string { 308 dir := Getenv("TMPDIR") 309 if dir == "" { 310 if runtime.GOOS == "android" { 311 dir = "/data/local/tmp" 312 } else { 313 dir = "/tmp" 314 } 315 } 316 return dir 317 } 318 319 // Link creates newname as a hard link to the oldname file. 320 // If there is an error, it will be of type *LinkError. 321 func Link(oldname, newname string) error { 322 e := syscall.Link(oldname, newname) 323 if e != nil { 324 return &LinkError{"link", oldname, newname, e} 325 } 326 return nil 327 } 328 329 // Symlink creates newname as a symbolic link to oldname. 330 // If there is an error, it will be of type *LinkError. 331 func Symlink(oldname, newname string) error { 332 e := syscall.Symlink(oldname, newname) 333 if e != nil { 334 return &LinkError{"symlink", oldname, newname, e} 335 } 336 return nil 337 }