diff --git a/src/create.c b/src/create.c index 4fe3e7c..3248eba 100644 --- a/src/create.c +++ b/src/create.c @@ -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) { diff --git a/src/list.c b/src/list.c index 90ef05e..5ee67e4 100644 --- a/src/list.c +++ b/src/list.c @@ -1,4 +1,4 @@ -#define _GNU_SOURCE +#define _POSIX_C_SOURCE 200809L #include "list.h" #include #include @@ -44,14 +44,61 @@ static int split_record(char *line, char *fields[NFIELDS], char delim) { p = sep + 1; } + /* Only the path may contain an escaped delimiter, and it is the last field, + so the six separators found above are always the real ones. Validating + what is left is the unescaper's job. */ fields[NFIELDS - 1] = p; - if (strchr(fields[NFIELDS - 1], delim) != NULL) - return -1; - return 0; } +/* Undo the escaping create applies: \\ -> \, \| -> |, \n -> newline. A bare + delimiter, an unknown escape, or a trailing backslash means the record is + malformed; that is reported with EINVAL to keep it apart from allocation + failure. */ +static char *unescape_path(const char *s, char delim) { + const size_t len = strlen(s); + + char *out = calloc(len + 1, sizeof(char)); + if (out == NULL) + return NULL; + + size_t j = 0; + for (size_t i = 0; i < len; i++) { + if (s[i] == delim) + goto malformed; + + if (s[i] != '\\') { + out[j++] = s[i]; + continue; + } + + /* s[len] is the terminator, so a trailing backslash lands on the + default case rather than reading past the end. */ + i++; + switch (s[i]) { + case '\\': + out[j++] = '\\'; + break; + case 'n': + out[j++] = '\n'; + break; + default: + if (s[i] != delim) + goto malformed; + out[j++] = delim; + break; + } + } + + return out; + +malformed: + free(out); + errno = EINVAL; + return NULL; +} + static int parse_file_type(const char *s, char *out) { if (s == NULL || s[0] == '\0' || s[1] != '\0') return -1; @@ -191,13 +238,18 @@ struct RecordObject *unpack(char *s) { return objp; } - char *path = calloc(strlen(fields[6]) + 1, sizeof(char)); + errno = 0; + char *path = unescape_path(fields[6], '|'); if (path == NULL) { + if (errno == EINVAL) { + field = PATH; + return objp; + } release(objp); return NULL; } - objp->path = strcpy(path, fields[6]); + objp->path = path; return objp; } diff --git a/src/walk.c b/src/walk.c index 1647b87..14a13b4 100644 --- a/src/walk.c +++ b/src/walk.c @@ -60,6 +60,7 @@ int walk(const char *directory, operation op, void *context) { if (path == NULL) goto out; +#ifdef DT_UNKNOWN switch (direntp->d_type) { case DT_UNKNOWN: // operating system or filesystem do not support dirent->d_type. @@ -78,6 +79,11 @@ int walk(const char *directory, operation op, void *context) { goto out; break; } +#else + if (handle_unknown(path, op, context) == -1) { + goto out; + } +#endif free(path); path = NULL;