From e21cae2d46db8148dc294c2dff08d9321274f815 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 12 Jun 2015 12:32:12 -0700 Subject: Cloud storage: sync the remote after save This change once again tests if the remote can be reached. Even with a fairly big data file and a medium speed internet connection the remote sync is fast enough to call it nearly instantaneous. Maybe a couple of seconds. We may need more checks / different heuristics / warnings if the sync didn't happen, etc. But for now this should allow more reasonable testing. Signed-off-by: Dirk Hohndel --- dive.h | 5 +++-- file.c | 2 +- git-access.c | 34 ++++++++++++++++++++++------------ save-git.c | 16 ++++++++++++---- save-xml.c | 6 +++--- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/dive.h b/dive.h index 8e54c3d73..d4ceb76e0 100644 --- a/dive.h +++ b/dive.h @@ -671,8 +671,9 @@ extern void save_one_dive_to_mb(struct membuffer *b, struct dive *dive); struct git_oid; struct git_repository; #define dummy_git_repository ((git_repository *)3ul) /* Random bogus pointer, not NULL */ -extern struct git_repository *is_git_repository(const char *filename, const char **branchp); -extern int git_save_dives(struct git_repository *, const char *, bool select_only); +extern struct git_repository *is_git_repository(const char *filename, const char **branchp, const char **remote); +extern int sync_with_remote(struct git_repository *repo, const char *remote, const char *branch); +extern int git_save_dives(struct git_repository *, const char *, const char *remote, bool select_only); extern int git_load_dives(struct git_repository *, const char *); extern const char *saved_git_id; extern void clear_git_id(void); diff --git a/file.c b/file.c index 5646f3ed2..24756a599 100644 --- a/file.c +++ b/file.c @@ -425,7 +425,7 @@ int parse_file(const char *filename) char *fmt; int ret; - git = is_git_repository(filename, &branch); + git = is_git_repository(filename, &branch, NULL); if (git && !git_load_dives(git, branch)) return 0; diff --git a/git-access.c b/git-access.c index e6fa4b966..32091e585 100644 --- a/git-access.c +++ b/git-access.c @@ -216,21 +216,14 @@ static int check_remote_status(git_repository *repo, git_remote *origin, const c git_reference_free(remote_ref); } -static git_repository *update_local_repo(const char *localdir, const char *remote, const char *branch) +int sync_with_remote(git_repository *repo, const char *remote, const char *branch) { int error; - git_repository *repo = NULL; git_remote *origin; enum remote_type rt; char *proxy_string; git_config *conf; - error = git_repository_open(&repo, localdir); - if (error) { - report_error("Unable to open git cache repository at %s: %s", - localdir, giterr_last()->message); - return NULL; - } if (strncmp(remote, "ssh://", 6) == 0) rt = SSH; else if (strncmp(remote, "https://", 8) == 0) @@ -254,11 +247,11 @@ static git_repository *update_local_repo(const char *localdir, const char *remot if (error) { report_error("Repository '%s' origin lookup failed (%s)", remote, giterr_last()->message); - return repo; + return 0; } if (rt == HTTPS && !canReachCloudServer()) - return repo; + return 0; #if USE_LIBGIT23_API git_fetch_options opts = GIT_FETCH_OPTIONS_INIT; if (rt == SSH) @@ -276,6 +269,20 @@ static git_repository *update_local_repo(const char *localdir, const char *remot check_remote_status(repo, origin, branch, rt); git_remote_free(origin); +} + +static git_repository *update_local_repo(const char *localdir, const char *remote, const char *branch) +{ + int error; + git_repository *repo = NULL; + + error = git_repository_open(&repo, localdir); + if (error) { + report_error("Unable to open git cache repository at %s: %s", + localdir, giterr_last()->message); + return NULL; + } + sync_with_remote(repo, remote, branch); return repo; } @@ -393,7 +400,7 @@ static struct git_repository *is_remote_git_repository(char *remote, const char /* * If it's not a git repo, return NULL. Be very conservative. */ -struct git_repository *is_git_repository(const char *filename, const char **branchp) +struct git_repository *is_git_repository(const char *filename, const char **branchp, const char **remote) { int flen, blen, ret; int offset = 1; @@ -446,7 +453,10 @@ struct git_repository *is_git_repository(const char *filename, const char **bran repo = is_remote_git_repository(loc, branch); if (repo) { - free(loc); + if (remote) + *remote = loc; + else + free(loc); *branchp = branch; return repo; } diff --git a/save-git.c b/save-git.c index 16db95f6c..662ec408f 100644 --- a/save-git.c +++ b/save-git.c @@ -1115,7 +1115,7 @@ static int write_git_tree(git_repository *repo, struct dir *tree, git_oid *resul return ret; } -static int do_git_save(git_repository *repo, const char *branch, bool select_only) +static int do_git_save(git_repository *repo, const char *branch, const char *remote, bool select_only) { struct dir tree; git_oid id; @@ -1134,16 +1134,24 @@ static int do_git_save(git_repository *repo, const char *branch, bool select_onl return report_error("git tree write failed"); /* And save the tree! */ - return create_new_commit(repo, branch, &id); + if (create_new_commit(repo, branch, &id)) + return report_error("creating commit failed"); + + if (prefs.cloud_background_sync) { + /* now sync the tree with the cloud server */ + if (strstr(remote, "https://cloud.subsurface-divelog.org")) { + sync_with_remote(repo, remote, branch); + } + } } -int git_save_dives(struct git_repository *repo, const char *branch, bool select_only) +int git_save_dives(struct git_repository *repo, const char *branch, const char *remote, bool select_only) { int ret; if (repo == dummy_git_repository) return report_error("Unable to open git repository '%s'", branch); - ret = do_git_save(repo, branch, select_only); + ret = do_git_save(repo, branch, remote, select_only); git_repository_free(repo); free((void *)branch); return ret; diff --git a/save-xml.c b/save-xml.c index 6fd089f7c..fdcaccc94 100644 --- a/save-xml.c +++ b/save-xml.c @@ -637,12 +637,12 @@ int save_dives_logic(const char *filename, const bool select_only) struct membuffer buf = { 0 }; FILE *f; void *git; - const char *branch; + const char *branch, *remote; int error; - git = is_git_repository(filename, &branch); + git = is_git_repository(filename, &branch, &remote); if (git) - return git_save_dives(git, branch, select_only); + return git_save_dives(git, branch, remote, select_only); try_to_backup(filename); -- cgit v1.2.3-70-g09d2