-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.go
110 lines (104 loc) · 2.7 KB
/
checks.go
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
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"strings"
"syscall"
)
const (
testFileA = "deduper_fileA.test"
testFileB = "deduper_fileB.test"
)
func validatePaths(pathA, pathB string) (cleanPathA, cleanPathB string, err error) {
cleanPathA = path.Clean(pathA)
cleanPathB = path.Clean(pathB)
// Standalone tests
pathAStat, err := validatePath(cleanPathA)
if err != nil {
err = fmt.Errorf("path A error: %w", err)
return
}
pathBStat, err := validatePath(cleanPathB)
if err != nil {
err = fmt.Errorf("path B error: %w", err)
return
}
// Relative tests
//// They can not contains each others
if strings.HasPrefix(cleanPathA, cleanPathB) {
err = fmt.Errorf("path A is contained within path B")
return
}
if strings.HasPrefix(cleanPathB, cleanPathA) {
err = fmt.Errorf("path B is contained within path A")
return
}
//// They need to have the same backing filesystem for hardlinks
pathADevID, err := getDeviceID(pathAStat)
if err != nil {
err = fmt.Errorf("can not retreive filesystem ID of path A: %w", err)
return
}
pathBDevID, err := getDeviceID(pathBStat)
if err != nil {
err = fmt.Errorf("can not retreive filesystem ID of path A: %w", err)
return
}
if pathADevID != pathBDevID {
err = errors.New("path A does not seems to be on the same filesystem than path B")
return
}
// Final check, let's try to make a hardlink
if apply {
err = hardlinkTest(cleanPathA, cleanPathB)
}
return
}
func validatePath(pathCandidate string) (pathStat fs.FileInfo, err error) {
if pathCandidate == "" {
err = errors.New("path is empty")
return
}
if !path.IsAbs(pathCandidate) {
err = errors.New("not an absolute path")
return
}
if pathStat, err = os.Stat(pathCandidate); err != nil {
err = fmt.Errorf("cannot stat path: %w", err)
return
}
if !pathStat.IsDir() {
err = errors.New("path is not a directory")
return
}
return
}
func getDeviceID(pathStat fs.FileInfo) (deviceID uint64, err error) {
system, ok := pathStat.Sys().(*syscall.Stat_t)
if !ok {
err = fmt.Errorf("backing storage device can not be checked: excepting '*syscall.Stat_t' got '%v'", system)
return
}
deviceID = system.Dev
return
}
func hardlinkTest(pathA, pathB string) (err error) {
// Create file on pathA
testPathA := path.Join(pathA, testFileA)
testFile, err := os.OpenFile(testPathA, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0640)
if err != nil {
return fmt.Errorf("failed to create test file for hardlinking: %w", err)
}
testFile.Close()
defer os.Remove(testPathA)
// Create a hardlink on pathB
testPathB := path.Join(pathB, testFileB)
if err = os.Link(testPathA, testPathB); err != nil {
return fmt.Errorf("failed to create a test hardlink: %w", err)
}
os.Remove(testPathB)
return
}