File: show_file_by_inode.py

package info (click to toggle)
python-btrfs 11-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 724 kB
  • sloc: python: 4,445; makefile: 195
file content (39 lines) | stat: -rwxr-xr-x 1,646 bytes parent folder | download
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
#!/usr/bin/python3

import btrfs
import sys

if len(sys.argv) < 4:
    print("Usage: {} <subvolid> <inode> <mountpoint>".format(sys.argv[0]))
    sys.exit(1)

tree = int(sys.argv[1])
inum = int(sys.argv[2])

with btrfs.FileSystem(sys.argv[3]) as fs:
    min_key = btrfs.ctree.Key(inum, 0, 0)
    max_key = btrfs.ctree.Key(inum + 1, 0, 0) - 1
    for header, data in btrfs.ioctl.search_v2(fs.fd, tree, min_key, max_key):
        if header.type == btrfs.ctree.INODE_ITEM_KEY:
            print(btrfs.ctree.InodeItem(header, data))
        elif header.type == btrfs.ctree.INODE_REF_KEY:
            inode_ref_list = btrfs.ctree.InodeRefList(header, data)
            print(inode_ref_list)
            for inode_ref in inode_ref_list:
                print("    {}".format(inode_ref))
        elif header.type == btrfs.ctree.INODE_EXTREF_KEY:
            inode_extref_list = btrfs.ctree.InodeExtrefList(header, data)
            print(inode_extref_list)
            for inode_extref in inode_extref_list:
                print("    {}".format(inode_extref))
        elif header.type == btrfs.ctree.XATTR_ITEM_KEY:
            xattr_item_list = btrfs.ctree.XAttrItemList(header, data)
            print(xattr_item_list)
            for xattr_item in xattr_item_list:
                print("    {}".format(xattr_item))
        elif header.type == btrfs.ctree.EXTENT_DATA_KEY:
            print(btrfs.ctree.FileExtentItem(header, data))
        else:
            print("Whoa, I didn't expect a {} key. This example can only handle metadata for a "
                  "regular file.".format(btrfs.ctree.key_type_str(header.type)))
            sys.exit(1)