2011-04-02 15:28:58 -06:00
|
|
|
// 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
|
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sameFile(fs1, fs2 *FileStat) bool {
|
|
|
|
a := fs1.Sys.(*Dir)
|
|
|
|
b := fs2.Sys.(*Dir)
|
|
|
|
return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
func fileInfoFromStat(d *Dir) FileInfo {
|
|
|
|
fs := &FileStat{
|
|
|
|
name: d.Name,
|
|
|
|
size: int64(d.Length),
|
|
|
|
modTime: time.Unix(int64(d.Mtime), 0),
|
|
|
|
Sys: d,
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-12-12 14:14:00 -07:00
|
|
|
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
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// arg is an open *File or a path string.
|
2011-11-01 19:49:08 -06:00
|
|
|
func dirstat(arg interface{}) (d *Dir, err error) {
|
2011-04-02 15:28:58 -06:00
|
|
|
var name string
|
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
// This is big enough for most stat messages
|
|
|
|
// and rounded to a multiple of 128 bytes.
|
|
|
|
size := (syscall.STATFIXLEN + 16*4 + 128) &^ 128
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
buf := make([]byte, size)
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
var n int
|
|
|
|
switch a := arg.(type) {
|
2011-04-02 15:28:58 -06:00
|
|
|
case *File:
|
2011-12-12 14:14:00 -07:00
|
|
|
name = a.name
|
|
|
|
n, err = syscall.Fstat(a.fd, buf)
|
2011-04-02 15:28:58 -06:00
|
|
|
case string:
|
2011-12-12 14:14:00 -07:00
|
|
|
name = a
|
|
|
|
n, err = syscall.Stat(name, buf)
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-12-12 14:14:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"stat", name, err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
if n < syscall.STATFIXLEN {
|
|
|
|
return nil, &PathError{"stat", name, Eshortstat}
|
|
|
|
}
|
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
// Pull the real size out of the stat message.
|
|
|
|
s, _ := gbit16(buf)
|
|
|
|
size = int(s)
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
// 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 = UnmarshalDir(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"stat", name, err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-12-12 14:14:00 -07:00
|
|
|
return
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &PathError{"stat", name, Ebadstat}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo structure describing the named file and an error, if any.
|
2011-12-12 14:14:00 -07:00
|
|
|
func Stat(name string) (FileInfo, error) {
|
2011-06-14 09:20:34 -06:00
|
|
|
d, err := dirstat(name)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err != nil {
|
2011-06-14 09:20:34 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2011-12-12 14:14:00 -07:00
|
|
|
return fileInfoFromStat(d), nil
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns the FileInfo structure describing the named file and an
|
|
|
|
// error, if any. If the file is a symbolic link (though Plan 9 does not have symbolic links),
|
|
|
|
// the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link.
|
2011-12-12 14:14:00 -07:00
|
|
|
func Lstat(name string) (FileInfo, error) {
|
|
|
|
return Stat(name)
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|