]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: inspect: add -z to logical-resolve and inode-resolve topics/logical-resolve-fixes
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 31 Oct 2026 00:26:49 +0000 (20:26 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 31 Oct 2026 00:26:49 +0000 (00:26 +0000)
The paths that logical-resolve and inode-resolve print are file names,
which may contain newlines, so a script cannot split the output into
paths reliably.  Add -z (--zero): end each path with a NUL character
instead of a newline.  With logical-resolve -P each inode line ends
with NUL instead.  Errors still go to stderr.

No btrfs-progs command has such an option yet.  The name follows the
coreutils commands that print file names, such as realpath, readlink,
dirname and basename (-z, --zero).  -0 is used by commands that read
NUL-separated input (xargs -0) or for a few outputs (du -0, env -0),
and -z is free in both commands.

Add -z cases to misc-tests 042 (a path with a space, tab, newline and
backslash, and -P) and 074 (hard links, one of them with a newline and a
space in its name).

Link: https://github.com/kdave/btrfs-progs/issues/400
Assisted-by: Claude:claude-opus-5-5
Documentation/btrfs-inspect-internal.rst
cmds/inspect.c
tests/misc-tests/042-inspect-internal-logical-resolve/test.sh
tests/misc-tests/074-inspect-internal-inode-resolve/test.sh

index e86135ffe882efcfbbe2b29d20874956692f4fe7..fceab9b1e0085e75b65138e376c11e6fac86031c 100644 (file)
@@ -137,7 +137,7 @@ dump-tree [options] <device> [device...]
                 * convenience aliases, e.g. DEVICE for the DEV tree, CHECKSUM for CSUM
                 * unrecognized ID is an error
 
-inode-resolve [-v] <ino> <path>
+inode-resolve [-vz] <ino> <path>
         (needs root privileges)
 
         resolve paths to all files with given inode number *ino* in a given subvolume
@@ -147,8 +147,11 @@ inode-resolve [-v] <ino> <path>
 
         -v
                 (deprecated) alias for global *-v* option
+        -z|--zero
+                end each path with a NUL character instead of a newline, for names that
+                contain newlines.  Errors still go to standard error.
 
-logical-resolve [-Pvo] [-s <bufsize>] <logical> <path>
+logical-resolve [-Pvoz] [-s <bufsize>] <logical> <path>
         (needs root privileges)
 
         resolve paths to all files at given *logical* address in the linear filesystem space
@@ -168,6 +171,10 @@ logical-resolve [-Pvo] [-s <bufsize>] <logical> <path>
                 (added in 4.15).
         -v
                 (deprecated) alias for global *-v* option
+        -z|--zero
+                end each line of output (each path, or each inode with *-P*) with a NUL
+                character instead of a newline, for names that contain newlines.  Errors
+                still go to standard error.
 
 list-chunks [options] <path>
         (needs root privileges)
index e1ad1579c8c339d997dd5b6293f7c7c9abfb41bd..88e37202ed9a95da26e0e7f2f632a48df847cb2e 100644 (file)
@@ -94,12 +94,15 @@ static const char *ino_path(const char *pathbuf, int i)
        return (const char *)(unsigned long)ptr;
 }
 
+/* What ends each line of output, '\0' with -z */
+static char line_end = '\n';
+
 static void print_ino_path(const char *prepend, const char *str)
 {
        if (prepend)
-               pr_default("%s/%s\n", prepend, str);
+               pr_default("%s/%s%c", prepend, str, line_end);
        else
-               pr_default("%s\n", str);
+               pr_default("%s%c", str, line_end);
 }
 
 static int __ino_to_path_fd(u64 inum, int fd, const char *prepend)
