github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 "runtime" 11 "syscall" 12 ) 13 14 func rename(oldname, newname string) error { 15 fi, err := Lstat(newname) 16 if err == nil && fi.IsDir() { 17 return &LinkError{"rename", oldname, newname, syscall.EEXIST} 18 } 19 e := syscall.Rename(oldname, newname) 20 if e != nil { 21 return &LinkError{"rename", oldname, newname, e} 22 } 23 return nil 24 } 25 26 // file is the real representation of *File. 27 // The extra level of indirection ensures that no clients of os 28 // can overwrite this data, which could cause the finalizer 29 // to close the wrong file descriptor. 30 type file struct { 31 fd int 32 name string 33 dirinfo *dirInfo // nil unless directory being read 34 } 35 36 // Fd returns the integer Unix file descriptor referencing the open file. 37 // The file descriptor is valid only until f.Close is called or f is garbage collected. 38 func (f *File) Fd() uintptr { 39 if f == nil { 40 return ^(uintptr(0)) 41 } 42 return uintptr(f.fd) 43 } 44 45 // NewFile returns a new File with the given file descriptor and name. 46 func NewFile(fd uintptr, name string) *File { 47 fdi := int(fd) 48 if fdi < 0 { 49 return nil 50 } 51 f := &File{&file{fd: fdi, name: name}} 52 runtime.SetFinalizer(f.file, (*file).close) 53 return f 54 } 55 56 // Auxiliary information if the File describes a directory 57 type dirInfo struct { 58 buf []byte // buffer for directory I/O 59 nbuf int // length of buf; return value from Getdirentries 60 bufp int // location of next record in buf. 61 } 62 63 // epipecheck raises SIGPIPE if we get an EPIPE error on standard 64 // output or standard error. See the SIGPIPE docs in os/signal, and 65 // issue 11845. 66 func epipecheck(file *File, e error) { 67 if e == syscall.EPIPE && (file.fd == 1 || file.fd == 2) { 68 sigpipe() 69 } 70 } 71 72 // DevNull is the name of the operating system's ``null device.'' 73 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL". 74 const DevNull = "/dev/null" 75 76 // OpenFile is the generalized open call; most users will use Open 77 // or Create instead. It opens the named file with specified flag 78 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, 79 // methods on the returned File can be used for I/O. 80 // If there is an error, it will be of type *PathError. 81 func OpenFile(name string, flag int, perm FileMode) (*File, error) { 82 chmod := false 83 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 { 84 if _, err := Stat(name); IsNotExist(err) { 85 chmod = true 86 } 87 } 88 89 var r int 90 for { 91 var e error 92 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm)) 93 if e == nil { 94 break 95 } 96 97 // On OS X, sigaction(2) doesn't guarantee that SA_RESTART will cause 98 // open(2) to be restarted for regular files. This is easy to reproduce on 99 // fuse file systems (see http://golang.org/issue/11180). 100 if runtime.GOOS == "darwin" && e == syscall.EINTR { 101 continue 102 } 103 104 return nil, &PathError{"open", name, e} 105 } 106 107 // open(2) itself won't handle the sticky bit on *BSD and Solaris 108 if chmod { 109 Chmod(name, perm) 110 } 111 112 // There's a race here with fork/exec, which we are 113 // content to live with. See ../syscall/exec_unix.go. 114 if !supportsCloseOnExec { 115 syscall.CloseOnExec(r) 116 } 117 118 return NewFile(uintptr(r), name), nil 119 } 120 121 // Close closes the File, rendering it unusable for I/O. 122 // It returns an error, if any. 123 func (f *File) Close() error { 124 if f == nil { 125 return ErrInvalid 126 } 127 return f.file.close() 128 } 129 130 func (file *file) close() error { 131 if file == nil || file.fd == badFd { 132 return syscall.EINVAL 133 } 134 var err error 135 if e := syscall.Close(file.fd); e != nil { 136 err = &PathError{"close", file.name, e} 137 } 138 file.fd = -1 // so it can't be closed again 139 140 // no need for a finalizer anymore 141 runtime.SetFinalizer(file, nil) 142 return err 143 } 144 145 // Darwin and FreeBSD can't read or write 2GB+ at a time, 146 // even on 64-bit systems. See golang.org/issue/7812. 147 // Use 1GB instead of, say, 2GB-1, to keep subsequent 148 // reads aligned. 149 const ( 150 needsMaxRW = runtime.GOOS == "darwin" || runtime.GOOS == "freebsd" 151 maxRW = 1 << 30 152 ) 153 154 // read reads up to len(b) bytes from the File. 155 // It returns the number of bytes read and an error, if any. 156 func (f *File) read(b []byte) (n int, err error) { 157 if needsMaxRW && len(b) > maxRW { 158 b = b[:maxRW] 159 } 160 return fixCount(syscall.Read(f.fd, b)) 161 } 162 163 // pread reads len(b) bytes from the File starting at byte offset off. 164 // It returns the number of bytes read and the error, if any. 165 // EOF is signaled by a zero count with err set to nil. 166 func (f *File) pread(b []byte, off int64) (n int, err error) { 167 if needsMaxRW && len(b) > maxRW { 168 b = b[:maxRW] 169 } 170 return fixCount(syscall.Pread(f.fd, b, off)) 171 } 172 173 // write writes len(b) bytes to the File. 174 // It returns the number of bytes written and an error, if any. 175 func (f *File) write(b []byte) (n int, err error) { 176 for { 177 bcap := b 178 if needsMaxRW && len(bcap) > maxRW { 179 bcap = bcap[:maxRW] 180 } 181 m, err := fixCount(syscall.Write(f.fd, bcap)) 182 n += m 183 184 // If the syscall wrote some data but not all (short write) 185 // or it returned EINTR, then assume it stopped early for 186 // reasons that are uninteresting to the caller, and try again. 187 if 0 < m && m < len(bcap) || err == syscall.EINTR { 188 b = b[m:] 189 continue 190 } 191 192 if needsMaxRW && len(bcap) != len(b) && err == nil { 193 b = b[m:] 194 continue 195 } 196 197 return n, err 198 } 199 } 200 201 // pwrite writes len(b) bytes to the File starting at byte offset off. 202 // It returns the number of bytes written and an error, if any. 203 func (f *File) pwrite(b []byte, off int64) (n int, err error) { 204 if needsMaxRW && len(b) > maxRW { 205 b = b[:maxRW] 206 } 207 return fixCount(syscall.Pwrite(f.fd, b, off)) 208 } 209 210 // seek sets the offset for the next Read or Write on file to offset, interpreted 211 // according to whence: 0 means relative to the origin of the file, 1 means 212 // relative to the current offset, and 2 means relative to the end. 213 // It returns the new offset and an error, if any. 214 func (f *File) seek(offset int64, whence int) (ret int64, err error) { 215 return syscall.Seek(f.fd, offset, whence) 216 } 217 218 // Truncate changes the size of the named file. 219 // If the file is a symbolic link, it changes the size of the link's target. 220 // If there is an error, it will be of type *PathError. 221 func Truncate(name string, size int64) error { 222 if e := syscall.Truncate(name, size); e != nil { 223 return &PathError{"truncate", name, e} 224 } 225 return nil 226 } 227 228 // Remove removes the named file or directory. 229 // If there is an error, it will be of type *PathError. 230 func Remove(name string) error { 231 // System call interface forces us to know 232 // whether name is a file or directory. 233 // Try both: it is cheaper on average than 234 // doing a Stat plus the right one. 235 e := syscall.Unlink(name) 236 if e == nil { 237 return nil 238 } 239 e1 := syscall.Rmdir(name) 240 if e1 == nil { 241 return nil 242 } 243 244 // Both failed: figure out which error to return. 245 // OS X and Linux differ on whether unlink(dir) 246 // returns EISDIR, so can't use that. However, 247 // both agree that rmdir(file) returns ENOTDIR, 248 // so we can use that to decide which error is real. 249 // Rmdir might also return ENOTDIR if given a bad 250 // file path, like /etc/passwd/foo, but in that case, 251 // both errors will be ENOTDIR, so it's okay to 252 // use the error from unlink. 253 if e1 != syscall.ENOTDIR { 254 e = e1 255 } 256 return &PathError{"remove", name, e} 257 } 258 259 // TempDir returns the default directory to use for temporary files. 260 func TempDir() string { 261 dir := Getenv("TMPDIR") 262 if dir == "" { 263 if runtime.GOOS == "android" { 264 dir = "/data/local/tmp" 265 } else { 266 dir = "/tmp" 267 } 268 } 269 return dir 270 } 271 272 // Link creates newname as a hard link to the oldname file. 273 // If there is an error, it will be of type *LinkError. 274 func Link(oldname, newname string) error { 275 e := syscall.Link(oldname, newname) 276 if e != nil { 277 return &LinkError{"link", oldname, newname, e} 278 } 279 return nil 280 } 281 282 // Symlink creates newname as a symbolic link to oldname. 283 // If there is an error, it will be of type *LinkError. 284 func Symlink(oldname, newname string) error { 285 e := syscall.Symlink(oldname, newname) 286 if e != nil { 287 return &LinkError{"symlink", oldname, newname, e} 288 } 289 return nil 290 }