diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-07-01 13:40:16 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-07-01 13:40:16 +0200 |
| commit | 7440c52ff48e957c7b4077d81cd5bc81b3153fd8 (patch) | |
| tree | f926871dfff0173a2f808ac94e0bcdf48285fa5e /fs/btrfs/ino_lookup/ino_lookup.c | |
| parent | f55cde81689a1a1a29666a3626b069a883be430d (diff) | |
| download | farga-7440c52ff48e957c7b4077d81cd5bc81b3153fd8.tar.gz farga-7440c52ff48e957c7b4077d81cd5bc81b3153fd8.zip | |
btrfs: add ino_lookup example
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'fs/btrfs/ino_lookup/ino_lookup.c')
| -rw-r--r-- | fs/btrfs/ino_lookup/ino_lookup.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/fs/btrfs/ino_lookup/ino_lookup.c b/fs/btrfs/ino_lookup/ino_lookup.c new file mode 100644 index 0000000..2233c9b --- /dev/null +++ b/fs/btrfs/ino_lookup/ino_lookup.c @@ -0,0 +1,57 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <linux/btrfs.h> +#include <linux/btrfs_tree.h> +#include <string.h> + +int main(int argc, char *argv[]) +{ + if (argc < 3) { + fprintf(stderr, "usage: %s <mount point> <tree ID> <inode number | --tree-id>\n", argv[0]); + exit(EXIT_FAILURE); + } + + const char *const path = argv[1]; + unsigned long long treeid = strtoull(argv[2], NULL, 10); + + unsigned long long ino; + if (strcmp(argv[3], "--tree-id") == 0) { + ino = BTRFS_FIRST_FREE_OBJECTID; + if (treeid > 0) { + printf("warning: the given tree ID value will be discarded.\n"); + } + treeid = 0; + } else { + ino = strtoull(argv[3], NULL, 10); + } + + int fd = open(path, O_RDONLY | O_DIRECTORY); + if (fd < 0) { + perror("open"); + return 1; + } + + // Check the README.md file on the args. + struct btrfs_ioctl_ino_lookup_args args; + memset(&args, 0, sizeof(args)); + args.treeid = treeid; + args.objectid = ino; + + if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args) < 0) { + perror("BTRFS_IOC_INO_LOOKUP"); + close(fd); + exit(EXIT_FAILURE); + } + + printf("tree ID: %llu\n", args.treeid); + printf("inode: %llu\n", ino); + printf("path: %s\n", args.name); + + close(fd); + return 0; +} |
