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