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