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-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.
|
2015-07-17 09:26:29 -06:00
|
|
|
func (file *File) Stat() (FileInfo, error) {
|
2013-08-19 22:33:03 -06:00
|
|
|
if file == nil {
|
|
|
|
return nil, ErrInvalid
|
|
|
|
}
|
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
|
2015-12-28 05:35:48 -07:00
|
|
|
return Stat(file.dirinfo.path)
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
2012-03-14 23:33:45 -06:00
|
|
|
if file.name == DevNull {
|
2013-01-30 23:17:37 -07:00
|
|
|
return &devNullStat, nil
|
2012-03-14 23:33:45 -06:00
|
|
|
}
|
2011-09-05 17:59:08 -06:00
|
|
|
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
|
|
|
}
|
2012-02-26 18:29:33 -07:00
|
|
|
return &fileStat{
|
2013-01-30 23:17:37 -07:00
|
|
|
name: basename(file.name),
|
|
|
|
sys: syscall.Win32FileAttributeData{
|
|
|
|
FileAttributes: d.FileAttributes,
|
|
|
|
CreationTime: d.CreationTime,
|
|
|
|
LastAccessTime: d.LastAccessTime,
|
|
|
|
LastWriteTime: d.LastWriteTime,
|
|
|
|
FileSizeHigh: d.FileSizeHigh,
|
|
|
|
FileSizeLow: d.FileSizeLow,
|
|
|
|
},
|
|
|
|
vol: d.VolumeSerialNumber,
|
|
|
|
idxhi: d.FileIndexHigh,
|
|
|
|
idxlo: d.FileIndexLow,
|
2012-02-26 18:29:33 -07:00
|
|
|
}, 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.
|
2015-07-17 09:26:29 -06:00
|
|
|
func Stat(name string) (FileInfo, error) {
|
|
|
|
var fi FileInfo
|
|
|
|
var err error
|
2014-07-17 01:02:46 -06:00
|
|
|
for {
|
|
|
|
fi, err = Lstat(name)
|
|
|
|
if err != nil {
|
2015-07-17 09:26:29 -06:00
|
|
|
return fi, err
|
2014-07-17 01:02:46 -06:00
|
|
|
}
|
|
|
|
if fi.Mode()&ModeSymlink == 0 {
|
2015-07-17 09:26:29 -06:00
|
|
|
return fi, nil
|
2014-07-17 01:02:46 -06:00
|
|
|
}
|
|
|
|
name, err = Readlink(name)
|
|
|
|
if err != nil {
|
2015-07-17 09:26:29 -06:00
|
|
|
return fi, err
|
2014-07-17 01:02:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat returns the FileInfo structure describing the named file.
|
|
|
|
// If the file is a symbolic link, the returned FileInfo
|
2016-03-01 16:21:55 -07:00
|
|
|
// describes the symbolic link. Lstat makes no attempt to follow the link.
|
2014-07-17 01:02:46 -06:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-07-17 09:26:29 -06:00
|
|
|
func Lstat(name string) (FileInfo, error) {
|
2011-09-05 17:59:08 -06:00
|
|
|
if len(name) == 0 {
|
2014-07-17 01:02:46 -06:00
|
|
|
return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
|
2011-09-05 17:59:08 -06:00
|
|
|
}
|
2012-03-14 23:33:45 -06:00
|
|
|
if name == DevNull {
|
2013-01-30 23:17:37 -07:00
|
|
|
return &devNullStat, nil
|
2012-03-14 23:33:45 -06:00
|
|
|
}
|
2013-01-30 23:17:37 -07:00
|
|
|
fs := &fileStat{name: basename(name)}
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 15:24:32 -06:00
|
|
|
namep, e := syscall.UTF16PtrFromString(name)
|
|
|
|
if e != nil {
|
2014-07-17 01:02:46 -06:00
|
|
|
return nil, &PathError{"Lstat", name, e}
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 15:24:32 -06:00
|
|
|
}
|
2013-01-30 23:17:37 -07:00
|
|
|
e = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys)))
|
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
|
|
|
}
|
2013-01-30 23:17:37 -07:00
|
|
|
fs.path = name
|
|
|
|
if !isAbs(fs.path) {
|
2014-08-18 22:59:56 -06:00
|
|
|
fs.path, e = syscall.FullPath(fs.path)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2012-02-26 18:29:33 -07:00
|
|
|
}
|
2013-01-30 23:17:37 -07:00
|
|
|
return fs, nil
|
2010-04-27 00:17:14 -06:00
|
|
|
}
|
|
|
|
|
2011-09-05 17:59:08 -06:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2012-02-26 18:29:33 -07:00
|
|
|
func isAbs(path string) (b bool) {
|
|
|
|
v := volumeName(path)
|
|
|
|
if v == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
path = path[len(v):]
|
|
|
|
if path == "" {
|
|
|
|
return false
|
|
|
|
}
|
2014-07-17 01:02:46 -06:00
|
|
|
return IsPathSeparator(path[0])
|
2011-11-30 10:38:54 -07:00
|
|
|
}
|
|
|
|
|
2012-02-26 18:29:33 -07:00
|
|
|
func volumeName(path string) (v string) {
|
|
|
|
if len(path) < 2 {
|
|
|
|
return ""
|
2012-02-02 19:16:18 -07:00
|
|
|
}
|
2012-02-26 18:29:33 -07:00
|
|
|
// with drive letter
|
|
|
|
c := path[0]
|
|
|
|
if path[1] == ':' &&
|
|
|
|
('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
|
|
|
|
'A' <= c && c <= 'Z') {
|
|
|
|
return path[:2]
|
|
|
|
}
|
|
|
|
// is it UNC
|
2014-07-17 01:02:46 -06:00
|
|
|
if l := len(path); l >= 5 && IsPathSeparator(path[0]) && IsPathSeparator(path[1]) &&
|
|
|
|
!IsPathSeparator(path[2]) && path[2] != '.' {
|
2012-02-26 18:29:33 -07:00
|
|
|
// first, leading `\\` and next shouldn't be `\`. its server name.
|
|
|
|
for n := 3; n < l-1; n++ {
|
|
|
|
// second, next '\' shouldn't be repeated.
|
2014-07-17 01:02:46 -06:00
|
|
|
if IsPathSeparator(path[n]) {
|
2012-02-26 18:29:33 -07:00
|
|
|
n++
|
|
|
|
// third, following something characters. its share name.
|
2014-07-17 01:02:46 -06:00
|
|
|
if !IsPathSeparator(path[n]) {
|
2012-02-26 18:29:33 -07:00
|
|
|
if path[n] == '.' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for ; n < l; n++ {
|
2014-07-17 01:02:46 -06:00
|
|
|
if IsPathSeparator(path[n]) {
|
2012-02-26 18:29:33 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path[:n]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|