| | @@ -25,109 +25,43 @@ |
| 25 | 25 | */ |
| 26 | 26 | #include "config.h" |
| 27 | 27 | #include "xfer.h" |
| 28 | 28 | |
| 29 | 29 | /* |
| 30 | | -** Try to locate a record that is similar to rid and is a likely |
| 31 | | -** candidate for delta against rid. The similar record must be |
| 32 | | -** referenced in the onremote table. |
| 33 | | -** |
| 34 | | -** Return the integer record ID of the similar record. Or return |
| 35 | | -** 0 if none is found. |
| 30 | +** This structure holds information about the current state of either |
| 31 | +** a client or a server that is participating in xfer. |
| 36 | 32 | */ |
| 37 | | -static int similar_record(int rid, int traceFlag){ |
| 38 | | - int inCnt, outCnt; |
| 39 | | - int i; |
| 40 | | - Stmt q; |
| 41 | | - int queue[100]; |
| 42 | | - static const char *azQuery[] = { |
| 43 | | - /* Scan the delta table first */ |
| 44 | | - "SELECT srcid, EXISTS(SELECT 1 FROM onremote WHERE rid=srcid)" |
| 45 | | - " FROM delta" |
| 46 | | - " WHERE rid=:x" |
| 47 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=srcid)" |
| 48 | | - " UNION ALL " |
| 49 | | - "SELECT rid, EXISTS(SELECT 1 FROM onremote WHERE rid=delta.rid)" |
| 50 | | - " FROM delta" |
| 51 | | - " WHERE srcid=:x" |
| 52 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=delta.rid)", |
| 53 | | - |
| 54 | | - /* Then the plink table */ |
| 55 | | - "SELECT pid, EXISTS(SELECT 1 FROM onremote WHERE rid=pid)" |
| 56 | | - " FROM plink" |
| 57 | | - " WHERE cid=:x" |
| 58 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)" |
| 59 | | - " UNION ALL " |
| 60 | | - "SELECT cid, EXISTS(SELECT 1 FROM onremote WHERE rid=cid)" |
| 61 | | - " FROM plink" |
| 62 | | - " WHERE pid=:x" |
| 63 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=cid)", |
| 64 | | - |
| 65 | | - /* Finally the mlink table */ |
| 66 | | - "SELECT pid, EXISTS(SELECT 1 FROM onremote WHERE rid=pid)" |
| 67 | | - " FROM mlink" |
| 68 | | - " WHERE fid=:x AND pid>0" |
| 69 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)" |
| 70 | | - " UNION ALL " |
| 71 | | - "SELECT fid, EXISTS(SELECT 1 FROM onremote WHERE rid=fid)" |
| 72 | | - " FROM mlink" |
| 73 | | - " WHERE pid=:x AND fid>0" |
| 74 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=fid)", |
| 75 | | - }; |
| 76 | | - |
| 77 | | - for(i=0; i<sizeof(azQuery)/sizeof(azQuery[0]); i++){ |
| 78 | | - db_prepare(&q, azQuery[i]); |
| 79 | | - queue[0] = rid; |
| 80 | | - inCnt = 1; |
| 81 | | - outCnt = 0; |
| 82 | | - if( traceFlag ) printf("PASS %d\n", i+1); |
| 83 | | - while( outCnt<inCnt ){ |
| 84 | | - int xid = queue[outCnt%64]; |
| 85 | | - outCnt++; |
| 86 | | - db_bind_int(&q, ":x", xid); |
| 87 | | - if( traceFlag ) printf("xid=%d\n", xid); |
| 88 | | - while( db_step(&q)==SQLITE_ROW ){ |
| 89 | | - int nid = db_column_int(&q, 0); |
| 90 | | - int hit = db_column_int(&q, 1); |
| 91 | | - if( traceFlag ) printf("nid=%d hit=%d\n", nid, hit); |
| 92 | | - if( hit ){ |
| 93 | | - db_finalize(&q); |
| 94 | | - return nid; |
| 95 | | - } |
| 96 | | - if( inCnt<sizeof(queue)/sizeof(queue[0]) ){ |
| 97 | | - int i; |
| 98 | | - for(i=0; i<inCnt && queue[i]!=nid; i++){} |
| 99 | | - if( i>=inCnt ){ |
| 100 | | - queue[inCnt++] = nid; |
| 101 | | - } |
| 102 | | - } |
| 103 | | - } |
| 104 | | - db_reset(&q); |
| 105 | | - } |
| 106 | | - db_finalize(&q); |
| 107 | | - } |
| 108 | | - return 0; |
| 109 | | -} |
| 33 | +typedef struct Xfer Xfer; |
| 34 | +struct Xfer { |
| 35 | + Blob *pIn; /* Input text from the other side */ |
| 36 | + Blob *pOut; /* Compose our reply here */ |
| 37 | + Blob line; /* The current line of input */ |
| 38 | + Blob aToken[5]; /* Tokenized version of line */ |
| 39 | + Blob err; /* Error message text */ |
| 40 | + int nToken; /* Number of tokens in line */ |
| 41 | + int nIGot; /* Number of "igot" messages sent */ |
| 42 | + int nFile; /* Number of files sent or received */ |
| 43 | + int nDelta; /* Number of deltas sent or received */ |
| 44 | + int nDanglingFile; /* Number of dangling deltas received */ |
| 45 | + int mxSend; /* Stop sending "file" with pOut reaches this size */ |
| 46 | +}; |
| 47 | + |
| 110 | 48 | |
| 111 | 49 | /* |
| 112 | | -** COMMAND: test-similar-record |
| 50 | +** The input blob contains a UUID. Convert it into a record ID. |
| 51 | +** Create a phantom record if no prior record exists and |
| 52 | +** phantomize is true. |
| 53 | +** |
| 54 | +** Compare to uuid_to_rid(). This routine takes a blob argument |
| 55 | +** and does less error checking. |
| 113 | 56 | */ |
| 114 | | -void test_similar_record(void){ |
| 115 | | - int i; |
| 116 | | - if( g.argc<4 ){ |
| 117 | | - usage("SRC ONREMOTE..."); |
| 118 | | - } |
| 119 | | - db_must_be_within_tree(); |
| 120 | | - db_multi_exec( |
| 121 | | - "CREATE TEMP TABLE onremote(rid INTEGER PRIMARY KEY);" |
| 122 | | - ); |
| 123 | | - for(i=3; i<g.argc; i++){ |
| 124 | | - int rid = name_to_rid(g.argv[i]); |
| 125 | | - printf("%s -> %d\n", g.argv[i], rid); |
| 126 | | - db_multi_exec("INSERT INTO onremote VALUES(%d)", rid); |
| 127 | | - } |
| 128 | | - printf("similar: %d\n", similar_record(name_to_rid(g.argv[2]), 1)); |
| 57 | +static int rid_from_uuid(Blob *pUuid, int phantomize){ |
| 58 | + int rid = db_int(0, "SELECT rid FROM blob WHERE uuid='%b'", pUuid); |
| 59 | + if( rid==0 && phantomize ){ |
| 60 | + rid = content_put(0, blob_str(pUuid), 0); |
| 61 | + } |
| 62 | + return rid; |
| 129 | 63 | } |
| 130 | 64 | |
| 131 | 65 | |
| 132 | 66 | /* |
| 133 | 67 | ** The aToken[0..nToken-1] blob array is a parse of a "file" line |
| | @@ -144,162 +78,227 @@ |
| 144 | 78 | ** content of DELTASRC. |
| 145 | 79 | ** |
| 146 | 80 | ** If any error occurs, write a message into pErr which has already |
| 147 | 81 | ** be initialized to an empty string. |
| 148 | 82 | */ |
| 149 | | -static void xfer_accept_file(Blob *pIn, Blob *aToken, int nToken, Blob *pErr){ |
| 83 | +static void xfer_accept_file(Xfer *pXfer){ |
| 150 | 84 | int n; |
| 151 | 85 | int rid; |
| 152 | 86 | Blob content, hash; |
| 153 | 87 | |
| 154 | | - if( nToken<3 || nToken>4 || !blob_is_uuid(&aToken[1]) |
| 155 | | - || !blob_is_int(&aToken[nToken-1], &n) || n<=0 |
| 156 | | - || (nToken==4 && !blob_is_uuid(&aToken[2])) ){ |
| 157 | | - blob_appendf(pErr, "malformed file line"); |
| 88 | + if( pXfer->nToken<3 |
| 89 | + || pXfer->nToken>4 |
| 90 | + || !blob_is_uuid(&pXfer->aToken[1]) |
| 91 | + || !blob_is_int(&pXfer->aToken[pXfer->nToken-1], &n) |
| 92 | + || n<=0 |
| 93 | + || (pXfer->nToken==4 && !blob_is_uuid(&pXfer->aToken[2])) |
| 94 | + ){ |
| 95 | + blob_appendf(&pXfer->err, "malformed file line"); |
| 158 | 96 | return; |
| 159 | 97 | } |
| 160 | 98 | blob_zero(&content); |
| 161 | 99 | blob_zero(&hash); |
| 162 | | - blob_extract(pIn, n, &content); |
| 163 | | - if( nToken==4 ){ |
| 100 | + blob_extract(pXfer->pIn, n, &content); |
| 101 | + if( pXfer->nToken==4 ){ |
| 164 | 102 | Blob src; |
| 165 | | - int srcid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &aToken[2]); |
| 166 | | - if( srcid==0 ){ |
| 167 | | - blob_appendf(pErr, "unknown delta source: %b", &aToken[2]); |
| 103 | + int srcid = rid_from_uuid(&pXfer->aToken[2], 1); |
| 104 | + if( content_get(srcid, &src)==0 ){ |
| 105 | + content_put(&content, blob_str(&hash), srcid); |
| 106 | + blob_appendf(pXfer->pOut, "gimme %b\n", &pXfer->aToken[2]); |
| 107 | + pXfer->nDanglingFile++; |
| 168 | 108 | return; |
| 169 | 109 | } |
| 170 | | - content_get(srcid, &src); |
| 110 | + pXfer->nDelta++; |
| 171 | 111 | blob_delta_apply(&src, &content, &content); |
| 172 | 112 | blob_reset(&src); |
| 113 | + }else{ |
| 114 | + pXfer->nFile++; |
| 173 | 115 | } |
| 174 | 116 | sha1sum_blob(&content, &hash); |
| 175 | | - if( !blob_eq_str(&aToken[1], blob_str(&hash), -1) ){ |
| 176 | | - blob_appendf(pErr, "content does not match sha1 hash"); |
| 117 | + if( !blob_eq_str(&pXfer->aToken[1], blob_str(&hash), -1) ){ |
| 118 | + blob_appendf(&pXfer->err, "content does not match sha1 hash"); |
| 177 | 119 | } |
| 178 | 120 | blob_reset(&hash); |
| 179 | | - rid = content_put(&content, 0); |
| 180 | | - manifest_crosslink(rid, &content); |
| 121 | + rid = content_put(&content, 0, 0); |
| 181 | 122 | if( rid==0 ){ |
| 182 | | - blob_appendf(pErr, "%s", g.zErrMsg); |
| 123 | + blob_appendf(&pXfer->err, "%s", g.zErrMsg); |
| 124 | + }else{ |
| 125 | + manifest_crosslink(rid, &content); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/* |
| 130 | +** Try to send a file as a delta. If successful, return the number |
| 131 | +** of bytes in the delta. If not, return zero. |
| 132 | +** |
| 133 | +** If srcId is specified, use it. If not, try to figure out a |
| 134 | +** reasonable srcId. |
| 135 | +*/ |
| 136 | +static int send_as_delta( |
| 137 | + Xfer *pXfer, /* The transfer context */ |
| 138 | + int rid, /* record id of the file to send */ |
| 139 | + Blob *pContent, /* The content of the file to send */ |
| 140 | + Blob *pUuid, /* The UUID of the file to send */ |
| 141 | + int srcId /* Send as a delta against this record */ |
| 142 | +){ |
| 143 | + static const char *azQuery[] = { |
| 144 | + "SELECT srcid FROM delta JOIN pending ON pending.rid=delta.srcid" |
| 145 | + " WHERE delta.rid=%d" |
| 146 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=srcid)", |
| 147 | + |
| 148 | + "SELECT delta.rid FROM delta JOIN pending ON pending.rid=delta.rid" |
| 149 | + " WHERE srcid=%d" |
| 150 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=delta.rid)", |
| 151 | + |
| 152 | + "SELECT pid FROM plink JOIN pending ON rid=pid" |
| 153 | + " WHERE cid=%d" |
| 154 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)", |
| 155 | + |
| 156 | + "SELECT cid FROM plink JOIN pending ON rid=cid" |
| 157 | + " WHERE pid=%d" |
| 158 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=cid)", |
| 159 | + |
| 160 | + "SELECT pid FROM mlink JOIN pending ON rid=pid" |
| 161 | + " WHERE fid=%d" |
| 162 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)", |
| 163 | + |
| 164 | + "SELECT fid FROM mlink JOIN pending ON rid=fid" |
| 165 | + " WHERE pid=%d" |
| 166 | + " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=fid)", |
| 167 | + }; |
| 168 | + int i; |
| 169 | + Blob src, delta; |
| 170 | + int size = 0; |
| 171 | + |
| 172 | + for(i=0; srcId==0 && i<count(azQuery); i++){ |
| 173 | + srcId = db_int(0, azQuery[i], rid); |
| 174 | + } |
| 175 | + if( srcId && content_get(srcId, &src) ){ |
| 176 | + char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", srcId); |
| 177 | + blob_delta_create(&src, pContent, &delta); |
| 178 | + size = blob_size(&delta); |
| 179 | + if( size>=blob_size(pContent)-50 ){ |
| 180 | + size = 0; |
| 181 | + }else{ |
| 182 | + blob_appendf(pXfer->pOut, "file %b %s %d\n", pUuid, zUuid, size); |
| 183 | + blob_append(pXfer->pOut, blob_buffer(&delta), size); |
| 184 | + blob_appendf(pXfer->pOut, "\n", 1); |
| 185 | + } |
| 186 | + blob_reset(&delta); |
| 187 | + free(zUuid); |
| 188 | + blob_reset(&src); |
| 183 | 189 | } |
| 190 | + return size; |
| 184 | 191 | } |
| 185 | 192 | |
| 186 | 193 | /* |
| 187 | 194 | ** Send the file identified by rid. |
| 188 | | -** |
| 189 | | -** If pOut is not NULL, then append the text of the send message |
| 190 | | -** to pOut. Otherwise, append the text to the CGI output. |
| 191 | 195 | */ |
| 192 | | -static int send_file(int rid, Blob *pOut){ |
| 196 | +static void send_file(Xfer *pXfer, int rid, Blob *pUuid, int srcId){ |
| 193 | 197 | Blob content, uuid; |
| 194 | | - int size; |
| 195 | | - int srcid; |
| 198 | + int size = 0; |
| 196 | 199 | |
| 197 | | - |
| 200 | + if( db_exists("SELECT 1 FROM sent WHERE rid=%d", rid) ){ |
| 201 | + return; |
| 202 | + } |
| 198 | 203 | blob_zero(&uuid); |
| 199 | | - db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d AND size>=0", rid); |
| 200 | | - if( blob_size(&uuid)==0 ){ |
| 201 | | - return 0; |
| 204 | + if( pUuid==0 ){ |
| 205 | + db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d AND size>=0", rid); |
| 206 | + if( blob_size(&uuid)==0 ){ |
| 207 | + return; |
| 208 | + } |
| 209 | + pUuid = &uuid; |
| 210 | + } |
| 211 | + if( pXfer->mxSend<=blob_size(pXfer->pOut) ){ |
| 212 | + blob_appendf(pXfer->pOut, "igot %b\n", pUuid); |
| 213 | + pXfer->nIGot++; |
| 214 | + blob_reset(&uuid); |
| 215 | + return; |
| 202 | 216 | } |
| 203 | 217 | content_get(rid, &content); |
| 204 | 218 | |
| 205 | 219 | if( blob_size(&content)>100 ){ |
| 206 | | - srcid = similar_record(rid, 0); |
| 207 | | - if( srcid ){ |
| 208 | | - Blob src; |
| 209 | | - content_get(srcid, &src); |
| 210 | | - if( blob_size(&src)>100 ){ |
| 211 | | - Blob delta; |
| 212 | | - blob_delta_create(&src, &content, &delta); |
| 213 | | - blob_reset(&content); |
| 214 | | - content = delta; |
| 215 | | - blob_append(&uuid, " ", 1); |
| 216 | | - blob_append(&content, "\n", 1); |
| 217 | | - db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d", srcid); |
| 218 | | - } |
| 219 | | - blob_reset(&src); |
| 220 | | - } |
| 221 | | - } |
| 222 | | - size = blob_size(&content); |
| 223 | | - if( pOut ){ |
| 224 | | - blob_appendf(pOut, "file %b %d\n", &uuid, size); |
| 225 | | - blob_append(pOut, blob_buffer(&content), size); |
| 226 | | - }else{ |
| 227 | | - cgi_printf("file %b %d\n", &uuid, size); |
| 228 | | - cgi_append_content(blob_buffer(&content), size); |
| 229 | | - } |
| 230 | | - blob_reset(&content); |
| 231 | | - blob_reset(&uuid); |
| 232 | | - db_multi_exec("INSERT OR IGNORE INTO onremote VALUES(%d)", rid); |
| 233 | | - return size; |
| 234 | | -} |
| 235 | | - |
| 236 | | - |
| 237 | | -/* |
| 238 | | -** Send all pending files. |
| 239 | | -*/ |
| 240 | | -static int send_all_pending(Blob *pOut){ |
| 241 | | - int rid, xid, i; |
| 242 | | - int nIgot = 0; |
| 243 | | - int sent = 0; |
| 244 | | - int nSent = 0; |
| 245 | | - int maxSize = db_get_int("http-msg-size", 500000); |
| 246 | | - static const char *azQuery[] = { |
| 247 | | - "SELECT srcid FROM delta JOIN pending ON pending.rid=delta.srcid" |
| 248 | | - " WHERE delta.rid=%d" |
| 249 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=srcid)", |
| 250 | | - |
| 251 | | - "SELECT delta.rid FROM delta JOIN pending ON pending.rid=delta.rid" |
| 252 | | - " WHERE srcid=%d" |
| 253 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=delta.rid)", |
| 254 | | - |
| 255 | | - "SELECT pid FROM plink JOIN pending ON rid=pid" |
| 256 | | - " WHERE cid=%d" |
| 257 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)", |
| 258 | | - |
| 259 | | - "SELECT cid FROM plink JOIN pending ON rid=cid" |
| 260 | | - " WHERE pid=%d" |
| 261 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=cid)", |
| 262 | | - |
| 263 | | - "SELECT pid FROM mlink JOIN pending ON rid=pid" |
| 264 | | - " WHERE fid=%d" |
| 265 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=pid)", |
| 266 | | - |
| 267 | | - "SELECT fid FROM mlink JOIN pending ON rid=fid" |
| 268 | | - " WHERE pid=%d" |
| 269 | | - " AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=fid)", |
| 270 | | - }; |
| 271 | | - |
| 272 | | - rid = db_int(0, "SELECT rid FROM pending"); |
| 273 | | - while( rid && nIgot<200 ){ |
| 274 | | - db_multi_exec("DELETE FROM pending WHERE rid=%d", rid); |
| 275 | | - if( sent<maxSize ){ |
| 276 | | - sent += send_file(rid, pOut); |
| 277 | | - nSent++; |
| 278 | | - }else{ |
| 279 | | - char *zUuid = db_text(0, |
| 280 | | - "SELECT uuid FROM blob WHERE rid=%d AND size>=0", rid); |
| 281 | | - if( zUuid ){ |
| 282 | | - if( pOut ){ |
| 283 | | - blob_appendf(pOut, "igot %s\n", zUuid); |
| 284 | | - }else{ |
| 285 | | - cgi_printf("igot %s\n", zUuid); |
| 286 | | - } |
| 287 | | - free(zUuid); |
| 288 | | - nIgot++; |
| 289 | | - } |
| 290 | | - } |
| 291 | | - xid = 0; |
| 292 | | - for(i=0; xid==0 && i<sizeof(azQuery)/sizeof(azQuery[0]); i++){ |
| 293 | | - xid = db_int(0, azQuery[i], rid); |
| 294 | | - } |
| 295 | | - rid = xid; |
| 296 | | - if( rid==0 ){ |
| 297 | | - rid = db_int(0, "SELECT rid FROM pending"); |
| 298 | | - } |
| 299 | | - } |
| 300 | | - return nSent; |
| 220 | + size = send_as_delta(pXfer, rid, &content, pUuid, srcId); |
| 221 | + } |
| 222 | + if( size==0 ){ |
| 223 | + int size = blob_size(&content); |
| 224 | + blob_appendf(pXfer->pOut, "file %b %d\n", pUuid, size); |
| 225 | + blob_append(pXfer->pOut, blob_buffer(&content), size); |
| 226 | + pXfer->nFile++; |
| 227 | + }else{ |
| 228 | + pXfer->nDelta++; |
| 229 | + } |
| 230 | + db_multi_exec("INSERT INTO sent VALUES(%d)", rid); |
| 231 | + blob_reset(&uuid); |
| 232 | +} |
| 233 | + |
| 234 | +/* |
| 235 | +** This routine runs when either client or server is notified that |
| 236 | +** the other side things rid is a leaf manifest. If we hold |
| 237 | +** children of rid, then send them over to the other side. |
| 238 | +*/ |
| 239 | +static void leaf_response(Xfer *pXfer, int rid){ |
| 240 | + Stmt q1, q2; |
| 241 | + db_prepare(&q1, |
| 242 | + "SELECT cid, uuid FROM plink, blob" |
| 243 | + " WHERE blob.rid=plink.cid" |
| 244 | + " AND plink.pid=%d", |
| 245 | + rid |
| 246 | + ); |
| 247 | + while( db_step(&q1)==SQLITE_ROW ){ |
| 248 | + Blob uuid; |
| 249 | + int cid; |
| 250 | + |
| 251 | + cid = db_column_int(&q1, 0); |
| 252 | + db_ephemeral_blob(&q1, 1, &uuid); |
| 253 | + send_file(pXfer, cid, &uuid, rid); |
| 254 | + db_prepare(&q2, |
| 255 | + "SELECT pid, uuid, fid FROM mlink, blob" |
| 256 | + " WHERE rid=fid AND mid=%d", |
| 257 | + cid |
| 258 | + ); |
| 259 | + while( db_step(&q2)==SQLITE_ROW ){ |
| 260 | + int pid, fid; |
| 261 | + pid = db_column_int(&q2, 0); |
| 262 | + db_ephemeral_blob(&q2, 1, &uuid); |
| 263 | + fid = db_column_int(&q2, 2); |
| 264 | + send_file(pXfer, fid, &uuid, pid); |
| 265 | + } |
| 266 | + db_finalize(&q2); |
| 267 | + if( blob_size(pXfer->pOut)<pXfer->mxSend ){ |
| 268 | + leaf_response(pXfer, cid); |
| 269 | + } |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +/* |
| 274 | +** Sent a leaf message for every leaf. |
| 275 | +*/ |
| 276 | +static void send_leaves(Xfer *pXfer){ |
| 277 | + Stmt q; |
| 278 | + db_prepare(&q, |
| 279 | + "SELECT uuid FROM blob WHERE rid IN" |
| 280 | + " (SELECT cid FROM plink EXCEPT SELECT pid FROM plink)" |
| 281 | + ); |
| 282 | + while( db_step(&q)==SQLITE_ROW ){ |
| 283 | + const char *zUuid = db_column_text(&q, 0); |
| 284 | + blob_appendf(pXfer->pOut, "leaf %s\n", zUuid); |
| 285 | + } |
| 286 | + db_finalize(&q); |
| 287 | +} |
| 288 | + |
| 289 | +/* |
| 290 | +** Sen a gimme message for every phantom. |
| 291 | +*/ |
| 292 | +static void request_phantoms(Xfer *pXfer){ |
| 293 | + Stmt q; |
| 294 | + db_prepare(&q, "SELECT uuid FROM phantom JOIN blob USING(rid)"); |
| 295 | + while( db_step(&q)==SQLITE_ROW ){ |
| 296 | + const char *zUuid = db_column_text(&q, 0); |
| 297 | + blob_appendf(pXfer->pOut, "gimme %s\n", zUuid); |
| 298 | + } |
| 299 | + db_finalize(&q); |
| 301 | 300 | } |
| 302 | 301 | |
| 303 | 302 | |
| 304 | 303 | /* |
| 305 | 304 | ** Check the signature on an application/x-fossil payload received by |
| | @@ -372,116 +371,126 @@ |
| 372 | 371 | void page_xfer(void){ |
| 373 | 372 | int nToken; |
| 374 | 373 | int isPull = 0; |
| 375 | 374 | int isPush = 0; |
| 376 | 375 | int nErr = 0; |
| 377 | | - Blob line, errmsg, aToken[5]; |
| 376 | + Xfer xfer; |
| 377 | + |
| 378 | + memset(&xfer, 0, sizeof(xfer)); |
| 379 | + blobarray_zero(xfer.aToken, count(xfer.aToken)); |
| 380 | + cgi_set_content_type(g.zContentType); |
| 381 | + blob_zero(&xfer.err); |
| 382 | + xfer.pIn = &g.cgiIn; |
| 383 | + xfer.pOut = cgi_output_blob(); |
| 378 | 384 | |
| 379 | 385 | db_begin_transaction(); |
| 380 | | - blobarray_zero(aToken, count(aToken)); |
| 381 | | - cgi_set_content_type(g.zContentType); |
| 382 | | - blob_zero(&errmsg); |
| 383 | 386 | db_multi_exec( |
| 384 | | - "CREATE TEMP TABLE onremote(rid INTEGER PRIMARY KEY);" /* Client has */ |
| 385 | | - "CREATE TEMP TABLE pending(rid INTEGER PRIMARY KEY);" /* Client needs */ |
| 387 | + "CREATE TEMP TABLE sent(rid INTEGER PRIMARY KEY);" |
| 386 | 388 | ); |
| 387 | | - while( blob_line(&g.cgiIn, &line) ){ |
| 388 | | - nToken = blob_tokenize(&line, aToken, count(aToken)); |
| 389 | + while( blob_line(xfer.pIn, &xfer.line) ){ |
| 390 | + xfer.nToken = blob_tokenize(&xfer.line, xfer.aToken, count(xfer.aToken)); |
| 389 | 391 | |
| 390 | 392 | /* file UUID SIZE \n CONTENT |
| 391 | 393 | ** file UUID DELTASRC SIZE \n CONTENT |
| 392 | 394 | ** |
| 393 | 395 | ** Accept a file from the client. |
| 394 | 396 | */ |
| 395 | | - if( blob_eq(&aToken[0], "file") && nToken>=3 && nToken<=4 ){ |
| 397 | + if( blob_eq(&xfer.aToken[0], "file") ){ |
| 396 | 398 | if( !isPush ){ |
| 397 | 399 | cgi_reset_content(); |
| 398 | 400 | @ error not\sauthorized\sto\swrite |
| 399 | 401 | nErr++; |
| 400 | 402 | break; |
| 401 | 403 | } |
| 402 | | - xfer_accept_file(&g.cgiIn, aToken, nToken, &errmsg); |
| 403 | | - if( blob_size(&errmsg) ){ |
| 404 | + xfer_accept_file(&xfer); |
| 405 | + if( blob_size(&xfer.err) ){ |
| 404 | 406 | cgi_reset_content(); |
| 405 | | - @ error %T(blob_str(&errmsg)) |
| 407 | + @ error %T(blob_str(&xfer.err)) |
| 406 | 408 | nErr++; |
| 407 | 409 | break; |
| 408 | 410 | } |
| 409 | 411 | }else |
| 410 | 412 | |
| 411 | 413 | /* gimme UUID |
| 412 | 414 | ** |
| 413 | 415 | ** Client is requesting a file |
| 414 | 416 | */ |
| 415 | | - if( blob_eq(&aToken[0], "gimme") && nToken==2 && blob_is_uuid(&aToken[1]) ){ |
| 417 | + if( blob_eq(&xfer.aToken[0], "gimme") |
| 418 | + && xfer.nToken==2 |
| 419 | + && blob_is_uuid(&xfer.aToken[1]) |
| 420 | + ){ |
| 416 | 421 | if( isPull ){ |
| 417 | | - db_multi_exec( |
| 418 | | - "INSERT OR IGNORE INTO pending(rid) " |
| 419 | | - "SELECT rid FROM blob WHERE uuid=%B AND size>=0", &aToken[1] |
| 420 | | - ); |
| 422 | + int rid = rid_from_uuid(&xfer.aToken[1], 0); |
| 423 | + if( rid ){ |
| 424 | + send_file(&xfer, rid, &xfer.aToken[1], 0); |
| 425 | + } |
| 421 | 426 | } |
| 422 | 427 | }else |
| 423 | 428 | |
| 424 | 429 | /* igot UUID |
| 425 | | - ** leaf UUID |
| 426 | 430 | ** |
| 427 | 431 | ** Client announces that it has a particular file |
| 428 | 432 | */ |
| 429 | | - if( nToken==2 |
| 430 | | - && (blob_eq(&aToken[0], "igot") || blob_eq(&aToken[0],"leaf")) |
| 431 | | - && blob_is_uuid(&aToken[1]) ){ |
| 432 | | - if( isPull || isPush ){ |
| 433 | | - int rid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &aToken[1]); |
| 434 | | - if( rid>0 ){ |
| 435 | | - db_multi_exec( |
| 436 | | - "INSERT OR IGNORE INTO onremote(rid) VALUES(%d)", rid |
| 437 | | - ); |
| 438 | | - if( isPull && blob_eq(&aToken[0], "leaf") ){ |
| 439 | | - db_multi_exec( |
| 440 | | - "INSERT OR IGNORE INTO pending(rid) " |
| 441 | | - "SELECT cid FROM plink WHERE pid=%d", rid |
| 442 | | - ); |
| 443 | | - } |
| 444 | | - }else if( isPush ){ |
| 445 | | - content_put(0, blob_str(&aToken[1])); |
| 446 | | - } |
| 433 | + if( xfer.nToken==2 |
| 434 | + && blob_eq(&xfer.aToken[0], "igot") |
| 435 | + && blob_is_uuid(&xfer.aToken[1]) |
| 436 | + ){ |
| 437 | + if( isPush ){ |
| 438 | + rid_from_uuid(&xfer.aToken[1], 1); |
| 439 | + } |
| 440 | + }else |
| 441 | + |
| 442 | + |
| 443 | + /* leaf UUID |
| 444 | + ** |
| 445 | + ** Client announces that it has a particular manifest |
| 446 | + */ |
| 447 | + if( xfer.nToken==2 |
| 448 | + && blob_eq(&xfer.aToken[0], "leaf") |
| 449 | + && blob_is_uuid(&xfer.aToken[1]) |
| 450 | + ){ |
| 451 | + if( isPull ){ |
| 452 | + int rid = rid_from_uuid(&xfer.aToken[1], 0); |
| 453 | + leaf_response(&xfer, rid); |
| 447 | 454 | } |
| 448 | 455 | }else |
| 449 | 456 | |
| 450 | 457 | /* pull SERVERCODE PROJECTCODE |
| 451 | 458 | ** push SERVERCODE PROJECTCODE |
| 452 | 459 | ** |
| 453 | 460 | ** The client wants either send or receive |
| 454 | 461 | */ |
| 455 | 462 | if( nToken==3 |
| 456 | | - && (blob_eq(&aToken[0], "pull") || blob_eq(&aToken[0], "push")) |
| 457 | | - && blob_is_uuid(&aToken[1]) && blob_is_uuid(&aToken[2]) ){ |
| 463 | + && (blob_eq(&xfer.aToken[0], "pull") || blob_eq(&xfer.aToken[0], "push")) |
| 464 | + && blob_is_uuid(&xfer.aToken[1]) |
| 465 | + && blob_is_uuid(&xfer.aToken[2]) |
| 466 | + ){ |
| 458 | 467 | const char *zSCode; |
| 459 | 468 | const char *zPCode; |
| 460 | 469 | |
| 461 | 470 | zSCode = db_get("server-code", 0); |
| 462 | 471 | if( zSCode==0 ){ |
| 463 | 472 | fossil_panic("missing server code"); |
| 464 | 473 | } |
| 465 | | - if( blob_eq_str(&aToken[1], zSCode, -1) ){ |
| 474 | + if( blob_eq_str(&xfer.aToken[1], zSCode, -1) ){ |
| 466 | 475 | cgi_reset_content(); |
| 467 | 476 | @ error server\sloop |
| 468 | 477 | nErr++; |
| 469 | 478 | break; |
| 470 | 479 | } |
| 471 | 480 | zPCode = db_get("project-code", 0); |
| 472 | 481 | if( zPCode==0 ){ |
| 473 | 482 | fossil_panic("missing project code"); |
| 474 | 483 | } |
| 475 | | - if( !blob_eq_str(&aToken[2], zPCode, -1) ){ |
| 484 | + if( !blob_eq_str(&xfer.aToken[2], zPCode, -1) ){ |
| 476 | 485 | cgi_reset_content(); |
| 477 | 486 | @ error wrong\sproject |
| 478 | 487 | nErr++; |
| 479 | 488 | break; |
| 480 | 489 | } |
| 481 | 490 | login_check_credentials(); |
| 482 | | - if( blob_eq(&aToken[0], "pull") ){ |
| 491 | + if( blob_eq(&xfer.aToken[0], "pull") ){ |
| 483 | 492 | if( !g.okRead ){ |
| 484 | 493 | cgi_reset_content(); |
| 485 | 494 | @ error not\sauthorized\sto\sread |
| 486 | 495 | nErr++; |
| 487 | 496 | break; |
| | @@ -492,91 +501,57 @@ |
| 492 | 501 | cgi_reset_content(); |
| 493 | 502 | @ error not\sauthorized\sto\swrite |
| 494 | 503 | nErr++; |
| 495 | 504 | break; |
| 496 | 505 | } |
| 506 | + send_leaves(&xfer); |
| 497 | 507 | isPush = 1; |
| 498 | | - |
| 499 | 508 | } |
| 500 | 509 | }else |
| 501 | 510 | |
| 502 | 511 | /* clone |
| 503 | 512 | ** |
| 504 | 513 | ** The client knows nothing. Tell all. |
| 505 | 514 | */ |
| 506 | | - if( blob_eq(&aToken[0], "clone") ){ |
| 515 | + if( blob_eq(&xfer.aToken[0], "clone") ){ |
| 507 | 516 | login_check_credentials(); |
| 508 | 517 | if( !g.okRead || !g.okHistory ){ |
| 509 | 518 | cgi_reset_content(); |
| 510 | 519 | @ error not\sauthorized\sto\sclone |
| 511 | 520 | nErr++; |
| 512 | 521 | break; |
| 513 | 522 | } |
| 514 | 523 | isPull = 1; |
| 515 | 524 | @ push %s(db_get("server-code", "x")) %s(db_get("project-code", "x")) |
| 516 | | - db_multi_exec( |
| 517 | | - "INSERT OR IGNORE INTO pending(rid) " |
| 518 | | - "SELECT mid FROM mlink JOIN blob ON mid=rid" |
| 519 | | - ); |
| 525 | + send_leaves(&xfer); |
| 520 | 526 | }else |
| 521 | 527 | |
| 522 | 528 | /* login USER NONCE SIGNATURE |
| 523 | 529 | ** |
| 524 | 530 | ** Check for a valid login. This has to happen before anything else. |
| 525 | 531 | */ |
| 526 | | - if( blob_eq(&aToken[0], "login") && nToken==4 ){ |
| 532 | + if( blob_eq(&xfer.aToken[0], "login") |
| 533 | + && nToken==4 |
| 534 | + ){ |
| 527 | 535 | if( disableLogin ){ |
| 528 | 536 | g.okRead = g.okWrite = 1; |
| 529 | 537 | }else{ |
| 530 | | - check_login(&aToken[1], &aToken[2], &aToken[3]); |
| 538 | + check_login(&xfer.aToken[1], &xfer.aToken[2], &xfer.aToken[3]); |
| 531 | 539 | } |
| 532 | 540 | }else |
| 533 | 541 | |
| 534 | 542 | /* Unknown message |
| 535 | 543 | */ |
| 536 | 544 | { |
| 537 | 545 | cgi_reset_content(); |
| 538 | | - @ error bad\scommand:\s%F(blob_str(&line)) |
| 546 | + @ error bad\scommand:\s%F(blob_str(&xfer.line)) |
| 539 | 547 | } |
| 540 | | - blobarray_reset(aToken, nToken); |
| 548 | + blobarray_reset(xfer.aToken, xfer.nToken); |
| 541 | 549 | } |
| 542 | | - |
| 543 | | - /* The input message has now been processed. Generate a reply. */ |
| 544 | 550 | if( isPush ){ |
| 545 | | - Stmt q; |
| 546 | | - int nReq = 0; |
| 547 | | - db_prepare(&q, "SELECT uuid, rid FROM phantom JOIN blob USING (rid)"); |
| 548 | | - while( db_step(&q)==SQLITE_ROW && nReq++ < 200 ){ |
| 549 | | - const char *zUuid = db_column_text(&q, 0); |
| 550 | | - int rid = db_column_int(&q, 1); |
| 551 | | - int xid = similar_record(rid, 0); |
| 552 | | - @ gimme %s(zUuid) |
| 553 | | - if( xid ){ |
| 554 | | - char *zXUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", xid); |
| 555 | | - @ igot %s(zXUuid); |
| 556 | | - free(zXUuid); |
| 557 | | - } |
| 558 | | - } |
| 559 | | - db_finalize(&q); |
| 560 | | - } |
| 561 | | - if( isPull ){ |
| 562 | | - send_all_pending(0); |
| 563 | | - } |
| 564 | | - if( isPush || isPull ){ |
| 565 | | - /* Always send our leaves */ |
| 566 | | - Stmt q; |
| 567 | | - db_prepare(&q, |
| 568 | | - "SELECT uuid FROM blob WHERE rid IN" |
| 569 | | - " (SELECT cid FROM plink EXCEPT SELECT pid FROM plink)" |
| 570 | | - ); |
| 571 | | - while( db_step(&q)==SQLITE_ROW ){ |
| 572 | | - const char *zUuid = db_column_text(&q, 0); |
| 573 | | - @ leaf %s(zUuid) |
| 574 | | - } |
| 575 | | - db_finalize(&q); |
| 576 | | - } |
| 577 | | - |
| 551 | + request_phantoms(&xfer); |
| 552 | + } |
| 578 | 553 | db_end_transaction(0); |
| 579 | 554 | } |
| 580 | 555 | |
| 581 | 556 | /* |
| 582 | 557 | ** COMMAND: test-xfer |
| | @@ -620,43 +595,40 @@ |
| 620 | 595 | ** are pulled if pullFlag is true. A full sync occurs if both are |
| 621 | 596 | ** true. |
| 622 | 597 | */ |
| 623 | 598 | void client_sync(int pushFlag, int pullFlag, int cloneFlag){ |
| 624 | 599 | int go = 1; /* Loop until zero */ |
| 625 | | - int nToken; |
| 626 | 600 | const char *zSCode = db_get("server-code", "x"); |
| 627 | 601 | const char *zPCode = db_get("project-code", 0); |
| 628 | | - int nFile = 0; |
| 629 | 602 | int nMsg = 0; |
| 630 | 603 | int nReq = 0; |
| 631 | | - int nFileSend; |
| 604 | + int nFileSend = 0; |
| 632 | 605 | int nNoFileCycle = 0; |
| 633 | 606 | Blob send; /* Text we are sending to the server */ |
| 634 | 607 | Blob recv; /* Reply we got back from the server */ |
| 635 | | - Blob line; /* A single line of the reply */ |
| 636 | | - Blob aToken[5]; /* A tokenization of line */ |
| 637 | | - Blob errmsg; /* Error message */ |
| 608 | + Xfer xfer; /* Transfer data */ |
| 609 | + |
| 610 | + memset(&xfer, 0, sizeof(xfer)); |
| 611 | + xfer.pIn = &recv; |
| 612 | + xfer.pOut = &send; |
| 638 | 613 | |
| 639 | 614 | assert( pushFlag || pullFlag || cloneFlag ); |
| 640 | 615 | assert( !g.urlIsFile ); /* This only works for networking */ |
| 641 | 616 | |
| 642 | 617 | db_begin_transaction(); |
| 643 | 618 | db_multi_exec( |
| 644 | | - /* Records which we know the other side also has */ |
| 645 | | - "CREATE TEMP TABLE onremote(rid INTEGER PRIMARY KEY);" |
| 646 | | - /* Records we know the other side needs */ |
| 647 | | - "CREATE TEMP TABLE pending(rid INTEGER PRIMARY KEY);" |
| 619 | + "CREATE TEMP TABLE sent(rid INTEGER PRIMARY KEY);" |
| 648 | 620 | ); |
| 649 | | - blobarray_zero(aToken, count(aToken)); |
| 621 | + blobarray_zero(xfer.aToken, count(xfer.aToken)); |
| 650 | 622 | blob_zero(&send); |
| 651 | 623 | blob_zero(&recv); |
| 652 | | - blob_zero(&errmsg); |
| 624 | + blob_zero(&xfer.err); |
| 653 | 625 | |
| 654 | 626 | |
| 655 | 627 | while( go ){ |
| 656 | 628 | go = 0; |
| 657 | | - nFile = nReq = nMsg = 0; |
| 629 | + nReq = nMsg = 0; |
| 658 | 630 | |
| 659 | 631 | /* Generate a request to be sent to the server. |
| 660 | 632 | ** Always begin with a clone, pull, or push message |
| 661 | 633 | */ |
| 662 | 634 | |
| | @@ -665,143 +637,102 @@ |
| 665 | 637 | pushFlag = 0; |
| 666 | 638 | pullFlag = 0; |
| 667 | 639 | nMsg++; |
| 668 | 640 | }else if( pullFlag ){ |
| 669 | 641 | blob_appendf(&send, "pull %s %s\n", zSCode, zPCode); |
| 642 | + send_leaves(&xfer); |
| 643 | + request_phantoms(&xfer); |
| 670 | 644 | nMsg++; |
| 671 | 645 | } |
| 672 | 646 | if( pushFlag ){ |
| 673 | 647 | blob_appendf(&send, "push %s %s\n", zSCode, zPCode); |
| 674 | 648 | nMsg++; |
| 675 | 649 | } |
| 676 | 650 | |
| 677 | | - if( pullFlag ){ |
| 678 | | - /* Send gimme message for every phantom that we hold. |
| 679 | | - */ |
| 680 | | - Stmt q; |
| 681 | | - db_prepare(&q, "SELECT uuid, rid FROM phantom JOIN blob USING (rid)"); |
| 682 | | - while( db_step(&q)==SQLITE_ROW && nReq<200 ){ |
| 683 | | - const char *zUuid = db_column_text(&q, 0); |
| 684 | | - int rid = db_column_int(&q, 1); |
| 685 | | - int xid = similar_record(rid, 0); |
| 686 | | - blob_appendf(&send,"gimme %s\n", zUuid); |
| 687 | | - nReq++; |
| 688 | | - if( xid ){ |
| 689 | | - blob_appendf(&send, "igot %z\n", |
| 690 | | - db_text(0, "SELECT uuid FROM blob WHERE rid=%d", xid)); |
| 691 | | - } |
| 692 | | - } |
| 693 | | - db_finalize(&q); |
| 694 | | - } |
| 695 | | - |
| 696 | | - if( pushFlag ){ |
| 697 | | - /* Send the server any files that the server has requested */ |
| 698 | | - nFile += send_all_pending(&send); |
| 699 | | - } |
| 700 | | - |
| 701 | | - if( pullFlag || pushFlag ){ |
| 702 | | - /* Always send our leaves */ |
| 703 | | - Stmt q; |
| 704 | | - db_prepare(&q, |
| 705 | | - "SELECT uuid FROM blob WHERE rid IN" |
| 706 | | - " (SELECT cid FROM plink EXCEPT SELECT pid FROM plink)" |
| 707 | | - ); |
| 708 | | - while( db_step(&q)==SQLITE_ROW ){ |
| 709 | | - const char *zUuid = db_column_text(&q, 0); |
| 710 | | - blob_appendf(&send, "leaf %s\n", zUuid); |
| 711 | | - nMsg++; |
| 712 | | - } |
| 713 | | - db_finalize(&q); |
| 714 | | - } |
| 715 | | - |
| 716 | 651 | /* Exchange messages with the server */ |
| 652 | + nFileSend = xfer.nFile + xfer.nDelta + xfer.nDanglingFile; |
| 717 | 653 | printf("Send: %3d files, %3d requests, %3d other msgs, %8d bytes\n", |
| 718 | | - nFile, nReq, nMsg, blob_size(&send)); |
| 719 | | - nFileSend = nFile; |
| 720 | | - nFile = nReq = nMsg = 0; |
| 654 | + nFileSend, nReq, nMsg, blob_size(&send)); |
| 655 | + xfer.nFile = 0; |
| 656 | + xfer.nDelta = 0; |
| 657 | + xfer.nDanglingFile = 0; |
| 658 | + nReq = nMsg = 0; |
| 721 | 659 | http_exchange(&send, &recv); |
| 722 | 660 | blob_reset(&send); |
| 723 | 661 | |
| 724 | 662 | /* Process the reply that came back from the server */ |
| 725 | | - while( blob_line(&recv, &line) ){ |
| 726 | | - nToken = blob_tokenize(&line, aToken, count(aToken)); |
| 663 | + while( blob_line(&recv, &xfer.line) ){ |
| 664 | + xfer.nToken = blob_tokenize(&xfer.line, xfer.aToken, count(xfer.aToken)); |
| 727 | 665 | |
| 728 | 666 | /* file UUID SIZE \n CONTENT |
| 729 | 667 | ** file UUID DELTASRC SIZE \n CONTENT |
| 730 | 668 | ** |
| 731 | 669 | ** Receive a file transmitted from the other side |
| 732 | 670 | */ |
| 733 | | - if( blob_eq(&aToken[0],"file") ){ |
| 734 | | - xfer_accept_file(&recv, aToken, nToken, &errmsg); |
| 735 | | - nFile++; |
| 736 | | - go = 1; |
| 671 | + if( blob_eq(&xfer.aToken[0],"file") ){ |
| 672 | + xfer_accept_file(&xfer); |
| 737 | 673 | }else |
| 738 | 674 | |
| 739 | 675 | /* gimme UUID |
| 740 | 676 | ** |
| 741 | 677 | ** Server is requesting a file |
| 742 | 678 | */ |
| 743 | | - if( blob_eq(&aToken[0], "gimme") && nToken==2 |
| 744 | | - && blob_is_uuid(&aToken[1]) ){ |
| 745 | | - nReq++; |
| 679 | + if( blob_eq(&xfer.aToken[0], "gimme") |
| 680 | + && xfer.nToken==2 |
| 681 | + && blob_is_uuid(&xfer.aToken[1]) |
| 682 | + ){ |
| 746 | 683 | if( pushFlag ){ |
| 747 | | - db_multi_exec( |
| 748 | | - "INSERT OR IGNORE INTO pending(rid) " |
| 749 | | - "SELECT rid FROM blob WHERE uuid=%B AND size>=0", &aToken[1] |
| 750 | | - ); |
| 751 | | - go = 1; |
| 684 | + int rid = rid_from_uuid(&xfer.aToken[1], 0); |
| 685 | + send_file(&xfer, rid, &xfer.aToken[1], 0); |
| 752 | 686 | } |
| 753 | 687 | }else |
| 754 | 688 | |
| 755 | 689 | /* igot UUID |
| 756 | | - ** leaf UUID |
| 690 | + ** |
| 691 | + ** Server announces that it has a particular file |
| 692 | + */ |
| 693 | + if( xfer.nToken==2 |
| 694 | + && blob_eq(&xfer.aToken[0], "igot") |
| 695 | + && blob_is_uuid(&xfer.aToken[1]) |
| 696 | + ){ |
| 697 | + if( pullFlag ){ |
| 698 | + rid_from_uuid(&xfer.aToken[1], 1); |
| 699 | + } |
| 700 | + }else |
| 701 | + |
| 702 | + |
| 703 | + /* leaf UUID |
| 757 | 704 | ** |
| 758 | | - ** Server proclaims that it has a particular file. A leaf message |
| 759 | | - ** means that the file is a leaf manifest on the server. |
| 705 | + ** Server announces that it has a particular manifest |
| 760 | 706 | */ |
| 761 | | - if( nToken==2 |
| 762 | | - && (blob_eq(&aToken[0], "igot") || blob_eq(&aToken[0], "leaf")) |
| 763 | | - && blob_is_uuid(&aToken[1]) ){ |
| 764 | | - int rid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &aToken[1]); |
| 765 | | - nMsg++; |
| 766 | | - if( rid>0 ){ |
| 767 | | - db_multi_exec( |
| 768 | | - "INSERT OR IGNORE INTO onremote(rid) VALUES(%d)", rid |
| 769 | | - ); |
| 770 | | - /* Add to the pending set all children of the server's leaves */ |
| 771 | | - if( pushFlag && blob_eq(&aToken[0], "leaf") ){ |
| 772 | | - db_multi_exec( |
| 773 | | - "INSERT OR IGNORE INTO pending(rid) " |
| 774 | | - "SELECT cid FROM plink WHERE pid=%d", rid |
| 775 | | - ); |
| 776 | | - if( db_changes()>0 ){ |
| 777 | | - go = 1; |
| 778 | | - } |
| 779 | | - } |
| 780 | | - if( pullFlag && !go && |
| 781 | | - db_exists("SELECT 1 FROM phantom WHERE rid=%d", rid) ){ |
| 782 | | - go = 1; |
| 783 | | - } |
| 784 | | - }else if( pullFlag ){ |
| 785 | | - go = 1; |
| 786 | | - content_put(0, blob_str(&aToken[1])); |
| 707 | + if( xfer.nToken==2 |
| 708 | + && blob_eq(&xfer.aToken[0], "leaf") |
| 709 | + && blob_is_uuid(&xfer.aToken[1]) |
| 710 | + ){ |
| 711 | + if( pushFlag ){ |
| 712 | + int rid = rid_from_uuid(&xfer.aToken[1], 0); |
| 713 | + leaf_response(&xfer, rid); |
| 787 | 714 | } |
| 788 | 715 | }else |
| 716 | + |
| 789 | 717 | |
| 790 | 718 | /* push SERVERCODE PRODUCTCODE |
| 791 | 719 | ** |
| 792 | 720 | ** Should only happen in response to a clone. |
| 793 | 721 | */ |
| 794 | | - if( blob_eq(&aToken[0],"push") && nToken==3 && cloneFlag |
| 795 | | - && blob_is_uuid(&aToken[1]) && blob_is_uuid(&aToken[2]) ){ |
| 796 | | - |
| 797 | | - if( blob_eq_str(&aToken[1], zSCode, -1) ){ |
| 722 | + if( blob_eq(&xfer.aToken[0],"push") |
| 723 | + && xfer.nToken==3 |
| 724 | + && cloneFlag |
| 725 | + && blob_is_uuid(&xfer.aToken[1]) |
| 726 | + && blob_is_uuid(&xfer.aToken[2]) |
| 727 | + ){ |
| 728 | + if( blob_eq_str(&xfer.aToken[1], zSCode, -1) ){ |
| 798 | 729 | fossil_fatal("server loop"); |
| 799 | 730 | } |
| 800 | 731 | nMsg++; |
| 801 | 732 | if( zPCode==0 ){ |
| 802 | | - zPCode = mprintf("%b", &aToken[2]); |
| 733 | + zPCode = mprintf("%b", &xfer.aToken[2]); |
| 803 | 734 | db_set("project-code", zPCode); |
| 804 | 735 | } |
| 805 | 736 | cloneFlag = 0; |
| 806 | 737 | pullFlag = 1; |
| 807 | 738 | }else |
| | @@ -808,41 +739,41 @@ |
| 808 | 739 | |
| 809 | 740 | /* error MESSAGE |
| 810 | 741 | ** |
| 811 | 742 | ** Report an error |
| 812 | 743 | */ |
| 813 | | - if( blob_eq(&aToken[0],"error") && nToken==2 ){ |
| 814 | | - char *zMsg = blob_terminate(&aToken[1]); |
| 744 | + if( blob_eq(&xfer.aToken[0],"error") && xfer.nToken==2 ){ |
| 745 | + char *zMsg = blob_terminate(&xfer.aToken[1]); |
| 815 | 746 | defossilize(zMsg); |
| 816 | | - blob_appendf(&errmsg, "server says: %s", zMsg); |
| 747 | + blob_appendf(&xfer.err, "server says: %s", zMsg); |
| 817 | 748 | }else |
| 818 | 749 | |
| 819 | 750 | /* Unknown message */ |
| 820 | 751 | { |
| 821 | | - blob_appendf(&errmsg, "unknown command: %b", &aToken[0]); |
| 752 | + blob_appendf(&xfer.err, "unknown command: %b", &xfer.aToken[0]); |
| 822 | 753 | } |
| 823 | 754 | |
| 824 | | - if( blob_size(&errmsg) ){ |
| 825 | | - fossil_fatal("%b", &errmsg); |
| 755 | + if( blob_size(&xfer.err) ){ |
| 756 | + fossil_fatal("%b", &xfer.err); |
| 826 | 757 | } |
| 827 | | - blobarray_reset(aToken, nToken); |
| 758 | + blobarray_reset(xfer.aToken, xfer.nToken); |
| 828 | 759 | } |
| 829 | 760 | printf("Received: %3d files, %3d requests, %3d other msgs, %8d bytes\n", |
| 830 | | - nFile, nReq, nMsg, blob_size(&recv)); |
| 761 | + xfer.nFile + xfer.nDelta + xfer.nDanglingFile, |
| 762 | + nReq, nMsg, blob_size(&recv)); |
| 831 | 763 | blob_reset(&recv); |
| 832 | | - if( nFileSend + nFile==0 ){ |
| 764 | + if( nFileSend + xfer.nFile + xfer.nDelta + xfer.nDanglingFile==0 ){ |
| 833 | 765 | nNoFileCycle++; |
| 834 | 766 | if( nNoFileCycle>1 ){ |
| 835 | 767 | go = 0; |
| 836 | 768 | } |
| 837 | 769 | }else{ |
| 838 | 770 | nNoFileCycle = 0; |
| 839 | 771 | } |
| 840 | | - nFile = nReq = nMsg = 0; |
| 772 | + nReq = nMsg = 0; |
| 773 | + xfer.nFile = 0; |
| 774 | + xfer.nDelta = 0; |
| 775 | + xfer.nDanglingFile = 0; |
| 841 | 776 | }; |
| 842 | 777 | http_close(); |
| 843 | 778 | db_end_transaction(0); |
| 844 | | - db_multi_exec( |
| 845 | | - "DROP TABLE onremote;" |
| 846 | | - "DROP TABLE pending;" |
| 847 | | - ); |
| 848 | 779 | } |
| 849 | 780 | |