[PATCH] Kill a bunch of pointer sign warnings for gcc4
[git/git.git] / http-pull.c
CommitLineData
6eb7ed54
DB
1#include <fcntl.h>
2#include <unistd.h>
3#include <string.h>
4#include <stdlib.h>
5#include "cache.h"
6#include "commit.h"
7#include <errno.h>
8#include <stdio.h>
9
4250a5e5
DB
10#include "pull.h"
11
6eb7ed54
DB
12#include <curl/curl.h>
13#include <curl/easy.h>
14
15static CURL *curl;
16
17static char *base;
18
6eb7ed54
DB
19static SHA_CTX c;
20static z_stream stream;
21
22static int local;
23static int zret;
24
25static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
26 void *data) {
bf0f910d 27 unsigned char expn[4096];
6eb7ed54
DB
28 size_t size = eltsize * nmemb;
29 int posn = 0;
30 do {
31 ssize_t retval = write(local, ptr + posn, size - posn);
32 if (retval < 0)
33 return posn;
34 posn += retval;
35 } while (posn < size);
36
37 stream.avail_in = size;
38 stream.next_in = ptr;
39 do {
40 stream.next_out = expn;
41 stream.avail_out = sizeof(expn);
42 zret = inflate(&stream, Z_SYNC_FLUSH);
43 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
44 } while (stream.avail_in && zret == Z_OK);
45 return size;
46}
47
4250a5e5 48int fetch(unsigned char *sha1)
6eb7ed54
DB
49{
50 char *hex = sha1_to_hex(sha1);
51 char *filename = sha1_file_name(sha1);
bf0f910d 52 unsigned char real_sha1[20];
6eb7ed54
DB
53 char *url;
54 char *posn;
55
6eb7ed54
DB
56 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
57
58 if (local < 0)
59 return error("Couldn't open %s\n", filename);
60
61 memset(&stream, 0, sizeof(stream));
62
63 inflateInit(&stream);
64
65 SHA1_Init(&c);
66
67 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
68 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
69
812666c8 70 url = xmalloc(strlen(base) + 50);
6eb7ed54
DB
71 strcpy(url, base);
72 posn = url + strlen(base);
73 strcpy(posn, "objects/");
74 posn += 8;
75 memcpy(posn, hex, 2);
76 posn += 2;
77 *(posn++) = '/';
78 strcpy(posn, hex + 2);
79
80 curl_easy_setopt(curl, CURLOPT_URL, url);
81
6eb7ed54
DB
82 if (curl_easy_perform(curl))
83 return error("Couldn't get %s for %s\n", url, hex);
84
85 close(local);
86 inflateEnd(&stream);
87 SHA1_Final(real_sha1, &c);
88 if (zret != Z_STREAM_END) {
89 unlink(filename);
90 return error("File %s (%s) corrupt\n", hex, url);
91 }
92 if (memcmp(sha1, real_sha1, 20)) {
93 unlink(filename);
94 return error("File %s has bad hash\n", hex);
95 }
96
e78d9772 97 pull_say("got %s\n", hex);
6eb7ed54
DB
98 return 0;
99}
100
6eb7ed54
DB
101int main(int argc, char **argv)
102{
103 char *commit_id;
104 char *url;
105 int arg = 1;
6eb7ed54
DB
106
107 while (arg < argc && argv[arg][0] == '-') {
108 if (argv[arg][1] == 't') {
4250a5e5 109 get_tree = 1;
6eb7ed54 110 } else if (argv[arg][1] == 'c') {
4250a5e5 111 get_history = 1;
6eb7ed54 112 } else if (argv[arg][1] == 'a') {
4250a5e5
DB
113 get_all = 1;
114 get_tree = 1;
115 get_history = 1;
e78d9772
JH
116 } else if (argv[arg][1] == 'v') {
117 get_verbosely = 1;
6eb7ed54
DB
118 }
119 arg++;
120 }
121 if (argc < arg + 2) {
e78d9772 122 usage("git-http-pull [-c] [-t] [-a] [-v] commit-id url");
6eb7ed54
DB
123 return 1;
124 }
125 commit_id = argv[arg];
126 url = argv[arg + 1];
127
6eb7ed54
DB
128 curl_global_init(CURL_GLOBAL_ALL);
129
130 curl = curl_easy_init();
131
132 base = url;
133
4250a5e5 134 if (pull(commit_id))
6eb7ed54
DB
135 return 1;
136
137 curl_global_cleanup();
138 return 0;
139}