Debian Codegraph @

tools/util_windows.go

raw symbols SCIP
1 //go:build windows
2 // +build windows
3
4 package tools
5
6 import (
7 "io"
8 "os"
9 "unsafe"
10
11 "golang.org/x/sys/windows"
12 )
13
14 var (
15 availableClusterSize = []int64{64 * 1024, 4 * 1024} // ReFS only supports 64KiB and 4KiB cluster.
16 GiB = int64(1024 * 1024 * 1024)
17 )
18
19 // fsctlDuplicateExtentsToFile = FSCTL_DUPLICATE_EXTENTS_TO_FILE IOCTL
20 // Instructs the file system to copy a range of file bytes on behalf of an application.
21 //
22 // https://docs.microsoft.com/windows/win32/api/winioctl/ni-winioctl-fsctl_duplicate_extents_to_file
23 const fsctlDuplicateExtentsToFile = 623428
24
25 // duplicateExtentsData = DUPLICATE_EXTENTS_DATA structure
26 // Contains parameters for the FSCTL_DUPLICATE_EXTENTS control code that performs the Block Cloning operation.
27 //
28 // https://docs.microsoft.com/windows/win32/api/winioctl/ns-winioctl-duplicate_extents_data
29 type duplicateExtentsData struct {
30 FileHandle windows.Handle
31 SourceFileOffset int64
32 TargetFileOffset int64
33 ByteCount int64
34 }
35
36 // CheckCloneFileSupported runs explicit test of clone file on supplied directory.
37 // This function creates some (src and dst) file in the directory and remove after test finished.
38 //
39 // If check failed (e.g. directory is read-only), returns err.
40 func CheckCloneFileSupported(dir string) (supported bool, err error) {
41 src, err := os.CreateTemp(dir, "src")
42 if err != nil {
43 return false, err
44 }
45 defer func() {
46 src.Close()
47 os.Remove(src.Name())
48 }()
49
50 // Make src file not empty.
51 // Because `FSCTL_DUPLICATE_EXTENTS_TO_FILE` on empty file is always success even filesystem don't support it.
52 _, err = src.WriteString("TESTING")
53 if err != nil {
54 return false, err
55 }
56
57 dst, err := os.CreateTemp(dir, "dst")
58 if err != nil {
59 return false, err
60 }
61 defer func() {
62 dst.Close()
63 os.Remove(dst.Name())
64 }()
65
66 return CloneFile(dst, src)
67 }
68
69 func CloneFileByPath(dst, src string) (success bool, err error) {
70 dstFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, 0666) // No truncate version of os.Create
71 if err != nil {
72 return
73 }
74 defer dstFile.Close()
75
76 srcFile, err := os.Open(src)
77 if err != nil {
78 return
79 }
80 defer srcFile.Close()
81
82 return CloneFile(dstFile, srcFile)
83 }
84
85 func CloneFile(writer io.Writer, reader io.Reader) (success bool, err error) {
86 dst, dstIsFile := writer.(*os.File)
87 src, srcIsFile := reader.(*os.File)
88 if !(dstIsFile && srcIsFile) {
89 return false, nil
90 }
91
92 srcStat, err := src.Stat()
93 if err != nil {
94 return
95 }
96
97 fileSize := srcStat.Size()
98
99 err = dst.Truncate(fileSize) // set file size. There is a requirement "The destination region must not extend past the end of file."
100 if err != nil {
101 return
102 }
103
104 offset := int64(0)
105
106 // Requirement
107 // * The source and destination regions must begin and end at a cluster boundary. (4KiB or 64KiB)
108 // * cloneRegionSize less than 4GiB.
109 // see https://docs.microsoft.com/windows/win32/fileio/block-cloning
110
111 // Clone first xGiB region.
112 for ; offset+GiB < fileSize; offset += GiB {
113 err = callDuplicateExtentsToFile(dst, src, offset, GiB)
114 if err != nil {
115 return false, err
116 }
117 }
118
119 // Clone tail. First try with 64KiB round up, then fallback to 4KiB.
120 for _, cloneRegionSize := range availableClusterSize {
121 err = callDuplicateExtentsToFile(dst, src, offset, roundUp(fileSize-offset, cloneRegionSize))
122 if err != nil {
123 continue
124 }
125 break
126 }
127
128 return err == nil, err
129 }
130
131 // call FSCTL_DUPLICATE_EXTENTS_TO_FILE IOCTL
132 // see https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_duplicate_extents_to_file
133 //
134 // memo: Overflow (cloneRegionSize is greater than file ends) is safe and just ignored by windows.
135 func callDuplicateExtentsToFile(dst, src *os.File, offset int64, cloneRegionSize int64) (err error) {
136 var (
137 bytesReturned uint32
138 overlapped windows.Overlapped
139 )
140
141 request := duplicateExtentsData{
142 FileHandle: windows.Handle(src.Fd()),
143 SourceFileOffset: offset,
144 TargetFileOffset: offset,
145 ByteCount: cloneRegionSize,
146 }
147
148 return windows.DeviceIoControl(
149 windows.Handle(dst.Fd()),
150 fsctlDuplicateExtentsToFile,
151 (*byte)(unsafe.Pointer(&request)),
152 uint32(unsafe.Sizeof(request)),
153 (*byte)(unsafe.Pointer(nil)), // = nullptr
154 0,
155 &bytesReturned,
156 &overlapped)
157 }
158
159 func roundUp(value, base int64) int64 {
160 mod := value % base
161 if mod == 0 {
162 return value
163 }
164
165 return value - mod + base
166 }