1
0
mirror of https://github.com/golang/go synced 2024-11-20 02:54:39 -07:00
go/src/os/stat_plan9.go
David du Colombier 2a74f436aa os: emulate plan 9 libc in stat
This change is a recreation of the CL written
by Nick Owens on http://golang.org/cl/150730043.

If the stat buffer is too short, the kernel
informs us by putting the 2-byte size in the
buffer, so we read that and try again.

This follows the same algorithm as /sys/src/libc/9sys/dirfstat.c.

Fixes #8781.

Change-Id: I01b4ad3a5e705dd4cab6673c7a119f8bef9bbd7c
Reviewed-on: https://go-review.googlesource.com/3281
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-01-26 06:34:07 +00:00

113 lines
2.5 KiB
Go

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import (
"syscall"
"time"
)
const _BIT16SZ = 2
func sameFile(fs1, fs2 *fileStat) bool {
a := fs1.sys.(*syscall.Dir)
b := fs2.sys.(*syscall.Dir)
return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev
}
func fileInfoFromStat(d *syscall.Dir) FileInfo {
fs := &fileStat{
name: d.Name,
size: int64(d.Length),
modTime: time.Unix(int64(d.Mtime), 0),
sys: d,
}
fs.mode = FileMode(d.Mode & 0777)
if d.Mode&syscall.DMDIR != 0 {
fs.mode |= ModeDir
}
if d.Mode&syscall.DMAPPEND != 0 {
fs.mode |= ModeAppend
}
if d.Mode&syscall.DMEXCL != 0 {
fs.mode |= ModeExclusive
}
if d.Mode&syscall.DMTMP != 0 {
fs.mode |= ModeTemporary
}
return fs
}
// arg is an open *File or a path string.
func dirstat(arg interface{}) (*syscall.Dir, error) {
var name string
var err error
size := syscall.STATFIXLEN + 16*4
for i := 0; i < 2; i++ {
buf := make([]byte, _BIT16SZ+size)
var n int
switch a := arg.(type) {
case *File:
name = a.name
n, err = syscall.Fstat(a.fd, buf)
case string:
name = a
n, err = syscall.Stat(a, buf)
default:
panic("phase error in dirstat")
}
if n < _BIT16SZ {
return nil, &PathError{"stat", name, syscall.ErrShortStat}
}
// Pull the real size out of the stat message.
size = int(uint16(buf[0]) | uint16(buf[1])<<8)
// If the stat message is larger than our buffer we will
// go around the loop and allocate one that is big enough.
if size <= n {
d, err := syscall.UnmarshalDir(buf[:n])
if err != nil {
return nil, &PathError{"stat", name, err}
}
return d, nil
}
}
if err == nil {
err = syscall.ErrBadStat
}
return nil, &PathError{"stat", name, err}
}
// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (fi FileInfo, err error) {
d, err := dirstat(name)
if err != nil {
return nil, err
}
return fileInfoFromStat(d), nil
}
// Lstat returns a FileInfo describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (fi FileInfo, err error) {
return Stat(name)
}
// For testing.
func atime(fi FileInfo) time.Time {
return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
}