Escape the path field so unusual filenames survive a round trip

A filename may contain any byte except NUL and '/', so it may contain the
field delimiter or a newline. Until now create wrote such paths verbatim:
a name with '|' shifted the field layout and a name with a newline split
the record in two, and list could only reject the result.

create now escapes the path before writing it -- '\' as '\\', '|' as
'\|', and a newline as '\n' -- and list decodes it back. The backslash
has to be escaped as well: without it '\|' would be ambiguous between an
escaped delimiter and a literal backslash followed by a delimiter.

Splitting stays escape-unaware on purpose. Only the path may hold an
escaped delimiter and it is the last of the seven fields, so the six
separators found while splitting are always the real ones; the check that
used to reject a delimiter in the last field moves into the unescaper,
which has the context to tell a valid escape from a malformed one. It
rejects a bare delimiter, an unknown escape, and a trailing backslash,
and reports them as an invalid path field rather than an unparsable line.

walk guards the d_type fast path with #ifdef DT_UNKNOWN and falls back to
the lstat path when the constants are unavailable, which SPEC 4.1 already
requires and which POSIX-only feature test macros need, since d_type is a
BSD extension rather than part of POSIX.

Verified by decoding every record of three trees -- one with pipes,
backslashes and a newline in the names, the SPEC 22 tree, and this
repository -- and confirming lstat finds all 315 decoded paths.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F3NNME8GifswDYLr9VpBzs
This commit is contained in:
Fabio Scotto di Santolo
2026-08-16 22:47:59 +02:00
parent 691988e505
commit 3d63af4202
3 changed files with 107 additions and 11 deletions

View File

@@ -32,6 +32,40 @@ static char file_type(mode_t mode) {
return '?';
}
static char *sanitize_path(const char *path) {
size_t len = strlen(path);
size_t escapes = 0;
for (size_t i = 0; i < len; i++) {
if (path[i] == '|' || path[i] == '\\' || path[i] == '\n')
escapes++;
}
char *s = calloc(len + escapes + 1, sizeof(char));
if (s == NULL)
return NULL;
for (size_t i = 0, j = 0; i < len; i++) {
switch (path[i]) {
case '\\':
s[j++] = '\\';
s[j++] = '\\';
break;
case '|':
s[j++] = '\\';
s[j++] = '|';
break;
case '\n':
s[j++] = '\\';
s[j++] = 'n';
break;
default:
s[j++] = path[i];
break;
}
}
return s;
}
static int write_record(const char *path, void *context) {
// skip snapshot files
const char *base = strrchr(path, '/');
@@ -52,12 +86,16 @@ static int write_record(const char *path, void *context) {
uintmax_t gid = (uintmax_t)st.st_gid;
intmax_t size = (intmax_t)st.st_size;
intmax_t time = (intmax_t)st.st_mtime;
char *s = sanitize_path(path);
if (s == NULL)
return -1;
// Write record in the stream file
return fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid,
size, time, path) < 0
? -1
: 0;
int ret = fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid,
gid, size, time, s);
free(s);
return ret < 0 ? -1 : 0;
}
int create(const char *directory, const char *output_file) {