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