-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_linux.go
More file actions
238 lines (206 loc) · 5.49 KB
/
platform_linux.go
File metadata and controls
238 lines (206 loc) · 5.49 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//go:build linux
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
const (
_FS_IOC_FIEMAP = 0xC020660B
_FIEMAP_FLAG_SYNC = 0x00000001
_FIEMAP_EXTENT_LAST = 0x00000001
_MAX_FIEMAP_EXTENTS = 512
_FICLONE = 0x40049409
)
// Raw kernel structs for FIEMAP ioctl. Field order and sizes must match
// the C definitions exactly (linux/fiemap.h).
type fiemapExtent struct {
logical uint64
physical uint64
length uint64
reserved64 [2]uint64
flags uint32
reserved32 [3]uint32
}
type fiemapReq struct {
start uint64
length uint64
flags uint32
mappedExtents uint32
extentCount uint32
reserved uint32
extents [_MAX_FIEMAP_EXTENTS]fiemapExtent
}
// getExtents returns the physical extent map of a file using the FIEMAP ioctl.
func getExtents(path string) ([]Extent, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var all []Extent
var start uint64
for {
req := fiemapReq{
start: start,
length: ^uint64(0),
flags: _FIEMAP_FLAG_SYNC,
extentCount: _MAX_FIEMAP_EXTENTS,
}
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
f.Fd(),
uintptr(_FS_IOC_FIEMAP),
uintptr(unsafe.Pointer(&req)),
)
if errno != 0 {
return nil, fmt.Errorf("FIEMAP ioctl on %s: %w", path, errno)
}
if req.mappedExtents == 0 {
break
}
for i := uint32(0); i < req.mappedExtents; i++ {
e := req.extents[i]
all = append(all, Extent{
Logical: e.logical,
Physical: e.physical,
Length: e.length,
Flags: e.flags,
})
}
last := req.extents[req.mappedExtents-1]
if last.flags&_FIEMAP_EXTENT_LAST != 0 {
break
}
start = last.logical + last.length
}
return all, nil
}
// reflinkCopy creates a reflink (CoW) copy of src at dst. The new file shares
// the same physical data blocks as src. Only works on btrfs/XFS with reflink.
func reflinkCopy(src, dst string, perm os.FileMode) error {
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("open source: %w", err)
}
defer srcFile.Close()
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return fmt.Errorf("create destination: %w", err)
}
defer dstFile.Close()
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
dstFile.Fd(),
uintptr(_FICLONE),
srcFile.Fd(),
)
if errno != 0 {
return fmt.Errorf("FICLONE ioctl: %w", errno)
}
return nil
}
// reflinkInPlace replaces the content of dst with a reflink clone of src,
// without creating or removing directory entries. The existing dst inode is
// truncated and FICLONE'd in place, so this works on write-protected directories.
func reflinkInPlace(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("open source: %w", err)
}
defer srcFile.Close()
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_TRUNC, 0)
if err != nil {
return fmt.Errorf("open destination: %w", err)
}
defer dstFile.Close()
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
dstFile.Fd(),
uintptr(_FICLONE),
srcFile.Fd(),
)
if errno != 0 {
return fmt.Errorf("FICLONE ioctl: %w", errno)
}
return nil
}
// isMountPoint checks whether path is a filesystem mount point by comparing
// device IDs with the parent directory.
func isMountPoint(path string) bool {
var pathStat, parentStat syscall.Stat_t
if err := syscall.Stat(path, &pathStat); err != nil {
return false
}
parent := filepath.Dir(path)
if parent == path {
return true // filesystem root
}
if err := syscall.Stat(parent, &parentStat); err != nil {
return false
}
return pathStat.Dev != parentStat.Dev
}
// fsFileEstimate returns the approximate number of files on the filesystem
// containing path, using statfs (total inodes - free inodes).
func fsFileEstimate(path string) int64 {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return 0
}
used := int64(stat.Files) - int64(stat.Ffree)
if used <= 0 {
return 0
}
return used
}
// fsUsedBytes returns the number of bytes used on the filesystem containing path.
func fsUsedBytes(path string) int64 {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return 0
}
total := int64(stat.Blocks) * int64(stat.Bsize)
free := int64(stat.Bfree) * int64(stat.Bsize)
used := total - free
if used <= 0 {
return 0
}
return used
}
// sameInode reports whether two paths refer to the same inode on the same device.
func sameInode(a, b string) (bool, error) {
var statA, statB syscall.Stat_t
if err := syscall.Stat(a, &statA); err != nil {
return false, err
}
if err := syscall.Stat(b, &statB); err != nil {
return false, err
}
return statA.Dev == statB.Dev && statA.Ino == statB.Ino, nil
}
// restoreMetadata copies ownership, permissions, and timestamps from the
// original file info onto the new file at path.
func restoreMetadata(path string, orig os.FileInfo) error {
stat, ok := orig.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("unexpected stat type for metadata restoration")
}
// Ownership (best-effort; may require root).
_ = os.Chown(path, int(stat.Uid), int(stat.Gid))
// Permissions.
if err := os.Chmod(path, orig.Mode()); err != nil {
return fmt.Errorf("chmod: %w", err)
}
// Timestamps.
atime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
mtime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
if err := os.Chtimes(path, atime, mtime); err != nil {
return fmt.Errorf("chtimes: %w", err)
}
return nil
}