]> Cypherpunks repositories - gostls13.git/commitdiff
os: factor out newFileStatFromWin32FileAttributeData
authorqmuntal <quimmuntal@gmail.com>
Fri, 23 Feb 2024 14:04:22 +0000 (15:04 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Wed, 28 Feb 2024 20:27:13 +0000 (20:27 +0000)
The stat function is quite long on Windows. Simplify it a bit by
factoring out the creation of a fileStat from a Win32FileAttributeData.

This also makes it more consistent with the creation of fileStats
from other sources, which all have their own dedicated functions.

Change-Id: I0443f96d892b70ce7f3b5e92c5049e4e4a240c6c
Reviewed-on: https://go-review.googlesource.com/c/go/+/566435
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/os/stat_windows.go
src/os/types_windows.go

index 7d0b6abfa4c14975835c4faff85442e28f1e1393..fd948ab0e37f5230a2309db1f2821e495fbd6f99 100644 (file)
@@ -33,6 +33,15 @@ func stat(funcname, name string, followSurrogates bool) (FileInfo, error) {
        // See https://golang.org/issues/19922#issuecomment-300031421 for details.
        var fa syscall.Win32FileAttributeData
        err = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fa)))
+       if err == nil && fa.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
+               // Not a surrogate for another named entity, because it isn't any kind of reparse point.
+               // The information we got from GetFileAttributesEx is good enough for now.
+               fs := newFileStatFromWin32FileAttributeData(&fa)
+               if err := fs.saveInfoFromPath(name); err != nil {
+                       return nil, err
+               }
+               return fs, nil
+       }
 
        // GetFileAttributesEx fails with ERROR_SHARING_VIOLATION error for
        // files like c:\pagefile.sys. Use FindFirstFile for such files.
@@ -53,23 +62,6 @@ func stat(funcname, name string, followSurrogates bool) (FileInfo, error) {
                }
        }
 
-       if err == nil && fa.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
-               // Not a surrogate for another named entity, because it isn't any kind of reparse point.
-               // The information we got from GetFileAttributesEx is good enough for now.
-               fs := &fileStat{
-                       FileAttributes: fa.FileAttributes,
-                       CreationTime:   fa.CreationTime,
-                       LastAccessTime: fa.LastAccessTime,
-                       LastWriteTime:  fa.LastWriteTime,
-                       FileSizeHigh:   fa.FileSizeHigh,
-                       FileSizeLow:    fa.FileSizeLow,
-               }
-               if err := fs.saveInfoFromPath(name); err != nil {
-                       return nil, err
-               }
-               return fs, nil
-       }
-
        // Use CreateFile to determine whether the file is a name surrogate and, if so,
        // save information about the link target.
        // Set FILE_FLAG_BACKUP_SEMANTICS so that CreateFile will create the handle
index a23dee7064bdefaf4b6d6e6bb829cef55c822654..5d4a669f710ce6ca22546cec1e2e56e93639cc67 100644 (file)
@@ -76,6 +76,19 @@ func newFileStatFromGetFileInformationByHandle(path string, h syscall.Handle) (f
        }, nil
 }
 
+// newFileStatFromWin32FileAttributeData copies all required information
+// from syscall.Win32FileAttributeData d into the newly created fileStat.
+func newFileStatFromWin32FileAttributeData(d *syscall.Win32FileAttributeData) *fileStat {
+       return &fileStat{
+               FileAttributes: d.FileAttributes,
+               CreationTime:   d.CreationTime,
+               LastAccessTime: d.LastAccessTime,
+               LastWriteTime:  d.LastWriteTime,
+               FileSizeHigh:   d.FileSizeHigh,
+               FileSizeLow:    d.FileSizeLow,
+       }
+}
+
 // newFileStatFromFileIDBothDirInfo copies all required information
 // from windows.FILE_ID_BOTH_DIR_INFO d into the newly created fileStat.
 func newFileStatFromFileIDBothDirInfo(d *windows.FILE_ID_BOTH_DIR_INFO) *fileStat {