]>
Commit | Line | Data |
---|---|---|
58e60dd2 NH |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "pack.h" | |
4 | #include "fetch.h" | |
5 | #include "tag.h" | |
6 | #include "blob.h" | |
7 | ||
8 | #include <curl/curl.h> | |
9 | #include <curl/easy.h> | |
10 | #include "expat.h" | |
11 | ||
12 | static const char http_push_usage[] = | |
13 | "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n"; | |
14 | ||
15 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
16 | #define USE_CURL_MULTI | |
17 | #define DEFAULT_MAX_REQUESTS 5 | |
18 | #endif | |
19 | ||
20 | #if LIBCURL_VERSION_NUM < 0x070704 | |
21 | #define curl_global_cleanup() do { /* nothing */ } while(0) | |
22 | #endif | |
23 | #if LIBCURL_VERSION_NUM < 0x070800 | |
24 | #define curl_global_init(a) do { /* nothing */ } while(0) | |
25 | #endif | |
26 | ||
27 | #if LIBCURL_VERSION_NUM < 0x070c04 | |
28 | #define NO_CURL_EASY_DUPHANDLE | |
29 | #endif | |
30 | ||
31 | #define RANGE_HEADER_SIZE 30 | |
32 | ||
33 | /* DAV method names and request body templates */ | |
34 | #define DAV_LOCK "LOCK" | |
35 | #define DAV_MKCOL "MKCOL" | |
36 | #define DAV_MOVE "MOVE" | |
37 | #define DAV_PROPFIND "PROPFIND" | |
38 | #define DAV_PUT "PUT" | |
39 | #define DAV_UNLOCK "UNLOCK" | |
40 | #define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>" | |
41 | #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>" | |
42 | ||
43 | static int active_requests = 0; | |
44 | static int data_received; | |
45 | static int pushing = 0; | |
46 | static int aborted = 0; | |
47 | ||
48 | #ifdef USE_CURL_MULTI | |
49 | static int max_requests = -1; | |
50 | static CURLM *curlm; | |
51 | #endif | |
52 | #ifndef NO_CURL_EASY_DUPHANDLE | |
53 | static CURL *curl_default; | |
54 | #endif | |
55 | static struct curl_slist *no_pragma_header; | |
56 | static struct curl_slist *default_headers; | |
57 | static char curl_errorstr[CURL_ERROR_SIZE]; | |
58 | static char *lock_token = NULL; | |
59 | ||
60 | static int push_verbosely = 0; | |
61 | static int push_all = 0; | |
62 | static int force_all = 0; | |
63 | ||
64 | struct buffer | |
65 | { | |
66 | size_t posn; | |
67 | size_t size; | |
68 | void *buffer; | |
69 | }; | |
70 | ||
71 | struct repo | |
72 | { | |
73 | char *url; | |
74 | struct packed_git *packs; | |
75 | }; | |
76 | ||
77 | static struct repo *remote = NULL; | |
78 | ||
79 | enum transfer_state { | |
80 | NEED_CHECK, | |
81 | RUN_HEAD, | |
82 | NEED_PUSH, | |
83 | RUN_MKCOL, | |
84 | RUN_PUT, | |
85 | RUN_MOVE, | |
86 | ABORTED, | |
87 | COMPLETE, | |
88 | }; | |
89 | ||
90 | struct transfer_request | |
91 | { | |
92 | unsigned char sha1[20]; | |
93 | char *url; | |
94 | char *dest; | |
95 | char *lock_token; | |
96 | struct curl_slist *headers; | |
97 | struct buffer buffer; | |
98 | char filename[PATH_MAX]; | |
99 | char tmpfile[PATH_MAX]; | |
100 | enum transfer_state state; | |
101 | CURLcode curl_result; | |
102 | char errorstr[CURL_ERROR_SIZE]; | |
103 | long http_code; | |
104 | unsigned char real_sha1[20]; | |
105 | SHA_CTX c; | |
106 | z_stream stream; | |
107 | int zret; | |
108 | int rename; | |
109 | struct active_request_slot *slot; | |
110 | struct transfer_request *next; | |
111 | }; | |
112 | ||
113 | struct active_request_slot | |
114 | { | |
115 | CURL *curl; | |
116 | FILE *local; | |
117 | int in_use; | |
118 | int done; | |
119 | CURLcode curl_result; | |
120 | long http_code; | |
121 | struct active_request_slot *next; | |
122 | }; | |
123 | ||
124 | static struct transfer_request *request_queue_head = NULL; | |
125 | static struct active_request_slot *active_queue_head = NULL; | |
126 | ||
127 | static int curl_ssl_verify = -1; | |
128 | static char *ssl_cert = NULL; | |
129 | #if LIBCURL_VERSION_NUM >= 0x070902 | |
130 | static char *ssl_key = NULL; | |
131 | #endif | |
132 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
133 | static char *ssl_capath = NULL; | |
134 | #endif | |
135 | static char *ssl_cainfo = NULL; | |
136 | static long curl_low_speed_limit = -1; | |
137 | static long curl_low_speed_time = -1; | |
138 | ||
139 | struct lockprop | |
140 | { | |
141 | int supported_lock; | |
142 | int lock_entry; | |
143 | int lock_scope; | |
144 | int lock_type; | |
145 | int lock_exclusive; | |
146 | int lock_exclusive_write; | |
147 | }; | |
148 | ||
149 | static int http_options(const char *var, const char *value) | |
150 | { | |
151 | if (!strcmp("http.sslverify", var)) { | |
152 | if (curl_ssl_verify == -1) { | |
153 | curl_ssl_verify = git_config_bool(var, value); | |
154 | } | |
155 | return 0; | |
156 | } | |
157 | ||
158 | if (!strcmp("http.sslcert", var)) { | |
159 | if (ssl_cert == NULL) { | |
160 | ssl_cert = xmalloc(strlen(value)+1); | |
161 | strcpy(ssl_cert, value); | |
162 | } | |
163 | return 0; | |
164 | } | |
165 | #if LIBCURL_VERSION_NUM >= 0x070902 | |
166 | if (!strcmp("http.sslkey", var)) { | |
167 | if (ssl_key == NULL) { | |
168 | ssl_key = xmalloc(strlen(value)+1); | |
169 | strcpy(ssl_key, value); | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | #endif | |
174 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
175 | if (!strcmp("http.sslcapath", var)) { | |
176 | if (ssl_capath == NULL) { | |
177 | ssl_capath = xmalloc(strlen(value)+1); | |
178 | strcpy(ssl_capath, value); | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | #endif | |
183 | if (!strcmp("http.sslcainfo", var)) { | |
184 | if (ssl_cainfo == NULL) { | |
185 | ssl_cainfo = xmalloc(strlen(value)+1); | |
186 | strcpy(ssl_cainfo, value); | |
187 | } | |
188 | return 0; | |
189 | } | |
190 | ||
191 | #ifdef USE_CURL_MULTI | |
192 | if (!strcmp("http.maxrequests", var)) { | |
193 | if (max_requests == -1) | |
194 | max_requests = git_config_int(var, value); | |
195 | return 0; | |
196 | } | |
197 | #endif | |
198 | ||
199 | if (!strcmp("http.lowspeedlimit", var)) { | |
200 | if (curl_low_speed_limit == -1) | |
201 | curl_low_speed_limit = (long)git_config_int(var, value); | |
202 | return 0; | |
203 | } | |
204 | if (!strcmp("http.lowspeedtime", var)) { | |
205 | if (curl_low_speed_time == -1) | |
206 | curl_low_speed_time = (long)git_config_int(var, value); | |
207 | return 0; | |
208 | } | |
209 | ||
210 | /* Fall back on the default ones */ | |
211 | return git_default_config(var, value); | |
212 | } | |
213 | ||
214 | static size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, | |
215 | struct buffer *buffer) | |
216 | { | |
217 | size_t size = eltsize * nmemb; | |
218 | if (size > buffer->size - buffer->posn) | |
219 | size = buffer->size - buffer->posn; | |
220 | memcpy(ptr, buffer->buffer + buffer->posn, size); | |
221 | buffer->posn += size; | |
222 | return size; | |
223 | } | |
224 | ||
225 | static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize, | |
226 | size_t nmemb, struct buffer *buffer) | |
227 | { | |
228 | size_t size = eltsize * nmemb; | |
229 | if (size > buffer->size - buffer->posn) { | |
230 | buffer->size = buffer->size * 3 / 2; | |
231 | if (buffer->size < buffer->posn + size) | |
232 | buffer->size = buffer->posn + size; | |
233 | buffer->buffer = xrealloc(buffer->buffer, buffer->size); | |
234 | } | |
235 | memcpy(buffer->buffer + buffer->posn, ptr, size); | |
236 | buffer->posn += size; | |
237 | data_received++; | |
238 | return size; | |
239 | } | |
240 | ||
241 | static size_t fwrite_null(const void *ptr, size_t eltsize, | |
242 | size_t nmemb, struct buffer *buffer) | |
243 | { | |
244 | data_received++; | |
245 | return eltsize * nmemb; | |
246 | } | |
247 | ||
248 | #ifdef USE_CURL_MULTI | |
249 | static void process_curl_messages(void); | |
250 | static void process_request_queue(void); | |
251 | #endif | |
252 | ||
253 | static CURL* get_curl_handle(void) | |
254 | { | |
255 | CURL* result = curl_easy_init(); | |
256 | ||
257 | curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify); | |
258 | #if LIBCURL_VERSION_NUM >= 0x070907 | |
259 | curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); | |
260 | #endif | |
261 | ||
262 | if (ssl_cert != NULL) | |
263 | curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert); | |
264 | #if LIBCURL_VERSION_NUM >= 0x070902 | |
265 | if (ssl_key != NULL) | |
266 | curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key); | |
267 | #endif | |
268 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
269 | if (ssl_capath != NULL) | |
270 | curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath); | |
271 | #endif | |
272 | if (ssl_cainfo != NULL) | |
273 | curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo); | |
274 | curl_easy_setopt(result, CURLOPT_FAILONERROR, 1); | |
275 | ||
276 | if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) { | |
277 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT, | |
278 | curl_low_speed_limit); | |
279 | curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME, | |
280 | curl_low_speed_time); | |
281 | } | |
282 | ||
283 | return result; | |
284 | } | |
285 | ||
286 | static struct active_request_slot *get_active_slot(void) | |
287 | { | |
288 | struct active_request_slot *slot = active_queue_head; | |
289 | struct active_request_slot *newslot; | |
290 | ||
291 | #ifdef USE_CURL_MULTI | |
292 | int num_transfers; | |
293 | ||
294 | /* Wait for a slot to open up if the queue is full */ | |
295 | while (active_requests >= max_requests) { | |
296 | curl_multi_perform(curlm, &num_transfers); | |
297 | if (num_transfers < active_requests) { | |
298 | process_curl_messages(); | |
299 | } | |
300 | } | |
301 | #endif | |
302 | ||
303 | while (slot != NULL && slot->in_use) { | |
304 | slot = slot->next; | |
305 | } | |
306 | if (slot == NULL) { | |
307 | newslot = xmalloc(sizeof(*newslot)); | |
308 | newslot->curl = NULL; | |
309 | newslot->in_use = 0; | |
310 | newslot->next = NULL; | |
311 | ||
312 | slot = active_queue_head; | |
313 | if (slot == NULL) { | |
314 | active_queue_head = newslot; | |
315 | } else { | |
316 | while (slot->next != NULL) { | |
317 | slot = slot->next; | |
318 | } | |
319 | slot->next = newslot; | |
320 | } | |
321 | slot = newslot; | |
322 | } | |
323 | ||
324 | if (slot->curl == NULL) { | |
325 | #ifdef NO_CURL_EASY_DUPHANDLE | |
326 | slot->curl = get_curl_handle(); | |
327 | #else | |
328 | slot->curl = curl_easy_duphandle(curl_default); | |
329 | #endif | |
330 | } | |
331 | ||
332 | active_requests++; | |
333 | slot->in_use = 1; | |
334 | slot->done = 0; | |
335 | slot->local = NULL; | |
336 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, default_headers); | |
337 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); | |
338 | ||
339 | return slot; | |
340 | } | |
341 | ||
342 | static int start_active_slot(struct active_request_slot *slot) | |
343 | { | |
344 | #ifdef USE_CURL_MULTI | |
345 | CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); | |
346 | ||
347 | if (curlm_result != CURLM_OK && | |
348 | curlm_result != CURLM_CALL_MULTI_PERFORM) { | |
349 | active_requests--; | |
350 | slot->in_use = 0; | |
351 | return 0; | |
352 | } | |
353 | #endif | |
354 | return 1; | |
355 | } | |
356 | ||
357 | static void run_active_slot(struct active_request_slot *slot) | |
358 | { | |
359 | #ifdef USE_CURL_MULTI | |
360 | int num_transfers; | |
361 | long last_pos = 0; | |
362 | long current_pos; | |
363 | fd_set readfds; | |
364 | fd_set writefds; | |
365 | fd_set excfds; | |
366 | int max_fd; | |
367 | struct timeval select_timeout; | |
368 | CURLMcode curlm_result; | |
369 | ||
370 | while (!slot->done) { | |
371 | data_received = 0; | |
372 | do { | |
373 | curlm_result = curl_multi_perform(curlm, | |
374 | &num_transfers); | |
375 | } while (curlm_result == CURLM_CALL_MULTI_PERFORM); | |
376 | if (num_transfers < active_requests) { | |
377 | process_curl_messages(); | |
378 | process_request_queue(); | |
379 | } | |
380 | ||
381 | if (!data_received && slot->local != NULL) { | |
382 | current_pos = ftell(slot->local); | |
383 | if (current_pos > last_pos) | |
384 | data_received++; | |
385 | last_pos = current_pos; | |
386 | } | |
387 | ||
388 | if (!slot->done && !data_received) { | |
389 | max_fd = 0; | |
390 | FD_ZERO(&readfds); | |
391 | FD_ZERO(&writefds); | |
392 | FD_ZERO(&excfds); | |
393 | select_timeout.tv_sec = 0; | |
394 | select_timeout.tv_usec = 50000; | |
395 | select(max_fd, &readfds, &writefds, | |
396 | &excfds, &select_timeout); | |
397 | } | |
398 | } | |
399 | #else | |
400 | slot->curl_result = curl_easy_perform(slot->curl); | |
401 | active_requests--; | |
402 | #endif | |
403 | } | |
404 | ||
405 | static void start_check(struct transfer_request *request) | |
406 | { | |
407 | char *hex = sha1_to_hex(request->sha1); | |
408 | struct active_request_slot *slot; | |
409 | char *posn; | |
410 | ||
411 | request->url = xmalloc(strlen(remote->url) + 55); | |
412 | strcpy(request->url, remote->url); | |
413 | posn = request->url + strlen(remote->url); | |
414 | strcpy(posn, "objects/"); | |
415 | posn += 8; | |
416 | memcpy(posn, hex, 2); | |
417 | posn += 2; | |
418 | *(posn++) = '/'; | |
419 | strcpy(posn, hex + 2); | |
420 | ||
421 | slot = get_active_slot(); | |
422 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr); | |
423 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
424 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); | |
425 | ||
426 | if (start_active_slot(slot)) { | |
427 | request->slot = slot; | |
428 | request->state = RUN_HEAD; | |
429 | } else { | |
430 | request->state = ABORTED; | |
431 | free(request->url); | |
432 | } | |
433 | } | |
434 | ||
435 | static void start_mkcol(struct transfer_request *request) | |
436 | { | |
437 | char *hex = sha1_to_hex(request->sha1); | |
438 | struct active_request_slot *slot; | |
439 | char *posn; | |
440 | ||
441 | request->url = xmalloc(strlen(remote->url) + 13); | |
442 | strcpy(request->url, remote->url); | |
443 | posn = request->url + strlen(remote->url); | |
444 | strcpy(posn, "objects/"); | |
445 | posn += 8; | |
446 | memcpy(posn, hex, 2); | |
447 | posn += 2; | |
448 | strcpy(posn, "/"); | |
449 | ||
450 | slot = get_active_slot(); | |
451 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */ | |
452 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
453 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr); | |
454 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL); | |
455 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
456 | ||
457 | if (start_active_slot(slot)) { | |
458 | request->slot = slot; | |
459 | request->state = RUN_MKCOL; | |
460 | } else { | |
461 | request->state = ABORTED; | |
462 | free(request->url); | |
463 | } | |
464 | } | |
465 | ||
466 | static void start_put(struct transfer_request *request) | |
467 | { | |
468 | char *hex = sha1_to_hex(request->sha1); | |
469 | struct active_request_slot *slot; | |
470 | char *posn; | |
471 | char type[20]; | |
472 | char hdr[50]; | |
473 | void *unpacked; | |
474 | unsigned long len; | |
475 | int hdrlen; | |
476 | ssize_t size; | |
477 | z_stream stream; | |
478 | ||
479 | unpacked = read_sha1_file(request->sha1, type, &len); | |
480 | hdrlen = sprintf(hdr, "%s %lu", type, len) + 1; | |
481 | ||
482 | /* Set it up */ | |
483 | memset(&stream, 0, sizeof(stream)); | |
484 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
485 | size = deflateBound(&stream, len + hdrlen); | |
486 | request->buffer.buffer = xmalloc(size); | |
487 | ||
488 | /* Compress it */ | |
489 | stream.next_out = request->buffer.buffer; | |
490 | stream.avail_out = size; | |
491 | ||
492 | /* First header.. */ | |
493 | stream.next_in = (void *)hdr; | |
494 | stream.avail_in = hdrlen; | |
495 | while (deflate(&stream, 0) == Z_OK) | |
496 | /* nothing */; | |
497 | ||
498 | /* Then the data itself.. */ | |
499 | stream.next_in = unpacked; | |
500 | stream.avail_in = len; | |
501 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
502 | /* nothing */; | |
503 | deflateEnd(&stream); | |
504 | free(unpacked); | |
505 | ||
506 | request->buffer.size = stream.total_out; | |
507 | request->buffer.posn = 0; | |
508 | ||
509 | if (request->url != NULL) | |
510 | free(request->url); | |
511 | request->url = xmalloc(strlen(remote->url) + | |
512 | strlen(request->lock_token) + 51); | |
513 | strcpy(request->url, remote->url); | |
514 | posn = request->url + strlen(remote->url); | |
515 | strcpy(posn, "objects/"); | |
516 | posn += 8; | |
517 | memcpy(posn, hex, 2); | |
518 | posn += 2; | |
519 | *(posn++) = '/'; | |
520 | strcpy(posn, hex + 2); | |
521 | request->dest = xmalloc(strlen(request->url) + 14); | |
522 | sprintf(request->dest, "Destination: %s", request->url); | |
523 | posn += 38; | |
524 | *(posn++) = '.'; | |
525 | strcpy(posn, request->lock_token); | |
526 | ||
527 | slot = get_active_slot(); | |
528 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer); | |
529 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size); | |
530 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); | |
531 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
532 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); | |
533 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
534 | curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); | |
535 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); | |
536 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
537 | ||
538 | if (start_active_slot(slot)) { | |
539 | request->slot = slot; | |
540 | request->state = RUN_PUT; | |
541 | } else { | |
542 | request->state = ABORTED; | |
543 | free(request->url); | |
544 | } | |
545 | } | |
546 | ||
547 | static void start_move(struct transfer_request *request) | |
548 | { | |
549 | struct active_request_slot *slot; | |
550 | struct curl_slist *dav_headers = NULL; | |
551 | ||
552 | slot = get_active_slot(); | |
553 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */ | |
554 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE); | |
555 | dav_headers = curl_slist_append(dav_headers, request->dest); | |
556 | dav_headers = curl_slist_append(dav_headers, "Overwrite: T"); | |
557 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
558 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
559 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
560 | ||
561 | if (start_active_slot(slot)) { | |
562 | request->slot = slot; | |
563 | request->state = RUN_MOVE; | |
564 | } else { | |
565 | request->state = ABORTED; | |
566 | free(request->url); | |
567 | } | |
568 | } | |
569 | ||
570 | static void finish_request(struct transfer_request *request) | |
571 | { | |
572 | request->curl_result = request->slot->curl_result; | |
573 | request->http_code = request->slot->http_code; | |
574 | request->slot = NULL; | |
575 | if (request->headers != NULL) | |
576 | curl_slist_free_all(request->headers); | |
577 | if (request->state == RUN_HEAD) { | |
578 | if (request->http_code == 404) { | |
579 | request->state = NEED_PUSH; | |
580 | } else if (request->curl_result == CURLE_OK) { | |
581 | request->state = COMPLETE; | |
582 | } else { | |
583 | fprintf(stderr, "HEAD %s failed, aborting (%d/%ld)\n", | |
584 | sha1_to_hex(request->sha1), | |
585 | request->curl_result, request->http_code); | |
586 | request->state = ABORTED; | |
587 | aborted = 1; | |
588 | } | |
589 | } else if (request->state == RUN_MKCOL) { | |
590 | if (request->curl_result == CURLE_OK || | |
591 | request->http_code == 405) { | |
592 | start_put(request); | |
593 | } else { | |
594 | fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n", | |
595 | sha1_to_hex(request->sha1), | |
596 | request->curl_result, request->http_code); | |
597 | request->state = ABORTED; | |
598 | aborted = 1; | |
599 | } | |
600 | } else if (request->state == RUN_PUT) { | |
601 | if (request->curl_result == CURLE_OK) { | |
602 | start_move(request); | |
603 | } else { | |
604 | fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n", | |
605 | sha1_to_hex(request->sha1), | |
606 | request->curl_result, request->http_code); | |
607 | request->state = ABORTED; | |
608 | aborted = 1; | |
609 | } | |
610 | } else if (request->state == RUN_MOVE) { | |
611 | if (request->curl_result == CURLE_OK) { | |
612 | if (push_verbosely) | |
613 | fprintf(stderr, | |
614 | "sent %s\n", | |
615 | sha1_to_hex(request->sha1)); | |
616 | request->state = COMPLETE; | |
617 | } else { | |
618 | fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n", | |
619 | sha1_to_hex(request->sha1), | |
620 | request->curl_result, request->http_code); | |
621 | request->state = ABORTED; | |
622 | aborted = 1; | |
623 | } | |
624 | } | |
625 | } | |
626 | ||
627 | static void release_request(struct transfer_request *request) | |
628 | { | |
629 | struct transfer_request *entry = request_queue_head; | |
630 | ||
631 | if (request == request_queue_head) { | |
632 | request_queue_head = request->next; | |
633 | } else { | |
634 | while (entry->next != NULL && entry->next != request) | |
635 | entry = entry->next; | |
636 | if (entry->next == request) | |
637 | entry->next = entry->next->next; | |
638 | } | |
639 | ||
640 | free(request->url); | |
641 | free(request); | |
642 | } | |
643 | ||
644 | #ifdef USE_CURL_MULTI | |
645 | void process_curl_messages(void) | |
646 | { | |
647 | int num_messages; | |
648 | struct active_request_slot *slot; | |
649 | struct transfer_request *request = NULL; | |
650 | CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); | |
651 | ||
652 | while (curl_message != NULL) { | |
653 | if (curl_message->msg == CURLMSG_DONE) { | |
654 | slot = active_queue_head; | |
655 | while (slot != NULL && | |
656 | slot->curl != curl_message->easy_handle) | |
657 | slot = slot->next; | |
658 | if (slot != NULL) { | |
659 | curl_multi_remove_handle(curlm, slot->curl); | |
660 | active_requests--; | |
661 | slot->done = 1; | |
662 | slot->in_use = 0; | |
663 | slot->curl_result = curl_message->data.result; | |
664 | curl_easy_getinfo(slot->curl, | |
665 | CURLINFO_HTTP_CODE, | |
666 | &slot->http_code); | |
667 | request = request_queue_head; | |
668 | while (request != NULL && | |
669 | request->slot != slot) | |
670 | request = request->next; | |
671 | if (request != NULL) | |
672 | finish_request(request); | |
673 | } else { | |
674 | fprintf(stderr, "Received DONE message for unknown request!\n"); | |
675 | } | |
676 | } else { | |
677 | fprintf(stderr, "Unknown CURL message received: %d\n", | |
678 | (int)curl_message->msg); | |
679 | } | |
680 | curl_message = curl_multi_info_read(curlm, &num_messages); | |
681 | } | |
682 | } | |
683 | ||
684 | void process_request_queue(void) | |
685 | { | |
686 | struct transfer_request *request = request_queue_head; | |
687 | struct active_request_slot *slot = active_queue_head; | |
688 | int num_transfers; | |
689 | ||
690 | if (aborted) | |
691 | return; | |
692 | ||
693 | while (active_requests < max_requests && request != NULL) { | |
694 | if (!pushing && request->state == NEED_CHECK) { | |
695 | start_check(request); | |
696 | curl_multi_perform(curlm, &num_transfers); | |
697 | } else if (pushing && request->state == NEED_PUSH) { | |
698 | start_mkcol(request); | |
699 | curl_multi_perform(curlm, &num_transfers); | |
700 | } | |
701 | request = request->next; | |
702 | } | |
703 | ||
704 | while (slot != NULL) { | |
705 | if (!slot->in_use && slot->curl != NULL) { | |
706 | curl_easy_cleanup(slot->curl); | |
707 | slot->curl = NULL; | |
708 | } | |
709 | slot = slot->next; | |
710 | } | |
711 | } | |
712 | #endif | |
713 | ||
714 | void process_waiting_requests(void) | |
715 | { | |
716 | struct active_request_slot *slot = active_queue_head; | |
717 | ||
718 | while (slot != NULL) | |
719 | if (slot->in_use) { | |
720 | run_active_slot(slot); | |
721 | slot = active_queue_head; | |
722 | } else { | |
723 | slot = slot->next; | |
724 | } | |
725 | } | |
726 | ||
727 | void add_request(unsigned char *sha1, char *lock_token) | |
728 | { | |
729 | struct transfer_request *request = request_queue_head; | |
58e60dd2 NH |
730 | struct packed_git *target; |
731 | ||
732 | while (request != NULL && memcmp(request->sha1, sha1, 20)) | |
733 | request = request->next; | |
734 | if (request != NULL) | |
735 | return; | |
736 | ||
737 | target = find_sha1_pack(sha1, remote->packs); | |
738 | if (target) | |
739 | return; | |
740 | ||
741 | request = xmalloc(sizeof(*request)); | |
742 | memcpy(request->sha1, sha1, 20); | |
743 | request->url = NULL; | |
744 | request->lock_token = lock_token; | |
745 | request->headers = NULL; | |
746 | request->state = NEED_CHECK; | |
c17fb6ee NH |
747 | request->next = request_queue_head; |
748 | request_queue_head = request; | |
58e60dd2 NH |
749 | #ifdef USE_CURL_MULTI |
750 | process_request_queue(); | |
751 | process_curl_messages(); | |
752 | #endif | |
753 | } | |
754 | ||
755 | static int fetch_index(unsigned char *sha1) | |
756 | { | |
757 | char *hex = sha1_to_hex(sha1); | |
758 | char *filename; | |
759 | char *url; | |
760 | char tmpfile[PATH_MAX]; | |
761 | long prev_posn = 0; | |
762 | char range[RANGE_HEADER_SIZE]; | |
763 | struct curl_slist *range_header = NULL; | |
764 | ||
765 | FILE *indexfile; | |
766 | struct active_request_slot *slot; | |
767 | ||
c17fb6ee NH |
768 | /* Don't use the index if the pack isn't there */ |
769 | url = xmalloc(strlen(remote->url) + 65); | |
770 | sprintf(url, "%s/objects/pack/pack-%s.pack", remote->url, hex); | |
771 | slot = get_active_slot(); | |
772 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
773 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1); | |
774 | if (start_active_slot(slot)) { | |
775 | run_active_slot(slot); | |
776 | if (slot->curl_result != CURLE_OK) { | |
777 | free(url); | |
778 | return error("Unable to verify pack %s is available", | |
779 | hex); | |
780 | } | |
781 | } else { | |
782 | return error("Unable to start request"); | |
783 | } | |
784 | ||
58e60dd2 NH |
785 | if (has_pack_index(sha1)) |
786 | return 0; | |
787 | ||
788 | if (push_verbosely) | |
789 | fprintf(stderr, "Getting index for pack %s\n", hex); | |
790 | ||
58e60dd2 NH |
791 | sprintf(url, "%s/objects/pack/pack-%s.idx", remote->url, hex); |
792 | ||
793 | filename = sha1_pack_index_name(sha1); | |
794 | snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename); | |
795 | indexfile = fopen(tmpfile, "a"); | |
796 | if (!indexfile) | |
797 | return error("Unable to open local file %s for pack index", | |
798 | filename); | |
799 | ||
800 | slot = get_active_slot(); | |
c17fb6ee NH |
801 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
802 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
58e60dd2 NH |
803 | curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile); |
804 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite); | |
805 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
806 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); | |
807 | slot->local = indexfile; | |
808 | ||
809 | /* If there is data present from a previous transfer attempt, | |
810 | resume where it left off */ | |
811 | prev_posn = ftell(indexfile); | |
812 | if (prev_posn>0) { | |
813 | if (push_verbosely) | |
814 | fprintf(stderr, | |
815 | "Resuming fetch of index for pack %s at byte %ld\n", | |
816 | hex, prev_posn); | |
817 | sprintf(range, "Range: bytes=%ld-", prev_posn); | |
818 | range_header = curl_slist_append(range_header, range); | |
819 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header); | |
820 | } | |
821 | ||
822 | if (start_active_slot(slot)) { | |
823 | run_active_slot(slot); | |
824 | if (slot->curl_result != CURLE_OK) { | |
825 | free(url); | |
826 | fclose(indexfile); | |
827 | return error("Unable to get pack index %s\n%s", url, | |
828 | curl_errorstr); | |
829 | } | |
830 | } else { | |
831 | free(url); | |
832 | return error("Unable to start request"); | |
833 | } | |
834 | ||
835 | free(url); | |
836 | fclose(indexfile); | |
837 | ||
838 | return move_temp_to_file(tmpfile, filename); | |
839 | } | |
840 | ||
841 | static int setup_index(unsigned char *sha1) | |
842 | { | |
843 | struct packed_git *new_pack; | |
58e60dd2 NH |
844 | |
845 | if (fetch_index(sha1)) | |
846 | return -1; | |
847 | ||
848 | new_pack = parse_pack_index(sha1); | |
849 | new_pack->next = remote->packs; | |
850 | remote->packs = new_pack; | |
851 | return 0; | |
852 | } | |
853 | ||
854 | static int fetch_indices() | |
855 | { | |
856 | unsigned char sha1[20]; | |
857 | char *url; | |
858 | struct buffer buffer; | |
859 | char *data; | |
860 | int i = 0; | |
861 | ||
862 | struct active_request_slot *slot; | |
863 | ||
864 | data = xmalloc(4096); | |
865 | memset(data, 0, 4096); | |
866 | buffer.size = 4096; | |
867 | buffer.posn = 0; | |
868 | buffer.buffer = data; | |
869 | ||
870 | if (push_verbosely) | |
871 | fprintf(stderr, "Getting pack list\n"); | |
872 | ||
873 | url = xmalloc(strlen(remote->url) + 21); | |
874 | sprintf(url, "%s/objects/info/packs", remote->url); | |
875 | ||
876 | slot = get_active_slot(); | |
877 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); | |
878 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, | |
879 | fwrite_buffer_dynamic); | |
880 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
881 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL); | |
882 | if (start_active_slot(slot)) { | |
883 | run_active_slot(slot); | |
884 | if (slot->curl_result != CURLE_OK) { | |
885 | free(buffer.buffer); | |
886 | free(url); | |
887 | if (slot->http_code == 404) | |
888 | return 0; | |
889 | else | |
890 | return error("%s", curl_errorstr); | |
891 | } | |
892 | } else { | |
893 | free(buffer.buffer); | |
894 | free(url); | |
895 | return error("Unable to start request"); | |
896 | } | |
897 | free(url); | |
898 | ||
899 | data = buffer.buffer; | |
900 | while (i < buffer.posn) { | |
901 | switch (data[i]) { | |
902 | case 'P': | |
903 | i++; | |
904 | if (i + 52 < buffer.posn && | |
905 | !strncmp(data + i, " pack-", 6) && | |
906 | !strncmp(data + i + 46, ".pack\n", 6)) { | |
907 | get_sha1_hex(data + i + 6, sha1); | |
908 | setup_index(sha1); | |
909 | i += 51; | |
910 | break; | |
911 | } | |
912 | default: | |
913 | while (data[i] != '\n') | |
914 | i++; | |
915 | } | |
916 | i++; | |
917 | } | |
918 | ||
919 | free(buffer.buffer); | |
920 | return 0; | |
921 | } | |
922 | ||
923 | static inline int needs_quote(int ch) | |
924 | { | |
925 | switch (ch) { | |
926 | case '/': case '-': case '.': | |
927 | case 'A'...'Z': case 'a'...'z': case '0'...'9': | |
928 | return 0; | |
929 | default: | |
930 | return 1; | |
931 | } | |
932 | } | |
933 | ||
934 | static inline int hex(int v) | |
935 | { | |
936 | if (v < 10) return '0' + v; | |
937 | else return 'A' + v - 10; | |
938 | } | |
939 | ||
940 | static char *quote_ref_url(const char *base, const char *ref) | |
941 | { | |
942 | const char *cp; | |
943 | char *dp, *qref; | |
944 | int len, baselen, ch; | |
945 | ||
946 | baselen = strlen(base); | |
947 | len = baselen + 12; /* "refs/heads/" + NUL */ | |
948 | for (cp = ref; (ch = *cp) != 0; cp++, len++) | |
949 | if (needs_quote(ch)) | |
950 | len += 2; /* extra two hex plus replacement % */ | |
951 | qref = xmalloc(len); | |
952 | memcpy(qref, base, baselen); | |
953 | memcpy(qref + baselen, "refs/heads/", 11); | |
954 | for (cp = ref, dp = qref + baselen + 11; (ch = *cp) != 0; cp++) { | |
955 | if (needs_quote(ch)) { | |
956 | *dp++ = '%'; | |
957 | *dp++ = hex((ch >> 4) & 0xF); | |
958 | *dp++ = hex(ch & 0xF); | |
959 | } | |
960 | else | |
961 | *dp++ = ch; | |
962 | } | |
963 | *dp = 0; | |
964 | ||
965 | return qref; | |
966 | } | |
967 | ||
968 | int fetch_ref(char *ref, unsigned char *sha1) | |
969 | { | |
970 | char *url; | |
971 | char hex[42]; | |
972 | struct buffer buffer; | |
973 | char *base = remote->url; | |
974 | struct active_request_slot *slot; | |
975 | buffer.size = 41; | |
976 | buffer.posn = 0; | |
977 | buffer.buffer = hex; | |
978 | hex[41] = '\0'; | |
979 | ||
980 | url = quote_ref_url(base, ref); | |
981 | slot = get_active_slot(); | |
982 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); | |
983 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, | |
984 | fwrite_buffer_dynamic); | |
985 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL); | |
986 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
987 | if (start_active_slot(slot)) { | |
988 | run_active_slot(slot); | |
989 | if (slot->curl_result != CURLE_OK) | |
990 | return error("Couldn't get %s for %s\n%s", | |
991 | url, ref, curl_errorstr); | |
992 | } else { | |
993 | return error("Unable to start request"); | |
994 | } | |
995 | ||
996 | hex[40] = '\0'; | |
997 | get_sha1_hex(hex, sha1); | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static void | |
1002 | start_lockprop_element(void *userData, const char *name, const char **atts) | |
1003 | { | |
1004 | struct lockprop *prop = (struct lockprop *)userData; | |
1005 | ||
1006 | if (prop->lock_type && !strcmp(name, "D:write")) { | |
1007 | if (prop->lock_exclusive) { | |
1008 | prop->lock_exclusive_write = 1; | |
1009 | } | |
1010 | } else if (prop->lock_scope && !strcmp(name, "D:exclusive")) { | |
1011 | prop->lock_exclusive = 1; | |
1012 | } else if (prop->lock_entry) { | |
1013 | if (!strcmp(name, "D:lockscope")) { | |
1014 | prop->lock_scope = 1; | |
1015 | } else if (!strcmp(name, "D:locktype")) { | |
1016 | prop->lock_type = 1; | |
1017 | } | |
1018 | } else if (prop->supported_lock) { | |
1019 | if (!strcmp(name, "D:lockentry")) { | |
1020 | prop->lock_entry = 1; | |
1021 | } | |
1022 | } else if (!strcmp(name, "D:supportedlock")) { | |
1023 | prop->supported_lock = 1; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | static void | |
1028 | end_lockprop_element(void *userData, const char *name) | |
1029 | { | |
1030 | struct lockprop *prop = (struct lockprop *)userData; | |
1031 | ||
1032 | if (!strcmp(name, "D:lockentry")) { | |
1033 | prop->lock_entry = 0; | |
1034 | prop->lock_scope = 0; | |
1035 | prop->lock_type = 0; | |
1036 | prop->lock_exclusive = 0; | |
1037 | } else if (!strcmp(name, "D:supportedlock")) { | |
1038 | prop->supported_lock = 0; | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | size_t process_lock_header( void *ptr, size_t size, size_t nmemb, void *stream) | |
1043 | { | |
1044 | size_t header_size = size*nmemb; | |
1045 | char *start; | |
1046 | char *end; | |
1047 | ||
1048 | if (!strncmp(ptr, "Lock-Token: <opaquelocktoken:", 29)) { | |
1049 | start = ptr + 29; | |
1050 | for (end = ptr + header_size; | |
1051 | *(end - 1) == '\r' || *(end - 1) == '\n' || *(end - 1) == '>'; | |
1052 | end--) {} | |
1053 | if (end > start) { | |
1054 | lock_token = xmalloc(end - start + 1); | |
1055 | memcpy(lock_token, start, end - start); | |
1056 | lock_token[end - start] = 0; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | return header_size; | |
1061 | } | |
1062 | ||
1063 | char *lock_remote(char *file, int timeout) | |
1064 | { | |
1065 | struct active_request_slot *slot; | |
1066 | struct buffer out_buffer; | |
1067 | char *out_data; | |
1068 | char *url; | |
1069 | char timeout_header[25]; | |
1070 | struct curl_slist *dav_headers = NULL; | |
1071 | ||
1072 | if (lock_token != NULL) | |
1073 | free(lock_token); | |
1074 | ||
1075 | out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2; | |
1076 | out_data = xmalloc(out_buffer.size + 1); | |
1077 | snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email); | |
1078 | out_buffer.posn = 0; | |
1079 | out_buffer.buffer = out_data; | |
1080 | ||
1081 | sprintf(timeout_header, "Timeout: Second-%d", timeout); | |
1082 | url = xmalloc(strlen(remote->url) + strlen(file) + 1); | |
1083 | sprintf(url, "%s%s", remote->url, file); | |
1084 | dav_headers = curl_slist_append(dav_headers, timeout_header); | |
1085 | dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); | |
1086 | ||
1087 | slot = get_active_slot(); | |
1088 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); | |
1089 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); | |
1090 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); | |
1091 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
1092 | curl_easy_setopt(slot->curl, CURLOPT_HEADERFUNCTION, | |
1093 | process_lock_header); | |
1094 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
1095 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1096 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK); | |
1097 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1098 | ||
1099 | if (start_active_slot(slot)) { | |
1100 | run_active_slot(slot); | |
1101 | free(out_data); | |
1102 | if (slot->curl_result != CURLE_OK) { | |
1103 | fprintf(stderr, "Got HTTP error %ld\n", slot->http_code); | |
1104 | return NULL; | |
1105 | } | |
1106 | } else { | |
1107 | free(out_data); | |
1108 | fprintf(stderr, "Unable to start request\n"); | |
1109 | } | |
1110 | ||
1111 | return strdup(lock_token); | |
1112 | } | |
1113 | ||
1114 | int unlock_remote(char *file, char *lock_token) | |
1115 | { | |
1116 | struct active_request_slot *slot; | |
1117 | char *url; | |
1118 | char *lock_token_header; | |
1119 | struct curl_slist *dav_headers = NULL; | |
1120 | int rc = 0; | |
1121 | ||
1122 | if (lock_token == NULL) { | |
1123 | fprintf(stderr, "Unable to unlock, no lock token"); | |
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | lock_token_header = xmalloc(strlen(lock_token) + 31); | |
1128 | sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>", | |
1129 | lock_token); | |
1130 | url = xmalloc(strlen(remote->url) + strlen(file) + 1); | |
1131 | sprintf(url, "%s%s", remote->url, file); | |
1132 | dav_headers = curl_slist_append(dav_headers, lock_token_header); | |
1133 | ||
1134 | slot = get_active_slot(); | |
1135 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
1136 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
1137 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK); | |
1138 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1139 | ||
1140 | if (start_active_slot(slot)) { | |
1141 | run_active_slot(slot); | |
1142 | if (slot->curl_result == CURLE_OK) | |
1143 | rc = 1; | |
1144 | else | |
1145 | fprintf(stderr, "Got HTTP error %ld\n", | |
1146 | slot->http_code); | |
1147 | } else { | |
1148 | fprintf(stderr, "Unable to start request\n"); | |
1149 | } | |
1150 | ||
1151 | curl_slist_free_all(dav_headers); | |
1152 | free(lock_token_header); | |
1153 | free(url); | |
1154 | ||
1155 | return rc; | |
1156 | } | |
1157 | ||
1158 | int check_locking() | |
1159 | { | |
1160 | struct active_request_slot *slot; | |
1161 | struct buffer in_buffer; | |
1162 | struct buffer out_buffer; | |
1163 | char *in_data; | |
1164 | char *out_data; | |
1165 | XML_Parser parser = XML_ParserCreate(NULL); | |
1166 | enum XML_Status result; | |
1167 | struct lockprop supported_lock; | |
1168 | struct curl_slist *dav_headers = NULL; | |
1169 | ||
1170 | out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2; | |
1171 | out_data = xmalloc(out_buffer.size + 1); | |
1172 | snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url); | |
1173 | out_buffer.posn = 0; | |
1174 | out_buffer.buffer = out_data; | |
1175 | ||
1176 | in_buffer.size = 4096; | |
1177 | in_data = xmalloc(in_buffer.size); | |
1178 | in_buffer.posn = 0; | |
1179 | in_buffer.buffer = in_data; | |
1180 | ||
1181 | dav_headers = curl_slist_append(dav_headers, "Depth: 0"); | |
1182 | dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); | |
1183 | ||
1184 | slot = get_active_slot(); | |
1185 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); | |
1186 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); | |
1187 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); | |
1188 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); | |
1189 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, | |
1190 | fwrite_buffer_dynamic); | |
1191 | curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url); | |
1192 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1193 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND); | |
1194 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1195 | ||
1196 | if (start_active_slot(slot)) { | |
1197 | run_active_slot(slot); | |
1198 | free(out_data); | |
1199 | if (slot->curl_result != CURLE_OK) { | |
1200 | free(in_buffer.buffer); | |
1201 | return -1; | |
1202 | } | |
1203 | ||
1204 | XML_SetUserData(parser, &supported_lock); | |
1205 | XML_SetElementHandler(parser, start_lockprop_element, | |
1206 | end_lockprop_element); | |
1207 | result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1); | |
1208 | free(in_buffer.buffer); | |
1209 | if (result != XML_STATUS_OK) | |
1210 | return error("%s", XML_ErrorString( | |
1211 | XML_GetErrorCode(parser))); | |
1212 | } else { | |
1213 | free(out_data); | |
1214 | free(in_buffer.buffer); | |
1215 | return error("Unable to start request"); | |
1216 | } | |
1217 | ||
1218 | if (supported_lock.lock_exclusive_write) | |
1219 | return 0; | |
1220 | else | |
1221 | return 1; | |
1222 | } | |
1223 | ||
1224 | int is_ancestor(unsigned char *sha1, struct commit *commit) | |
1225 | { | |
1226 | struct commit_list *parents; | |
1227 | ||
1228 | if (parse_commit(commit)) | |
1229 | return 0; | |
1230 | parents = commit->parents; | |
1231 | for (; parents; parents = parents->next) { | |
1232 | if (!memcmp(sha1, parents->item->object.sha1, 20)) { | |
1233 | return 1; | |
1234 | } else if (parents->item->object.type == commit_type) { | |
1235 | if (is_ancestor( | |
1236 | sha1, | |
1237 | (struct commit *)&parents->item->object | |
1238 | )) | |
1239 | return 1; | |
1240 | } | |
1241 | } | |
1242 | return 0; | |
1243 | } | |
1244 | ||
1245 | void get_delta(unsigned char *sha1, struct object *obj, char *lock_token) | |
1246 | { | |
1247 | struct commit *commit; | |
1248 | struct commit_list *parents; | |
1249 | struct tree *tree; | |
1250 | struct tree_entry_list *entry; | |
1251 | ||
1252 | if (sha1 && !memcmp(sha1, obj->sha1, 20)) | |
1253 | return; | |
1254 | ||
1255 | if (aborted) | |
1256 | return; | |
1257 | ||
1258 | if (obj->type == commit_type) { | |
1259 | if (push_verbosely) | |
1260 | fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1)); | |
1261 | add_request(obj->sha1, lock_token); | |
1262 | commit = (struct commit *)obj; | |
1263 | if (parse_commit(commit)) { | |
1264 | fprintf(stderr, "Error parsing commit %s\n", | |
1265 | sha1_to_hex(obj->sha1)); | |
1266 | aborted = 1; | |
1267 | return; | |
1268 | } | |
1269 | parents = commit->parents; | |
1270 | for (; parents; parents = parents->next) | |
1271 | if (sha1 == NULL || | |
1272 | memcmp(sha1, parents->item->object.sha1, 20)) | |
1273 | get_delta(sha1, &parents->item->object, | |
1274 | lock_token); | |
1275 | get_delta(sha1, &commit->tree->object, lock_token); | |
1276 | } else if (obj->type == tree_type) { | |
1277 | if (push_verbosely) | |
1278 | fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1)); | |
1279 | add_request(obj->sha1, lock_token); | |
1280 | tree = (struct tree *)obj; | |
1281 | if (parse_tree(tree)) { | |
1282 | fprintf(stderr, "Error parsing tree %s\n", | |
1283 | sha1_to_hex(obj->sha1)); | |
1284 | aborted = 1; | |
1285 | return; | |
1286 | } | |
1287 | entry = tree->entries; | |
1288 | tree->entries = NULL; | |
1289 | while (entry) { | |
1290 | struct tree_entry_list *next = entry->next; | |
1291 | get_delta(sha1, entry->item.any, lock_token); | |
1292 | free(entry->name); | |
1293 | free(entry); | |
1294 | entry = next; | |
1295 | } | |
1296 | } else if (obj->type == blob_type || obj->type == tag_type) { | |
1297 | add_request(obj->sha1, lock_token); | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | int update_remote(char *remote_path, unsigned char *sha1, char *lock_token) | |
1302 | { | |
1303 | struct active_request_slot *slot; | |
1304 | char *url; | |
1305 | char *out_data; | |
1306 | char *if_header; | |
1307 | struct buffer out_buffer; | |
1308 | struct curl_slist *dav_headers = NULL; | |
1309 | int i; | |
1310 | ||
1311 | url = xmalloc(strlen(remote->url) + strlen(remote_path) + 1); | |
1312 | sprintf(url, "%s%s", remote->url, remote_path); | |
1313 | ||
1314 | if_header = xmalloc(strlen(lock_token) + 25); | |
1315 | sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock_token); | |
1316 | dav_headers = curl_slist_append(dav_headers, if_header); | |
1317 | ||
1318 | out_buffer.size = 41; | |
1319 | out_data = xmalloc(out_buffer.size + 1); | |
1320 | i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1)); | |
1321 | if (i != out_buffer.size) { | |
1322 | fprintf(stderr, "Unable to initialize PUT request body\n"); | |
1323 | return 0; | |
1324 | } | |
1325 | out_buffer.posn = 0; | |
1326 | out_buffer.buffer = out_data; | |
1327 | ||
1328 | slot = get_active_slot(); | |
1329 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); | |
1330 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size); | |
1331 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); | |
1332 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
1333 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); | |
1334 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1335 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1336 | curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); | |
1337 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
1338 | ||
1339 | if (start_active_slot(slot)) { | |
1340 | run_active_slot(slot); | |
1341 | free(out_data); | |
1342 | free(if_header); | |
1343 | free(url); | |
1344 | if (slot->curl_result != CURLE_OK) { | |
1345 | fprintf(stderr, | |
1346 | "PUT error: curl result=%d, HTTP code=%ld\n", | |
1347 | slot->curl_result, slot->http_code); | |
1348 | /* We should attempt recovery? */ | |
1349 | return 0; | |
1350 | } | |
1351 | } else { | |
1352 | free(out_data); | |
1353 | free(if_header); | |
1354 | free(url); | |
1355 | fprintf(stderr, "Unable to start PUT request\n"); | |
1356 | return 0; | |
1357 | } | |
1358 | ||
1359 | return 1; | |
1360 | } | |
1361 | ||
1362 | int main(int argc, char **argv) | |
1363 | { | |
1364 | struct active_request_slot *slot; | |
1365 | struct active_request_slot *next_slot; | |
1366 | struct transfer_request *request; | |
1367 | struct transfer_request *next_request; | |
1368 | int nr_refspec = 0; | |
1369 | char **refspec = NULL; | |
1370 | int do_remote_update; | |
1371 | int new_branch; | |
1372 | int force_this; | |
1373 | char *local_ref; | |
1374 | unsigned char local_sha1[20]; | |
1375 | struct object *local_object = NULL; | |
1376 | char *remote_ref = NULL; | |
1377 | unsigned char remote_sha1[20]; | |
1378 | char *remote_lock = NULL; | |
1379 | char *remote_path = NULL; | |
1380 | char *low_speed_limit; | |
1381 | char *low_speed_time; | |
1382 | int rc = 0; | |
1383 | int i; | |
1384 | ||
1385 | setup_ident(); | |
1386 | ||
1387 | remote = xmalloc(sizeof(*remote)); | |
1388 | remote->url = NULL; | |
1389 | remote->packs = NULL; | |
1390 | ||
1391 | argv++; | |
1392 | for (i = 1; i < argc; i++, argv++) { | |
1393 | char *arg = *argv; | |
1394 | ||
1395 | if (*arg == '-') { | |
1396 | if (!strcmp(arg, "--complete")) { | |
1397 | push_all = 1; | |
1398 | continue; | |
1399 | } | |
1400 | if (!strcmp(arg, "--force")) { | |
1401 | force_all = 1; | |
1402 | continue; | |
1403 | } | |
1404 | if (!strcmp(arg, "--verbose")) { | |
1405 | push_verbosely = 1; | |
1406 | continue; | |
1407 | } | |
1408 | usage(http_push_usage); | |
1409 | } | |
1410 | if (!remote->url) { | |
1411 | remote->url = arg; | |
1412 | continue; | |
1413 | } | |
1414 | refspec = argv; | |
1415 | nr_refspec = argc - i; | |
1416 | break; | |
1417 | } | |
1418 | ||
1419 | curl_global_init(CURL_GLOBAL_ALL); | |
1420 | ||
1421 | #ifdef USE_CURL_MULTI | |
1422 | { | |
1423 | char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); | |
1424 | if (http_max_requests != NULL) | |
1425 | max_requests = atoi(http_max_requests); | |
1426 | } | |
1427 | ||
1428 | curlm = curl_multi_init(); | |
1429 | if (curlm == NULL) { | |
1430 | fprintf(stderr, "Error creating curl multi handle.\n"); | |
1431 | return 1; | |
1432 | } | |
1433 | #endif | |
1434 | ||
1435 | if (getenv("GIT_SSL_NO_VERIFY")) | |
1436 | curl_ssl_verify = 0; | |
1437 | ||
1438 | ssl_cert = getenv("GIT_SSL_CERT"); | |
1439 | #if LIBCURL_VERSION_NUM >= 0x070902 | |
1440 | ssl_key = getenv("GIT_SSL_KEY"); | |
1441 | #endif | |
1442 | #if LIBCURL_VERSION_NUM >= 0x070908 | |
1443 | ssl_capath = getenv("GIT_SSL_CAPATH"); | |
1444 | #endif | |
1445 | ssl_cainfo = getenv("GIT_SSL_CAINFO"); | |
1446 | ||
1447 | low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT"); | |
1448 | if (low_speed_limit != NULL) | |
1449 | curl_low_speed_limit = strtol(low_speed_limit, NULL, 10); | |
1450 | low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME"); | |
1451 | if (low_speed_time != NULL) | |
1452 | curl_low_speed_time = strtol(low_speed_time, NULL, 10); | |
1453 | ||
1454 | git_config(http_options); | |
1455 | ||
1456 | if (curl_ssl_verify == -1) | |
1457 | curl_ssl_verify = 1; | |
1458 | ||
1459 | #ifdef USE_CURL_MULTI | |
1460 | if (max_requests < 1) | |
1461 | max_requests = DEFAULT_MAX_REQUESTS; | |
1462 | #endif | |
1463 | ||
1464 | no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); | |
1465 | default_headers = curl_slist_append(default_headers, "Range:"); | |
1466 | default_headers = curl_slist_append(default_headers, "Destination:"); | |
1467 | default_headers = curl_slist_append(default_headers, "If:"); | |
1468 | default_headers = curl_slist_append(default_headers, | |
1469 | "Pragma: no-cache"); | |
1470 | ||
1471 | #ifndef NO_CURL_EASY_DUPHANDLE | |
1472 | curl_default = get_curl_handle(); | |
1473 | #endif | |
1474 | ||
1475 | /* Verify DAV compliance/lock support */ | |
1476 | if (check_locking() != 0) { | |
1477 | fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url); | |
1478 | rc = 1; | |
1479 | goto cleanup; | |
1480 | } | |
1481 | ||
1482 | /* Process each refspec */ | |
1483 | for (i = 0; i < nr_refspec; i++) { | |
1484 | char *ep; | |
1485 | force_this = 0; | |
1486 | do_remote_update = 0; | |
1487 | new_branch = 0; | |
1488 | local_ref = refspec[i]; | |
1489 | if (*local_ref == '+') { | |
1490 | force_this = 1; | |
1491 | local_ref++; | |
1492 | } | |
1493 | ep = strchr(local_ref, ':'); | |
1494 | if (ep) { | |
1495 | remote_ref = ep + 1; | |
1496 | *ep = 0; | |
1497 | } | |
1498 | else | |
1499 | remote_ref = local_ref; | |
1500 | ||
1501 | /* Lock remote branch ref */ | |
1502 | if (remote_path) | |
1503 | free(remote_path); | |
1504 | remote_path = xmalloc(strlen(remote_ref) + 12); | |
1505 | sprintf(remote_path, "refs/heads/%s", remote_ref); | |
1506 | remote_lock = lock_remote(remote_path, 3600); | |
1507 | if (remote_lock == NULL) { | |
1508 | fprintf(stderr, "Unable to lock remote branch %s\n", | |
1509 | remote_ref); | |
1510 | rc = 1; | |
1511 | continue; | |
1512 | } | |
1513 | ||
1514 | /* Resolve local and remote refs */ | |
1515 | if (fetch_ref(remote_ref, remote_sha1) != 0) { | |
1516 | fprintf(stderr, | |
1517 | "Remote branch %s does not exist on %s\n", | |
1518 | remote_ref, remote->url); | |
1519 | new_branch = 1; | |
1520 | } | |
1521 | if (get_sha1(local_ref, local_sha1) != 0) { | |
1522 | fprintf(stderr, "Error resolving local branch %s\n", | |
1523 | local_ref); | |
1524 | rc = 1; | |
1525 | goto unlock; | |
1526 | } | |
1527 | ||
1528 | /* Find relationship between local and remote */ | |
1529 | local_object = parse_object(local_sha1); | |
1530 | if (!local_object) { | |
1531 | fprintf(stderr, "Unable to parse local object %s\n", | |
1532 | sha1_to_hex(local_sha1)); | |
1533 | rc = 1; | |
1534 | goto unlock; | |
1535 | } else if (new_branch) { | |
1536 | do_remote_update = 1; | |
1537 | } else { | |
1538 | if (!memcmp(local_sha1, remote_sha1, 20)) { | |
1539 | fprintf(stderr, | |
1540 | "* %s: same as branch '%s' of %s\n", | |
1541 | local_ref, remote_ref, remote->url); | |
1542 | } else if (is_ancestor(remote_sha1, | |
1543 | (struct commit *)local_object)) { | |
1544 | fprintf(stderr, | |
1545 | "Remote %s will fast-forward to local %s\n", | |
1546 | remote_ref, local_ref); | |
1547 | do_remote_update = 1; | |
1548 | } else if (force_all || force_this) { | |
1549 | fprintf(stderr, | |
1550 | "* %s on %s does not fast forward to local branch '%s', overwriting\n", | |
1551 | remote_ref, remote->url, local_ref); | |
1552 | do_remote_update = 1; | |
1553 | } else { | |
1554 | fprintf(stderr, | |
1555 | "* %s on %s does not fast forward to local branch '%s'\n", | |
1556 | remote_ref, remote->url, local_ref); | |
1557 | rc = 1; | |
1558 | goto unlock; | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | /* Generate and check list of required objects */ | |
1563 | pushing = 0; | |
1564 | if (do_remote_update || push_all) | |
1565 | fetch_indices(); | |
1566 | get_delta(push_all ? NULL : remote_sha1, | |
1567 | local_object, remote_lock); | |
1568 | process_waiting_requests(); | |
1569 | ||
1570 | /* Push missing objects to remote, this would be a | |
1571 | convenient time to pack them first if appropriate. */ | |
1572 | pushing = 1; | |
1573 | process_request_queue(); | |
1574 | process_waiting_requests(); | |
1575 | ||
1576 | /* Update the remote branch if all went well */ | |
1577 | if (do_remote_update) { | |
1578 | if (!aborted && update_remote(remote_path, | |
1579 | local_sha1, | |
1580 | remote_lock)) { | |
1581 | fprintf(stderr, "%s remote branch %s\n", | |
1582 | new_branch ? "Created" : "Updated", | |
1583 | remote_ref); | |
1584 | } else { | |
1585 | fprintf(stderr, | |
1586 | "Unable to %s remote branch %s\n", | |
1587 | new_branch ? "create" : "update", | |
1588 | remote_ref); | |
1589 | rc = 1; | |
1590 | goto unlock; | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | unlock: | |
1595 | unlock_remote(remote_path, remote_lock); | |
1596 | free(remote_path); | |
1597 | free(remote_lock); | |
1598 | } | |
1599 | ||
1600 | cleanup: | |
1601 | free(remote); | |
1602 | ||
1603 | curl_slist_free_all(no_pragma_header); | |
1604 | curl_slist_free_all(default_headers); | |
1605 | ||
1606 | slot = active_queue_head; | |
1607 | while (slot != NULL) { | |
1608 | next_slot = slot->next; | |
1609 | if (slot->curl != NULL) | |
1610 | curl_easy_cleanup(slot->curl); | |
1611 | free(slot); | |
1612 | slot = next_slot; | |
1613 | } | |
1614 | ||
1615 | request = request_queue_head; | |
1616 | while (request != NULL) { | |
1617 | next_request = request->next; | |
1618 | release_request(request); | |
58e60dd2 NH |
1619 | request = next_request; |
1620 | } | |
1621 | ||
1622 | #ifndef NO_CURL_EASY_DUPHANDLE | |
1623 | curl_easy_cleanup(curl_default); | |
1624 | #endif | |
1625 | #ifdef USE_CURL_MULTI | |
1626 | curl_multi_cleanup(curlm); | |
1627 | #endif | |
1628 | curl_global_cleanup(); | |
1629 | return rc; | |
1630 | } |