-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
107 lines (99 loc) · 2.29 KB
/
utils.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
package btrfs
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"unsafe"
"github.com/dennwc/btrfs/mtab"
)
func isBtrfs(path string) (bool, error) {
var stfs syscall.Statfs_t
if err := syscall.Statfs(path, &stfs); err != nil {
return false, &os.PathError{Op: "statfs", Path: path, Err: err}
}
fsType := uint32(stfs.Type)
return fsType == SuperMagic, nil
}
func findMountRoot(path string) (string, error) {
mounts, err := mtab.Mounts()
if err != nil {
return "", err
}
longest := ""
isBtrfs := false
for _, m := range mounts {
if !strings.HasPrefix(path, m.Mount) {
continue
}
if len(longest) < len(m.Mount) {
longest = m.Mount
isBtrfs = m.Type == "btrfs"
}
}
if longest == "" {
return "", os.ErrNotExist
} else if !isBtrfs {
return "", ErrNotBtrfs{Path: longest}
}
return filepath.Abs(longest)
}
// openDir does the following checks before calling Open:
// 1: path is in a btrfs filesystem
// 2: path is a directory
func openDir(path string) (*os.File, error) {
if ok, err := isBtrfs(path); err != nil {
return nil, err
} else if !ok {
return nil, ErrNotBtrfs{Path: path}
}
file, err := os.Open(path)
if err != nil {
return nil, err
} else if st, err := file.Stat(); err != nil {
file.Close()
return nil, err
} else if !st.IsDir() {
file.Close()
return nil, fmt.Errorf("not a directory: %s", path)
}
return file, nil
}
type searchResult struct {
TransID uint64
ObjectID objectID
Type treeKeyType
Offset uint64
Data []byte
}
func treeSearchRaw(mnt *os.File, key btrfs_ioctl_search_key) (out []searchResult, _ error) {
args := btrfs_ioctl_search_args{
key: key,
}
if err := iocTreeSearch(mnt, &args); err != nil {
return nil, err
}
out = make([]searchResult, 0, args.key.nr_items)
buf := args.buf[:]
for i := 0; i < int(args.key.nr_items); i++ {
h := (*btrfs_ioctl_search_header)(unsafe.Pointer(&buf[0]))
buf = buf[unsafe.Sizeof(btrfs_ioctl_search_header{}):]
out = append(out, searchResult{
TransID: h.transid,
ObjectID: h.objectid,
Offset: h.offset,
Type: h.typ,
Data: buf[:h.len:h.len], // TODO: reallocate?
})
buf = buf[h.len:]
}
return out, nil
}
func stringFromBytes(input []byte) string {
if i := bytes.IndexByte(input, 0); i >= 0 {
input = input[:i]
}
return string(input)
}