| 1 | package btrfs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | "unsafe" |
| 11 | |
| 12 | "github.com/dennwc/btrfs/mtab" |
| 13 | ) |
| 14 | |
| 15 | func isBtrfs(path string) (bool, error) { |
| 16 | var stfs syscall.Statfs_t |
| 17 | if err := syscall.Statfs(path, &stfs); err != nil { |
| 18 | return false, &os.PathError{Op: "statfs", Path: path, Err: err} |
| 19 | } |
| 20 | fsType := uint32(stfs.Type) |
| 21 | return fsType == SuperMagic, nil |
| 22 | } |
| 23 | |
| 24 | func findMountRoot(path string) (string, error) { |
| 25 | mounts, err := mtab.Mounts() |
| 26 | if err != nil { |
| 27 | return "", err |
| 28 | } |
| 29 | longest := "" |
| 30 | isBtrfs := false |
| 31 | for _, m := range mounts { |
| 32 | if !strings.HasPrefix(path, m.Mount) { |
| 33 | continue |
| 34 | } |
| 35 | if len(longest) < len(m.Mount) { |
| 36 | longest = m.Mount |
| 37 | isBtrfs = m.Type == "btrfs" |
| 38 | } |
| 39 | } |
| 40 | if longest == "" { |
| 41 | return "", os.ErrNotExist |
| 42 | } else if !isBtrfs { |
| 43 | return "", ErrNotBtrfs{Path: longest} |
| 44 | } |
| 45 | return filepath.Abs(longest) |
| 46 | } |
| 47 | |
| 48 | // openDir does the following checks before calling Open: |
| 49 | // 1: path is in a btrfs filesystem |
| 50 | // 2: path is a directory |
| 51 | func openDir(path string) (*os.File, error) { |
| 52 | if ok, err := isBtrfs(path); err != nil { |
| 53 | return nil, err |
| 54 | } else if !ok { |
| 55 | return nil, ErrNotBtrfs{Path: path} |
| 56 | } |
| 57 | file, err := os.Open(path) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } else if st, err := file.Stat(); err != nil { |
| 61 | file.Close() |
| 62 | return nil, err |
| 63 | } else if !st.IsDir() { |
| 64 | file.Close() |
| 65 | return nil, fmt.Errorf("not a directory: %s", path) |
| 66 | } |
| 67 | return file, nil |
| 68 | } |
| 69 | |
| 70 | type searchResult struct { |
| 71 | TransID uint64 |
| 72 | ObjectID objectID |
| 73 | Type treeKeyType |
| 74 | Offset uint64 |
| 75 | Data []byte |
| 76 | } |
| 77 | |
| 78 | func treeSearchRaw(mnt *os.File, key btrfs_ioctl_search_key) (out []searchResult, _ error) { |
| 79 | args := btrfs_ioctl_search_args{ |
| 80 | key: key, |
| 81 | } |
| 82 | if err := iocTreeSearch(mnt, &args); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | out = make([]searchResult, 0, args.key.nr_items) |
| 86 | buf := args.buf[:] |
| 87 | for i := 0; i < int(args.key.nr_items); i++ { |
| 88 | h := (*btrfs_ioctl_search_header)(unsafe.Pointer(&buf[0])) |
| 89 | buf = buf[unsafe.Sizeof(btrfs_ioctl_search_header{}):] |
| 90 | out = append(out, searchResult{ |
| 91 | TransID: h.transid, |
| 92 | ObjectID: h.objectid, |
| 93 | Offset: h.offset, |
| 94 | Type: h.typ, |
| 95 | Data: buf[:h.len:h.len], // TODO: reallocate? |
| 96 | }) |
| 97 | buf = buf[h.len:] |
| 98 | } |
| 99 | return out, nil |
| 100 | } |
| 101 | |
| 102 | func stringFromBytes(input []byte) string { |
| 103 | if i := bytes.IndexByte(input, 0); i >= 0 { |
| 104 | input = input[:i] |
| 105 | } |
| 106 | return string(input) |
| 107 | } |