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