From d71839afd88589247d8dd42b2b09d024f521749d Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Tue, 21 Oct 2014 23:34:29 -0400 Subject: journal-upload: verify state file can be saved before uploading Do our best verify that we can actually write the state file before upload commences to avoid duplicate messages on the server. --- src/journal-remote/journal-upload.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/journal-remote') diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c index 87d6ff7c06..37c12f0e07 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -31,6 +31,7 @@ #include "util.h" #include "build.h" #include "fileio.h" +#include "mkdir.h" #include "conf-parser.h" #include "journal-upload.h" @@ -93,6 +94,32 @@ static size_t output_callback(char *buf, return size * nmemb; } +static int check_cursor_updating(Uploader *u) { + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + int r; + + if (!u->state_file) + return 0; + + r = mkdir_parents(u->state_file, 0755); + if (r < 0) { + log_error("Cannot create parent directory of state file %s: %s", + u->state_file, strerror(-r)); + return r; + } + + r = fopen_temporary(u->state_file, &f, &temp_path); + if (r < 0) { + log_error("Cannot save state to %s: %s", + u->state_file, strerror(-r)); + return r; + } + unlink(temp_path); + + return 0; +} + static int update_cursor_state(Uploader *u) { _cleanup_free_ char *temp_path = NULL; _cleanup_fclose_ FILE *f = NULL; @@ -779,6 +806,10 @@ int main(int argc, char **argv) { sd_event_set_watchdog(u.events, true); + r = check_cursor_updating(&u); + if (r < 0) + goto cleanup; + log_debug("%s running as pid "PID_FMT, program_invocation_short_name, getpid()); -- cgit v1.2.3-54-g00ecf