2014-09-21 17:33:12 +00: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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A fileStat is the implementation of FileInfo returned by Stat and Lstat.
|
|
|
|
type fileStat struct {
|
2018-12-28 15:30:48 +00:00
|
|
|
name string
|
|
|
|
sys syscall.Win32FileAttributeData
|
|
|
|
filetype uint32 // what syscall.GetFileType returns
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
// used to implement SameFile
|
|
|
|
sync.Mutex
|
2018-12-28 15:30:48 +00:00
|
|
|
path string
|
|
|
|
vol uint32
|
|
|
|
idxhi uint32
|
|
|
|
idxlo uint32
|
|
|
|
appendNameToPath bool
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileStat) Size() int64 {
|
|
|
|
return int64(fs.sys.FileSizeHigh)<<32 + int64(fs.sys.FileSizeLow)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileStat) Mode() (m FileMode) {
|
|
|
|
if fs == &devNullStat {
|
|
|
|
return ModeDevice | ModeCharDevice | 0666
|
|
|
|
}
|
|
|
|
if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_READONLY != 0 {
|
|
|
|
m |= 0444
|
|
|
|
} else {
|
|
|
|
m |= 0666
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 {
|
2018-12-28 15:30:48 +00:00
|
|
|
return m | ModeSymlink
|
|
|
|
}
|
|
|
|
if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
|
|
|
m |= ModeDir | 0111
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
switch fs.filetype {
|
|
|
|
case syscall.FILE_TYPE_PIPE:
|
2017-10-07 00:16:47 +00:00
|
|
|
m |= ModeNamedPipe
|
2018-12-28 15:30:48 +00:00
|
|
|
case syscall.FILE_TYPE_CHAR:
|
|
|
|
m |= ModeCharDevice
|
2017-10-07 00:16:47 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileStat) ModTime() time.Time {
|
|
|
|
return time.Unix(0, fs.sys.LastWriteTime.Nanoseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sys returns syscall.Win32FileAttributeData for file fs.
|
|
|
|
func (fs *fileStat) Sys() interface{} { return &fs.sys }
|
|
|
|
|
|
|
|
func (fs *fileStat) loadFileId() error {
|
|
|
|
fs.Lock()
|
|
|
|
defer fs.Unlock()
|
|
|
|
if fs.path == "" {
|
|
|
|
// already done
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
var path string
|
|
|
|
if fs.appendNameToPath {
|
|
|
|
path = fs.path + `\` + fs.name
|
|
|
|
} else {
|
|
|
|
path = fs.path
|
|
|
|
}
|
|
|
|
pathp, err := syscall.UTF16PtrFromString(path)
|
2014-09-21 17:33:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h, err := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.CloseHandle(h)
|
|
|
|
var i syscall.ByHandleFileInformation
|
2017-10-07 00:16:47 +00:00
|
|
|
err = syscall.GetFileInformationByHandle(h, &i)
|
2014-09-21 17:33:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fs.path = ""
|
|
|
|
fs.vol = i.VolumeSerialNumber
|
|
|
|
fs.idxhi = i.FileIndexHigh
|
|
|
|
fs.idxlo = i.FileIndexLow
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// devNullStat is fileStat structure describing DevNull file ("NUL").
|
|
|
|
var devNullStat = fileStat{
|
|
|
|
name: DevNull,
|
|
|
|
// hopefully this will work for SameFile
|
|
|
|
vol: 0,
|
|
|
|
idxhi: 0,
|
|
|
|
idxlo: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameFile(fs1, fs2 *fileStat) bool {
|
|
|
|
e := fs1.loadFileId()
|
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
e = fs2.loadFileId()
|
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fs1.vol == fs2.vol && fs1.idxhi == fs2.idxhi && fs1.idxlo == fs2.idxlo
|
|
|
|
}
|
|
|
|
|
|
|
|
// For testing.
|
|
|
|
func atime(fi FileInfo) time.Time {
|
|
|
|
return time.Unix(0, fi.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
|
|
|
|
}
|