From ef73193623ccf282340e912160c22e123f7ead39 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 17 Jul 2024 03:40:43 +0200 Subject: [PATCH] btrfs-progs: dump-tree: escape special characters in paths or xattrs Filenames can contain a newline (or other funny characters), this makes the dump-tree output confusing, same for xattr names or values that can binary data. Encode the special characters in the C-style ('\e' -> "\e", or \NNN if there's no single letter representation). This is based on the isprint() as it's espected either on a terminal or in a dump file. Issue: #350 Issue: #407 Signed-off-by: David Sterba --- Documentation/btrfs-inspect-internal.rst | 8 ++++++-- kernel-shared/print-tree.c | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Documentation/btrfs-inspect-internal.rst b/Documentation/btrfs-inspect-internal.rst index b72651ed..79713c30 100644 --- a/Documentation/btrfs-inspect-internal.rst +++ b/Documentation/btrfs-inspect-internal.rst @@ -66,8 +66,12 @@ dump-tree [options] [device...] a positive educational effect on understanding the internal filesystem structure. .. note:: - Contains file names, consider that if you're asked to send the dump for - analysis. Does not contain file data. + By default contains file names, consider that if you're asked + to send the dump for analysis and use *--hide-names* eventually. + Does not contain file data. + + Special characters in file names, xattr names and values are escaped, + in the C style like ``\n`` and octal encoding ``\NNN``. ``Options`` diff --git a/kernel-shared/print-tree.c b/kernel-shared/print-tree.c index 1692e647..ab85432e 100644 --- a/kernel-shared/print-tree.c +++ b/kernel-shared/print-tree.c @@ -93,7 +93,9 @@ static void print_dir_item(struct extent_buffer *eb, u32 size, } else { read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len); - printf("\t\tname: %.*s\n", len, namebuf); + printf("\t\tname: "); + string_print_escape_special_len(namebuf, len); + printf("\n"); } if (data_len) { @@ -104,7 +106,9 @@ static void print_dir_item(struct extent_buffer *eb, u32 size, } else { read_extent_buffer(eb, namebuf, (unsigned long)(di + 1) + name_len, len); - printf("\t\tdata %.*s\n", len, namebuf); + printf("\t\tdata "); + string_print_escape_special_len(namebuf, len); + printf("\n"); } } len = sizeof(*di) + name_len + data_len; @@ -137,7 +141,9 @@ static void print_inode_extref_item(struct extent_buffer *eb, u32 size, } else { read_extent_buffer(eb, namebuf, (unsigned long)extref->name, len); - printf("name: %.*s\n", len, namebuf); + printf("name: "); + string_print_escape_special_len(namebuf, len); + printf("\n"); } len = sizeof(*extref) + name_len; @@ -167,7 +173,9 @@ static void print_inode_ref_item(struct extent_buffer *eb, u32 size, } else { read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len); - printf("name: %.*s\n", len, namebuf); + printf("name: "); + string_print_escape_special_len(namebuf, len); + printf("\n"); } len = sizeof(*ref) + name_len; ref = (struct btrfs_inode_ref *)((char *)ref + len);