Define a built-in attribute macro "binary".
[gitweb.git] / http-fetch.c
index 259292d4de4959d29abad60f0c9cbd55e4b9b6d3..09baedc18ae320a2f09fa61d9065bedb69c02a41 100644 (file)
@@ -4,55 +4,25 @@
 #include "fetch.h"
 #include "http.h"
 
-#ifndef NO_EXPAT
-#include <expat.h>
-
-/* Definitions for DAV requests */
-#define DAV_PROPFIND "PROPFIND"
-#define DAV_PROPFIND_RESP ".multistatus.response"
-#define DAV_PROPFIND_NAME ".multistatus.response.href"
-#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
-#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
-
-/* Definitions for processing XML DAV responses */
-#ifndef XML_STATUS_OK
-enum XML_Status {
-  XML_STATUS_OK = 1,
-  XML_STATUS_ERROR = 0
-};
-#define XML_STATUS_OK    1
-#define XML_STATUS_ERROR 0
-#endif
-
-/* Flags that control remote_ls processing */
-#define PROCESS_FILES (1u << 0)
-#define PROCESS_DIRS  (1u << 1)
-#define RECURSIVE     (1u << 2)
-
-/* Flags that remote_ls passes to callback functions */
-#define IS_DIR (1u << 0)
-#endif
-
 #define PREV_BUF_SIZE 4096
 #define RANGE_HEADER_SIZE 30
 
-static int commits_on_stdin = 0;
+static int commits_on_stdin;
 
 static int got_alternates = -1;
-static int corrupt_object_found = 0;
+static int corrupt_object_found;
 
 static struct curl_slist *no_pragma_header;
 
 struct alt_base
 {
-       const char *base;
-       int path_len;
+       char *base;
        int got_indices;
        struct packed_git *packs;
        struct alt_base *next;
 };
 
-static struct alt_base *alt = NULL;
+static struct alt_base *alt;
 
 enum object_request_state {
        WAITING,
@@ -90,31 +60,7 @@ struct alternates_request {
        int http_specific;
 };
 
-#ifndef NO_EXPAT
-struct xml_ctx
-{
-       char *name;
-       int len;
-       char *cdata;
-       void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
-       void *userData;
-};
-
-struct remote_ls_ctx
-{
-       struct alt_base *repo;
-       char *path;
-       void (*userFunc)(struct remote_ls_ctx *ls);
-       void *userData;
-       int flags;
-       char *dentry_name;
-       int dentry_flags;
-       int rc;
-       struct remote_ls_ctx *parent;
-};
-#endif
-
-static struct object_request *object_queue_head = NULL;
+static struct object_request *object_queue_head;
 
 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
                               void *data)
