Source file src/os/file_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  	"errors"
     9  	"internal/filepathlite"
    10  	"internal/godebug"
    11  	"internal/poll"
    12  	"internal/syscall/windows"
    13  	"runtime"
    14  	"sync"
    15  	"sync/atomic"
    16  	"syscall"
    17  	"unsafe"
    18  )
    19  
    20  // This matches the value in syscall/syscall_windows.go.
    21  const _UTIME_OMIT = -1
    22  
    23  // file is the real representation of *File.
    24  // The extra level of indirection ensures that no clients of os
    25  // can overwrite this data, which could cause the finalizer
    26  // to close the wrong file descriptor.
    27  type file struct {
    28  	pfd        poll.FD
    29  	name       string
    30  	dirinfo    atomic.Pointer[dirInfo] // nil unless directory being read
    31  	appendMode bool                    // whether file is opened for appending
    32  }
    33  
    34  // fd is the Windows implementation of Fd.
    35  func (file *File) fd() uintptr {
    36  	if file == nil {
    37  		return uintptr(syscall.InvalidHandle)
    38  	}
    39  	// Try to disassociate the file from the runtime poller.
    40  	// File.Fd doesn't return an error, so we don't have a way to
    41  	// report it. We just ignore it. It's up to the caller to call
    42  	// it when there are no concurrent IO operations.
    43  	_ = file.pfd.DisassociateIOCP()
    44  	return uintptr(file.pfd.Sysfd)
    45  }
    46  
    47  // newFileKind describes the kind of file to newFile.
    48  type newFileKind int
    49  
    50  const (
    51  	// kindNewFile means that the descriptor was passed to us via NewFile.
    52  	kindNewFile newFileKind = iota
    53  	// kindOpenFile means that the descriptor was opened using
    54  	// Open, Create, or OpenFile.
    55  	kindOpenFile
    56  	// kindPipe means that the descriptor was opened using Pipe.
    57  	kindPipe
    58  	// kindSock means that the descriptor is a network file descriptor
    59  	// that was created from net package and was opened using net_newUnixFile.
    60  	kindSock
    61  	// kindConsole means that the descriptor is a console handle.
    62  	kindConsole
    63  )
    64  
    65  // newFile returns a new File with the given file handle and name.
    66  // Unlike NewFile, it does not check that h is syscall.InvalidHandle.
    67  // If nonBlocking is true, it tries to add the file to the runtime poller.
    68  func newFile(h syscall.Handle, name string, kind newFileKind, nonBlocking bool) *File {
    69  	typ := "file"
    70  	switch kind {
    71  	case kindNewFile, kindOpenFile:
    72  		t, err := syscall.GetFileType(h)
    73  		if err != nil || t == syscall.FILE_TYPE_CHAR {
    74  			var m uint32
    75  			if syscall.GetConsoleMode(h, &m) == nil {
    76  				typ = "console"
    77  				// Console handles are always blocking.
    78  				break
    79  			}
    80  		} else if t == syscall.FILE_TYPE_PIPE {
    81  			typ = "pipe"
    82  		}
    83  	case kindPipe:
    84  		typ = "pipe"
    85  	case kindSock:
    86  		typ = "file+net"
    87  	case kindConsole:
    88  		typ = "console"
    89  	default:
    90  		panic("newFile with unknown kind")
    91  	}
    92  
    93  	// Completion notification modes are shared by all handles to the file
    94  	// object. Preserve them for handles passed to NewFile, since other users
    95  	// of the file object may rely on those modes. See go.dev/issue/80979.
    96  	f := &File{&file{
    97  		pfd: poll.FD{
    98  			Sysfd:                   h,
    99  			IsStream:                true,
   100  			ZeroReadIsEOF:           true,
   101  			KeepFileCompletionModes: kind == kindNewFile,
   102  		},
   103  		name: name,
   104  	}}
   105  	runtime.SetFinalizer(f.file, (*file).close)
   106  
   107  	overlapped := &nonBlocking
   108  	if kind == kindNewFile && typ != "console" {
   109  		// Detecting the mode can block behind outstanding synchronous I/O.
   110  		// Defer it until first use, including for inherited standard handles.
   111  		// See go.dev/issue/75949 and go.dev/issue/76391.
   112  		overlapped = nil
   113  	}
   114  	// Ignore initialization errors.
   115  	// Assume any problems will show up in later I/O.
   116  	f.pfd.Init(typ, overlapped)
   117  	return f
   118  }
   119  
   120  // newConsoleFile creates new File that will be used as console.
   121  func newConsoleFile(h syscall.Handle, name string) *File {
   122  	return newFile(h, name, kindConsole, false)
   123  }
   124  
   125  // newFileFromNewFile is called by [NewFile].
   126  func newFileFromNewFile(fd uintptr, name string) *File {
   127  	h := syscall.Handle(fd)
   128  	if h == syscall.InvalidHandle {
   129  		return nil
   130  	}
   131  	return newFile(h, name, kindNewFile, false)
   132  }
   133  
   134  // net_newWindowsFile is a hidden entry point called by net.conn.File.
   135  // This is used so that the File.pfd.close method calls [syscall.Closesocket]
   136  // instead of [syscall.CloseHandle].
   137  //
   138  //go:linkname net_newWindowsFile net.newWindowsFile
   139  func net_newWindowsFile(h syscall.Handle, name string) *File {
   140  	if h == syscall.InvalidHandle {
   141  		panic("invalid FD")
   142  	}
   143  	return newFile(h, name, kindSock, true)
   144  }
   145  
   146  func epipecheck(file *File, e error) {
   147  }
   148  
   149  // DevNull is the name of the operating system's “null device.”
   150  // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
   151  const DevNull = "NUL"
   152  
   153  // openFileNolog is the Windows implementation of OpenFile.
   154  func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
   155  	if name == "" {
   156  		return nil, &PathError{Op: "open", Path: name, Err: syscall.ENOENT}
   157  	}
   158  	path := fixLongPath(name)
   159  	r, err := syscall.Open(path, flag|syscall.O_CLOEXEC, syscallMode(perm))
   160  	if err != nil {
   161  		return nil, &PathError{Op: "open", Path: name, Err: err}
   162  	}
   163  	nonblocking := flag&windows.O_FILE_FLAG_OVERLAPPED != 0
   164  	return newFile(r, name, kindOpenFile, nonblocking), nil
   165  }
   166  
   167  func openDirNolog(name string) (*File, error) {
   168  	return openFileNolog(name, O_RDONLY|windows.O_DIRECTORY, 0)
   169  }
   170  
   171  func (file *file) close() error {
   172  	if file == nil {
   173  		return syscall.EINVAL
   174  	}
   175  	if info := file.dirinfo.Swap(nil); info != nil {
   176  		info.close()
   177  	}
   178  	var err error
   179  	if e := file.pfd.Close(); e != nil {
   180  		if e == poll.ErrFileClosing {
   181  			e = ErrClosed
   182  		}
   183  		err = &PathError{Op: "close", Path: file.name, Err: e}
   184  	}
   185  
   186  	// no need for a finalizer anymore
   187  	runtime.SetFinalizer(file, nil)
   188  	return err
   189  }
   190  
   191  // seek sets the offset for the next Read or Write on file to offset, interpreted
   192  // according to whence: 0 means relative to the origin of the file, 1 means
   193  // relative to the current offset, and 2 means relative to the end.
   194  // It returns the new offset and an error, if any.
   195  func (f *File) seek(offset int64, whence int) (ret int64, err error) {
   196  	if info := f.dirinfo.Swap(nil); info != nil {
   197  		// Free cached dirinfo, so we allocate a new one if we
   198  		// access this file as a directory again. See #35767 and #37161.
   199  		info.close()
   200  	}
   201  	ret, err = f.pfd.Seek(offset, whence)
   202  	runtime.KeepAlive(f)
   203  	return ret, err
   204  }
   205  
   206  // Truncate changes the size of the named file.
   207  // If the file is a symbolic link, it changes the size of the link's target.
   208  func Truncate(name string, size int64) error {
   209  	f, e := OpenFile(name, O_WRONLY, 0666)
   210  	if e != nil {
   211  		return e
   212  	}
   213  	defer f.Close()
   214  	e1 := f.Truncate(size)
   215  	if e1 != nil {
   216  		return e1
   217  	}
   218  	return nil
   219  }
   220  
   221  // Remove removes the named file or directory.
   222  // If there is an error, it will be of type [*PathError].
   223  func Remove(name string) error {
   224  	p, e := syscall.UTF16PtrFromString(fixLongPath(name))
   225  	if e != nil {
   226  		return &PathError{Op: "remove", Path: name, Err: e}
   227  	}
   228  
   229  	// Go file interface forces us to know whether
   230  	// name is a file or directory. Try both.
   231  	e = syscall.DeleteFile(p)
   232  	if e == nil {
   233  		return nil
   234  	}
   235  	e1 := syscall.RemoveDirectory(p)
   236  	if e1 == nil {
   237  		return nil
   238  	}
   239  
   240  	// Both failed: figure out which error to return.
   241  	if e1 != e {
   242  		a, e2 := syscall.GetFileAttributes(p)
   243  		if e2 != nil {
   244  			e = e2
   245  		} else {
   246  			if a&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
   247  				e = e1
   248  			} else if a&syscall.FILE_ATTRIBUTE_READONLY != 0 {
   249  				if e1 = syscall.SetFileAttributes(p, a&^syscall.FILE_ATTRIBUTE_READONLY); e1 == nil {
   250  					if e = syscall.DeleteFile(p); e == nil {
   251  						return nil
   252  					}
   253  				}
   254  			}
   255  		}
   256  	}
   257  	return &PathError{Op: "remove", Path: name, Err: e}
   258  }
   259  
   260  func rename(oldname, newname string) error {
   261  	e := windows.Rename(fixLongPath(oldname), fixLongPath(newname))
   262  	if e != nil {
   263  		return &LinkError{"rename", oldname, newname, e}
   264  	}
   265  	return nil
   266  }
   267  
   268  // Pipe returns a connected pair of Files; reads from r return bytes written to w.
   269  // It returns the files and an error, if any. The Windows handles underlying
   270  // the returned files are marked as inheritable by child processes.
   271  func Pipe() (r *File, w *File, err error) {
   272  	var p [2]syscall.Handle
   273  	e := syscall.Pipe(p[:])
   274  	if e != nil {
   275  		return nil, nil, NewSyscallError("pipe", e)
   276  	}
   277  	// syscall.Pipe always returns a non-blocking handle.
   278  	return newFile(p[0], "|0", kindPipe, false), newFile(p[1], "|1", kindPipe, false), nil
   279  }
   280  
   281  var useGetTempPath2 = sync.OnceValue(func() bool {
   282  	return windows.ErrorLoadingGetTempPath2() == nil
   283  })
   284  
   285  func tempDir() string {
   286  	getTempPath := syscall.GetTempPath
   287  	if useGetTempPath2() {
   288  		getTempPath = windows.GetTempPath2
   289  	}
   290  	n := uint32(syscall.MAX_PATH)
   291  	for {
   292  		b := make([]uint16, n)
   293  		n, _ = getTempPath(uint32(len(b)), &b[0])
   294  		if n > uint32(len(b)) {
   295  			continue
   296  		}
   297  		if n == 3 && b[1] == ':' && b[2] == '\\' {
   298  			// Do nothing for path, like C:\.
   299  		} else if n > 0 && b[n-1] == '\\' {
   300  			// Otherwise remove terminating \.
   301  			n--
   302  		}
   303  		return syscall.UTF16ToString(b[:n])
   304  	}
   305  }
   306  
   307  // Link creates newname as a hard link to the oldname file.
   308  // If there is an error, it will be of type *LinkError.
   309  func Link(oldname, newname string) error {
   310  	n, err := syscall.UTF16PtrFromString(fixLongPath(newname))
   311  	if err != nil {
   312  		return &LinkError{"link", oldname, newname, err}
   313  	}
   314  	o, err := syscall.UTF16PtrFromString(fixLongPath(oldname))
   315  	if err != nil {
   316  		return &LinkError{"link", oldname, newname, err}
   317  	}
   318  	err = syscall.CreateHardLink(n, o, 0)
   319  	if err != nil {
   320  		return &LinkError{"link", oldname, newname, err}
   321  	}
   322  	return nil
   323  }
   324  
   325  // Symlink creates newname as a symbolic link to oldname.
   326  // On Windows, a symlink to a non-existent oldname creates a file symlink;
   327  // if oldname is later created as a directory the symlink will not work.
   328  // If there is an error, it will be of type *LinkError.
   329  func Symlink(oldname, newname string) error {
   330  	// '/' does not work in link's content
   331  	oldname = filepathlite.FromSlash(oldname)
   332  
   333  	// need the exact location of the oldname when it's relative to determine if it's a directory
   334  	destpath := oldname
   335  	if v := filepathlite.VolumeName(oldname); v == "" {
   336  		if len(oldname) > 0 && IsPathSeparator(oldname[0]) {
   337  			// oldname is relative to the volume containing newname.
   338  			if v = filepathlite.VolumeName(newname); v != "" {
   339  				// Prepend the volume explicitly, because it may be different from the
   340  				// volume of the current working directory.
   341  				destpath = v + oldname
   342  			}
   343  		} else {
   344  			// oldname is relative to newname.
   345  			destpath = dirname(newname) + `\` + oldname
   346  		}
   347  	}
   348  
   349  	fi, err := Stat(destpath)
   350  	isdir := err == nil && fi.IsDir()
   351  
   352  	n, err := syscall.UTF16PtrFromString(fixLongPath(newname))
   353  	if err != nil {
   354  		return &LinkError{"symlink", oldname, newname, err}
   355  	}
   356  	var o *uint16
   357  	if filepathlite.IsAbs(oldname) {
   358  		o, err = syscall.UTF16PtrFromString(fixLongPath(oldname))
   359  	} else {
   360  		// Do not use fixLongPath on oldname for relative symlinks,
   361  		// as it would turn the name into an absolute path thus making
   362  		// an absolute symlink instead.
   363  		// Notice that CreateSymbolicLinkW does not fail for relative
   364  		// symlinks beyond MAX_PATH, so this does not prevent the
   365  		// creation of an arbitrary long path name.
   366  		o, err = syscall.UTF16PtrFromString(oldname)
   367  	}
   368  	if err != nil {
   369  		return &LinkError{"symlink", oldname, newname, err}
   370  	}
   371  
   372  	var flags uint32 = windows.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
   373  	if isdir {
   374  		flags |= syscall.SYMBOLIC_LINK_FLAG_DIRECTORY
   375  	}
   376  	err = syscall.CreateSymbolicLink(n, o, flags)
   377  	if err != nil {
   378  		// the unprivileged create flag is unsupported
   379  		// below Windows 10 (1703, v10.0.14972). retry without it.
   380  		flags &^= windows.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
   381  		err = syscall.CreateSymbolicLink(n, o, flags)
   382  		if err != nil {
   383  			return &LinkError{"symlink", oldname, newname, err}
   384  		}
   385  	}
   386  	return nil
   387  }
   388  
   389  // openSymlink calls CreateFile Windows API with FILE_FLAG_OPEN_REPARSE_POINT
   390  // parameter, so that Windows does not follow symlink, if path is a symlink.
   391  // openSymlink returns opened file handle.
   392  func openSymlink(path string) (syscall.Handle, error) {
   393  	p, err := syscall.UTF16PtrFromString(path)
   394  	if err != nil {
   395  		return 0, err
   396  	}
   397  	attrs := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
   398  	// Use FILE_FLAG_OPEN_REPARSE_POINT, otherwise CreateFile will follow symlink.
   399  	// See https://docs.microsoft.com/en-us/windows/desktop/FileIO/symbolic-link-effects-on-file-systems-functions#createfile-and-createfiletransacted
   400  	attrs |= syscall.FILE_FLAG_OPEN_REPARSE_POINT
   401  	h, err := syscall.CreateFile(p, 0, 0, nil, syscall.OPEN_EXISTING, attrs, 0)
   402  	if err != nil {
   403  		return 0, err
   404  	}
   405  	return h, nil
   406  }
   407  
   408  var winreadlinkvolume = godebug.New("winreadlinkvolume")
   409  
   410  // normaliseLinkPath converts absolute paths returned by
   411  // DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, ...)
   412  // into paths acceptable by all Windows APIs.
   413  // For example, it converts
   414  //
   415  //	\??\C:\foo\bar into C:\foo\bar
   416  //	\??\UNC\foo\bar into \\foo\bar
   417  //	\??\Volume{abc}\ into \\?\Volume{abc}\
   418  func normaliseLinkPath(path string) (string, error) {
   419  	if len(path) < 4 || path[:4] != `\??\` {
   420  		// unexpected path, return it as is
   421  		return path, nil
   422  	}
   423  	// we have path that start with \??\
   424  	s := path[4:]
   425  	switch {
   426  	case len(s) >= 2 && s[1] == ':': // \??\C:\foo\bar
   427  		return s, nil
   428  	case len(s) >= 4 && s[:4] == `UNC\`: // \??\UNC\foo\bar
   429  		return `\\` + s[4:], nil
   430  	}
   431  
   432  	// \??\Volume{abc}\
   433  	if winreadlinkvolume.Value() != "0" {
   434  		return `\\?\` + path[4:], nil
   435  	}
   436  	winreadlinkvolume.IncNonDefault()
   437  
   438  	h, err := openSymlink(path)
   439  	if err != nil {
   440  		return "", err
   441  	}
   442  	defer syscall.CloseHandle(h)
   443  
   444  	buf := make([]uint16, 100)
   445  	for {
   446  		n, err := windows.GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), windows.VOLUME_NAME_DOS)
   447  		if err != nil {
   448  			return "", err
   449  		}
   450  		if n < uint32(len(buf)) {
   451  			break
   452  		}
   453  		buf = make([]uint16, n)
   454  	}
   455  	s = syscall.UTF16ToString(buf)
   456  	if len(s) > 4 && s[:4] == `\\?\` {
   457  		s = s[4:]
   458  		if len(s) > 3 && s[:3] == `UNC` {
   459  			// return path like \\server\share\...
   460  			return `\` + s[3:], nil
   461  		}
   462  		return s, nil
   463  	}
   464  	return "", errors.New("GetFinalPathNameByHandle returned unexpected path: " + s)
   465  }
   466  
   467  func readReparseLink(path string) (string, error) {
   468  	h, err := openSymlink(path)
   469  	if err != nil {
   470  		return "", err
   471  	}
   472  	defer syscall.CloseHandle(h)
   473  	return readReparseLinkHandle(h)
   474  }
   475  
   476  func readReparseLinkHandle(h syscall.Handle) (string, error) {
   477  	rdbbuf := make([]byte, syscall.MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
   478  	var bytesReturned uint32
   479  	err := syscall.DeviceIoControl(h, syscall.FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil)
   480  	if err != nil {
   481  		return "", err
   482  	}
   483  
   484  	rdb := (*windows.REPARSE_DATA_BUFFER)(unsafe.Pointer(&rdbbuf[0]))
   485  	switch rdb.ReparseTag {
   486  	case syscall.IO_REPARSE_TAG_SYMLINK:
   487  		rb := (*windows.SymbolicLinkReparseBuffer)(unsafe.Pointer(&rdb.DUMMYUNIONNAME))
   488  		s := rb.Path()
   489  		if rb.Flags&windows.SYMLINK_FLAG_RELATIVE != 0 {
   490  			return s, nil
   491  		}
   492  		return normaliseLinkPath(s)
   493  	case windows.IO_REPARSE_TAG_MOUNT_POINT:
   494  		return normaliseLinkPath((*windows.MountPointReparseBuffer)(unsafe.Pointer(&rdb.DUMMYUNIONNAME)).Path())
   495  	default:
   496  		// the path is not a symlink or junction but another type of reparse
   497  		// point
   498  		return "", syscall.ENOENT
   499  	}
   500  }
   501  
   502  func readlink(name string) (string, error) {
   503  	s, err := readReparseLink(fixLongPath(name))
   504  	if err != nil {
   505  		return "", &PathError{Op: "readlink", Path: name, Err: err}
   506  	}
   507  	return s, nil
   508  }
   509  

View as plain text