mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									492369b312
								
							
						
					
					
						commit
						e21cae2d46
					
				
					 5 changed files with 41 additions and 22 deletions
				
			
		
							
								
								
									
										5
									
								
								dive.h
									
										
									
									
									
								
							
							
						
						
									
										5
									
								
								dive.h
									
										
									
									
									
								
							| 
						 | 
					@ -671,8 +671,9 @@ extern void save_one_dive_to_mb(struct membuffer *b, struct dive *dive);
 | 
				
			||||||
struct git_oid;
 | 
					struct git_oid;
 | 
				
			||||||
struct git_repository;
 | 
					struct git_repository;
 | 
				
			||||||
#define dummy_git_repository ((git_repository *)3ul) /* Random bogus pointer, not NULL */
 | 
					#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 struct git_repository *is_git_repository(const char *filename, const char **branchp, const char **remote);
 | 
				
			||||||
extern int git_save_dives(struct git_repository *, const char *, bool select_only);
 | 
					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 int git_load_dives(struct git_repository *, const char *);
 | 
				
			||||||
extern const char *saved_git_id;
 | 
					extern const char *saved_git_id;
 | 
				
			||||||
extern void clear_git_id(void);
 | 
					extern void clear_git_id(void);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										2
									
								
								file.c
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								file.c
									
										
									
									
									
								
							| 
						 | 
					@ -425,7 +425,7 @@ int parse_file(const char *filename)
 | 
				
			||||||
	char *fmt;
 | 
						char *fmt;
 | 
				
			||||||
	int ret;
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	git = is_git_repository(filename, &branch);
 | 
						git = is_git_repository(filename, &branch, NULL);
 | 
				
			||||||
	if (git && !git_load_dives(git, branch))
 | 
						if (git && !git_load_dives(git, branch))
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										32
									
								
								git-access.c
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								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);
 | 
						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;
 | 
						int error;
 | 
				
			||||||
	git_repository *repo = NULL;
 | 
					 | 
				
			||||||
	git_remote *origin;
 | 
						git_remote *origin;
 | 
				
			||||||
	enum remote_type rt;
 | 
						enum remote_type rt;
 | 
				
			||||||
	char *proxy_string;
 | 
						char *proxy_string;
 | 
				
			||||||
	git_config *conf;
 | 
						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)
 | 
						if (strncmp(remote, "ssh://", 6) == 0)
 | 
				
			||||||
		rt = SSH;
 | 
							rt = SSH;
 | 
				
			||||||
	else if (strncmp(remote, "https://", 8) == 0)
 | 
						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) {
 | 
						if (error) {
 | 
				
			||||||
		report_error("Repository '%s' origin lookup failed (%s)",
 | 
							report_error("Repository '%s' origin lookup failed (%s)",
 | 
				
			||||||
			remote, giterr_last()->message);
 | 
								remote, giterr_last()->message);
 | 
				
			||||||
		return repo;
 | 
							return 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (rt == HTTPS && !canReachCloudServer())
 | 
						if (rt == HTTPS && !canReachCloudServer())
 | 
				
			||||||
		return repo;
 | 
							return 0;
 | 
				
			||||||
#if USE_LIBGIT23_API
 | 
					#if USE_LIBGIT23_API
 | 
				
			||||||
	git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
 | 
						git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
 | 
				
			||||||
	if (rt == SSH)
 | 
						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);
 | 
							check_remote_status(repo, origin, branch, rt);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	git_remote_free(origin);
 | 
						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;
 | 
						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.
 | 
					 * 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 flen, blen, ret;
 | 
				
			||||||
	int offset = 1;
 | 
						int offset = 1;
 | 
				
			||||||
| 
						 | 
					@ -446,6 +453,9 @@ struct git_repository *is_git_repository(const char *filename, const char **bran
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	repo = is_remote_git_repository(loc, branch);
 | 
						repo = is_remote_git_repository(loc, branch);
 | 
				
			||||||
	if (repo) {
 | 
						if (repo) {
 | 
				
			||||||
 | 
							if (remote)
 | 
				
			||||||
 | 
								*remote = loc;
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
			free(loc);
 | 
								free(loc);
 | 
				
			||||||
		*branchp = branch;
 | 
							*branchp = branch;
 | 
				
			||||||
		return repo;
 | 
							return repo;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										16
									
								
								save-git.c
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								save-git.c
									
										
									
									
									
								
							| 
						 | 
					@ -1115,7 +1115,7 @@ static int write_git_tree(git_repository *repo, struct dir *tree, git_oid *resul
 | 
				
			||||||
	return ret;
 | 
						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;
 | 
						struct dir tree;
 | 
				
			||||||
	git_oid id;
 | 
						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");
 | 
							return report_error("git tree write failed");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* And save the tree! */
 | 
						/* 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;
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (repo == dummy_git_repository)
 | 
						if (repo == dummy_git_repository)
 | 
				
			||||||
		return report_error("Unable to open git repository '%s'", branch);
 | 
							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);
 | 
						git_repository_free(repo);
 | 
				
			||||||
	free((void *)branch);
 | 
						free((void *)branch);
 | 
				
			||||||
	return ret;
 | 
						return ret;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -637,12 +637,12 @@ int save_dives_logic(const char *filename, const bool select_only)
 | 
				
			||||||
	struct membuffer buf = { 0 };
 | 
						struct membuffer buf = { 0 };
 | 
				
			||||||
	FILE *f;
 | 
						FILE *f;
 | 
				
			||||||
	void *git;
 | 
						void *git;
 | 
				
			||||||
	const char *branch;
 | 
						const char *branch, *remote;
 | 
				
			||||||
	int error;
 | 
						int error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	git = is_git_repository(filename, &branch);
 | 
						git = is_git_repository(filename, &branch, &remote);
 | 
				
			||||||
	if (git)
 | 
						if (git)
 | 
				
			||||||
		return git_save_dives(git, branch, select_only);
 | 
							return git_save_dives(git, branch, remote, select_only);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	try_to_backup(filename);
 | 
						try_to_backup(filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue