Source file src/os/stat_windows.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os
     6  
     7  import (
     8  	"internal/syscall/windows"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  // Stat returns the [FileInfo] structure describing file.
    14  // If there is an error, it will be of type [*PathError].
    15  func (file *File) Stat() (FileInfo, error) {
    16  	if file == nil {
    17  		return nil, ErrInvalid
    18  	}
    19  	return statHandle(file.name, file.pfd.Sysfd)
    20  }
    21  
    22  // stat implements both Stat and Lstat of a file.
    23  func stat(funcname, name string, followSurrogates bool) (FileInfo, error) {
    24  	if len(name) == 0 {
    25  		return nil, &PathError{Op: funcname, Path: name, Err: syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
    26  	}
    27  	namep, err := syscall.UTF16PtrFromString(fixLongPath(name))
    28  	if err != nil {
    29  		return nil, &PathError{Op: funcname, Path: name, Err: err}
    30  	}
    31  
    32  	// Try GetFileAttributesEx first, because it is faster than CreateFile.
    33  	// See https://golang.org/issues/19922#issuecomment-300031421 for details.
    34  	var fa syscall.Win32FileAttributeData
    35  	err = syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fa)))
    36  	if err == nil && fa.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
    37  		// Not a surrogate for another named entity, because it isn't any kind of reparse point.
    38  		// The information we got from GetFileAttributesEx is good enough for now.
    39  		fs := newFileStatFromWin32FileAttributeData(&fa)
    40  		if err := fs.saveInfoFromPath(name); err != nil {
    41  			return nil, err
    42  		}
    43  		return fs, nil
    44  	}
    45  
    46  	// GetFileAttributesEx fails with ERROR_SHARING_VIOLATION error for
    47  	// files like c:\pagefile.sys. Use FindFirstFile for such files.
    48  	if err == windows.ERROR_SHARING_VIOLATION {
    49  		var fd syscall.Win32finddata
    50  		sh, err := syscall.FindFirstFile(namep, &fd)
    51  		if err != nil {
    52  			return nil, &PathError{Op: "FindFirstFile", Path: name, Err: err}
    53  		}
    54  		syscall.FindClose(sh)
    55  		if fd.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
    56  			// Not a surrogate for another named entity. FindFirstFile is good enough.
    57  			fs := newFileStatFromWin32finddata(&fd)
    58  			if err := fs.saveInfoFromPath(name); err != nil {
    59  				return nil, err
    60  			}
    61  			return fs, nil
    62  		}
    63  	}
    64  
    65  	// Use CreateFile to determine whether the file is a name surrogate and, if so,
    66  	// save information about the link target.
    67  	// Set FILE_FLAG_BACKUP_SEMANTICS so that CreateFile will create the handle
    68  	// even if name refers to a directory.
    69  	var flags uint32 = syscall.FILE_FLAG_BACKUP_SEMANTICS | syscall.FILE_FLAG_OPEN_REPARSE_POINT
    70  	h, err := syscall.CreateFile(namep, 0, 0, nil, syscall.OPEN_EXISTING, flags, 0)
    71  
    72  	if err == windows.ERROR_INVALID_PARAMETER {
    73  		// Console handles, like "\\.\con", require generic read access. See
    74  		// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew#consoles.
    75  		// We haven't set it previously because it is normally not required
    76  		// to read attributes and some files may not allow it.
    77  		h, err = syscall.CreateFile(namep, syscall.GENERIC_READ, 0, nil, syscall.OPEN_EXISTING, flags, 0)
    78  	}
    79  	if err != nil {
    80  		// Since CreateFile failed, we can't determine whether name refers to a
    81  		// name surrogate, or some other kind of reparse point. Since we can't return a
    82  		// FileInfo with a known-accurate Mode, we must return an error.
    83  		return nil, &PathError{Op: "CreateFile", Path: name, Err: err}
    84  	}
    85  
    86  	fi, err := statHandle(name, h)
    87  	syscall.CloseHandle(h)
    88  	if err == nil && followSurrogates && fi.(*fileStat).isReparseTagNameSurrogate() {
    89  		// To obtain information about the link target, we reopen the file without
    90  		// FILE_FLAG_OPEN_REPARSE_POINT and examine the resulting handle.
    91  		// (See https://devblogs.microsoft.com/oldnewthing/20100212-00/?p=14963.)
    92  		h, err = syscall.CreateFile(namep, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
    93  		if err != nil {
    94  			// name refers to a symlink, but we couldn't resolve the symlink target.
    95  			return nil, &PathError{Op: "CreateFile", Path: name, Err: err}
    96  		}
    97  		defer syscall.CloseHandle(h)
    98  		return statHandle(name, h)
    99  	}
   100  	return fi, err
   101  }
   102  
   103  func statHandle(name string, h syscall.Handle) (FileInfo, error) {
   104  	ft, err := syscall.GetFileType(h)
   105  	if err != nil {
   106  		return nil, &PathError{Op: "GetFileType", Path: name, Err: err}
   107  	}
   108  	switch ft {
   109  	case syscall.FILE_TYPE_PIPE, syscall.FILE_TYPE_CHAR:
   110  		return &fileStat{name: basename(name), filetype: ft}, nil
   111  	}
   112  	fs, err := newFileStatFromGetFileInformationByHandle(name, h)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	fs.filetype = ft
   117  	return fs, err
   118  }
   119  
   120  // statNolog implements Stat for Windows.
   121  func statNolog(name string) (FileInfo, error) {
   122  	return stat("Stat", name, true)
   123  }
   124  
   125  // lstatNolog implements Lstat for Windows.
   126  func lstatNolog(name string) (FileInfo, error) {
   127  	followSurrogates := false
   128  	if name != "" && IsPathSeparator(name[len(name)-1]) {
   129  		// We try to implement POSIX semantics for Lstat path resolution
   130  		// (per https://pubs.opengroup.org/onlinepubs/9699919799.2013edition/basedefs/V1_chap04.html#tag_04_12):
   131  		// symlinks before the last separator in the path must be resolved. Since
   132  		// the last separator in this case follows the last path element, we should
   133  		// follow symlinks in the last path element.
   134  		followSurrogates = true
   135  	}
   136  	return stat("Lstat", name, followSurrogates)
   137  }
   138  

View as plain text