Code

sscanf/strtoul: parse integers robustly
[git.git] / http-fetch.c
index 396552da022a1dca9c43738be4d87c664f31a06c..09baedc18ae320a2f09fa61d9065bedb69c02a41 100644 (file)
@@ -16,8 +16,7 @@ static struct curl_slist *no_pragma_header;
 
 struct alt_base
 {
 
 struct alt_base
 {
-       const char *base;
-       int path_len;
+       char *base;
        int got_indices;
        struct packed_git *packs;
        struct alt_base *next;
        int got_indices;
        struct packed_git *packs;
        struct alt_base *next;
@@ -71,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 {
        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;
                                       (char *) ptr + posn, size - posn);
                if (retval < 0)
                        return posn;
@@ -158,12 +157,12 @@ static void start_object_request(struct object_request *obj_req)
 
        SHA1_Init(&obj_req->c);
 
 
        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(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++) = '/';
        memcpy(posn, hex, 2);
        posn += 2;
        *(posn++) = '/';
@@ -175,7 +174,7 @@ static void start_object_request(struct object_request *obj_req)
        prevlocal = open(prevfile, O_RDONLY);
        if (prevlocal != -1) {
                do {
        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,
                        if (prev_read>0) {
                                if (fwrite_sha1_file(prev_buf,
                                                     1,
@@ -199,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;
                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);
                }
        }
                        ftruncate(obj_req->local, 0);
                }
        }
@@ -515,7 +514,6 @@ static void process_alternates_response(void *callback_data)
                        int serverlen = 0;
                        struct alt_base *newalt;
                        char *target = NULL;
                        int serverlen = 0;
                        struct alt_base *newalt;
                        char *target = NULL;
-                       char *path;
                        if (data[i] == '/') {
                                /* This counts
                                 * http://git.host/pub/scm/linux.git/
                        if (data[i] == '/') {
                                /* This counts
                                 * http://git.host/pub/scm/linux.git/
@@ -583,12 +581,6 @@ static void process_alternates_response(void *callback_data)
                                newalt->base = target;
                                newalt->got_indices = 0;
                                newalt->packs = NULL;
                                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;
 
                                while (tail->next != NULL)
                                        tail = tail->next;
@@ -717,8 +709,8 @@ static int fetch_indices(struct alt_base *repo)
                case 'P':
                        i++;
                        if (i + 52 <= buffer.posn &&
                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;
                                get_sha1_hex(data + i + 6, sha1);
                                setup_index(repo, sha1);
                                i += 51;
@@ -809,6 +801,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
                return error("Unable to start request");
        }
 
                return error("Unable to start request");
        }
 
+       target->pack_size = ftell(packfile);
        fclose(packfile);
 
        ret = move_temp_to_file(tmpfile, filename);
        fclose(packfile);
 
        ret = move_temp_to_file(tmpfile, filename);
@@ -937,14 +930,14 @@ static char *quote_ref_url(const char *base, const char *ref)
        int len, baselen, ch;
 
        baselen = strlen(base);
        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);
        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);
                if (needs_quote(ch)) {
                        *dp++ = '%';
                        *dp++ = hex((ch >> 4) & 0xF);
@@ -998,11 +991,10 @@ int main(int argc, const char **argv)
        const char **write_ref = NULL;
        char **commit_id;
        const char *url;
        const char **write_ref = NULL;
        char **commit_id;
        const char *url;
-       char *path;
+       char *s;
        int arg = 1;
        int rc = 0;
 
        int arg = 1;
        int rc = 0;
 
-       setup_ident();
        setup_git_directory();
        git_config(git_default_config);
 
        setup_git_directory();
        git_config(git_default_config);
 
@@ -1044,16 +1036,13 @@ int main(int argc, const char **argv)
        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
 
        alt = xmalloc(sizeof(*alt));
        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;
        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;
 
        if (pull(commits, commit_id, write_ref, url))
                rc = 1;
@@ -1069,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"
                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;
 }
        }
        return rc;
 }