github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/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 "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 // The file descriptor is valid only until f.Close is called or f is garbage collected. 33 func (f *File) Fd() uintptr { 34 if f == nil { 35 return ^(uintptr(0)) 36 } 37 return uintptr(f.fd) 38 } 39 40 // NewFile returns a new File with the given file descriptor and name. 41 func NewFile(fd uintptr, name string) *File { 42 fdi := int(fd) 43 if fdi < 0 { 44 return nil 45 } 46 f := &File{&file{fd: fdi, name: name}} 47 runtime.SetFinalizer(f.file, (*file).close) 48 return f 49 } 50 51 // Auxiliary information if the File describes a directory 52 type dirInfo struct { 53 buf []byte // buffer for directory I/O 54 dir *syscall.DIR // from opendir 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 if !supportsCloseOnExec { 85 syscall.CloseOnExec(r) 86 } 87 88 return NewFile(uintptr(r), name), nil 89 } 90 91 // Close closes the File, rendering it unusable for I/O. 92 // It returns an error, if any. 93 func (f *File) Close() error { 94 if f == nil { 95 return ErrInvalid 96 } 97 return f.file.close() 98 } 99 100 func (file *file) close() error { 101 if file == nil || file.fd < 0 { 102 return syscall.EINVAL 103 } 104 var err error 105 if e := syscall.Close(file.fd); e != nil { 106 err = &PathError{"close", file.name, e} 107 } 108 109 if file.dirinfo != nil { 110 syscall.Entersyscall() 111 i := libc_closedir(file.dirinfo.dir) 112 errno := syscall.GetErrno() 113 syscall.Exitsyscall() 114 file.dirinfo = nil 115 if i < 0 && err == nil { 116 err = &PathError{"closedir", file.name, errno} 117 } 118 } 119 120 file.fd = -1 // so it can't be closed again 121 122 // no need for a finalizer anymore 123 runtime.SetFinalizer(file, nil) 124 return err 125 } 126 127 // Stat returns the FileInfo structure describing file. 128 // If there is an error, it will be of type *PathError. 129 func (f *File) Stat() (fi FileInfo, err error) { 130 if f == nil { 131 return nil, ErrInvalid 132 } 133 var stat syscall.Stat_t 134 err = syscall.Fstat(f.fd, &stat) 135 if err != nil { 136 return nil, &PathError{"stat", f.name, err} 137 } 138 return fileInfoFromStat(&stat, f.name), nil 139 } 140 141 // Stat returns a FileInfo describing the named file. 142 // If there is an error, it will be of type *PathError. 143 func Stat(name string) (fi FileInfo, err error) { 144 var stat syscall.Stat_t 145 err = syscall.Stat(name, &stat) 146 if err != nil { 147 return nil, &PathError{"stat", name, err} 148 } 149 return fileInfoFromStat(&stat, name), nil 150 } 151 152 // Lstat returns a FileInfo describing the named file. 153 // If the file is a symbolic link, the returned FileInfo 154 // describes the symbolic link. Lstat makes no attempt to follow the link. 155 // If there is an error, it will be of type *PathError. 156 func Lstat(name string) (fi FileInfo, err error) { 157 var stat syscall.Stat_t 158 err = syscall.Lstat(name, &stat) 159 if err != nil { 160 return nil, &PathError{"lstat", name, err} 161 } 162 return fileInfoFromStat(&stat, name), nil 163 } 164 165 func (f *File) readdir(n int) (fi []FileInfo, err error) { 166 dirname := f.name 167 if dirname == "" { 168 dirname = "." 169 } 170 names, err := f.Readdirnames(n) 171 fi = make([]FileInfo, 0, len(names)) 172 for _, filename := range names { 173 fip, lerr := lstat(dirname + "/" + filename) 174 if IsNotExist(lerr) { 175 // File disappeared between readdir + stat. 176 // Just treat it as if it didn't exist. 177 continue 178 } 179 if lerr != nil { 180 return fi, lerr 181 } 182 fi = append(fi, fip) 183 } 184 return fi, err 185 } 186 187 // Darwin and FreeBSD can't read or write 2GB+ at a time, 188 // even on 64-bit systems. See golang.org/issue/7812. 189 // Use 1GB instead of, say, 2GB-1, to keep subsequent 190 // reads aligned. 191 const ( 192 needsMaxRW = runtime.GOOS == "darwin" || runtime.GOOS == "freebsd" 193 maxRW = 1 << 30 194 ) 195 196 // read reads up to len(b) bytes from the File. 197 // It returns the number of bytes read and an error, if any. 198 func (f *File) read(b []byte) (n int, err error) { 199 if needsMaxRW && len(b) > maxRW { 200 b = b[:maxRW] 201 } 202 return fixCount(syscall.Read(f.fd, b)) 203 } 204 205 // pread reads len(b) bytes from the File starting at byte offset off. 206 // It returns the number of bytes read and the error, if any. 207 // EOF is signaled by a zero count with err set to nil. 208 func (f *File) pread(b []byte, off int64) (n int, err error) { 209 if needsMaxRW && len(b) > maxRW { 210 b = b[:maxRW] 211 } 212 return fixCount(syscall.Pread(f.fd, b, off)) 213 } 214 215 // write writes len(b) bytes to the File. 216 // It returns the number of bytes written and an error, if any. 217 func (f *File) write(b []byte) (n int, err error) { 218 for { 219 bcap := b 220 if needsMaxRW && len(bcap) > maxRW { 221 bcap = bcap[:maxRW] 222 } 223 m, err := fixCount(syscall.Write(f.fd, bcap)) 224 n += m 225 226 // If the syscall wrote some data but not all (short write) 227 // or it returned EINTR, then assume it stopped early for 228 // reasons that are uninteresting to the caller, and try again. 229 if 0 < m && m < len(bcap) || err == syscall.EINTR { 230 b = b[m:] 231 continue 232 } 233 234 if needsMaxRW && len(bcap) != len(b) && err == nil { 235 b = b[m:] 236 continue 237 } 238 239 return n, err 240 } 241 } 242 243 // pwrite writes len(b) bytes to the File starting at byte offset off. 244 // It returns the number of bytes written and an error, if any. 245 func (f *File) pwrite(b []byte, off int64) (n int, err error) { 246 if needsMaxRW && len(b) > maxRW { 247 b = b[:maxRW] 248 } 249 return fixCount(syscall.Pwrite(f.fd, b, off)) 250 } 251 252 // seek sets the offset for the next Read or Write on file to offset, interpreted 253 // according to whence: 0 means relative to the origin of the file, 1 means 254 // relative to the current offset, and 2 means relative to the end. 255 // It returns the new offset and an error, if any. 256 func (f *File) seek(offset int64, whence int) (ret int64, err error) { 257 return syscall.Seek(f.fd, offset, whence) 258 } 259 260 // Truncate changes the size of the named file. 261 // If the file is a symbolic link, it changes the size of the link's target. 262 // If there is an error, it will be of type *PathError. 263 func Truncate(name string, size int64) error { 264 if e := syscall.Truncate(name, size); e != nil { 265 return &PathError{"truncate", name, e} 266 } 267 return nil 268 } 269 270 // Remove removes the named file or directory. 271 // If there is an error, it will be of type *PathError. 272 func Remove(name string) error { 273 // System call interface forces us to know 274 // whether name is a file or directory. 275 // Try both: it is cheaper on average than 276 // doing a Stat plus the right one. 277 e := syscall.Unlink(name) 278 if e == nil { 279 return nil 280 } 281 e1 := syscall.Rmdir(name) 282 if e1 == nil { 283 return nil 284 } 285 286 // Both failed: figure out which error to return. 287 // OS X and Linux differ on whether unlink(dir) 288 // returns EISDIR, so can't use that. However, 289 // both agree that rmdir(file) returns ENOTDIR, 290 // so we can use that to decide which error is real. 291 // Rmdir might also return ENOTDIR if given a bad 292 // file path, like /etc/passwd/foo, but in that case, 293 // both errors will be ENOTDIR, so it's okay to 294 // use the error from unlink. 295 if e1 != syscall.ENOTDIR { 296 e = e1 297 } 298 return &PathError{"remove", name, e} 299 } 300 301 // basename removes trailing slashes and the leading directory name from path name 302 func basename(name string) string { 303 i := len(name) - 1 304 // Remove trailing slashes 305 for ; i > 0 && name[i] == '/'; i-- { 306 name = name[:i] 307 } 308 // Remove leading directory name 309 for i--; i >= 0; i-- { 310 if name[i] == '/' { 311 name = name[i+1:] 312 break 313 } 314 } 315 316 return name 317 } 318 319 // TempDir returns the default directory to use for temporary files. 320 func TempDir() string { 321 dir := Getenv("TMPDIR") 322 if dir == "" { 323 if runtime.GOOS == "android" { 324 dir = "/data/local/tmp" 325 } else { 326 dir = "/tmp" 327 } 328 } 329 return dir 330 } 331 332 // Link creates newname as a hard link to the oldname file. 333 // If there is an error, it will be of type *LinkError. 334 func Link(oldname, newname string) error { 335 e := syscall.Link(oldname, newname) 336 if e != nil { 337 return &LinkError{"link", oldname, newname, e} 338 } 339 return nil 340 } 341 342 // Symlink creates newname as a symbolic link to oldname. 343 // If there is an error, it will be of type *LinkError. 344 func Symlink(oldname, newname string) error { 345 e := syscall.Symlink(oldname, newname) 346 if e != nil { 347 return &LinkError{"symlink", oldname, newname, e} 348 } 349 return nil 350 }