char *url;
char *data;
int i = 0;
+ int http_specific = 1;
if (got_alternates)
return 0;
data = xmalloc(4096);
- buffer.size = 4096;
+ buffer.size = 4095;
buffer.posn = 0;
buffer.buffer = data;
curl_easy_setopt(curl, CURLOPT_URL, url);
if (curl_easy_perform(curl) || !buffer.posn) {
+ http_specific = 0;
+
sprintf(url, "%s/objects/info/alternates", base);
curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
}
}
+ data[buffer.posn] = '\0';
+
while (i < buffer.posn) {
int posn = i;
while (posn < buffer.posn && data[posn] != '\n')
posn++;
if (data[posn] == '\n') {
+ int okay = 0;
+ int serverlen = 0;
+ struct alt_base *newalt;
+ char *target = NULL;
if (data[i] == '/') {
- int serverlen = strchr(base + 8, '/') - base;
- // skip 'objects' at end
- char *target =
- xmalloc(serverlen + posn - i - 6);
- struct alt_base *newalt;
+ serverlen = strchr(base + 8, '/') - base;
+ okay = 1;
+ } else if (!memcmp(data + i, "../", 3)) {
+ i += 3;
+ serverlen = strlen(base);
+ while (i + 2 < posn &&
+ !memcmp(data + i, "../", 3)) {
+ do {
+ serverlen--;
+ } while (serverlen &&
+ base[serverlen - 1] != '/');
+ i += 3;
+ }
+ // If the server got removed, give up.
+ okay = strchr(base, ':') - base + 3 <
+ serverlen;
+ } else if (http_specific) {
+ char *colon = strchr(data + i, ':');
+ char *slash = strchr(data + i, '/');
+ if (colon && slash && colon < data + posn &&
+ slash < data + posn && colon < slash) {
+ okay = 1;
+ }
+ }
+ // skip 'objects' at end
+ if (okay) {
+ target = xmalloc(serverlen + posn - i - 6);
strncpy(target, base, serverlen);
strncpy(target + serverlen, data + i,
posn - i - 7);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
if (curl_easy_perform(curl)) {
- return error("Unable to get pack index %s", url);
+ return -1;
}
while (i < buffer.posn) {
char *hex = sha1_to_hex(sha1);
char *filename = sha1_file_name(sha1);
unsigned char real_sha1[20];
+ char tmpfile[PATH_MAX];
+ int ret;
char *url;
char *posn;
- local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
+ snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX",
+ get_object_directory());
+ local = mkstemp(tmpfile);
if (local < 0)
- return error("Couldn't open local object %s\n", filename);
+ return error("Couldn't create temporary file %s for %s: %s\n",
+ tmpfile, filename, strerror(errno));
memset(&stream, 0, sizeof(stream));
return -1;
}
+ fchmod(local, 0444);
close(local);
inflateEnd(&stream);
SHA1_Final(real_sha1, &c);
if (zret != Z_STREAM_END) {
- unlink(filename);
+ unlink(tmpfile);
return error("File %s (%s) corrupt\n", hex, url);
}
if (memcmp(sha1, real_sha1, 20)) {
- unlink(filename);
+ unlink(tmpfile);
return error("File %s has bad hash\n", hex);
}
-
+ ret = link(tmpfile, filename);
+ if (ret < 0) {
+ /* Same Coda hack as in write_sha1_file(sha1_file.c) */
+ ret = errno;
+ if (ret == EXDEV && !rename(tmpfile, filename))
+ goto out;
+ }
+ unlink(tmpfile);
+ if (ret) {
+ if (ret != EEXIST)
+ return error("unable to write sha1 filename %s: %s",
+ filename, strerror(ret));
+ }
+ out:
pull_say("got %s\n", hex);
return 0;
}