@@ -124,7 +70,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
        int posn = 0;
        struct object_request *obj_req = (struct object_request *)data;
        do {
-               ssize_t retval = write(obj_req->local,
+               ssize_t retval = xwrite(obj_req->local,
                                       (char *) ptr + posn, size - posn);
                if (retval < 0)
                        return posn;
@@ -144,6 +90,19 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
        return size;
 }
 
+static int missing__target(int code, int result)
+{
+       return  /* file:// URL -- do we ever use one??? */
+               (result == CURLE_FILE_COULDNT_READ_FILE) ||
+               /* http:// and https:// URL */
+               (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
+               /* ftp:// URL */
+               (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
+               ;
+}
+
+#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
+
 static void fetch_alternates(const char *base);
 
 static void process_object_response(void *callback_data);
@@ -198,12 +157,12 @@ static void start_object_request(struct object_request *obj_req)
 
        SHA1_Init(&obj_req->c);
 
-       url = xmalloc(strlen(obj_req->repo->base) + 50);
-       obj_req->url = xmalloc(strlen(obj_req->repo->base) + 50);
+       url = xmalloc(strlen(obj_req->repo->base) + 51);
+       obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51);
        strcpy(url, obj_req->repo->base);
        posn = url + strlen(obj_req->repo->base);
-       strcpy(posn, "objects/");
-       posn += 8;
+       strcpy(posn, "/objects/");
+       posn += 9;
        memcpy(posn, hex, 2);
        posn += 2;
        *(posn++) = '/';
@@ -215,7 +174,7 @@ static void start_object_request(struct object_request *obj_req)
        prevlocal = open(prevfile, O_RDONLY);
        if (prevlocal != -1) {
                do {
-                       prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
+                       prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
                        if (prev_read>0) {
                                if (fwrite_sha1_file(prev_buf,
                                                     1,
@@ -239,7 +198,7 @@ static void start_object_request(struct object_request *obj_req)
                SHA1_Init(&obj_req->c);
                if (prev_posn>0) {
                        prev_posn = 0;
-                       lseek(obj_req->local, SEEK_SET, 0);
+                       lseek(obj_req->local, 0, SEEK_SET);
                        ftruncate(obj_req->local, 0);
                }
        }
@@ -301,7 +260,7 @@ static void finish_object_request(struct object_request *obj_req)
                unlink(obj_req->tmpfile);
                return;
        }
-       if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
+       if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
                unlink(obj_req->tmpfile);
                return;
        }
@@ -323,8 +282,7 @@ static void process_object_response(void *callback_data)
        obj_req->state = COMPLETE;
 
        /* Use alternates if necessary */
-       if (obj_req->http_code == 404 ||
-           obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
+       if (missing_target(obj_req)) {
                fetch_alternates(alt->base);
                if (obj_req->repo->next != NULL) {
                        obj_req->repo =
@@ -393,7 +351,7 @@ void prefetch(unsigned char *sha1)
        char *filename = sha1_file_name(sha1);
 
        newreq = xmalloc(sizeof(*newreq));
-       memcpy(newreq->sha1, sha1, 20);
+       hashcpy(newreq->sha1, sha1);
        newreq->repo = alt;
        newreq->url = NULL;
        newreq->local = -1;
@@ -537,8 +495,7 @@ static void process_alternates_response(void *callback_data)
                        return;
                }
        } else if (slot->curl_result != CURLE_OK) {
-               if (slot->http_code != 404 &&
-                   slot->curl_result != CURLE_FILE_COULDNT_READ_FILE) {
+               if (!missing_target(slot)) {
                        got_alternates = -1;
                        return;
                }
@@ -557,7 +514,6 @@ static void process_alternates_response(void *callback_data)
                        int serverlen = 0;
                        struct alt_base *newalt;
                        char *target = NULL;
-                       char *path;
                        if (data[i] == '/') {
                                /* This counts
                                 * http://git.host/pub/scm/linux.git/
@@ -625,12 +581,6 @@ static void process_alternates_response(void *callback_data)
                                newalt->base = target;
                                newalt->got_indices = 0;
                                newalt->packs = NULL;
-                               path = strstr(target, "//");
-                               if (path) {
-                                       path = strchr(path+2, '/');
-                                       if (path)
-                                               newalt->path_len = strlen(path);
-                               }
 
                                while (tail->next != NULL)
                                        tail = tail->next;
@@ -703,209 +653,6 @@ static void fetch_alternates(const char *base)
        free(url);
 }
 
-#ifndef NO_EXPAT
-static void
-xml_start_tag(void *userData, const char *name, const char **atts)
-{
-       struct xml_ctx *ctx = (struct xml_ctx *)userData;
-       const char *c = strchr(name, ':');
-       int new_len;
-
-       if (c == NULL)
-               c = name;
-       else
-               c++;
-
-       new_len = strlen(ctx->name) + strlen(c) + 2;
-
-       if (new_len > ctx->len) {
-               ctx->name = xrealloc(ctx->name, new_len);
-               ctx->len = new_len;
-       }
-       strcat(ctx->name, ".");
-       strcat(ctx->name, c);
-
-       if (ctx->cdata) {
-               free(ctx->cdata);
-               ctx->cdata = NULL;
-       }
-
-       ctx->userFunc(ctx, 0);
-}
-
-static void
-xml_end_tag(void *userData, const char *name)
-{
-       struct xml_ctx *ctx = (struct xml_ctx *)userData;
-       const char *c = strchr(name, ':');
-       char *ep;
-
-       ctx->userFunc(ctx, 1);
-
-       if (c == NULL)
-               c = name;
-       else
-               c++;
-
-       ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
-       *ep = 0;
-}
-
-static void
-xml_cdata(void *userData, const XML_Char *s, int len)
-{
-       struct xml_ctx *ctx = (struct xml_ctx *)userData;
-       if (ctx->cdata)
-               free(ctx->cdata);
-       ctx->cdata = xmalloc(len + 1);
-       strlcpy(ctx->cdata, s, len + 1);
-}
-
-static int remote_ls(struct alt_base *repo, const char *path, int flags,
-                    void (*userFunc)(struct remote_ls_ctx *ls),
-                    void *userData);
-
-static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
-{
-       struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
-
-       if (tag_closed) {
-               if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
-                       if (ls->dentry_flags & IS_DIR) {
-                               if (ls->flags & PROCESS_DIRS) {
-                                       ls->userFunc(ls);
-                               }
-                               if (strcmp(ls->dentry_name, ls->path) &&
-                                   ls->flags & RECURSIVE) {
-                                       ls->rc = remote_ls(ls->repo,
-                                                          ls->dentry_name,
-                                                          ls->flags,
-                                                          ls->userFunc,
-                                                          ls->userData);
-                               }
-                       } else if (ls->flags & PROCESS_FILES) {
-                               ls->userFunc(ls);
-                       }
-               } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
-                       ls->dentry_name = xmalloc(strlen(ctx->cdata) -
-                                                 ls->repo->path_len + 1);
-                       strcpy(ls->dentry_name, ctx->cdata + ls->repo->path_len);
-               } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
-                       ls->dentry_flags |= IS_DIR;
-               }
-       } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
-               if (ls->dentry_name) {
-                       free(ls->dentry_name);
-               }
-               ls->dentry_name = NULL;
-               ls->dentry_flags = 0;
-       }
-}
-
-static int remote_ls(struct alt_base *repo, const char *path, int flags,
-                    void (*userFunc)(struct remote_ls_ctx *ls),
-                    void *userData)
-{
-       char *url = xmalloc(strlen(repo->base) + strlen(path) + 1);
-       struct active_request_slot *slot;
-       struct slot_results results;
-       struct buffer in_buffer;
-       struct buffer out_buffer;
-       char *in_data;
-       char *out_data;
-       XML_Parser parser = XML_ParserCreate(NULL);
-       enum XML_Status result;
-       struct curl_slist *dav_headers = NULL;
-       struct xml_ctx ctx;
-       struct remote_ls_ctx ls;
-
-       ls.flags = flags;
-       ls.repo = repo;
-       ls.path = strdup(path);
-       ls.dentry_name = NULL;
-       ls.dentry_flags = 0;
-       ls.userData = userData;
-       ls.userFunc = userFunc;
-       ls.rc = 0;
-
-       sprintf(url, "%s%s", repo->base, path);
-
-       out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
-       out_data = xmalloc(out_buffer.size + 1);
-       snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
-       out_buffer.posn = 0;
-       out_buffer.buffer = out_data;
-
-       in_buffer.size = 4096;
-       in_data = xmalloc(in_buffer.size);
-       in_buffer.posn = 0;
-       in_buffer.buffer = in_data;
-
-       dav_headers = curl_slist_append(dav_headers, "Depth: 1");
-       dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
-
-       slot = get_active_slot();
-       slot->results = &results;
-       curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
-
-       if (start_active_slot(slot)) {
-               run_active_slot(slot);
-               if (results.curl_result == CURLE_OK) {
-                       ctx.name = xcalloc(10, 1);
-                       ctx.len = 0;
-                       ctx.cdata = NULL;
-                       ctx.userFunc = handle_remote_ls_ctx;
-                       ctx.userData = &ls;
-                       XML_SetUserData(parser, &ctx);
-                       XML_SetElementHandler(parser, xml_start_tag,
-                                             xml_end_tag);
-                       XML_SetCharacterDataHandler(parser, xml_cdata);
-                       result = XML_Parse(parser, in_buffer.buffer,
-                                          in_buffer.posn, 1);
-                       free(ctx.name);
-
-                       if (result != XML_STATUS_OK) {
-                               ls.rc = error("XML error: %s",
-                                             XML_ErrorString(
-                                                     XML_GetErrorCode(parser)));
-                       }
-               } else {
-                       ls.rc = -1;
-               }
-       } else {
-               ls.rc = error("Unable to start PROPFIND request");
-       }
-
-       free(ls.path);
-       free(url);
-       free(out_data);
-       free(in_buffer.buffer);
-       curl_slist_free_all(dav_headers);
-
-       return ls.rc;
-}
-
-static void process_ls_pack(struct remote_ls_ctx *ls)
-{
-       unsigned char sha1[20];
-
-       if (strlen(ls->dentry_name) == 63 &&
-           !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
-           has_extension(ls->dentry_name, ".pack")) {
-               get_sha1_hex(ls->dentry_name + 18, sha1);
-               setup_index(ls->repo, sha1);
-       }
-}
-#endif
-
 static int fetch_indices(struct alt_base *repo)
 {
        unsigned char sha1[20];
@@ -928,12 +675,6 @@ static int fetch_indices(struct alt_base *repo)
        if (get_verbosely)
                fprintf(stderr, "Getting pack list for %s\n", repo->base);
 
-#ifndef NO_EXPAT
-       if (remote_ls(repo, "objects/pack/", PROCESS_FILES,
-                     process_ls_pack, NULL) == 0)
-               return 0;
-#endif
-
        url = xmalloc(strlen(repo->base) + 21);
        sprintf(url, "%s/objects/info/packs", repo->base);
 
@@ -946,8 +687,7 @@ static int fetch_indices(struct alt_base *repo)
        if (start_active_slot(slot)) {
                run_active_slot(slot);
                if (results.curl_result != CURLE_OK) {
-                       if (results.http_code == 404 ||
-                           results.curl_result == CURLE_FILE_COULDNT_READ_FILE) {
+                       if (missing_target(&results)) {
                                repo->got_indices = 1;
                                free(buffer.buffer);
                                return 0;
@@ -969,8 +709,8 @@ static int fetch_indices(struct alt_base *repo)
                case 'P':
                        i++;
                        if (i + 52 <= buffer.posn &&
-                           !strncmp(data + i, " pack-", 6) &&
-                           !strncmp(data + i + 46, ".pack\n", 6)) {
+                           !prefixcmp(data + i, " pack-") &&
+                           !prefixcmp(data + i + 46, ".pack\n")) {
                                get_sha1_hex(data + i + 6, sha1);
                                setup_index(repo, sha1);
                                i += 51;
@@ -1061,6 +801,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
                return error("Unable to start request");
        }
 
+       target->pack_size = ftell(packfile);
        fclose(packfile);
 
        ret = move_temp_to_file(tmpfile, filename);
@@ -1099,7 +840,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
        int ret = 0;
        struct object_request *obj_req = object_queue_head;
 
-       while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
+       while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
                obj_req = obj_req->next;
        if (obj_req == NULL)
                return error("Couldn't find request for %s in the queue", hex);
@@ -1128,8 +869,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
                ret = error("Request for %s aborted", hex);
        } else if (obj_req->curl_result != CURLE_OK &&
                   obj_req->http_code != 416) {
-               if (obj_req->http_code == 404 ||
-                   obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE)
+               if (missing_target(obj_req))
                        ret = -1; /* Be silent, it is probably in a pack. */
                else
                        ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
@@ -1138,7 +878,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
        } else if (obj_req->zret != Z_STREAM_END) {
                corrupt_object_found++;
                ret = error("File %s (%s) corrupt", hex, obj_req->url);
-       } else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
+       } else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
                ret = error("File %s has bad hash", hex);
        } else if (obj_req->rename < 0) {
                ret = error("unable to write sha1 filename %s",
@@ -1190,14 +930,14 @@ static char *quote_ref_url(const char *base, const char *ref)
        int len, baselen, ch;
 
        baselen = strlen(base);
-       len = baselen + 6; /* "refs/" + NUL */
+       len = baselen + 7; /* "/refs/" + NUL */
        for (cp = ref; (ch = *cp) != 0; cp++, len++)
                if (needs_quote(ch))
                        len += 2; /* extra two hex plus replacement % */
        qref = xmalloc(len);
        memcpy(qref, base, baselen);
-       memcpy(qref + baselen, "refs/", 5);
-       for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
+       memcpy(qref + baselen, "/refs/", 6);
+       for (cp = ref, dp = qref + baselen + 6; (ch = *cp) != 0; cp++) {
                if (needs_quote(ch)) {
                        *dp++ = '%';
                        *dp++ = hex((ch >> 4) & 0xF);
@@ -1251,11 +991,10 @@ int main(int argc, const char **argv)
        const char **write_ref = NULL;
        char **commit_id;
        const char *url;
-       char *path;
+       char *s;
        int arg = 1;
        int rc = 0;
 
-       setup_ident();
        setup_git_directory();
        git_config(git_default_config);
 
@@ -1297,16 +1036,13 @@ int main(int argc, const char **argv)
        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
 
        alt = xmalloc(sizeof(*alt));
-       alt->base = url;
+       alt->base = xmalloc(strlen(url) + 1);
+       strcpy(alt->base, url);
+       for (s = alt->base + strlen(alt->base) - 1; *s == '/'; --s)
+               *s = 0;
        alt->got_indices = 0;
        alt->packs = NULL;
        alt->next = NULL;
-       path = strstr(url, "//");
-       if (path) {
-               path = strchr(path+2, '/');
-               if (path)
-                       alt->path_len = strlen(path);
-       }
 
        if (pull(commits, commit_id, write_ref, url))
                rc = 1;
@@ -1322,7 +1058,7 @@ int main(int argc, const char **argv)
                fprintf(stderr,
 "Some loose object were found to be corrupt, but they might be just\n"
 "a false '404 Not Found' error message sent with incorrect HTTP\n"
-"status code.  Suggest running git fsck-objects.\n");
+"status code.  Suggest running git-fsck.\n");
        }
        return rc;
 }