PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ssfossil/fossil/src/sync.c

https://github.com/paulfitz/coopy
C | 251 lines | 124 code | 9 blank | 118 comment | 29 complexity | d01386d6a18819ee13ff69701ba781af MD5 | raw file
  1. /*
  2. ** Copyright (c) 2007 D. Richard Hipp
  3. **
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the Simplified BSD License (also
  6. ** known as the "2-Clause License" or "FreeBSD License".)
  7. ** This program is distributed in the hope that it will be useful,
  8. ** but without any warranty; without even the implied warranty of
  9. ** merchantability or fitness for a particular purpose.
  10. **
  11. ** Author contact information:
  12. ** drh@hwaci.com
  13. ** http://www.hwaci.com/drh/
  14. **
  15. *******************************************************************************
  16. **
  17. ** This file contains code used to push, pull, and sync a repository
  18. */
  19. #include "config.h"
  20. #include "sync.h"
  21. #include <assert.h>
  22. #if INTERFACE
  23. /*
  24. ** Flags used to determine which direction(s) an autosync goes in.
  25. */
  26. #define AUTOSYNC_PUSH 1
  27. #define AUTOSYNC_PULL 2
  28. #endif /* INTERFACE */
  29. /*
  30. ** If the respository is configured for autosyncing, then do an
  31. ** autosync. This will be a pull if the argument is true or a push
  32. ** if the argument is false.
  33. */
  34. void autosync(int flags){
  35. const char *zUrl;
  36. const char *zAutosync;
  37. const char *zPw;
  38. int configSync = 0; /* configuration changes transferred */
  39. if( g.fNoSync ){
  40. return;
  41. }
  42. zAutosync = db_get("autosync", 0);
  43. if( zAutosync ){
  44. if( (flags & AUTOSYNC_PUSH)!=0 && memcmp(zAutosync,"pull",4)==0 ){
  45. return; /* Do not auto-push when autosync=pullonly */
  46. }
  47. if( is_false(zAutosync) ){
  48. return; /* Autosync is completely off */
  49. }
  50. }else{
  51. /* Autosync defaults on. To make it default off, "return" here. */
  52. }
  53. zUrl = db_get("last-sync-url", 0);
  54. if( zUrl==0 ){
  55. return; /* No default server */
  56. }
  57. zPw = db_get("last-sync-pw", 0);
  58. url_parse(zUrl);
  59. if( g.urlUser!=0 && g.urlPasswd==0 ){
  60. g.urlPasswd = mprintf("%s", zPw);
  61. }
  62. if( (flags & AUTOSYNC_PULL)!=0 && db_get_boolean("auto-shun",1) ){
  63. /* When doing an automatic pull, also automatically pull shuns from
  64. ** the server if pull_shuns is enabled.
  65. **
  66. ** TODO: What happens if the shun list gets really big?
  67. ** Maybe the shunning list should only be pulled on every 10th
  68. ** autosync, or something?
  69. */
  70. configSync = CONFIGSET_SHUN;
  71. }
  72. printf("Autosync: %s\n", g.urlCanonical);
  73. url_enable_proxy("via proxy: ");
  74. client_sync((flags & AUTOSYNC_PUSH)!=0, 1, 0, configSync, 0);
  75. }
  76. /*
  77. ** This routine processes the command-line argument for push, pull,
  78. ** and sync. If a command-line argument is given, that is the URL
  79. ** of a server to sync against. If no argument is given, use the
  80. ** most recently synced URL. Remember the current URL for next time.
  81. */
  82. static int process_sync_args(void){
  83. const char *zUrl = 0;
  84. const char *zPw = 0;
  85. int configSync = 0;
  86. int urlOptional = find_option("autourl",0,0)!=0;
  87. g.dontKeepUrl = find_option("once",0,0)!=0;
  88. url_proxy_options();
  89. db_find_and_open_repository(1);
  90. db_open_config(0);
  91. if( g.argc==2 ){
  92. zUrl = db_get("last-sync-url", 0);
  93. zPw = db_get("last-sync-pw", 0);
  94. if( db_get_boolean("auto-sync",1) ) configSync = CONFIGSET_SHUN;
  95. }else if( g.argc==3 ){
  96. zUrl = g.argv[2];
  97. }
  98. if( zUrl==0 ){
  99. if( urlOptional ) fossil_exit(0);
  100. usage("URL");
  101. }
  102. url_parse(zUrl);
  103. if( !g.dontKeepUrl ){
  104. db_set("last-sync-url", g.urlCanonical, 0);
  105. if( g.urlPasswd ) db_set("last-sync-pw", g.urlPasswd, 0);
  106. }
  107. if( g.urlUser!=0 && g.urlPasswd==0 ){
  108. if( zPw==0 ){
  109. url_prompt_for_password();
  110. }else{
  111. g.urlPasswd = mprintf("%s", zPw);
  112. }
  113. }
  114. user_select();
  115. if( g.argc==2 ){
  116. printf("Server: %s\n", g.urlCanonical);
  117. }
  118. url_enable_proxy("via proxy: ");
  119. return configSync;
  120. }
  121. /*
  122. ** COMMAND: pull
  123. **
  124. ** Usage: %fossil pull ?URL? ?options?
  125. **
  126. ** Pull changes from a remote repository into the local repository.
  127. ** Use the "-R REPO" or "--repository REPO" command-line options
  128. ** to specify an alternative repository file.
  129. **
  130. ** If the URL is not specified, then the URL from the most recent
  131. ** clone, push, pull, remote-url, or sync command is used.
  132. **
  133. ** The URL specified normally becomes the new "remote-url" used for
  134. ** subsequent push, pull, and sync operations. However, the "--once"
  135. ** command-line option makes the URL a one-time-use URL that is not
  136. ** saved.
  137. **
  138. ** See also: clone, push, sync, remote-url
  139. */
  140. void pull_cmd(void){
  141. int syncFlags = process_sync_args();
  142. client_sync(0,1,0,syncFlags,0);
  143. }
  144. /*
  145. ** COMMAND: push
  146. **
  147. ** Usage: %fossil push ?URL? ?options?
  148. **
  149. ** Push changes in the local repository over into a remote repository.
  150. ** Use the "-R REPO" or "--repository REPO" command-line options
  151. ** to specify an alternative repository file.
  152. **
  153. ** If the URL is not specified, then the URL from the most recent
  154. ** clone, push, pull, remote-url, or sync command is used.
  155. **
  156. ** The URL specified normally becomes the new "remote-url" used for
  157. ** subsequent push, pull, and sync operations. However, the "--once"
  158. ** command-line option makes the URL a one-time-use URL that is not
  159. ** saved.
  160. **
  161. ** See also: clone, pull, sync, remote-url
  162. */
  163. void push_cmd(void){
  164. process_sync_args();
  165. client_sync(1,0,0,0,0);
  166. }
  167. /*
  168. ** COMMAND: sync
  169. **
  170. ** Usage: %fossil sync ?URL? ?options?
  171. **
  172. ** Synchronize the local repository with a remote repository. This is
  173. ** the equivalent of running both "push" and "pull" at the same time.
  174. ** Use the "-R REPO" or "--repository REPO" command-line options
  175. ** to specify an alternative repository file.
  176. **
  177. ** If a user-id and password are required, specify them as follows:
  178. **
  179. ** http://userid:password@www.domain.com:1234/path
  180. **
  181. ** If the URL is not specified, then the URL from the most recent successful
  182. ** clone, push, pull, remote-url, or sync command is used.
  183. **
  184. ** The URL specified normally becomes the new "remote-url" used for
  185. ** subsequent push, pull, and sync operations. However, the "--once"
  186. ** command-line option makes the URL a one-time-use URL that is not
  187. ** saved.
  188. **
  189. ** See also: clone, push, pull, remote-url
  190. */
  191. void sync_cmd(void){
  192. int syncFlags = process_sync_args();
  193. client_sync(1,1,0,syncFlags,0);
  194. }
  195. /*
  196. ** COMMAND: remote-url
  197. **
  198. ** Usage: %fossil remote-url ?URL|off?
  199. **
  200. ** Query and/or change the default server URL used by the "pull", "push",
  201. ** and "sync" commands.
  202. **
  203. ** The remote-url is set automatically by a "clone" command or by any
  204. ** "sync", "push", or "pull" command that specifies an explicit URL.
  205. ** The default remote-url is used by auto-syncing and by "sync", "push",
  206. ** "pull" that omit the server URL.
  207. **
  208. ** See also: clone, push, pull, sync
  209. */
  210. void remote_url_cmd(void){
  211. char *zUrl;
  212. db_find_and_open_repository(1);
  213. if( g.argc!=2 && g.argc!=3 ){
  214. usage("remote-url ?URL|off?");
  215. }
  216. if( g.argc==3 ){
  217. if( strcmp(g.argv[2],"off")==0 ){
  218. db_unset("last-sync-url", 0);
  219. db_unset("last-sync-pw", 0);
  220. }else{
  221. url_parse(g.argv[2]);
  222. if( g.urlUser && g.urlPasswd==0 ){
  223. url_prompt_for_password();
  224. }
  225. db_set("last-sync-url", g.urlCanonical, 0);
  226. if( g.urlPasswd ){
  227. db_set("last-sync-pw", g.urlPasswd, 0);
  228. }else{
  229. db_unset("last-sync-pw", 0);
  230. }
  231. }
  232. }
  233. zUrl = db_get("last-sync-url", 0);
  234. if( zUrl==0 ){
  235. printf("off\n");
  236. return;
  237. }else{
  238. url_parse(zUrl);
  239. printf("%s\n", g.urlCanonical);
  240. }
  241. }