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"
|
|
|
|
)
|
|
|
|
|
2015-01-24 18:19:39 -07:00
|
|
|
const _BIT16SZ = 2
|
|
|
|
|
2013-01-30 23:17:37 -07:00
|
|
|
func sameFile(fs1, fs2 *fileStat) bool {
|
|
|
|
a := fs1.sys.(*syscall.Dir)
|
|
|
|
b := fs2.sys.(*syscall.Dir)
|
2011-12-12 14:14:00 -07:00
|
|
|
return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
func fileInfoFromStat(d *syscall.Dir) FileInfo {
|
2012-02-02 19:16:18 -07:00
|
|
|
fs := &fileStat{
|
2011-12-12 14:14:00 -07:00
|
|
|
name: d.Name,
|
|
|
|
size: int64(d.Length),
|
|
|
|
modTime: time.Unix(int64(d.Mtime), 0),
|
2012-02-02 19:16:18 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-30 14:38:01 -06:00
|
|
|
// arg is an open *File or a path string.
|
2012-11-26 16:26:46 -07:00
|
|
|
func dirstat(arg interface{}) (*syscall.Dir, error) {
|
2011-04-02 15:28:58 -06:00
|
|
|
var name string
|
2015-01-24 18:19:39 -07:00
|
|
|
var err error
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2015-01-24 18:19:39 -07:00
|
|
|
size := syscall.STATFIXLEN + 16*4
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
for i := 0; i < 2; i++ {
|
2015-01-24 18:19:39 -07:00
|
|
|
buf := make([]byte, _BIT16SZ+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
|
2012-11-26 16:26:46 -07:00
|
|
|
n, err = syscall.Stat(a, buf)
|
|
|
|
default:
|
|
|
|
panic("phase error in dirstat")
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2015-01-24 18:19:39 -07:00
|
|
|
|
|
|
|
if n < _BIT16SZ {
|
2012-11-26 16:26:46 -07:00
|
|
|
return nil, &PathError{"stat", name, syscall.ErrShortStat}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2011-12-12 14:14:00 -07:00
|
|
|
// Pull the real size out of the stat message.
|
2012-11-26 16:26:46 -07:00
|
|
|
size = int(uint16(buf[0]) | uint16(buf[1])<<8)
|
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.
|
2015-01-24 18:19:39 -07:00
|
|
|
if size <= n {
|
|
|
|
d, err := syscall.UnmarshalDir(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"stat", name, err}
|
|
|
|
}
|
|
|
|
return d, nil
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2012-11-26 16:26:46 -07:00
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2015-01-24 18:19:39 -07:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = syscall.ErrBadStat
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &PathError{"stat", name, err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// Stat returns a FileInfo describing the named file.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-05-01 23:44:38 -06:00
|
|
|
func Stat(name string) (fi FileInfo, err 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
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// 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.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-05-01 23:44:38 -06:00
|
|
|
func Lstat(name string) (fi FileInfo, err error) {
|
2011-12-12 14:14:00 -07:00
|
|
|
return Stat(name)
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2012-01-25 01:15:44 -07:00
|
|
|
|
|
|
|
// For testing.
|
|
|
|
func atime(fi FileInfo) time.Time {
|
2012-11-26 16:26:46 -07:00
|
|
|
return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
|
2012-01-25 01:15:44 -07:00
|
|
|
}
|