Debian Codegraph @

fileutil_windows.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright (c) 2014 The fileutil 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 fileutil // import "modernc.org/fileutil"
6
7 import (
8 "io"
9 "os"
10 "sync"
11 "syscall"
12 "unsafe"
13 )
14
15 const hasPunchHole = true
16
17 // PunchHole deallocates space inside a file in the byte range starting at
18 // offset and continuing for len bytes. Not supported on Windows.
19 func PunchHole(f *os.File, off, len int64) error {
20 return puncher(f, off, len)
21 }
22
23 // Fadvise predeclares an access pattern for file data. See also 'man 2
24 // posix_fadvise'. Not supported on Windows.
25 func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
26 return nil
27 }
28
29 // IsEOF reports whether err is an EOF condition.
30 func IsEOF(err error) bool {
31 if err == io.EOF {
32 return true
33 }
34
35 // http://social.technet.microsoft.com/Forums/windowsserver/en-US/1a16311b-c625-46cf-830b-6a26af488435/how-to-solve-error-38-0x26-errorhandleeof-using-fsctlgetretrievalpointers
36 x, ok := err.(*os.PathError)
37 return ok && x.Op == "read" && x.Err.(syscall.Errno) == 0x26
38 }
39
40 var (
41 modkernel32 = syscall.NewLazyDLL("kernel32.dll")
42
43 procDeviceIOControl = modkernel32.NewProc("DeviceIoControl")
44
45 sparseFilesMu sync.Mutex
46 sparseFiles map[uintptr]struct{}
47 )
48
49 func init() {
50 // sparseFiles is an fd set for already "sparsed" files - according to
51 // msdn.microsoft.com/en-us/library/windows/desktop/aa364225(v=vs.85).aspx
52 // the file handles are unique per process.
53 sparseFiles = make(map[uintptr]struct{})
54 }
55
56 // puncHoleWindows punches a hole into the given file starting at offset,
57 // measuring "size" bytes
58 // (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364597%28v=vs.85%29.aspx)
59 func puncher(file *os.File, offset, size int64) error {
60 if err := ensureFileSparse(file); err != nil {
61 return err
62 }
63
64 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa364411%28v=vs.85%29.aspx
65 // typedef struct _FILE_ZERO_DATA_INFORMATION {
66 // LARGE_INTEGER FileOffset;
67 // LARGE_INTEGER BeyondFinalZero;
68 //} FILE_ZERO_DATA_INFORMATION, *PFILE_ZERO_DATA_INFORMATION;
69 type fileZeroDataInformation struct {
70 FileOffset, BeyondFinalZero int64
71 }
72
73 lpInBuffer := fileZeroDataInformation{
74 FileOffset: offset,
75 BeyondFinalZero: offset + size}
76 return deviceIOControl(false, file.Fd(), uintptr(unsafe.Pointer(&lpInBuffer)), 16)
77 }
78
79 // // http://msdn.microsoft.com/en-us/library/windows/desktop/cc948908%28v=vs.85%29.aspx
80 // type fileSetSparseBuffer struct {
81 // SetSparse bool
82 // }
83
84 func ensureFileSparse(file *os.File) (err error) {
85 fd := file.Fd()
86 sparseFilesMu.Lock()
87 if _, ok := sparseFiles[fd]; ok {
88 sparseFilesMu.Unlock()
89 return nil
90 }
91
92 if err = deviceIOControl(true, fd, 0, 0); err == nil {
93 sparseFiles[fd] = struct{}{}
94 }
95 sparseFilesMu.Unlock()
96 return err
97 }
98
99 func deviceIOControl(setSparse bool, fd, inBuf, inBufLen uintptr) (err error) {
100 const (
101 //http://source.winehq.org/source/include/winnt.h#L4605
102 file_read_data = 1
103 file_write_data = 2
104
105 // METHOD_BUFFERED 0
106 method_buffered = 0
107 // FILE_ANY_ACCESS 0
108 file_any_access = 0
109 // FILE_DEVICE_FILE_SYSTEM 0x00000009
110 file_device_file_system = 0x00000009
111 // FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS)
112 file_special_access = file_any_access
113 file_read_access = file_read_data
114 file_write_access = file_write_data
115
116 // http://source.winehq.org/source/include/winioctl.h
117 // #define CTL_CODE ( DeviceType,
118 // Function,
119 // Method,
120 // Access )
121 // ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
122
123 // FSCTL_SET_COMPRESSION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 16, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
124 fsctl_set_compression = (file_device_file_system << 16) | ((file_read_access | file_write_access) << 14) | (16 << 2) | method_buffered
125 // FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
126 fsctl_set_sparse = (file_device_file_system << 16) | (file_special_access << 14) | (49 << 2) | method_buffered
127 // FSCTL_SET_ZERO_DATA CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 50, METHOD_BUFFERED, FILE_WRITE_DATA)
128 fsctl_set_zero_data = (file_device_file_system << 16) | (file_write_data << 14) | (50 << 2) | method_buffered
129 )
130 retPtr := uintptr(unsafe.Pointer(&(make([]byte, 8)[0])))
131 var r1 uintptr
132 var e1 syscall.Errno
133 if setSparse {
134 // BOOL
135 // WINAPI
136 // DeviceIoControl( (HANDLE) hDevice, // handle to a file
137 // FSCTL_SET_SPARSE, // dwIoControlCode
138 // (PFILE_SET_SPARSE_BUFFER) lpInBuffer, // input buffer
139 // (DWORD) nInBufferSize, // size of input buffer
140 // NULL, // lpOutBuffer
141 // 0, // nOutBufferSize
142 // (LPDWORD) lpBytesReturned, // number of bytes returned
143 // (LPOVERLAPPED) lpOverlapped ); // OVERLAPPED structure
144 r1, _, e1 = syscall.Syscall9(procDeviceIOControl.Addr(), 8,
145 fd,
146 uintptr(fsctl_set_sparse),
147 // If the lpInBuffer parameter is NULL, the operation will behave the same as if the SetSparse member of the FILE_SET_SPARSE_BUFFER structure were TRUE. In other words, the operation sets the file to a sparse file.
148 0, // uintptr(unsafe.Pointer(&lpInBuffer)),
149 0, // 1,
150 0,
151 0,
152 retPtr,
153 0,
154 0)
155 } else {
156 // BOOL
157 // WINAPI
158 // DeviceIoControl( (HANDLE) hDevice, // handle to a file
159 // FSCTL_SET_ZERO_DATA, // dwIoControlCode
160 // (LPVOID) lpInBuffer, // input buffer
161 // (DWORD) nInBufferSize, // size of input buffer
162 // NULL, // lpOutBuffer
163 // 0, // nOutBufferSize
164 // (LPDWORD) lpBytesReturned, // number of bytes returned
165 // (LPOVERLAPPED) lpOverlapped ); // OVERLAPPED structure
166 r1, _, e1 = syscall.Syscall9(procDeviceIOControl.Addr(), 8,
167 fd,
168 uintptr(fsctl_set_zero_data),
169 inBuf,
170 inBufLen,
171 0,
172 0,
173 retPtr,
174 0,
175 0)
176 }
177 if r1 == 0 {
178 if e1 != 0 {
179 err = error(e1)
180 } else {
181 err = syscall.EINVAL
182 }
183 }
184 return err
185 }