From 637563be3ca6e2ed020db7005e1bb761e08979f6 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Mon, 7 Nov 2016 11:42:02 +0100 Subject: [PATCH] btrfs-progs: send dump: introduce helper for printing escaped path The send dump prints one line per stream operation, we'd like to prevent any line breakage, so the characters are printed escaped in the C style. Signed-off-by: David Sterba --- send-dump.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/send-dump.c b/send-dump.c index 8540913c..9cc84c23 100644 --- a/send-dump.c +++ b/send-dump.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include "utils.h" @@ -45,6 +46,47 @@ } \ }) +/* + * Print path and escape chaacters (in a C way) that could break the line. + * Returns the length of the escaped characters. Unprintable characters are + * escaped as octals. + */ +static int print_path_escaped(const char *path) +{ + size_t i; + size_t path_len = strlen(path); + int len = 0; + + for (i = 0; i < path_len; i++) { + char c = path[i]; + + len++; + switch (c) { + case '\a': putchar('\\'); putchar('a'); len++; break; + case '\b': putchar('\\'); putchar('b'); len++; break; + case '\e': putchar('\\'); putchar('e'); len++; break; + case '\f': putchar('\\'); putchar('f'); len++; break; + case '\n': putchar('\\'); putchar('n'); len++; break; + case '\r': putchar('\\'); putchar('r'); len++; break; + case '\t': putchar('\\'); putchar('t'); len++; break; + case '\v': putchar('\\'); putchar('v'); len++; break; + case ' ': putchar('\\'); putchar(' '); len++; break; + case '\\': putchar('\\'); putchar('\\'); len++; break; + default: + if (!isprint(c)) { + printf("\\%c%c%c", + '0' + ((c & 0300) >> 6), + '0' + ((c & 070) >> 3), + '0' + (c & 07)); + len += 3; + } else { + putchar(c); + } + } + } + return len; +} + /* * Underlying PRINT_DUMP, the only difference is how we handle * the full path.