From 7508c9cda0b841eeaa657e34f2cfae2cdf3715d2 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 23 Aug 2022 07:44:05 +0200 Subject: [PATCH 1/5] Add format check macro to printf-style functions --- cmirror.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmirror.c b/cmirror.c index 1e1f5d5..5e6b97b 100644 --- a/cmirror.c +++ b/cmirror.c @@ -36,7 +36,7 @@ static const char *log_prefix; static int noisy_abort; -static void warn(const char *restrict fmt, ...) { +static G_GNUC_PRINTF(1, 2) void warn(const char *restrict fmt, ...) { if (log_prefix) fprintf(stderr, "%s: ", log_prefix); va_list ap; @@ -45,7 +45,7 @@ static void warn(const char *restrict fmt, ...) { va_end(ap); } -static G_NORETURN void die(const char *restrict fmt, ...) { +static G_NORETURN G_GNUC_PRINTF(1, 2) void die(const char *restrict fmt, ...) { if (fmt) { if (log_prefix) fprintf(stderr, "%s: ", log_prefix); From 80b0d48b6d446ec1655d8318aa85edd16cd46fe1 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 23 Aug 2022 07:57:18 +0200 Subject: [PATCH 2/5] Fix printf-format warnings and errors --- cmirror.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cmirror.c b/cmirror.c index 5e6b97b..c16e58a 100644 --- a/cmirror.c +++ b/cmirror.c @@ -181,7 +181,7 @@ static struct FileInfo *fileinfo_lstat(char *path) { fi->target = g_malloc(sb.st_size + 1); int res = readlink(path, fi->target, sb.st_size+1); if (res == -1) - die("readlink %s: %M\n", path); + die("readlink %s: %m\n", path); if (res > sb.st_size) die("readlink %s: target string to long\n", path); fi->target[res] = '\0'; @@ -302,7 +302,7 @@ static char data_buffer[10240+2]; static void fileop_mknod(char *path, char type, dev_t rdev) { if (fileop_debug) - warn("fileop: mknod %s %c %d\n", path, type, rdev); + warn("fileop: mknod %s %c %lu\n", path, type, rdev); if (fileop_noop) return; mode_t mode; @@ -410,7 +410,7 @@ static void fileop_lchown(uid_t uid, gid_t gid, char *path) { static void fileop_lmtime(time_t mtime, char *path) { if (fileop_debug) - warn("fileop: lmtime %d %s\n", mtime, path); + warn("fileop: lmtime %ld %s\n", mtime, path); if (fileop_noop) return; struct timespec timespec[2] = { { 0, UTIME_OMIT } , { mtime, 0 } }; @@ -501,7 +501,7 @@ static void add_clean_dir(char *dir) { GError *error; GDir *d = g_dir_open(dir, 0, &error); if (!d) - die("%s %s\n", error->message); + die("%s: %s\n", dir, error->message); const char *entry; while ( (entry = g_dir_read_name(d)) ) { char *path = g_strdup_printf("%s/%s", dir, entry); @@ -544,10 +544,10 @@ static ssize_t receive_record(FILE *in, char *buffer, size_t buflen) { return 0; unsigned int len = ntohs(*(uint16_t *)b); if (len > buflen) - die("data overrun %d > %d\n", len, buflen); + die("data overrun %u > %lu\n", len, buflen); size_t l2 = fread(buffer, 1, len, in); if (l2 != len) - die("data underrun %d != %d\n", l1, l2); + die("data underrun %lu != %lu\n", l1, l2); return len; } @@ -565,7 +565,7 @@ static void receive_file(FILE *in, char *filename, off_t expected_size, mode_t p if(l2 == -1) die("%s: %m\n", filename); if (l2 != l1) - die("%s: short write\n"); + die("%s: short write\n", filename); expected_size -= l2; } fdatasync(fd); @@ -916,7 +916,7 @@ static void slave(char *slave_path) { GSList *d = CLEAN_DIRS; while (d) { if (!quiet) - warn("rm -r %s\n", d->data); + warn("rm -r %s\n", (char *)d->data); d = d->next; } } @@ -937,7 +937,7 @@ static void slave(char *slave_path) { if (statbuf.st_mtime == mt->mtime) continue; if (!quiet) - warn("fix directory mtime of %s -> %d\n", mt->path, mt->mtime); + warn("fix directory mtime of %s -> %ld\n", mt->path, mt->mtime); if (!fileop_noop) { struct timespec timespec[2] = { { 0, UTIME_OMIT } , { mt->mtime, 0 } }; if (utimensat(AT_FDCWD, mt->path, timespec, AT_SYMLINK_NOFOLLOW) == -1) @@ -1257,7 +1257,7 @@ static void master(char *master_path, char *target) { unsetenv("SSH_ORIGINAL_COMMAND"); execvp(args->pdata[0], (char **)args->pdata); - die("exec %s: %m\n", args->pdata[0]); + die("exec %s: %m\n", (char *)args->pdata[0]); } else if (pid == -1) die("fork: %m"); From b069db6a8b7f7a28ef9f54a18a99b76e6566740b Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 23 Aug 2022 07:59:47 +0200 Subject: [PATCH 3/5] Fix missing deletion of CLEAN_DIR directories The actual deletion of directories which where added to CLEAN_DIRS by out_of_the_way() is missing in the code: buczek@theinternet:~/git/cmirror (fix-printf-formats)$ rm -rf /scratch/local/a buczek@theinternet:~/git/cmirror (fix-printf-formats)$ rm -rf /scratch/local/b buczek@theinternet:~/git/cmirror (fix-printf-formats)$ mkdir /scratch/local/a buczek@theinternet:~/git/cmirror (fix-printf-formats)$ touch /scratch/local/a/test buczek@theinternet:~/git/cmirror (fix-printf-formats)$ mkdir /scratch/local/b buczek@theinternet:~/git/cmirror (fix-printf-formats)$ mkdir /scratch/local/b/test buczek@theinternet:~/git/cmirror (fix-printf-formats)$ cmirror --delete /scratch/local/a /scratch/local/b theinternet.molgen.mpg.de: creating empty ./test theinternet.molgen.mpg.de: rm -r ./test.deleteme theinternet.molgen.mpg.de: fix directory mtime of . -> 1661234914 buczek@theinternet:~/git/cmirror (fix-printf-formats)$ find /scratch/local/b /scratch/local/b /scratch/local/b/test.deleteme /scratch/local/b/test Add call to remove directory. --- cmirror.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmirror.c b/cmirror.c index c16e58a..575f291 100644 --- a/cmirror.c +++ b/cmirror.c @@ -915,8 +915,10 @@ static void slave(char *slave_path) { } GSList *d = CLEAN_DIRS; while (d) { + char *path = (char *)d->data; if (!quiet) - warn("rm -r %s\n", (char *)d->data); + warn("rm -r %s\n", path); + fileop_rmdir_recurse(path); d = d->next; } } From 06cf18d9c7812bb21e88e42c089bb5ab2842d5bb Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 23 Aug 2022 08:17:45 +0200 Subject: [PATCH 4/5] Honor --noop for type change of target directory If we are running under --noop and a target directory would be replaced by another file type, we currently delete the target directory, although the actual replacement is not installed because of --noop. Do not remove to-be-replaced directory when --noop is selected. --- cmirror.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmirror.c b/cmirror.c index 575f291..55f89e1 100644 --- a/cmirror.c +++ b/cmirror.c @@ -515,7 +515,7 @@ static void add_clean_dir(char *dir) { } static void out_of_the_way(FileInfo *fi) { - if (fi->type == 'D') { + if (fi->type == 'D' && !fileop_noop) { char *deleteme = g_strdup_printf("%s.deleteme", fi->name); fileop_mv(fi->name, deleteme); CLEAN_DIRS = g_slist_prepend(CLEAN_DIRS, deleteme); From 85fdb784f964cda60f6cbb32d86e9137be059626 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 23 Aug 2022 08:29:54 +0200 Subject: [PATCH 5/5] Remove replaced directory even without --delete When a directory is replaced by another type (e.g. a plain file), it is first renamed and later removed. This later removal should be done regardless of whether --delete was selected or not. --- cmirror.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cmirror.c b/cmirror.c index 55f89e1..987d3e4 100644 --- a/cmirror.c +++ b/cmirror.c @@ -913,18 +913,19 @@ static void slave(char *slave_path) { fileop_rm(path); } } - GSList *d = CLEAN_DIRS; - while (d) { - char *path = (char *)d->data; - if (!quiet) - warn("rm -r %s\n", path); - fileop_rmdir_recurse(path); - d = d->next; - } } if (reduce) return; + GSList *d = CLEAN_DIRS; + while (d) { + char *path = (char *)d->data; + if (!quiet) + warn("rm -r %s\n", path); + fileop_rmdir_recurse(path); + d = d->next; + } + DIR_MTIME_QUEUE = g_list_reverse(DIR_MTIME_QUEUE); for (GList *e = DIR_MTIME_QUEUE ; e != NULL ; e = e->next ) { DirMtime *mt = e->data;