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
* 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
-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
(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)
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)
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
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;
case 'v':
bconf_be_verbose();
break;
+ case 'z':
+ line_end = '\0';
+ break;
default:
usage_unknown_option(cmd, argv);
}
}
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"),
"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
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;
case 's':
size = arg_strtou64(optarg);
break;
+ case 'z':
+ line_end = '\0';
+ break;
default:
usage_unknown_option(cmd, argv);
}
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;
}
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
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
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"
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"