github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/os/file_plan9.go (about) 1 // Copyright 2011 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 package os 6 7 import ( 8 "internal/poll" 9 "io" 10 "runtime" 11 "syscall" 12 "time" 13 ) 14 15 // fixLongPath is a noop on non-Windows platforms. 16 func fixLongPath(path string) string { 17 return path 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 } 29 30 // Fd returns the integer Plan 9 file descriptor referencing the open file. 31 // The file descriptor is valid only until f.Close is called or f is garbage collected. 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 40 // name. The returned value will be nil if fd is not a valid file 41 // descriptor. 42 func NewFile(fd uintptr, name string) *File { 43 fdi := int(fd) 44 if fdi < 0 { 45 return nil 46 } 47 f := &File{&file{fd: fdi, name: name}} 48 runtime.SetFinalizer(f.file, (*file).close) 49 return f 50 } 51 52 // Auxiliary information if the File describes a directory 53 type dirInfo struct { 54 buf [syscall.STATMAX]byte // buffer for directory I/O 55 nbuf int // length of buf; return value from Read 56 bufp int // location of next record in buf. 57 } 58 59 func epipecheck(file *File, e error) { 60 } 61 62 // DevNull is the name of the operating system's ``null device.'' 63 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL". 64 const DevNull = "/dev/null" 65 66 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits. 67 func syscallMode(i FileMode) (o uint32) { 68 o |= uint32(i.Perm()) 69 if i&ModeAppend != 0 { 70 o |= syscall.DMAPPEND 71 } 72 if i&ModeExclusive != 0 { 73 o |= syscall.DMEXCL 74 } 75 if i&ModeTemporary != 0 { 76 o |= syscall.DMTMP 77 } 78 return 79 } 80 81 // OpenFile is the generalized open call; most users will use Open 82 // or Create instead. It opens the named file with specified flag 83 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, 84 // methods on the returned File can be used for I/O. 85 // If there is an error, it will be of type *PathError. 86 func OpenFile(name string, flag int, perm FileMode) (*File, error) { 87 var ( 88 fd int 89 e error 90 create bool 91 excl bool 92 trunc bool 93 append bool 94 ) 95 96 if flag&O_CREATE == O_CREATE { 97 flag = flag & ^O_CREATE 98 create = true 99 } 100 if flag&O_EXCL == O_EXCL { 101 excl = true 102 } 103 if flag&O_TRUNC == O_TRUNC { 104 trunc = true 105 } 106 // O_APPEND is emulated on Plan 9 107 if flag&O_APPEND == O_APPEND { 108 flag = flag &^ O_APPEND 109 append = true 110 } 111 112 if (create && trunc) || excl { 113 fd, e = syscall.Create(name, flag, syscallMode(perm)) 114 } else { 115 fd, e = syscall.Open(name, flag) 116 if e != nil && create { 117 var e1 error 118 fd, e1 = syscall.Create(name, flag, syscallMode(perm)) 119 if e1 == nil { 120 e = nil 121 } 122 } 123 } 124 125 if e != nil { 126 return nil, &PathError{"open", name, e} 127 } 128 129 if append { 130 if _, e = syscall.Seek(fd, 0, io.SeekEnd); e != nil { 131 return nil, &PathError{"seek", name, e} 132 } 133 } 134 135 return NewFile(uintptr(fd), name), nil 136 } 137 138 // Close closes the File, rendering it unusable for I/O. 139 // It returns an error, if any. 140 func (f *File) Close() error { 141 if err := f.checkValid("close"); err != nil { 142 return err 143 } 144 return f.file.close() 145 } 146 147 func (file *file) close() error { 148 if file == nil || file.fd == badFd { 149 return ErrInvalid 150 } 151 var err error 152 if e := syscall.Close(file.fd); e != nil { 153 err = &PathError{"close", file.name, e} 154 } 155 file.fd = badFd // so it can't be closed again 156 157 // no need for a finalizer anymore 158 runtime.SetFinalizer(file, nil) 159 return err 160 } 161 162 // Stat returns the FileInfo structure describing file. 163 // If there is an error, it will be of type *PathError. 164 func (f *File) Stat() (FileInfo, error) { 165 if f == nil { 166 return nil, ErrInvalid 167 } 168 d, err := dirstat(f) 169 if err != nil { 170 return nil, err 171 } 172 return fileInfoFromStat(d), nil 173 } 174 175 // Truncate changes the size of the file. 176 // It does not change the I/O offset. 177 // If there is an error, it will be of type *PathError. 178 func (f *File) Truncate(size int64) error { 179 if f == nil { 180 return ErrInvalid 181 } 182 183 var d syscall.Dir 184 d.Null() 185 d.Length = size 186 187 var buf [syscall.STATFIXLEN]byte 188 n, err := d.Marshal(buf[:]) 189 if err != nil { 190 return &PathError{"truncate", f.name, err} 191 } 192 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { 193 return &PathError{"truncate", f.name, err} 194 } 195 return nil 196 } 197 198 const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm) 199 200 func (f *File) chmod(mode FileMode) error { 201 if f == nil { 202 return ErrInvalid 203 } 204 var d syscall.Dir 205 206 odir, e := dirstat(f) 207 if e != nil { 208 return &PathError{"chmod", f.name, e} 209 } 210 d.Null() 211 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask 212 213 var buf [syscall.STATFIXLEN]byte 214 n, err := d.Marshal(buf[:]) 215 if err != nil { 216 return &PathError{"chmod", f.name, err} 217 } 218 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { 219 return &PathError{"chmod", f.name, err} 220 } 221 return nil 222 } 223 224 // Sync commits the current contents of the file to stable storage. 225 // Typically, this means flushing the file system's in-memory copy 226 // of recently written data to disk. 227 func (f *File) Sync() error { 228 if f == nil { 229 return ErrInvalid 230 } 231 var d syscall.Dir 232 d.Null() 233 234 var buf [syscall.STATFIXLEN]byte 235 n, err := d.Marshal(buf[:]) 236 if err != nil { 237 return NewSyscallError("fsync", err) 238 } 239 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { 240 return NewSyscallError("fsync", err) 241 } 242 return nil 243 } 244 245 // read reads up to len(b) bytes from the File. 246 // It returns the number of bytes read and an error, if any. 247 func (f *File) read(b []byte) (n int, err error) { 248 n, e := fixCount(syscall.Read(f.fd, b)) 249 if n == 0 && len(b) > 0 && e == nil { 250 return 0, io.EOF 251 } 252 return n, e 253 } 254 255 // pread reads len(b) bytes from the File starting at byte offset off. 256 // It returns the number of bytes read and the error, if any. 257 // EOF is signaled by a zero count with err set to nil. 258 func (f *File) pread(b []byte, off int64) (n int, err error) { 259 n, e := fixCount(syscall.Pread(f.fd, b, off)) 260 if n == 0 && len(b) > 0 && e == nil { 261 return 0, io.EOF 262 } 263 return n, e 264 } 265 266 // write writes len(b) bytes to the File. 267 // It returns the number of bytes written and an error, if any. 268 // Since Plan 9 preserves message boundaries, never allow 269 // a zero-byte write. 270 func (f *File) write(b []byte) (n int, err error) { 271 if len(b) == 0 { 272 return 0, nil 273 } 274 return fixCount(syscall.Write(f.fd, b)) 275 } 276 277 // pwrite writes len(b) bytes to the File starting at byte offset off. 278 // It returns the number of bytes written and an error, if any. 279 // Since Plan 9 preserves message boundaries, never allow 280 // a zero-byte write. 281 func (f *File) pwrite(b []byte, off int64) (n int, err error) { 282 if len(b) == 0 { 283 return 0, nil 284 } 285 return fixCount(syscall.Pwrite(f.fd, b, off)) 286 } 287 288 // seek sets the offset for the next Read or Write on file to offset, interpreted 289 // according to whence: 0 means relative to the origin of the file, 1 means 290 // relative to the current offset, and 2 means relative to the end. 291 // It returns the new offset and an error, if any. 292 func (f *File) seek(offset int64, whence int) (ret int64, err error) { 293 return syscall.Seek(f.fd, offset, whence) 294 } 295 296 // Truncate changes the size of the named file. 297 // If the file is a symbolic link, it changes the size of the link's target. 298 // If there is an error, it will be of type *PathError. 299 func Truncate(name string, size int64) error { 300 var d syscall.Dir 301 302 d.Null() 303 d.Length = size 304 305 var buf [syscall.STATFIXLEN]byte 306 n, err := d.Marshal(buf[:]) 307 if err != nil { 308 return &PathError{"truncate", name, err} 309 } 310 if err = syscall.Wstat(name, buf[:n]); err != nil { 311 return &PathError{"truncate", name, err} 312 } 313 return nil 314 } 315 316 // Remove removes the named file or directory. 317 // If there is an error, it will be of type *PathError. 318 func Remove(name string) error { 319 if e := syscall.Remove(name); e != nil { 320 return &PathError{"remove", name, e} 321 } 322 return nil 323 } 324 325 // HasPrefix from the strings package. 326 func hasPrefix(s, prefix string) bool { 327 return len(s) >= len(prefix) && s[0:len(prefix)] == prefix 328 } 329 330 // LastIndexByte from the strings package. 331 func lastIndex(s string, sep byte) int { 332 for i := len(s) - 1; i >= 0; i-- { 333 if s[i] == sep { 334 return i 335 } 336 } 337 return -1 338 } 339 340 func rename(oldname, newname string) error { 341 dirname := oldname[:lastIndex(oldname, '/')+1] 342 if hasPrefix(newname, dirname) { 343 newname = newname[len(dirname):] 344 } else { 345 return &LinkError{"rename", oldname, newname, ErrInvalid} 346 } 347 348 // If newname still contains slashes after removing the oldname 349 // prefix, the rename is cross-directory and must be rejected. 350 if lastIndex(newname, '/') >= 0 { 351 return &LinkError{"rename", oldname, newname, ErrInvalid} 352 } 353 354 var d syscall.Dir 355 356 d.Null() 357 d.Name = newname 358 359 buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) 360 n, err := d.Marshal(buf[:]) 361 if err != nil { 362 return &LinkError{"rename", oldname, newname, err} 363 } 364 365 // If newname already exists and is not a directory, rename replaces it. 366 f, err := Stat(dirname + newname) 367 if err == nil && !f.IsDir() { 368 Remove(dirname + newname) 369 } 370 371 if err = syscall.Wstat(oldname, buf[:n]); err != nil { 372 return &LinkError{"rename", oldname, newname, err} 373 } 374 return nil 375 } 376 377 // See docs in file.go:Chmod. 378 func chmod(name string, mode FileMode) error { 379 var d syscall.Dir 380 381 odir, e := dirstat(name) 382 if e != nil { 383 return &PathError{"chmod", name, e} 384 } 385 d.Null() 386 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask 387 388 var buf [syscall.STATFIXLEN]byte 389 n, err := d.Marshal(buf[:]) 390 if err != nil { 391 return &PathError{"chmod", name, err} 392 } 393 if err = syscall.Wstat(name, buf[:n]); err != nil { 394 return &PathError{"chmod", name, err} 395 } 396 return nil 397 } 398 399 // Chtimes changes the access and modification times of the named 400 // file, similar to the Unix utime() or utimes() functions. 401 // 402 // The underlying filesystem may truncate or round the values to a 403 // less precise time unit. 404 // If there is an error, it will be of type *PathError. 405 func Chtimes(name string, atime time.Time, mtime time.Time) error { 406 var d syscall.Dir 407 408 d.Null() 409 d.Atime = uint32(atime.Unix()) 410 d.Mtime = uint32(mtime.Unix()) 411 412 var buf [syscall.STATFIXLEN]byte 413 n, err := d.Marshal(buf[:]) 414 if err != nil { 415 return &PathError{"chtimes", name, err} 416 } 417 if err = syscall.Wstat(name, buf[:n]); err != nil { 418 return &PathError{"chtimes", name, err} 419 } 420 return nil 421 } 422 423 // Pipe returns a connected pair of Files; reads from r return bytes 424 // written to w. It returns the files and an error, if any. 425 func Pipe() (r *File, w *File, err error) { 426 var p [2]int 427 428 if e := syscall.Pipe(p[0:]); e != nil { 429 return nil, nil, NewSyscallError("pipe", e) 430 } 431 432 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil 433 } 434 435 // not supported on Plan 9 436 437 // Link creates newname as a hard link to the oldname file. 438 // If there is an error, it will be of type *LinkError. 439 func Link(oldname, newname string) error { 440 return &LinkError{"link", oldname, newname, syscall.EPLAN9} 441 } 442 443 // Symlink creates newname as a symbolic link to oldname. 444 // If there is an error, it will be of type *LinkError. 445 func Symlink(oldname, newname string) error { 446 return &LinkError{"symlink", oldname, newname, syscall.EPLAN9} 447 } 448 449 // Readlink returns the destination of the named symbolic link. 450 // If there is an error, it will be of type *PathError. 451 func Readlink(name string) (string, error) { 452 return "", &PathError{"readlink", name, syscall.EPLAN9} 453 } 454 455 // Chown changes the numeric uid and gid of the named file. 456 // If the file is a symbolic link, it changes the uid and gid of the link's target. 457 // If there is an error, it will be of type *PathError. 458 func Chown(name string, uid, gid int) error { 459 return &PathError{"chown", name, syscall.EPLAN9} 460 } 461 462 // Lchown changes the numeric uid and gid of the named file. 463 // If the file is a symbolic link, it changes the uid and gid of the link itself. 464 // If there is an error, it will be of type *PathError. 465 func Lchown(name string, uid, gid int) error { 466 return &PathError{"lchown", name, syscall.EPLAN9} 467 } 468 469 // Chown changes the numeric uid and gid of the named file. 470 // If there is an error, it will be of type *PathError. 471 func (f *File) Chown(uid, gid int) error { 472 if f == nil { 473 return ErrInvalid 474 } 475 return &PathError{"chown", f.name, syscall.EPLAN9} 476 } 477 478 func tempDir() string { 479 return "/tmp" 480 } 481 482 // Chdir changes the current working directory to the file, 483 // which must be a directory. 484 // If there is an error, it will be of type *PathError. 485 func (f *File) Chdir() error { 486 if err := f.checkValid("chdir"); err != nil { 487 return err 488 } 489 if e := syscall.Fchdir(f.fd); e != nil { 490 return &PathError{"chdir", f.name, e} 491 } 492 return nil 493 } 494 495 // setDeadline sets the read and write deadline. 496 func (f *File) setDeadline(time.Time) error { 497 if err := f.checkValid("SetDeadline"); err != nil { 498 return err 499 } 500 return poll.ErrNoDeadline 501 } 502 503 // setReadDeadline sets the read deadline. 504 func (f *File) setReadDeadline(time.Time) error { 505 if err := f.checkValid("SetReadDeadline"); err != nil { 506 return err 507 } 508 return poll.ErrNoDeadline 509 } 510 511 // setWriteDeadline sets the write deadline. 512 func (f *File) setWriteDeadline(time.Time) error { 513 if err := f.checkValid("SetWriteDeadline"); err != nil { 514 return err 515 } 516 return poll.ErrNoDeadline 517 } 518 519 // checkValid checks whether f is valid for use. 520 // If not, it returns an appropriate error, perhaps incorporating the operation name op. 521 func (f *File) checkValid(op string) error { 522 if f == nil { 523 return ErrInvalid 524 } 525 if f.fd == badFd { 526 return &PathError{op, f.name, ErrClosed} 527 } 528 return nil 529 }