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 (
|
2016-05-04 20:44:28 -06:00
|
|
|
"internal/syscall/windows"
|
2017-12-04 11:57:48 -07:00
|
|
|
"internal/testlog"
|
2011-09-05 17:59:08 -06:00
|
|
|
"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
|
|
|
|
}
|
2017-07-20 01:20:46 -06:00
|
|
|
|
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
|
|
|
}
|
2016-03-17 22:00:26 -06:00
|
|
|
|
2017-02-10 16:17:38 -07:00
|
|
|
ft, err := file.pfd.GetFileType()
|
2016-03-17 22:00:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"GetFileType", file.name, err}
|
|
|
|
}
|
2016-12-06 16:49:45 -07:00
|
|
|
switch ft {
|
|
|
|
case syscall.FILE_TYPE_PIPE, syscall.FILE_TYPE_CHAR:
|
|
|
|
return &fileStat{name: basename(file.name), filetype: ft}, nil
|
2016-03-17 22:00:26 -06:00
|
|
|
}
|
|
|
|
|
2011-09-05 17:59:08 -06:00
|
|
|
var d syscall.ByHandleFileInformation
|
2017-02-10 16:17:38 -07:00
|
|
|
err = file.pfd.GetFileInformationByHandle(&d)
|
2016-03-17 22:00:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"GetFileInformationByHandle", file.name, err}
|
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,
|
|
|
|
},
|
2016-12-06 16:49:45 -07:00
|
|
|
filetype: ft,
|
|
|
|
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) {
|
2017-12-04 11:57:48 -07:00
|
|
|
testlog.Stat(name)
|
2017-04-27 01:43:33 -06:00
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
|
|
|
|
}
|
|
|
|
if name == DevNull {
|
|
|
|
return &devNullStat, nil
|
|
|
|
}
|
|
|
|
namep, err := syscall.UTF16PtrFromString(fixLongPath(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"Stat", name, err}
|
|
|
|
}
|
2017-11-02 23:48:43 -06:00
|
|
|
// Apparently (see https://golang.org/issues/19922#issuecomment-300031421)
|
2017-05-09 00:50:41 -06:00
|
|
|
// GetFileAttributesEx is fastest approach to get file info.
|
|
|
|
// It does not work for symlinks. But symlinks are rare,
|
|
|
|
// so try GetFileAttributesEx first.
|
|
|
|
var fs fileStat
|
|
|
|
err = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys)))
|
|
|
|
if err == nil && fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
|
|
|
|
fs.path = name
|
|
|
|
if !isAbs(fs.path) {
|
|
|
|
fs.path, err = syscall.FullPath(fs.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"FullPath", name, err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.name = basename(name)
|
|
|
|
return &fs, nil
|
|
|
|
}
|
2017-05-07 12:04:24 -06:00
|
|
|
// Use Windows I/O manager to dereference the symbolic link, as per
|
2017-04-27 01:43:33 -06:00
|
|
|
// https://blogs.msdn.microsoft.com/oldnewthing/20100212-00/?p=14963/
|
|
|
|
h, err := syscall.CreateFile(namep, 0, 0, nil,
|
|
|
|
syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
|
|
|
|
if err != nil {
|
|
|
|
if err == windows.ERROR_SHARING_VIOLATION {
|
|
|
|
// try FindFirstFile now that CreateFile failed
|
|
|
|
return statWithFindFirstFile(name, namep)
|
2014-07-17 01:02:46 -06:00
|
|
|
}
|
2017-04-27 01:43:33 -06:00
|
|
|
return nil, &PathError{"CreateFile", name, err}
|
|
|
|
}
|
|
|
|
defer syscall.CloseHandle(h)
|
|
|
|
|
|
|
|
var d syscall.ByHandleFileInformation
|
|
|
|
err = syscall.GetFileInformationByHandle(h, &d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"GetFileInformationByHandle", name, err}
|
|
|
|
}
|
|
|
|
return &fileStat{
|
|
|
|
name: basename(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,
|
2017-05-07 12:04:24 -06:00
|
|
|
// fileStat.path is used by os.SameFile to decide if it needs
|
2017-04-27 01:43:33 -06:00
|
|
|
// to fetch vol, idxhi and idxlo. But these are already set,
|
|
|
|
// so set fileStat.path to "" to prevent os.SameFile doing it again.
|
|
|
|
// Also do not set fileStat.filetype, because it is only used for
|
|
|
|
// console and stdin/stdout. But you cannot call os.Stat for these.
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-07 12:04:24 -06:00
|
|
|
// statWithFindFirstFile is used by Stat to handle special case of statting
|
|
|
|
// c:\pagefile.sys. We might discover that other files need similar treatment.
|
2017-04-27 01:43:33 -06:00
|
|
|
func statWithFindFirstFile(name string, namep *uint16) (FileInfo, error) {
|
|
|
|
var fd syscall.Win32finddata
|
|
|
|
h, err := syscall.FindFirstFile(namep, &fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"FindFirstFile", name, err}
|
|
|
|
}
|
|
|
|
syscall.FindClose(h)
|
|
|
|
|
|
|
|
fullpath := name
|
|
|
|
if !isAbs(fullpath) {
|
|
|
|
fullpath, err = syscall.FullPath(fullpath)
|
2014-07-17 01:02:46 -06:00
|
|
|
if err != nil {
|
2017-04-27 01:43:33 -06:00
|
|
|
return nil, &PathError{"FullPath", name, err}
|
2017-04-07 05:32:46 -06:00
|
|
|
}
|
2014-07-17 01:02:46 -06:00
|
|
|
}
|
2017-04-27 01:43:33 -06:00
|
|
|
return &fileStat{
|
|
|
|
name: basename(name),
|
|
|
|
path: fullpath,
|
|
|
|
sys: syscall.Win32FileAttributeData{
|
|
|
|
FileAttributes: fd.FileAttributes,
|
|
|
|
CreationTime: fd.CreationTime,
|
|
|
|
LastAccessTime: fd.LastAccessTime,
|
|
|
|
LastWriteTime: fd.LastWriteTime,
|
|
|
|
FileSizeHigh: fd.FileSizeHigh,
|
|
|
|
FileSizeLow: fd.FileSizeLow,
|
|
|
|
},
|
|
|
|
}, nil
|
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) {
|
2017-12-04 11:57:48 -07:00
|
|
|
testlog.Stat(name)
|
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)}
|
2016-10-28 11:01:51 -06:00
|
|
|
namep, e := syscall.UTF16PtrFromString(fixLongPath(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
|
|
|
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 {
|
2016-05-04 20:44:28 -06:00
|
|
|
if e != windows.ERROR_SHARING_VIOLATION {
|
|
|
|
return nil, &PathError{"GetFileAttributesEx", name, e}
|
|
|
|
}
|
|
|
|
// try FindFirstFile now that GetFileAttributesEx failed
|
2017-04-27 01:43:33 -06:00
|
|
|
return statWithFindFirstFile(name, namep)
|
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 {
|
2017-05-09 00:50:41 -06:00
|
|
|
return nil, &PathError{"FullPath", name, e}
|
2014-08-18 22:59:56 -06:00
|
|
|
}
|
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
|
|
|
}
|