http-walker.con commit Merge branch 'mg/cherry-pick-multi-on-unborn' into maint (25227f0)
   1#include "cache.h"
   2#include "commit.h"
   3#include "walker.h"
   4#include "http.h"
   5
   6struct alt_base {
   7        char *base;
   8        int got_indices;
   9        struct packed_git *packs;
  10        struct alt_base *next;
  11};
  12
  13enum object_request_state {
  14        WAITING,
  15        ABORTED,
  16        ACTIVE,
  17        COMPLETE
  18};
  19
  20struct object_request {
  21        struct walker *walker;
  22        unsigned char sha1[20];
  23        struct alt_base *repo;
  24        enum object_request_state state;
  25        struct http_object_request *req;
  26        struct object_request *next;
  27};
  28
  29struct alternates_request {
  30        struct walker *walker;
  31        const char *base;
  32        struct strbuf *url;
  33        struct strbuf *buffer;
  34        struct active_request_slot *slot;
  35        int http_specific;
  36};
  37
  38struct walker_data {
  39        const char *url;
  40        int got_alternates;
  41        struct alt_base *alt;
  42};
  43
  44static struct object_request *object_queue_head;
  45
  46static void fetch_alternates(struct walker *walker, const char *base);
  47
  48static void process_object_response(void *callback_data);
  49
  50static void start_object_request(struct walker *walker,
  51                                 struct object_request *obj_req)
  52{
  53        struct active_request_slot *slot;
  54        struct http_object_request *req;
  55
  56        req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
  57        if (req == NULL) {
  58                obj_req->state = ABORTED;
  59                return;
  60        }
  61        obj_req->req = req;
  62
  63        slot = req->slot;
  64        slot->callback_func = process_object_response;
  65        slot->callback_data = obj_req;
  66
  67        /* Try to get the request started, abort the request on error */
  68        obj_req->state = ACTIVE;
  69        if (!start_active_slot(slot)) {
  70                obj_req->state = ABORTED;
  71                release_http_object_request(req);
  72                return;
  73        }
  74}
  75
  76static void finish_object_request(struct object_request *obj_req)
  77{
  78        if (finish_http_object_request(obj_req->req))
  79                return;
  80
  81        if (obj_req->req->rename == 0)
  82                walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
  83}
  84
  85static void process_object_response(void *callback_data)
  86{
  87        struct object_request *obj_req =
  88                (struct object_request *)callback_data;
  89        struct walker *walker = obj_req->walker;
  90        struct walker_data *data = walker->data;
  91        struct alt_base *alt = data->alt;
  92
  93        process_http_object_request(obj_req->req);
  94        obj_req->state = COMPLETE;
  95
  96        /* Use alternates if necessary */
  97        if (missing_target(obj_req->req)) {
  98                fetch_alternates(walker, alt->base);
  99                if (obj_req->repo->next != NULL) {
 100                        obj_req->repo =
 101                                obj_req->repo->next;
 102                        release_http_object_request(obj_req->req);
 103                        start_object_request(walker, obj_req);
 104                        return;
 105                }
 106        }
 107
 108        finish_object_request(obj_req);
 109}
 110
 111static void release_object_request(struct object_request *obj_req)
 112{
 113        struct object_request *entry = object_queue_head;
 114
 115        if (obj_req->req !=NULL && obj_req->req->localfile != -1)
 116                error("fd leakage in release: %d", obj_req->req->localfile);
 117        if (obj_req == object_queue_head) {
 118                object_queue_head = obj_req->next;
 119        } else {
 120                while (entry->next != NULL && entry->next != obj_req)
 121                        entry = entry->next;
 122                if (entry->next == obj_req)
 123                        entry->next = entry->next->next;
 124        }
 125
 126        free(obj_req);
 127}
 128
 129#ifdef USE_CURL_MULTI
 130static int fill_active_slot(struct walker *walker)
 131{
 132        struct object_request *obj_req;
 133
 134        for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
 135                if (obj_req->state == WAITING) {
 136                        if (has_sha1_file(obj_req->sha1))
 137                                obj_req->state = COMPLETE;
 138                        else {
 139                                start_object_request(walker, obj_req);
 140                                return 1;
 141                        }
 142                }
 143        }
 144        return 0;
 145}
 146#endif
 147
 148static void prefetch(struct walker *walker, unsigned char *sha1)
 149{
 150        struct object_request *newreq;
 151        struct object_request *tail;
 152        struct walker_data *data = walker->data;
 153
 154        newreq = xmalloc(sizeof(*newreq));
 155        newreq->walker = walker;
 156        hashcpy(newreq->sha1, sha1);
 157        newreq->repo = data->alt;
 158        newreq->state = WAITING;
 159        newreq->req = NULL;
 160        newreq->next = NULL;
 161
 162        http_is_verbose = walker->get_verbosely;
 163
 164        if (object_queue_head == NULL) {
 165                object_queue_head = newreq;
 166        } else {
 167                tail = object_queue_head;
 168                while (tail->next != NULL)
 169                        tail = tail->next;
 170                tail->next = newreq;
 171        }
 172
 173#ifdef USE_CURL_MULTI
 174        fill_active_slots();
 175        step_active_slots();
 176#endif
 177}
 178
 179static void process_alternates_response(void *callback_data)
 180{
 181        struct alternates_request *alt_req =
 182                (struct alternates_request *)callback_data;
 183        struct walker *walker = alt_req->walker;
 184        struct walker_data *cdata = walker->data;
 185        struct active_request_slot *slot = alt_req->slot;
 186        struct alt_base *tail = cdata->alt;
 187        const char *base = alt_req->base;
 188        const char null_byte = '\0';
 189        char *data;
 190        int i = 0;
 191
 192        if (alt_req->http_specific) {
 193                if (slot->curl_result != CURLE_OK ||
 194                    !alt_req->buffer->len) {
 195
 196                        /* Try reusing the slot to get non-http alternates */
 197                        alt_req->http_specific = 0;
 198                        strbuf_reset(alt_req->url);
 199                        strbuf_addf(alt_req->url, "%s/objects/info/alternates",
 200                                    base);
 201                        curl_easy_setopt(slot->curl, CURLOPT_URL,
 202                                         alt_req->url->buf);
 203                        active_requests++;
 204                        slot->in_use = 1;
 205                        if (slot->finished != NULL)
 206                                (*slot->finished) = 0;
 207                        if (!start_active_slot(slot)) {
 208                                cdata->got_alternates = -1;
 209                                slot->in_use = 0;
 210                                if (slot->finished != NULL)
 211                                        (*slot->finished) = 1;
 212                        }
 213                        return;
 214                }
 215        } else if (slot->curl_result != CURLE_OK) {
 216                if (!missing_target(slot)) {
 217                        cdata->got_alternates = -1;
 218                        return;
 219                }
 220        }
 221
 222        fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
 223        alt_req->buffer->len--;
 224        data = alt_req->buffer->buf;
 225
 226        while (i < alt_req->buffer->len) {
 227                int posn = i;
 228                while (posn < alt_req->buffer->len && data[posn] != '\n')
 229                        posn++;
 230                if (data[posn] == '\n') {
 231                        int okay = 0;
 232                        int serverlen = 0;
 233                        struct alt_base *newalt;
 234                        if (data[i] == '/') {
 235                                /*
 236                                 * This counts
 237                                 * http://git.host/pub/scm/linux.git/
 238                                 * -----------here^
 239                                 * so memcpy(dst, base, serverlen) will
 240                                 * copy up to "...git.host".
 241                                 */
 242                                const char *colon_ss = strstr(base,"://");
 243                                if (colon_ss) {
 244                                        serverlen = (strchr(colon_ss + 3, '/')
 245                                                     - base);
 246                                        okay = 1;
 247                                }
 248                        } else if (!memcmp(data + i, "../", 3)) {
 249                                /*
 250                                 * Relative URL; chop the corresponding
 251                                 * number of subpath from base (and ../
 252                                 * from data), and concatenate the result.
 253                                 *
 254                                 * The code first drops ../ from data, and
 255                                 * then drops one ../ from data and one path
 256                                 * from base.  IOW, one extra ../ is dropped
 257                                 * from data than path is dropped from base.
 258                                 *
 259                                 * This is not wrong.  The alternate in
 260                                 *     http://git.host/pub/scm/linux.git/
 261                                 * to borrow from
 262                                 *     http://git.host/pub/scm/linus.git/
 263                                 * is ../../linus.git/objects/.  You need
 264                                 * two ../../ to borrow from your direct
 265                                 * neighbour.
 266                                 */
 267                                i += 3;
 268                                serverlen = strlen(base);
 269                                while (i + 2 < posn &&
 270                                       !memcmp(data + i, "../", 3)) {
 271                                        do {
 272                                                serverlen--;
 273                                        } while (serverlen &&
 274                                                 base[serverlen - 1] != '/');
 275                                        i += 3;
 276                                }
 277                                /* If the server got removed, give up. */
 278                                okay = strchr(base, ':') - base + 3 <
 279                                       serverlen;
 280                        } else if (alt_req->http_specific) {
 281                                char *colon = strchr(data + i, ':');
 282                                char *slash = strchr(data + i, '/');
 283                                if (colon && slash && colon < data + posn &&
 284                                    slash < data + posn && colon < slash) {
 285                                        okay = 1;
 286                                }
 287                        }
 288                        /* skip "objects\n" at end */
 289                        if (okay) {
 290                                struct strbuf target = STRBUF_INIT;
 291                                strbuf_add(&target, base, serverlen);
 292                                strbuf_add(&target, data + i, posn - i - 7);
 293                                if (walker->get_verbosely)
 294                                        fprintf(stderr, "Also look at %s\n",
 295                                                target.buf);
 296                                newalt = xmalloc(sizeof(*newalt));
 297                                newalt->next = NULL;
 298                                newalt->base = strbuf_detach(&target, NULL);
 299                                newalt->got_indices = 0;
 300                                newalt->packs = NULL;
 301
 302                                while (tail->next != NULL)
 303                                        tail = tail->next;
 304                                tail->next = newalt;
 305                        }
 306                }
 307                i = posn + 1;
 308        }
 309
 310        cdata->got_alternates = 1;
 311}
 312
 313static void fetch_alternates(struct walker *walker, const char *base)
 314{
 315        struct strbuf buffer = STRBUF_INIT;
 316        struct strbuf url = STRBUF_INIT;
 317        struct active_request_slot *slot;
 318        struct alternates_request alt_req;
 319        struct walker_data *cdata = walker->data;
 320
 321        /*
 322         * If another request has already started fetching alternates,
 323         * wait for them to arrive and return to processing this request's
 324         * curl message
 325         */
 326#ifdef USE_CURL_MULTI
 327        while (cdata->got_alternates == 0) {
 328                step_active_slots();
 329        }
 330#endif
 331
 332        /* Nothing to do if they've already been fetched */
 333        if (cdata->got_alternates == 1)
 334                return;
 335
 336        /* Start the fetch */
 337        cdata->got_alternates = 0;
 338
 339        if (walker->get_verbosely)
 340                fprintf(stderr, "Getting alternates list for %s\n", base);
 341
 342        strbuf_addf(&url, "%s/objects/info/http-alternates", base);
 343
 344        /*
 345         * Use a callback to process the result, since another request
 346         * may fail and need to have alternates loaded before continuing
 347         */
 348        slot = get_active_slot();
 349        slot->callback_func = process_alternates_response;
 350        alt_req.walker = walker;
 351        slot->callback_data = &alt_req;
 352
 353        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 354        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 355        curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
 356
 357        alt_req.base = base;
 358        alt_req.url = &url;
 359        alt_req.buffer = &buffer;
 360        alt_req.http_specific = 1;
 361        alt_req.slot = slot;
 362
 363        if (start_active_slot(slot))
 364                run_active_slot(slot);
 365        else
 366                cdata->got_alternates = -1;
 367
 368        strbuf_release(&buffer);
 369        strbuf_release(&url);
 370}
 371
 372static int fetch_indices(struct walker *walker, struct alt_base *repo)
 373{
 374        int ret;
 375
 376        if (repo->got_indices)
 377                return 0;
 378
 379        if (walker->get_verbosely)
 380                fprintf(stderr, "Getting pack list for %s\n", repo->base);
 381
 382        switch (http_get_info_packs(repo->base, &repo->packs)) {
 383        case HTTP_OK:
 384        case HTTP_MISSING_TARGET:
 385                repo->got_indices = 1;
 386                ret = 0;
 387                break;
 388        default:
 389                repo->got_indices = 0;
 390                ret = -1;
 391        }
 392
 393        return ret;
 394}
 395
 396static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
 397{
 398        struct packed_git *target;
 399        int ret;
 400        struct slot_results results;
 401        struct http_pack_request *preq;
 402
 403        if (fetch_indices(walker, repo))
 404                return -1;
 405        target = find_sha1_pack(sha1, repo->packs);
 406        if (!target)
 407                return -1;
 408
 409        if (walker->get_verbosely) {
 410                fprintf(stderr, "Getting pack %s\n",
 411                        sha1_to_hex(target->sha1));
 412                fprintf(stderr, " which contains %s\n",
 413                        sha1_to_hex(sha1));
 414        }
 415
 416        preq = new_http_pack_request(target, repo->base);
 417        if (preq == NULL)
 418                goto abort;
 419        preq->lst = &repo->packs;
 420        preq->slot->results = &results;
 421
 422        if (start_active_slot(preq->slot)) {
 423                run_active_slot(preq->slot);
 424                if (results.curl_result != CURLE_OK) {
 425                        error("Unable to get pack file %s\n%s", preq->url,
 426                              curl_errorstr);
 427                        goto abort;
 428                }
 429        } else {
 430                error("Unable to start request");
 431                goto abort;
 432        }
 433
 434        ret = finish_http_pack_request(preq);
 435        release_http_pack_request(preq);
 436        if (ret)
 437                return ret;
 438
 439        return 0;
 440
 441abort:
 442        return -1;
 443}
 444
 445static void abort_object_request(struct object_request *obj_req)
 446{
 447        release_object_request(obj_req);
 448}
 449
 450static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
 451{
 452        char *hex = sha1_to_hex(sha1);
 453        int ret = 0;
 454        struct object_request *obj_req = object_queue_head;
 455        struct http_object_request *req;
 456
 457        while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
 458                obj_req = obj_req->next;
 459        if (obj_req == NULL)
 460                return error("Couldn't find request for %s in the queue", hex);
 461
 462        if (has_sha1_file(obj_req->sha1)) {
 463                if (obj_req->req != NULL)
 464                        abort_http_object_request(obj_req->req);
 465                abort_object_request(obj_req);
 466                return 0;
 467        }
 468
 469#ifdef USE_CURL_MULTI
 470        while (obj_req->state == WAITING)
 471                step_active_slots();
 472#else
 473        start_object_request(walker, obj_req);
 474#endif
 475
 476        /*
 477         * obj_req->req might change when fetching alternates in the callback
 478         * process_object_response; therefore, the "shortcut" variable, req,
 479         * is used only after we're done with slots.
 480         */
 481        while (obj_req->state == ACTIVE)
 482                run_active_slot(obj_req->req->slot);
 483
 484        req = obj_req->req;
 485
 486        if (req->localfile != -1) {
 487                close(req->localfile);
 488                req->localfile = -1;
 489        }
 490
 491        if (obj_req->state == ABORTED) {
 492                ret = error("Request for %s aborted", hex);
 493        } else if (req->curl_result != CURLE_OK &&
 494                   req->http_code != 416) {
 495                if (missing_target(req))
 496                        ret = -1; /* Be silent, it is probably in a pack. */
 497                else
 498                        ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
 499                                    req->errorstr, req->curl_result,
 500                                    req->http_code, hex);
 501        } else if (req->zret != Z_STREAM_END) {
 502                walker->corrupt_object_found++;
 503                ret = error("File %s (%s) corrupt", hex, req->url);
 504        } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
 505                ret = error("File %s has bad hash", hex);
 506        } else if (req->rename < 0) {
 507                ret = error("unable to write sha1 filename %s",
 508                            sha1_file_name(req->sha1));
 509        }
 510
 511        release_http_object_request(req);
 512        release_object_request(obj_req);
 513        return ret;
 514}
 515
 516static int fetch(struct walker *walker, unsigned char *sha1)
 517{
 518        struct walker_data *data = walker->data;
 519        struct alt_base *altbase = data->alt;
 520
 521        if (!fetch_object(walker, altbase, sha1))
 522                return 0;
 523        while (altbase) {
 524                if (!http_fetch_pack(walker, altbase, sha1))
 525                        return 0;
 526                fetch_alternates(walker, data->alt->base);
 527                altbase = altbase->next;
 528        }
 529        return error("Unable to find %s under %s", sha1_to_hex(sha1),
 530                     data->alt->base);
 531}
 532
 533static int fetch_ref(struct walker *walker, struct ref *ref)
 534{
 535        struct walker_data *data = walker->data;
 536        return http_fetch_ref(data->alt->base, ref);
 537}
 538
 539static void cleanup(struct walker *walker)
 540{
 541        struct walker_data *data = walker->data;
 542        struct alt_base *alt, *alt_next;
 543
 544        if (data) {
 545                alt = data->alt;
 546                while (alt) {
 547                        alt_next = alt->next;
 548
 549                        free(alt->base);
 550                        free(alt);
 551
 552                        alt = alt_next;
 553                }
 554                free(data);
 555                walker->data = NULL;
 556        }
 557}
 558
 559struct walker *get_http_walker(const char *url)
 560{
 561        char *s;
 562        struct walker_data *data = xmalloc(sizeof(struct walker_data));
 563        struct walker *walker = xmalloc(sizeof(struct walker));
 564
 565        data->alt = xmalloc(sizeof(*data->alt));
 566        data->alt->base = xstrdup(url);
 567        for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
 568                *s = 0;
 569
 570        data->alt->got_indices = 0;
 571        data->alt->packs = NULL;
 572        data->alt->next = NULL;
 573        data->got_alternates = -1;
 574
 575        walker->corrupt_object_found = 0;
 576        walker->fetch = fetch;
 577        walker->fetch_ref = fetch_ref;
 578        walker->prefetch = prefetch;
 579        walker->cleanup = cleanup;
 580        walker->data = data;
 581
 582#ifdef USE_CURL_MULTI
 583        add_fill_function(walker, (int (*)(void *)) fill_active_slot);
 584#endif
 585
 586        return walker;
 587}