diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit a8cd189f0df149ccef77f2b8ca2cf9d7f6d73cf7
parent 847f323ad69b083709994dc86f411a13dc9aa338
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Wed, 19 Jan 2022 00:23:07 +0100

feat(caldav): rm existing remote entry before upload

Diffstat:
Msrc/caldav.c | 65+++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/caldav.c b/src/caldav.c @@ -101,6 +101,7 @@ char* read_tokenfile() { token_buf[token_bytes] = '\0'; char* new_access_token = extract_json_value(token_buf, "access_token", true); + fprintf(stderr, "New access token: %s\n", new_access_token); strncpy(access_token, new_access_token, 200); free(new_access_token); @@ -390,12 +391,6 @@ char* caldav_req(struct tm* date, char* url, char* http_method, char* postfields return NULL; } - // // if access_token is NULL, the program segfaults - // // while construcing the bearer_token below - // if (access_token == NULL) { - // return NULL; - // } - CURLcode res; curl = curl_easy_init(); @@ -693,63 +688,56 @@ int caldav_sync(struct tm* date, // fetch event for the cursor date sprintf(uri, "%s%s", GOOGLE_API_URI, calendar_href); - // free memory allocated to store curl response - free(home_set); char* event = caldav_req(date, uri, "REPORT", caldata_postfields, 0); // todo: warn if multiple events, // multistatus has more than just one caldav:calendar-data elements + // currently, the code below will just extract the first occurance of + // LAST-MODIFIED, UID and DESCRIPTION (from the first event) // get path of entry char path[100]; char* ppath = path; fpath(CONFIG.dir, strlen(CONFIG.dir), date, &ppath, sizeof path); - struct tm* localfile_time; - time_t epoch_zero = 0; + // assume local file does not exist + struct tm* localfile_time = gmtime(&epoch_zero); + // check last modification time of local time struct stat attr; - if (stat(path, &attr) != 0) { - perror("Stat failed"); - // local file does probably not exist - localfile_time = gmtime(&epoch_zero); - } else { - // remember utc time of file on disk + if (stat(path, &attr) == 0) { + // if local file does exists, remember utc time of file on disk localfile_time = gmtime(&attr.st_mtime); } - // set to negative value, so mktime uses timezone information and system databases - // to attempt to determine whether DST is in effect at the specified time - // localfile_time->tm_isdst = -1; time_t localfile_date = mktime(localfile_time); - struct tm remote_datetime = {0}; - time_t remote_date; + struct tm* remote_datetime; + time_t remote_date = 0; long search_pos = 0; // check remote LAST-MODIFIED:20210521T212441Z of remote event char* remote_last_mod = extract_ical_field(event, "LAST-MODIFIED", &search_pos, false); char* remote_uid = extract_ical_field(event, "UID", &search_pos, false); - if (remote_last_mod == NULL || remote_uid == NULL) { - // remote file does probably not exist, set remote date to 1970 - remote_date = epoch_zero; - } else { - strptime(remote_last_mod, "%Y%m%dT%H%M%SZ", &remote_datetime); - // remote_datetime.tm_isdst = -1; - remote_date = mktime(&remote_datetime); + // init remote date to 1970, assume remote file does not exist + remote_datetime = gmtime(&epoch_zero); + + if (remote_last_mod != NULL) { + // if remote date does exist, set date to the correct timestamp + strptime(remote_last_mod, "%Y%m%dT%H%M%SZ", remote_datetime); free(remote_last_mod); - free(remote_uid); } + remote_date = mktime(remote_datetime); + fprintf(stderr, "Info - remote last mod: %s\n", ctime(&remote_date)); fprintf(stderr, "Info - local last mod: %s\n", ctime(&localfile_date)); double timediff = difftime(localfile_date, remote_date); fprintf(stderr, "Info - Time diff between local and remote mod time:%e\n", timediff); - char* rmt_desc; char dstr[16]; int conf_ch = 0; @@ -760,9 +748,21 @@ int caldav_sync(struct tm* date, wclear(header); // free memory allocated to store curl response free(event); + free(remote_uid); + free(home_set); return 0; } else if (timediff > 0) { fputs("Info - Local file is newer. Uploading to remote.\n", stderr); + + if (remote_uid) { + // purge any existing daily calendar entries on the remote side + char event_uri[300]; + sprintf(event_uri, "%s%s%s.ics", GOOGLE_API_URI, calendar_href, remote_uid); + fprintf(stderr, "DELETE Url: %s\n", event_uri); + char* response = caldav_req(date, event_uri, "DELETE", NULL, 0); + free(response); + } + put_event(date, dir, dir_size, uri); pthread_cancel(progress_tid); @@ -775,6 +775,9 @@ int caldav_sync(struct tm* date, fprintf(stderr, "Error - Could not fetch description of remote event. Aborting sync.\n"); pthread_cancel(progress_tid); wclear(header); + free(event); + free(remote_uid); + free(home_set); return -1; } @@ -828,6 +831,8 @@ int caldav_sync(struct tm* date, echo(); curs_set(0); free(rmt_desc); + free(remote_uid); + free(home_set); // home_set was required for sprintfing calendar_href to event_uri } // free memory allocated to store curl response