From fcf3d5e45222517d69f3ef10132286897f6805d9 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sat, 24 Jan 2026 21:53:18 +0100 Subject: Implement synchronisation detection --- src/main.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 59ad231..85bf4ce 100644 --- a/src/main.c +++ b/src/main.c @@ -1075,13 +1075,44 @@ static void ftag_query(int argc, char **argv) static void ftag_sync(int argc, char **argv) { - struct stat st; char *remote_path = FTAG_REMOTE_ROOT "/ftag.sqlite3"; char cmd[1024]; strbuild(cmd, "ssh %s sh -c 'test -f %s && stat --format=%%Y %s || echo 0'", FTAG_REMOTE_HOST, remote_path, remote_path); - FILE *stream = popen(cmd, "r"); - int rc = stat(DATABASE_PATH, &st); + FILE *pipe = popen(cmd, "r"); + if (!pipe) { + fprintf(stderr, "popen: ssh %s: ", FTAG_REMOTE_HOST); + perror(""); + exit(EXIT_FAILURE); + } + size_t line_size = 32; + char *line = malloc(line_size); + ssize_t line_len; + printf("Output of \"%s\":\n", cmd); + printf("BEGIN\n"); + while ((line_len = getline(&line, &line_size, pipe)) != -1) { + printf("%s", line); + } + printf("END\n"); + if (ferror(pipe)) { + perror("getline"); + exit(EXIT_FAILURE); + } + int rc = pclose(pipe); + if (rc == 1) { + fprintf(stderr, "pclose: ssh %s: ", FTAG_REMOTE_HOST); + perror(""); + exit(EXIT_FAILURE); + } + time_t remote_mtime = atol(line); + free(line); + struct stat st; + rc = stat(DATABASE_PATH, &st); + time_t local_mtime = st.st_mtim.tv_sec; + if (local_mtime > remote_mtime) + printf("ftag sync: local is more recent than remote !\n"); + else + printf("ftag sync: local is more recent than remote !\n"); } /* Check that the tag we are trying to create does not exist yet. If this -- cgit v1.2.3