From ceeac3bc42f99490e8b0027aa69ab195a843adb7 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sun, 8 Feb 2026 12:09:16 +0100 Subject: Rework ftag sync Use rsync rather than scp or ssh, it is more flexible and closer to what we want: preserving modification time, copying recursively. --- src/main.c | 73 ++++++++++++++++++++++---------------------------------------- 1 file changed, 26 insertions(+), 47 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 230b7ed..f9a398a 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,6 @@ #include #include #include -#include #ifndef FTAG_REMOTE_HOST #define FTAG_REMOTE_HOST "localhost" @@ -19,7 +18,7 @@ #ifndef FTAG_REMOTE_ROOT /* HOME on remote host */ -#define FTAG_REMOTE_ROOT "." +#define FTAG_REMOTE_ROOT "ftag" #endif #define DATABASE_PATH (FTAG_ROOT "/ftag.sqlite3") @@ -1150,62 +1149,42 @@ static void ftag_sync_help(int, char **) puts(" push update the remote database from the local"); } -static void ftag_sync_pull(int, char **) +/* Run rsync to perform either the push or the pull action. If argument PUSH is + * non-zero, perform a push, else perform a pull. */ +static void ftag_sync_run_rsync(int push) { - if (!ftag_sync_is_remote_newer()) - return; - char remote_arg[128]; - strbuild(remote_arg, "%s:%s/%s", - FTAG_REMOTE_HOST, FTAG_REMOTE_ROOT, "ftag.sqlite3"); + char remote_root[128]; + strbuild(remote_root, "%s:%s/", + FTAG_REMOTE_HOST, FTAG_REMOTE_ROOT); + char *first = remote_root; + char *second = FTAG_ROOT "/"; + if (push) { + first = FTAG_ROOT "/"; + second = remote_root; + } char *cmd[] = { - "scp", - remote_arg, - DATABASE_PATH, + "rsync", + "--verbose", /* temporary */ + "--archive", + first, + second, NULL }; ftag_execvp(cmd, 1); - time_t remote_mtime = get_remote_mtime(); - struct utimbuf times = { - .actime = remote_mtime, - .modtime = remote_mtime - }; - int rc = utime(DATABASE_PATH, ×); - if (rc == -1) { - fprintf(stderr, "utime: %s:", DATABASE_PATH); - perror(""); - exit(EXIT_FAILURE); - } +} + +static void ftag_sync_pull(int, char **) +{ + if (!ftag_sync_is_remote_newer()) + return; + ftag_sync_run_rsync(0); } static void ftag_sync_push(int, char **) { if (!ftag_sync_is_local_newer()) return; - char remote_arg[128]; - strbuild(remote_arg, "%s:%s/%s", - FTAG_REMOTE_HOST, FTAG_REMOTE_ROOT, "ftag.sqlite3"); - char *cmd_scp[] = { - "scp", - DATABASE_PATH, - remote_arg, - NULL - }; - ftag_execvp(cmd_scp, 1); - time_t local_mtime = get_local_mtime(); - char mtime_date[64]; - strbuild(mtime_date, "@%ld", local_mtime); - char remote_path[128]; - strbuild(remote_path, "%s/ftag.sqlite3", FTAG_REMOTE_ROOT); - char *cmd_ssh_touch[] = { - "ssh", - FTAG_REMOTE_HOST, - "touch", - "--date", - mtime_date, - remote_path, - NULL - }; - ftag_execvp(cmd_ssh_touch, 1); + ftag_sync_run_rsync(1); } static void ftag_sync(int argc, char **argv) -- cgit v1.2.3