| 1 | package btrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.12 bytes/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.12 fmt/" |
| 6 | "osscip-go gomod github.com/golang/go/src go1.12 os/" |
| 7 | "path/filepathscip-go gomod github.com/golang/go/src go1.12 `path/filepath`/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.12 strings/" |
| 9 | "syscallscip-go gomod github.com/golang/go/src go1.12 syscall/" |
| 10 | "unsafescip-go gomod github.com/golang/go/src go1.12 unsafe/" |
| 11 | |
| 12 | "github.com/dennwc/btrfs/mtabscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/" |
| 13 | ) |
| 14 | |
| 15 | func isBtrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/isBtrfs().(path string) (bool, error) { |
| 16 | var stfs syscallscip-go gomod github.com/golang/go/src go1.12 syscall/.Statfs_tscip-go gomod github.com/golang/go/src go1.12 syscall/Statfs_t# |
| 17 | if err := syscallscip-go gomod github.com/golang/go/src go1.12 syscall/.Statfsscip-go gomod github.com/golang/go/src go1.12 syscall/Statfs().(path, &stfs); err != nil { |
| 18 | return false, &osscip-go gomod github.com/golang/go/src go1.12 os/.PathErrorscip-go gomod github.com/golang/go/src go1.12 os/PathError#{Opscip-go gomod github.com/golang/go/src go1.12 `io/fs`/PathError#Op.: "statfs", Pathscip-go gomod github.com/golang/go/src go1.12 `io/fs`/PathError#Path.: path, Errscip-go gomod github.com/golang/go/src go1.12 `io/fs`/PathError#Err.: err} |
| 19 | } |
| 20 | fsType := uint32(stfs.Typescip-go gomod github.com/golang/go/src go1.12 syscall/Statfs_t#Type.) |
| 21 | return fsType == SuperMagicscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/SuperMagic., nil |
| 22 | } |
| 23 | |
| 24 | func findMountRootscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/findMountRoot().(path string) (string, error) { |
| 25 | mounts, err := mtabscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/.Mountsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/Mounts().() |
| 26 | if err != nil { |
| 27 | return "", err |
| 28 | } |
| 29 | longest := "" |
| 30 | isBtrfs := false |
| 31 | for _, m := range mounts { |
| 32 | if !stringsscip-go gomod github.com/golang/go/src go1.12 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.12 strings/HasPrefix().(path, m.Mountscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/MountPoint#Mount.) { |
| 33 | continue |
| 34 | } |
| 35 | if len(longest) < len(m.Mountscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/MountPoint#Mount.) { |
| 36 | longest = m.Mountscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/MountPoint#Mount. |
| 37 | isBtrfs = m.Typescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs/mtab`/MountPoint#Type. == "btrfs" |
| 38 | } |
| 39 | } |
| 40 | if longest == "" { |
| 41 | return "", osscip-go gomod github.com/golang/go/src go1.12 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.12 os/ErrNotExist. |
| 42 | } else if !isBtrfs { |
| 43 | return "", ErrNotBtrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ErrNotBtrfs#{Pathscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ErrNotBtrfs#Path.: longest} |
| 44 | } |
| 45 | return filepathscip-go gomod github.com/golang/go/src go1.12 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.12 `path/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 openDirscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/openDir().(path string) (*osscip-go gomod github.com/golang/go/src go1.12 os/.Filescip-go gomod github.com/golang/go/src go1.12 os/File#, error) { |
| 52 | if ok, err := isBtrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/isBtrfs().(path); err != nil { |
| 53 | return nil, err |
| 54 | } else if !ok { |
| 55 | return nil, ErrNotBtrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ErrNotBtrfs#{Pathscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ErrNotBtrfs#Path.: path} |
| 56 | } |
| 57 | file, err := osscip-go gomod github.com/golang/go/src go1.12 os/.Openscip-go gomod github.com/golang/go/src go1.12 os/Open().(path) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } else if st, err := file.Statscip-go gomod github.com/golang/go/src go1.12 os/File#Stat().(); err != nil { |
| 61 | file.Closescip-go gomod github.com/golang/go/src go1.12 os/File#Close().() |
| 62 | return nil, err |
| 63 | } else if !st.IsDirscip-go gomod github.com/golang/go/src go1.12 `io/fs`/FileInfo#IsDir().() { |
| 64 | file.Closescip-go gomod github.com/golang/go/src go1.12 os/File#Close().() |
| 65 | return nil, fmtscip-go gomod github.com/golang/go/src go1.12 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.12 fmt/Errorf().("not a directory: %s", path) |
| 66 | } |
| 67 | return file, nil |
| 68 | } |
| 69 | |
| 70 | type searchResultscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult# struct { |
| 71 | TransIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#TransID. uint64 |
| 72 | ObjectIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#ObjectID. objectIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/objectID# |
| 73 | Typescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Type. treeKeyTypescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/treeKeyType# |
| 74 | Offsetscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Offset. uint64 |
| 75 | Datascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Data. []byte |
| 76 | } |
| 77 | |
| 78 | func treeSearchRawscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/treeSearchRaw().(mnt *osscip-go gomod github.com/golang/go/src go1.12 os/.Filescip-go gomod github.com/golang/go/src go1.12 os/File#, key btrfs_ioctl_search_keyscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_key#) (out []searchResultscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#, _ error) { |
| 79 | args := btrfs_ioctl_search_argsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_args#{ |
| 80 | keyscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_args#key.: key, |
| 81 | } |
| 82 | if err := iocTreeSearchscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/iocTreeSearch().(mnt, &args); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | out = make([]searchResultscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#, 0, args.keyscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_args#key..nr_itemsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_key#nr_items.) |
| 86 | buf := args.bufscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_args#buf.[:] |
| 87 | for i := 0; i < int(args.keyscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_args#key..nr_itemsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_key#nr_items.); i++ { |
| 88 | h := (*btrfs_ioctl_search_headerscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#)(unsafescip-go gomod github.com/golang/go/src go1.12 unsafe/.Pointer(&buf[0])) |
| 89 | buf = buf[unsafescip-go gomod github.com/golang/go/src go1.12 unsafe/.Sizeof(btrfs_ioctl_search_headerscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#{}):] |
| 90 | out = append(out, searchResultscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#{ |
| 91 | TransIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#TransID.: h.transidscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#transid., |
| 92 | ObjectIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#ObjectID.: h.objectidscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#objectid., |
| 93 | Offsetscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Offset.: h.offsetscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#offset., |
| 94 | Typescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Type.: h.typscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#typ., |
| 95 | Datascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/searchResult#Data.: buf[:h.lenscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#len.:h.lenscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#len.], // TODO: reallocate? |
| 96 | }) |
| 97 | buf = buf[h.lenscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_search_header#len.:] |
| 98 | } |
| 99 | return out, nil |
| 100 | } |
| 101 | |
| 102 | func stringFromBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/stringFromBytes().(input []byte) string { |
| 103 | if i := bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.12 bytes/IndexByte().(input, 0); i >= 0 { |
| 104 | input = input[:i] |
| 105 | } |
| 106 | return string(input) |
| 107 | } |