2010-04-02 02:11:17 -06:00
|
|
|
// Copyright 2009 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-09-05 17:59:08 -06:00
|
|
|
import (
|
|
|
|
"syscall"
|
2011-11-30 10:01:46 -07:00
|
|
|
"time"
|
2011-11-02 13:54:16 -06:00
|
|
|
"unsafe"
|
2011-09-05 17:59:08 -06:00
|
|
|
)
|
2010-04-02 02:11:17 -06:00
|
|
|
|
2011-09-05 17:59:08 -06:00
|
|
|
// Stat returns the FileInfo structure describing file.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-30 10:04:16 -07:00
|
|
|
func (file *File) Stat() (fi FileInfo, err error) {
|
2011-09-05 17:59:08 -06:00
|
|
|
if file == nil || file.fd < 0 {
|
2012-02-16 16:04:29 -07:00
|
|
|
return nil, syscall.EINVAL
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
|
|
|
if file.isdir() {
|
|
|
|
// I don't know any better way to do that for directory
|
|
|
|
return Stat(file.name)
|
|
|
|
}
|
|
|
|
var d syscall.ByHandleFileInformation
|
|
|
|
e := syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"GetFileInformationByHandle", file.name, e}
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
return toFileInfo(basename(file.name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil
|
2010-04-27 00:17:14 -06:00
|
|
|
}
|
|
|
|
|
2012-02-08 22:55:36 -07:00
|
|
|
// Stat returns a FileInfo structure describing the named file.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-30 10:04:16 -07:00
|
|
|
func Stat(name string) (fi FileInfo, err error) {
|
2011-09-05 17:59:08 -06:00
|
|
|
if len(name) == 0 {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
|
|
|
var d syscall.Win32FileAttributeData
|
|
|
|
e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d)))
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"GetFileAttributesEx", name, e}
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
return toFileInfo(basename(name), d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime), nil
|
2010-04-27 00:17:14 -06:00
|
|
|
}
|
|
|
|
|
2012-02-08 22:55:36 -07:00
|
|
|
// Lstat returns the FileInfo structure describing the named file.
|
|
|
|
// If the file is a symbolic link, the returned FileInfo
|
2011-09-05 17:59:08 -06:00
|
|
|
// 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.
|
2011-11-30 10:04:16 -07:00
|
|
|
func Lstat(name string) (fi FileInfo, err error) {
|
2011-09-05 17:59:08 -06:00
|
|
|
// No links on Windows
|
|
|
|
return Stat(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// basename removes trailing slashes and the leading
|
|
|
|
// directory name and drive letter from path name.
|
|
|
|
func basename(name string) string {
|
|
|
|
// Remove drive letter
|
|
|
|
if len(name) == 2 && name[1] == ':' {
|
|
|
|
name = "."
|
|
|
|
} else if len(name) > 2 && name[1] == ':' {
|
|
|
|
name = name[2:]
|
|
|
|
}
|
|
|
|
i := len(name) - 1
|
|
|
|
// Remove trailing slashes
|
|
|
|
for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i-- {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
|
|
|
// Remove leading directory name
|
|
|
|
for i--; i >= 0; i-- {
|
2010-04-27 00:17:14 -06:00
|
|
|
if name[i] == '/' || name[i] == '\\' {
|
|
|
|
name = name[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 17:59:08 -06:00
|
|
|
return name
|
2010-04-27 00:17:14 -06:00
|
|
|
}
|
|
|
|
|
2011-11-30 10:38:54 -07:00
|
|
|
type winTimes struct {
|
|
|
|
atime, ctime syscall.Filetime
|
|
|
|
}
|
|
|
|
|
|
|
|
func toFileInfo(name string, fa, sizehi, sizelo uint32, ctime, atime, mtime syscall.Filetime) FileInfo {
|
2012-02-02 19:16:18 -07:00
|
|
|
fs := &fileStat{
|
|
|
|
name: name,
|
|
|
|
size: int64(sizehi)<<32 + int64(sizelo),
|
|
|
|
modTime: time.Unix(0, mtime.Nanoseconds()),
|
|
|
|
sys: &winTimes{atime, ctime},
|
|
|
|
}
|
2010-10-20 17:29:45 -06:00
|
|
|
if fa&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2011-11-30 10:04:16 -07:00
|
|
|
fs.mode |= ModeDir
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2010-10-20 17:29:45 -06:00
|
|
|
if fa&syscall.FILE_ATTRIBUTE_READONLY != 0 {
|
2011-11-30 10:04:16 -07:00
|
|
|
fs.mode |= 0444
|
2010-04-13 17:30:11 -06:00
|
|
|
} else {
|
2011-11-30 10:04:16 -07:00
|
|
|
fs.mode |= 0666
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2012-02-02 19:16:18 -07:00
|
|
|
func sameFile(sys1, sys2 interface{}) bool {
|
2011-11-30 11:42:14 -07:00
|
|
|
// TODO(rsc): Do better than this, but this matches what
|
|
|
|
// used to happen when code compared .Dev and .Ino,
|
|
|
|
// which were both always zero. Obviously not all files
|
|
|
|
// are the same.
|
|
|
|
return true
|
2010-04-02 02:11:17 -06:00
|
|
|
}
|
2011-11-30 10:38:54 -07:00
|
|
|
|
|
|
|
// For testing.
|
|
|
|
func atime(fi FileInfo) time.Time {
|
2012-02-02 19:16:18 -07:00
|
|
|
return time.Unix(0, fi.Sys().(*winTimes).atime.Nanoseconds())
|
2011-11-30 10:38:54 -07:00
|
|
|
}
|