github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/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 freebsd linux netbsd openbsd 6 7 package os 8 9 import ( 10 "runtime" 11 "sync/atomic" 12 "syscall" 13 ) 14 15 // File represents an open file descriptor. 16 type File struct { 17 *file 18 } 19 20 // file is the real representation of *File. 21 // The extra level of indirection ensures that no clients of os 22 // can overwrite this data, which could cause the finalizer 23 // to close the wrong file descriptor. 24 type file struct { 25 fd int 26 name string 27 dirinfo *dirInfo // nil unless directory being read 28 nepipe int32 // number of consecutive EPIPE in Write 29 } 30 31 // Fd returns the integer Unix file descriptor referencing the open file. 32 func (f *File) Fd() uintptr { 33 if f == nil { 34 return ^(uintptr(0)) 35 } 36 return uintptr(f.fd) 37 } 38 39 // NewFile returns a new File with the given file descriptor and name. 40 func NewFile(fd uintptr, name string) *File { 41 fdi := int(fd) 42 if fdi < 0 { 43 return nil 44 } 45 f := &File{&file{fd: fdi, name: name}} 46 runtime.SetFinalizer(f.file, (*file).close) 47 return f 48 } 49 50 // Auxiliary information if the File describes a directory 51 type dirInfo struct { 52 buf []byte // buffer for directory I/O 53 nbuf int // length of buf; return value from Getdirentries 54 bufp int // location of next record in buf. 55 } 56 57 func epipecheck(file *File, e error) { 58 if e == syscall.EPIPE { 59 if atomic.AddInt32(&file.nepipe, 1) >= 10 { 60 sigpipe() 61 } 62 } else { 63 atomic.StoreInt32(&file.nepipe, 0) 64 } 65 } 66 67 // DevNull is the name of the operating system's ``null device.'' 68 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL". 69 const DevNull = "/dev/null" 70 71 // OpenFile is the generalized open call; most users will use Open 72 // or Create instead. It opens the named file with specified flag 73 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, 74 // methods on the returned File can be used for I/O. 75 // If there is an error, it will be of type *PathError. 76 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) { 77 r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm)) 78 if e != nil { 79 return nil, &PathError{"open", name, e} 80 } 81 82 // There's a race here with fork/exec, which we are 83 // content to live with. See ../syscall/exec_unix.go. 84 // On OS X 10.6, the O_CLOEXEC flag is not respected. 85 // On OS X 10.7, the O_CLOEXEC flag works. 86 // Without a cheap & reliable way to detect 10.6 vs 10.7 at 87 // runtime, we just always call syscall.CloseOnExec on Darwin. 88 // Once >=10.7 is prevalent, this extra call can removed. 89 if syscall.O_CLOEXEC == 0 || runtime.GOOS == "darwin" { // O_CLOEXEC not supported 90 syscall.CloseOnExec(r) 91 } 92 93 return NewFile(uintptr(r), name), nil 94 } 95 96 // Close closes the File, rendering it unusable for I/O. 97 // It returns an error, if any. 98 func (f *File) Close() error { 99 return f.file.close() 100 } 101 102 func (file *file) close() error { 103 if file == nil || file.fd < 0 { 104 return syscall.EINVAL 105 } 106 var err error 107 if e := syscall.Close(file.fd); e != nil { 108 err = &PathError{"close", file.name, e} 109 } 110 file.fd = -1 // so it can't be closed again 111 112 // no need for a finalizer anymore 113 runtime.SetFinalizer(file, nil) 114 return err 115 } 116 117 // Stat returns the FileInfo structure describing file. 118 // If there is an error, it will be of type *PathError. 119 func (f *File) Stat() (fi FileInfo, err error) { 120 var stat syscall.Stat_t 121 err = syscall.Fstat(f.fd, &stat) 122 if err != nil { 123 return nil, &PathError{"stat", f.name, err} 124 } 125 return fileInfoFromStat(&stat, f.name), nil 126 } 127 128 // Stat returns a FileInfo describing the named file. 129 // If there is an error, it will be of type *PathError. 130 func Stat(name string) (fi FileInfo, err error) { 131 var stat syscall.Stat_t 132 err = syscall.Stat(name, &stat) 133 if err != nil { 134 return nil, &PathError{"stat", name, err} 135 } 136 return fileInfoFromStat(&stat, name), nil 137 } 138 139 // Lstat returns a FileInfo describing the named file. 140 // If the file is a symbolic link, the returned FileInfo 141 // describes the symbolic link. Lstat makes no attempt to follow the link. 142 // If there is an error, it will be of type *PathError. 143 func Lstat(name string) (fi FileInfo, err error) { 144 var stat syscall.Stat_t 145 err = syscall.Lstat(name, &stat) 146 if err != nil { 147 return nil, &PathError{"lstat", name, err} 148 } 149 return fileInfoFromStat(&stat, name), nil 150 } 151 152 func (f *File) readdir(n int) (fi []FileInfo, err error) { 153 dirname := f.name 154 if dirname == "" { 155 dirname = "." 156 } 157 dirname += "/" 158 names, err := f.Readdirnames(n) 159 fi = make([]FileInfo, len(names)) 160 for i, filename := range names { 161 fip, err := Lstat(dirname + filename) 162 if err == nil { 163 fi[i] = fip 164 } else { 165 fi[i] = &fileStat{name: filename} 166 } 167 } 168 return fi, err 169 } 170 171 // read reads up to len(b) bytes from the File. 172 // It returns the number of bytes read and an error, if any. 173 func (f *File) read(b []byte) (n int, err error) { 174 return syscall.Read(f.fd, b) 175 } 176 177 // pread reads len(b) bytes from the File starting at byte offset off. 178 // It returns the number of bytes read and the error, if any. 179 // EOF is signaled by a zero count with err set to 0. 180 func (f *File) pread(b []byte, off int64) (n int, err error) { 181 return syscall.Pread(f.fd, b, off) 182 } 183 184 // write writes len(b) bytes to the File. 185 // It returns the number of bytes written and an error, if any. 186 func (f *File) write(b []byte) (n int, err error) { 187 for { 188 m, err := syscall.Write(f.fd, b) 189 n += m 190 191 // If the syscall wrote some data but not all (short write) 192 // or it returned EINTR, then assume it stopped early for 193 // reasons that are uninteresting to the caller, and try again. 194 if 0 < m && m < len(b) || err == syscall.EINTR { 195 b = b[m:] 196 continue 197 } 198 199 return n, err 200 } 201 } 202 203 // pwrite writes len(b) bytes to the File starting at byte offset off. 204 // It returns the number of bytes written and an error, if any. 205 func (f *File) pwrite(b []byte, off int64) (n int, err error) { 206 return syscall.Pwrite(f.fd, b, off) 207 } 208 209 // seek sets the offset for the next Read or Write on file to offset, interpreted 210 // according to whence: 0 means relative to the origin of the file, 1 means 211 // relative to the current offset, and 2 means relative to the end. 212 // It returns the new offset and an error, if any. 213 func (f *File) seek(offset int64, whence int) (ret int64, err error) { 214 return syscall.Seek(f.fd, offset, whence) 215 } 216 217 // Truncate changes the size of the named file. 218 // If the file is a symbolic link, it changes the size of the link's target. 219 // If there is an error, it will be of type *PathError. 220 func Truncate(name string, size int64) error { 221 if e := syscall.Truncate(name, size); e != nil { 222 return &PathError{"truncate", name, e} 223 } 224 return nil 225 } 226 227 // Remove removes the named file or directory. 228 // If there is an error, it will be of type *PathError. 229 func Remove(name string) error { 230 // System call interface forces us to know 231 // whether name is a file or directory. 232 // Try both: it is cheaper on average than 233 // doing a Stat plus the right one. 234 e := syscall.Unlink(name) 235 if e == nil { 236 return nil 237 } 238 e1 := syscall.Rmdir(name) 239 if e1 == nil { 240 return nil 241 } 242 243 // Both failed: figure out which error to return. 244 // OS X and Linux differ on whether unlink(dir) 245 // returns EISDIR, so can't use that. However, 246 // both agree that rmdir(file) returns ENOTDIR, 247 // so we can use that to decide which error is real. 248 // Rmdir might also return ENOTDIR if given a bad 249 // file path, like /etc/passwd/foo, but in that case, 250 // both errors will be ENOTDIR, so it's okay to 251 // use the error from unlink. 252 if e1 != syscall.ENOTDIR { 253 e = e1 254 } 255 return &PathError{"remove", name, e} 256 } 257 258 // basename removes trailing slashes and the leading directory name from path name 259 func basename(name string) string { 260 i := len(name) - 1 261 // Remove trailing slashes 262 for ; i > 0 && name[i] == '/'; i-- { 263 name = name[:i] 264 } 265 // Remove leading directory name 266 for i--; i >= 0; i-- { 267 if name[i] == '/' { 268 name = name[i+1:] 269 break 270 } 271 } 272 273 return name 274 } 275 276 // TempDir returns the default directory to use for temporary files. 277 func TempDir() string { 278 dir := Getenv("TMPDIR") 279 if dir == "" { 280 dir = "/tmp" 281 } 282 return dir 283 }