-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_clone.go
More file actions
78 lines (65 loc) · 1.47 KB
/
test_clone.go
File metadata and controls
78 lines (65 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//go:build windows
// +build windows
package main
import (
"fmt"
"os"
"unsafe"
"golang.org/x/sys/windows"
)
const FSCTL_DUPLICATE_EXTENTS_TO_FILE = 0x98344
type duplicateExtentsData struct {
FileHandle windows.Handle
SourceFileOffset int64
TargetFileOffset int64
ByteCount int64
}
func main() {
src := "test_clone_src.txt"
dst := "test_clone_dst.txt"
os.WriteFile(src, []byte("hello world, testing block cloning"), 0644)
defer os.Remove(src)
defer os.Remove(dst)
s, err := os.Open(src)
if err != nil {
fmt.Println("Error opening src:", err)
return
}
defer s.Close()
stat, _ := s.Stat()
size := stat.Size()
d, err := os.Create(dst)
if err != nil {
fmt.Println("Error creating dst:", err)
return
}
defer d.Close()
sRc, _ := s.SyscallConn()
var sourceHandle windows.Handle
sRc.Control(func(fd uintptr) { sourceHandle = windows.Handle(fd) })
dRc, _ := d.SyscallConn()
var destHandle windows.Handle
dRc.Control(func(fd uintptr) { destHandle = windows.Handle(fd) })
data := duplicateExtentsData{
FileHandle: sourceHandle,
SourceFileOffset: 0,
TargetFileOffset: 0,
ByteCount: size,
}
var bytesReturned uint32
err = windows.DeviceIoControl(
destHandle,
FSCTL_DUPLICATE_EXTENTS_TO_FILE,
(*byte)(unsafe.Pointer(&data)),
uint32(unsafe.Sizeof(data)),
nil,
0,
&bytesReturned,
nil,
)
if err != nil {
fmt.Printf("DeviceIoControl failed: %v\n", err)
} else {
fmt.Println("Block cloning succeeded!")
}
}