@@ -120,10 +123,12 @@ static int resolve_ino_paths(int fd, const char *arg, const char *arg_fs_path,
                             const u8 *fsid, u64 root, u64 inum);
 
 static const char * const cmd_inspect_inode_resolve_usage[] = {
-       "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
+       "btrfs inspect-internal inode-resolve [-vz] <inode> <path>",
        "Get file system paths for the given inode",
        "",
        OPTLINE("-v", "deprecated, alias for global -v option"),
+       OPTLINE("-z, --zero", "end each path with a NUL character instead of a newline, "
+               "for names that contain newlines"),
        HELPINFO_INSERT_GLOBALS,
        HELPINFO_INSERT_VERBOSE,
        NULL
@@ -143,7 +148,12 @@ static int cmd_inspect_inode_resolve(const struct cmd_struct *cmd,
 
        optind = 0;
        while (1) {
-               int c = getopt(argc, argv, "v");
+               static const struct option long_options[] = {
+                       { "zero", no_argument, NULL, 'z' },
+                       { NULL, 0, NULL, 0 }
+               };
+               int c = getopt_long(argc, argv, "vz", long_options, NULL);
+
                if (c < 0)
                        break;
 
@@ -151,6 +161,9 @@ static int cmd_inspect_inode_resolve(const struct cmd_struct *cmd,
                case 'v':
                        bconf_be_verbose();
                        break;
+               case 'z':
+                       line_end = '\0';
+                       break;
                default:
                        usage_unknown_option(cmd, argv);
                }
@@ -572,7 +585,7 @@ out:
 }
 
 static const char * const cmd_inspect_logical_resolve_usage[] = {
-       "btrfs inspect-internal logical-resolve [-Pvo] [-s bufsize] <logical> <path>",
+       "btrfs inspect-internal logical-resolve [-Pvoz] [-s bufsize] <logical> <path>",
        "Get file system paths for the given logical address",
        "",
        OPTLINE("-P", "skip the path resolving and print the inodes instead"),
@@ -582,6 +595,8 @@ static const char * const cmd_inspect_logical_resolve_usage[] = {
                "resolved results. The max value one can set is 64k with the "
                "v1 ioctl. Sizes over 64k will use the v2 ioctl (kernel 4.15+)"),
        OPTLINE("-v", "deprecated, alias for global -v option"),
+       OPTLINE("-z, --zero", "end each line of output (path, or inode with -P) with a NUL "
+               "character instead of a newline, for names that contain newlines"),
        HELPINFO_INSERT_GLOBALS,
        HELPINFO_INSERT_VERBOSE,
        NULL
@@ -604,7 +619,12 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,
 
        optind = 0;
        while (1) {
-               int c = getopt(argc, argv, "Pvos:");
+               static const struct option long_options[] = {
+                       { "zero", no_argument, NULL, 'z' },
+                       { NULL, 0, NULL, 0 }
+               };
+               int c = getopt_long(argc, argv, "Pvos:z", long_options, NULL);
+
                if (c < 0)
                        break;
 
@@ -621,6 +641,9 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,
                case 's':
                        size = arg_strtou64(optarg);
                        break;
+               case 'z':
+                       line_end = '\0';
+                       break;
                default:
                        usage_unknown_option(cmd, argv);
                }
@@ -686,8 +709,8 @@ static int cmd_inspect_logical_resolve(const struct cmd_struct *cmd,
                int ret2;
 
                if (!getpath) {
-                       pr_default("inode %llu offset %llu root %llu\n", inum,
-                               offset, root);
+                       pr_default("inode %llu offset %llu root %llu%c", inum,
+                               offset, root, line_end);
                        continue;
                }
 
index 36987b4be95e4ea8032ef53ad469f394652a1bdb..1823038c6353a6088999f5b1a51581879f3151b3 100755 (executable)
@@ -202,6 +202,24 @@ check_logical_offset_exact()
        fi
 }
 
+# Resolve $1 with $2 as the path argument and -z: the output must be exactly
+# $3..., each followed by a NUL character
+check_logical_offset_zero()
+{
+       local offset="$1"
+       local path="$2"
+       local tmp
+
+       shift 2
+       tmp=$(_mktemp logical-resolve-zero)
+       run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                               logical-resolve -z "$offset" "$path" > "$tmp"
+       if ! printf '%s\0' "$@" | cmp -s - "$tmp"; then
+               _fail "logical-resolve -z $offset $path did not print exactly: $*"
+       fi
+       rm -f -- "$tmp"
+}
+
 # Mount points and mounted directories with special characters: mnt6 is a
 # bind mount of the odd directory of subvol2 at an odd mount point, mnt7
 # the unrelated subvol1, then mnt8 subvol2 itself at an odd mount point
@@ -213,6 +231,18 @@ run_check_umount_test_dev "$TEST_MNT"
 for offset in $file6offsets; do
        check_logical_offset_exact "$offset" "mnt6 $odd" "mnt6 $odd/file6"
        check_logical_offset_exact "$offset" mnt7 "$PWD/mnt6 $odd/file6"
+       check_logical_offset_zero "$offset" "mnt6 $odd" "mnt6 $odd/file6"
+       check_logical_offset_zero "$offset" mnt7 "$PWD/mnt6 $odd/file6"
+       # -P -z: the same records as -P, each ended by NUL instead of newline
+       lines=$(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                       logical-resolve -P "$offset" mnt7 | wc -l)
+       nuls=$(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                       logical-resolve -P -z "$offset" mnt7 | tr -cd '\0' | wc -c)
+       newlines=$(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                       logical-resolve -P -z "$offset" mnt7 | tr -cd '\n' | wc -c)
+       if [ "$lines" -eq 0 ] || [ "$nuls" -ne "$lines" ] || [ "$newlines" -ne 0 ]; then
+               _fail "logical-resolve -P -z $offset: $nuls NULs, $newlines newlines, $lines lines"
+       fi
 done
 run_check $SUDO_HELPER mount -o subvol=/@/vol1/bdir/sub/subvol2 "$TEST_DEV" "mnt8 $odd"
 for offset in $file6offsets; do
index 0e46e4b832d3658531acc25da7efefd85661e324..c70a1b3c5870ebcb732de1112b8ea9cbdd5db7a7 100755 (executable)
@@ -50,6 +50,24 @@ check_inode_resolve()
        fi
 }
 
+# Resolve inode $1 with $2 as the path argument and -z: the paths printed,
+# each followed by a NUL character, must be exactly $3... in any order
+check_inode_resolve_zero()
+{
+       local ino="$1"
+       local path="$2"
+       local tmp
+
+       shift 2
+       tmp=$(_mktemp inode-resolve-zero)
+       run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                               inode-resolve -z "$ino" "$path" > "$tmp"
+       if ! cmp -s <(printf '%s\0' "$@" | sort -z) <(sort -z "$tmp"); then
+               _fail "inode-resolve -z $ino $path did not print exactly: $*"
+       fi
+       rm -f -- "$tmp"
+}
+
 run_check_mkfs_test_dev
 run_check_mount_test_dev
 run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/vol"
@@ -83,6 +101,15 @@ check_inode_resolve "$ino" "$bind1" 0 "$bind1/sub/file" \
 check_inode_resolve "$ino" "$TEST_MNT/idir/sub" 0 "$TEST_MNT/idir/sub/file" \
        "$TEST_MNT/ldir/link" "$TEST_MNT/link"
 
+# With -z, each path ends with NUL, also for a name with a newline and a space
+name=$'new\nline and space'
+run_check $SUDO_HELPER ln "$TEST_MNT/idir/sub/file" "$TEST_MNT/ldir/$name"
+check_inode_resolve_zero "$ino" "$TEST_MNT" "$TEST_MNT/idir/sub/file" \
+       "$TEST_MNT/ldir/link" "$TEST_MNT/link" "$TEST_MNT/ldir/$name"
+check_inode_resolve_zero "$ino" "$bind1" "$bind1/sub/file" \
+       "$TEST_MNT/ldir/link" "$TEST_MNT/link" "$TEST_MNT/ldir/$name"
+run_check $SUDO_HELPER rm -- "$TEST_MNT/ldir/$name"
+
 # Without it, through the bind mount of the directory they are in, if any
 run_check_umount_test_dev "$TEST_MNT"
 check_inode_resolve "$ino" "$bind1" 1 "$bind1/sub/file" "$bind2/link"