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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-06-12 12:32:12 -07:00
parent 492369b312
commit e21cae2d46
5 changed files with 41 additions and 22 deletions

5
dive.h
View file

@ -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);

2
file.c
View file

@ -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;

View file

@ -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;
}

View file

@ -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;

View file

@ -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);