Debian Codegraph @

diskwriter_windows.go

raw symbols SCIP
1 //go:build windows
2 // +build windows
3
4 package fsutil
5
6 import (
7 "fmt"
8 iofs "io/fs"
9 "os"
10 "syscall"
11
12 "github.com/Microsoft/go-winio"
13 "github.com/pkg/errors"
14 "github.com/tonistiigi/fsutil/types"
15 )
16
17 func rewriteMetadata(p string, stat *types.Stat) error {
18 return chtimes(p, stat.ModTime)
19 }
20
21 // handleTarTypeBlockCharFifo is an OS-specific helper function used by
22 // createTarFile to handle the following types of header: Block; Char; Fifo
23 func handleTarTypeBlockCharFifo(_ string, _ *types.Stat) error {
24 return errors.New("Not implemented on windows")
25 }
26
27 func getFileHandle(path string, info iofs.FileInfo) (syscall.Handle, error) {
28 p, err := syscall.UTF16PtrFromString(path)
29 if err != nil {
30 return 0, errors.Wrap(err, "converting string to UTF-16")
31 }
32 attrs := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
33 if info.Mode()&os.ModeSymlink != 0 {
34 // Use FILE_FLAG_OPEN_REPARSE_POINT, otherwise CreateFile will follow symlink.
35 // See https://docs.microsoft.com/en-us/windows/desktop/FileIO/symbolic-link-effects-on-file-systems-functions#createfile-and-createfiletransacted
36 attrs |= syscall.FILE_FLAG_OPEN_REPARSE_POINT
37 }
38 h, err := syscall.CreateFile(p, 0, 0, nil, syscall.OPEN_EXISTING, attrs, 0)
39 if err != nil {
40 return 0, errors.Wrap(err, "getting file handle")
41 }
42 return h, nil
43 }
44
45 func readlink(path string, info iofs.FileInfo) ([]byte, error) {
46 h, err := getFileHandle(path, info)
47 if err != nil {
48 return nil, errors.Wrap(err, "getting file handle")
49 }
50 defer syscall.CloseHandle(h)
51
52 rdbbuf := make([]byte, syscall.MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
53 var bytesReturned uint32
54 err = syscall.DeviceIoControl(h, syscall.FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil)
55 if err != nil {
56 return nil, errors.Wrap(err, "sending I/O control command")
57 }
58 return rdbbuf[:bytesReturned], nil
59 }
60
61 func getReparsePoint(path string, info iofs.FileInfo) (*winio.ReparsePoint, error) {
62 target, err := readlink(path, info)
63 if err != nil {
64 return nil, errors.Wrap(err, "fetching link")
65 }
66 rp, err := winio.DecodeReparsePoint(target)
67 if err != nil {
68 return nil, errors.Wrap(err, "decoding reparse point")
69 }
70 return rp, nil
71 }
72
73 func renameFile(src, dst string) error {
74 info, err := os.Lstat(dst)
75 if err != nil {
76 if !os.IsNotExist(err) {
77 return errors.Wrap(err, "getting file info")
78 }
79 }
80
81 if info != nil && info.Mode()&os.ModeSymlink != 0 {
82 dstInfoRp, err := getReparsePoint(dst, info)
83 if err != nil {
84 return errors.Wrap(err, "getting reparse point")
85 }
86 if dstInfoRp.IsMountPoint {
87 return fmt.Errorf("%s is a mount point", dst)
88 }
89 if err := os.Remove(dst); err != nil {
90 return errors.Wrapf(err, "removing %s", dst)
91 }
92 }
93 if err := os.Rename(src, dst); err != nil {
94 return errors.Wrapf(err, "failed to rename %s to %s", src, dst)
95 }
96 return nil
97 }