OLD | NEW |
1 /* | 1 /* |
2 ** 2014 August 30 | 2 ** 2014 August 30 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains a command-line application that uses the RBU | 13 ** This file contains a command-line application that uses the RBU |
14 ** extension. See the usage() function below for an explanation. | 14 ** extension. See the usage() function below for an explanation. |
15 */ | 15 */ |
16 | 16 |
17 #include "sqlite3rbu.h" | 17 #include "sqlite3rbu.h" |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
20 #include <string.h> | 20 #include <string.h> |
21 | 21 |
22 /* | 22 /* |
23 ** Print a usage message and exit. | 23 ** Print a usage message and exit. |
24 */ | 24 */ |
25 void usage(const char *zArgv0){ | 25 void usage(const char *zArgv0){ |
26 fprintf(stderr, | 26 fprintf(stderr, |
27 "Usage: %s [-step NSTEP] TARGET-DB RBU-DB\n" | 27 "Usage: %s ?OPTIONS? TARGET-DB RBU-DB\n" |
28 "\n" | 28 "\n" |
29 " Argument RBU-DB must be an RBU database containing an update suitable for\n" | 29 "Where options are:\n" |
30 " target database TARGET-DB. If NSTEP is set to less than or equal to zero\n" | 30 "\n" |
31 " (the default value), this program attempts to apply the entire update to\n" | 31 " -step NSTEP\n" |
32 " the target database.\n" | 32 " -vacuum\n" |
| 33 "\n" |
| 34 " If the -vacuum switch is not present, argument RBU-DB must be an RBU\n" |
| 35 " database containing an update suitable for target database TARGET-DB.\n" |
| 36 " Or, if -vacuum is specified, then TARGET-DB is a database to vacuum using\n" |
| 37 " RBU, and RBU-DB is used as the state database for the vacuum (refer to\n" |
| 38 " API documentation for details).\n" |
| 39 "\n" |
| 40 " If NSTEP is set to less than or equal to zero (the default value), this \n" |
| 41 " program attempts to perform the entire update or vacuum operation before\n" |
| 42 " exiting\n" |
33 "\n" | 43 "\n" |
34 " If NSTEP is greater than zero, then a maximum of NSTEP calls are made\n" | 44 " If NSTEP is greater than zero, then a maximum of NSTEP calls are made\n" |
35 " to sqlite3rbu_step(). If the RBU update has not been completely applied\n" | 45 " to sqlite3rbu_step(). If the RBU update has not been completely applied\n" |
36 " after the NSTEP'th call is made, the state is saved in the database RBU-DB\n" | 46 " after the NSTEP'th call is made, the state is saved in the database RBU-DB\n" |
37 " and the program exits. Subsequent invocations of this (or any other RBU)\n" | 47 " and the program exits. Subsequent invocations of this (or any other RBU)\n" |
38 " application will use this state to resume applying the RBU update to the\n" | 48 " application will use this state to resume applying the RBU update to the\n" |
39 " target db.\n" | 49 " target db.\n" |
40 "\n" | 50 "\n" |
41 , zArgv0); | 51 , zArgv0); |
42 exit(1); | 52 exit(1); |
(...skipping 19 matching lines...) Expand all Loading... |
62 } | 72 } |
63 | 73 |
64 int main(int argc, char **argv){ | 74 int main(int argc, char **argv){ |
65 int i; | 75 int i; |
66 const char *zTarget; /* Target database to apply RBU to */ | 76 const char *zTarget; /* Target database to apply RBU to */ |
67 const char *zRbu; /* Database containing RBU */ | 77 const char *zRbu; /* Database containing RBU */ |
68 char zBuf[200]; /* Buffer for printf() */ | 78 char zBuf[200]; /* Buffer for printf() */ |
69 char *zErrmsg; /* Error message, if any */ | 79 char *zErrmsg; /* Error message, if any */ |
70 sqlite3rbu *pRbu; /* RBU handle */ | 80 sqlite3rbu *pRbu; /* RBU handle */ |
71 int nStep = 0; /* Maximum number of step() calls */ | 81 int nStep = 0; /* Maximum number of step() calls */ |
| 82 int bVacuum = 0; |
72 int rc; | 83 int rc; |
73 sqlite3_int64 nProgress = 0; | 84 sqlite3_int64 nProgress = 0; |
| 85 int nArg = argc-2; |
74 | 86 |
75 /* Process command line arguments. Following this block local variables | 87 if( argc<3 ) usage(argv[0]); |
76 ** zTarget, zRbu and nStep are all set. */ | 88 for(i=1; i<nArg; i++){ |
77 if( argc==5 ){ | 89 const char *zArg = argv[i]; |
78 int nArg1 = strlen(argv[1]); | 90 int nArg = strlen(zArg); |
79 if( nArg1>5 || nArg1<2 || memcmp("-step", argv[1], nArg1) ) usage(argv[0]); | 91 if( nArg>1 && nArg<=8 && 0==memcmp(zArg, "-vacuum", nArg) ){ |
80 nStep = atoi(argv[2]); | 92 bVacuum = 1; |
81 }else if( argc!=3 ){ | 93 }else if( nArg>1 && nArg<=5 && 0==memcmp(zArg, "-step", nArg) && i<nArg-1 ){ |
82 usage(argv[0]); | 94 i++; |
| 95 nStep = atoi(argv[i]); |
| 96 }else{ |
| 97 usage(argv[0]); |
| 98 } |
83 } | 99 } |
| 100 |
84 zTarget = argv[argc-2]; | 101 zTarget = argv[argc-2]; |
85 zRbu = argv[argc-1]; | 102 zRbu = argv[argc-1]; |
86 | 103 |
87 report_default_vfs(); | 104 report_default_vfs(); |
88 | 105 |
89 /* Open an RBU handle. If nStep is less than or equal to zero, call | 106 /* Open an RBU handle. A vacuum handle if -vacuum was specified, or a |
| 107 ** regular RBU update handle otherwise. */ |
| 108 if( bVacuum ){ |
| 109 pRbu = sqlite3rbu_vacuum(zTarget, zRbu); |
| 110 }else{ |
| 111 pRbu = sqlite3rbu_open(zTarget, zRbu, 0); |
| 112 } |
| 113 report_rbu_vfs(pRbu); |
| 114 |
| 115 /* If nStep is less than or equal to zero, call |
90 ** sqlite3rbu_step() until either the RBU has been completely applied | 116 ** sqlite3rbu_step() until either the RBU has been completely applied |
91 ** or an error occurs. Or, if nStep is greater than zero, call | 117 ** or an error occurs. Or, if nStep is greater than zero, call |
92 ** sqlite3rbu_step() a maximum of nStep times. */ | 118 ** sqlite3rbu_step() a maximum of nStep times. */ |
93 pRbu = sqlite3rbu_open(zTarget, zRbu, 0); | |
94 report_rbu_vfs(pRbu); | |
95 for(i=0; (nStep<=0 || i<nStep) && sqlite3rbu_step(pRbu)==SQLITE_OK; i++); | 119 for(i=0; (nStep<=0 || i<nStep) && sqlite3rbu_step(pRbu)==SQLITE_OK; i++); |
96 nProgress = sqlite3rbu_progress(pRbu); | 120 nProgress = sqlite3rbu_progress(pRbu); |
97 rc = sqlite3rbu_close(pRbu, &zErrmsg); | 121 rc = sqlite3rbu_close(pRbu, &zErrmsg); |
98 | 122 |
99 /* Let the user know what happened. */ | 123 /* Let the user know what happened. */ |
100 switch( rc ){ | 124 switch( rc ){ |
101 case SQLITE_OK: | 125 case SQLITE_OK: |
102 sqlite3_snprintf(sizeof(zBuf), zBuf, | 126 sqlite3_snprintf(sizeof(zBuf), zBuf, |
103 "SQLITE_OK: rbu update incomplete (%lld operations so far)\n", | 127 "SQLITE_OK: rbu update incomplete (%lld operations so far)\n", |
104 nProgress | 128 nProgress |
105 ); | 129 ); |
106 fprintf(stdout, zBuf); | 130 fprintf(stdout, "%s", zBuf); |
107 break; | 131 break; |
108 | 132 |
109 case SQLITE_DONE: | 133 case SQLITE_DONE: |
110 sqlite3_snprintf(sizeof(zBuf), zBuf, | 134 sqlite3_snprintf(sizeof(zBuf), zBuf, |
111 "SQLITE_DONE: rbu update completed (%lld operations)\n", | 135 "SQLITE_DONE: rbu update completed (%lld operations)\n", |
112 nProgress | 136 nProgress |
113 ); | 137 ); |
114 fprintf(stdout, zBuf); | 138 fprintf(stdout, "%s", zBuf); |
115 break; | 139 break; |
116 | 140 |
117 default: | 141 default: |
118 fprintf(stderr, "error=%d: %s\n", rc, zErrmsg); | 142 fprintf(stderr, "error=%d: %s\n", rc, zErrmsg); |
119 break; | 143 break; |
120 } | 144 } |
121 | 145 |
122 sqlite3_free(zErrmsg); | 146 sqlite3_free(zErrmsg); |
123 return (rc==SQLITE_OK || rc==SQLITE_DONE) ? 0 : 1; | 147 return (rc==SQLITE_OK || rc==SQLITE_DONE) ? 0 : 1; |
124 } | 148 } |
125 | |
OLD | NEW |