1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-20 04:06:09 +02:00

Make http-fetch request types more clear

Rename object request functions and data to make it more clear which type
of request is being processed - this is a response to the introduction of
slot callbacks and the definition of different types of requests such as
alternates_request.

Signed-off-by: Nick Hengeveld <nickh@reactrix.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Nick Hengeveld 2005-11-18 11:03:04 -08:00 committed by Junio C Hamano
parent 29508e1efb
commit e388ab74db

View File

@ -21,14 +21,14 @@ struct alt_base
static struct alt_base *alt = NULL; static struct alt_base *alt = NULL;
enum transfer_state { enum object_request_state {
WAITING, WAITING,
ABORTED, ABORTED,
ACTIVE, ACTIVE,
COMPLETE, COMPLETE,
}; };
struct transfer_request struct object_request
{ {
unsigned char sha1[20]; unsigned char sha1[20];
struct alt_base *repo; struct alt_base *repo;
@ -36,7 +36,7 @@ struct transfer_request
char filename[PATH_MAX]; char filename[PATH_MAX];
char tmpfile[PATH_MAX]; char tmpfile[PATH_MAX];
int local; int local;
enum transfer_state state; enum object_request_state state;
CURLcode curl_result; CURLcode curl_result;
char errorstr[CURL_ERROR_SIZE]; char errorstr[CURL_ERROR_SIZE];
long http_code; long http_code;
@ -46,10 +46,10 @@ struct transfer_request
int zret; int zret;
int rename; int rename;
struct active_request_slot *slot; struct active_request_slot *slot;
struct transfer_request *next; struct object_request *next;
}; };
struct alt_request { struct alternates_request {
char *base; char *base;
char *url; char *url;
struct buffer *buffer; struct buffer *buffer;
@ -57,7 +57,7 @@ struct alt_request {
int http_specific; int http_specific;
}; };
static struct transfer_request *request_queue_head = NULL; static struct object_request *object_queue_head = NULL;
static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
void *data) void *data)
@ -65,24 +65,24 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
unsigned char expn[4096]; unsigned char expn[4096];
size_t size = eltsize * nmemb; size_t size = eltsize * nmemb;
int posn = 0; int posn = 0;
struct transfer_request *request = (struct transfer_request *)data; struct object_request *obj_req = (struct object_request *)data;
do { do {
ssize_t retval = write(request->local, ssize_t retval = write(obj_req->local,
ptr + posn, size - posn); ptr + posn, size - posn);
if (retval < 0) if (retval < 0)
return posn; return posn;
posn += retval; posn += retval;
} while (posn < size); } while (posn < size);
request->stream.avail_in = size; obj_req->stream.avail_in = size;
request->stream.next_in = ptr; obj_req->stream.next_in = ptr;
do { do {
request->stream.next_out = expn; obj_req->stream.next_out = expn;
request->stream.avail_out = sizeof(expn); obj_req->stream.avail_out = sizeof(expn);
request->zret = inflate(&request->stream, Z_SYNC_FLUSH); obj_req->zret = inflate(&obj_req->stream, Z_SYNC_FLUSH);
SHA1_Update(&request->c, expn, SHA1_Update(&obj_req->c, expn,
sizeof(expn) - request->stream.avail_out); sizeof(expn) - obj_req->stream.avail_out);
} while (request->stream.avail_in && request->zret == Z_OK); } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
data_received++; data_received++;
return size; return size;
} }
@ -91,9 +91,9 @@ static void fetch_alternates(char *base);
static void process_object_response(void *callback_data); static void process_object_response(void *callback_data);
static void start_request(struct transfer_request *request) static void start_object_request(struct object_request *obj_req)
{ {
char *hex = sha1_to_hex(request->sha1); char *hex = sha1_to_hex(obj_req->sha1);
char prevfile[PATH_MAX]; char prevfile[PATH_MAX];
char *url; char *url;
char *posn; char *posn;
@ -105,53 +105,53 @@ static void start_request(struct transfer_request *request)
struct curl_slist *range_header = NULL; struct curl_slist *range_header = NULL;
struct active_request_slot *slot; struct active_request_slot *slot;
snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename); snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
unlink(prevfile); unlink(prevfile);
rename(request->tmpfile, prevfile); rename(obj_req->tmpfile, prevfile);
unlink(request->tmpfile); unlink(obj_req->tmpfile);
if (request->local != -1) if (obj_req->local != -1)
error("fd leakage in start: %d", request->local); error("fd leakage in start: %d", obj_req->local);
request->local = open(request->tmpfile, obj_req->local = open(obj_req->tmpfile,
O_WRONLY | O_CREAT | O_EXCL, 0666); O_WRONLY | O_CREAT | O_EXCL, 0666);
/* This could have failed due to the "lazy directory creation"; /* This could have failed due to the "lazy directory creation";
* try to mkdir the last path component. * try to mkdir the last path component.
*/ */
if (request->local < 0 && errno == ENOENT) { if (obj_req->local < 0 && errno == ENOENT) {
char *dir = strrchr(request->tmpfile, '/'); char *dir = strrchr(obj_req->tmpfile, '/');
if (dir) { if (dir) {
*dir = 0; *dir = 0;
mkdir(request->tmpfile, 0777); mkdir(obj_req->tmpfile, 0777);
*dir = '/'; *dir = '/';
} }
request->local = open(request->tmpfile, obj_req->local = open(obj_req->tmpfile,
O_WRONLY | O_CREAT | O_EXCL, 0666); O_WRONLY | O_CREAT | O_EXCL, 0666);
} }
if (request->local < 0) { if (obj_req->local < 0) {
request->state = ABORTED; obj_req->state = ABORTED;
error("Couldn't create temporary file %s for %s: %s\n", error("Couldn't create temporary file %s for %s: %s\n",
request->tmpfile, request->filename, strerror(errno)); obj_req->tmpfile, obj_req->filename, strerror(errno));
return; return;
} }
memset(&request->stream, 0, sizeof(request->stream)); memset(&obj_req->stream, 0, sizeof(obj_req->stream));
inflateInit(&request->stream); inflateInit(&obj_req->stream);
SHA1_Init(&request->c); SHA1_Init(&obj_req->c);
url = xmalloc(strlen(request->repo->base) + 50); url = xmalloc(strlen(obj_req->repo->base) + 50);
request->url = xmalloc(strlen(request->repo->base) + 50); obj_req->url = xmalloc(strlen(obj_req->repo->base) + 50);
strcpy(url, request->repo->base); strcpy(url, obj_req->repo->base);
posn = url + strlen(request->repo->base); posn = url + strlen(obj_req->repo->base);
strcpy(posn, "objects/"); strcpy(posn, "objects/");
posn += 8; posn += 8;
memcpy(posn, hex, 2); memcpy(posn, hex, 2);
posn += 2; posn += 2;
*(posn++) = '/'; *(posn++) = '/';
strcpy(posn, hex + 2); strcpy(posn, hex + 2);
strcpy(request->url, url); strcpy(obj_req->url, url);
/* If a previous temp file is present, process what was already /* If a previous temp file is present, process what was already
fetched. */ fetched. */
@ -163,7 +163,7 @@ static void start_request(struct transfer_request *request)
if (fwrite_sha1_file(prev_buf, if (fwrite_sha1_file(prev_buf,
1, 1,
prev_read, prev_read,
request) == prev_read) { obj_req) == prev_read) {
prev_posn += prev_read; prev_posn += prev_read;
} else { } else {
prev_read = -1; prev_read = -1;
@ -177,24 +177,24 @@ static void start_request(struct transfer_request *request)
/* Reset inflate/SHA1 if there was an error reading the previous temp /* Reset inflate/SHA1 if there was an error reading the previous temp
file; also rewind to the beginning of the local file. */ file; also rewind to the beginning of the local file. */
if (prev_read == -1) { if (prev_read == -1) {
memset(&request->stream, 0, sizeof(request->stream)); memset(&obj_req->stream, 0, sizeof(obj_req->stream));
inflateInit(&request->stream); inflateInit(&obj_req->stream);
SHA1_Init(&request->c); SHA1_Init(&obj_req->c);
if (prev_posn>0) { if (prev_posn>0) {
prev_posn = 0; prev_posn = 0;
lseek(request->local, SEEK_SET, 0); lseek(obj_req->local, SEEK_SET, 0);
ftruncate(request->local, 0); ftruncate(obj_req->local, 0);
} }
} }
slot = get_active_slot(); slot = get_active_slot();
slot->callback_func = process_object_response; slot->callback_func = process_object_response;
slot->callback_data = request; slot->callback_data = obj_req;
request->slot = slot; obj_req->slot = slot;
curl_easy_setopt(slot->curl, CURLOPT_FILE, request); curl_easy_setopt(slot->curl, CURLOPT_FILE, obj_req);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr); curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr);
curl_easy_setopt(slot->curl, CURLOPT_URL, url); curl_easy_setopt(slot->curl, CURLOPT_URL, url);
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
@ -212,109 +212,111 @@ static void start_request(struct transfer_request *request)
} }
/* Try to get the request started, abort the request on error */ /* Try to get the request started, abort the request on error */
request->state = ACTIVE; obj_req->state = ACTIVE;
if (!start_active_slot(slot)) { if (!start_active_slot(slot)) {
request->state = ABORTED; obj_req->state = ABORTED;
request->slot = NULL; obj_req->slot = NULL;
close(request->local); request->local = -1; close(obj_req->local); obj_req->local = -1;
free(request->url); free(obj_req->url);
return;
} }
} }
static void finish_request(struct transfer_request *request) static void finish_object_request(struct object_request *obj_req)
{ {
struct stat st; struct stat st;
fchmod(request->local, 0444); fchmod(obj_req->local, 0444);
close(request->local); request->local = -1; close(obj_req->local); obj_req->local = -1;
if (request->http_code == 416) { if (obj_req->http_code == 416) {
fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n"); fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
} else if (request->curl_result != CURLE_OK) { } else if (obj_req->curl_result != CURLE_OK) {
if (stat(request->tmpfile, &st) == 0) if (stat(obj_req->tmpfile, &st) == 0)
if (st.st_size == 0) if (st.st_size == 0)
unlink(request->tmpfile); unlink(obj_req->tmpfile);
return; return;
} }
inflateEnd(&request->stream); inflateEnd(&obj_req->stream);
SHA1_Final(request->real_sha1, &request->c); SHA1_Final(obj_req->real_sha1, &obj_req->c);
if (request->zret != Z_STREAM_END) { if (obj_req->zret != Z_STREAM_END) {
unlink(request->tmpfile); unlink(obj_req->tmpfile);
return; return;
} }
if (memcmp(request->sha1, request->real_sha1, 20)) { if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
unlink(request->tmpfile); unlink(obj_req->tmpfile);
return; return;
} }
request->rename = obj_req->rename =
move_temp_to_file(request->tmpfile, request->filename); move_temp_to_file(obj_req->tmpfile, obj_req->filename);
if (request->rename == 0) if (obj_req->rename == 0)
pull_say("got %s\n", sha1_to_hex(request->sha1)); pull_say("got %s\n", sha1_to_hex(obj_req->sha1));
} }
static void process_object_response(void *callback_data) static void process_object_response(void *callback_data)
{ {
struct transfer_request *request = struct object_request *obj_req =
(struct transfer_request *)callback_data; (struct object_request *)callback_data;
request->curl_result = request->slot->curl_result; obj_req->curl_result = obj_req->slot->curl_result;
request->http_code = request->slot->http_code; obj_req->http_code = obj_req->slot->http_code;
request->slot = NULL; obj_req->slot = NULL;
request->state = COMPLETE; obj_req->state = COMPLETE;
/* Use alternates if necessary */ /* Use alternates if necessary */
if (request->http_code == 404) { if (obj_req->http_code == 404) {
fetch_alternates(alt->base); fetch_alternates(alt->base);
if (request->repo->next != NULL) { if (obj_req->repo->next != NULL) {
request->repo = obj_req->repo =
request->repo->next; obj_req->repo->next;
close(request->local); close(obj_req->local);
request->local = -1; obj_req->local = -1;
start_request(request); start_object_request(obj_req);
return; return;
} }
} }
finish_request(request); finish_object_request(obj_req);
} }
static void release_request(struct transfer_request *request) static void release_object_request(struct object_request *obj_req)
{ {
struct transfer_request *entry = request_queue_head; struct object_request *entry = object_queue_head;
if (request->local != -1) if (obj_req->local != -1)
error("fd leakage in release: %d", request->local); error("fd leakage in release: %d", obj_req->local);
if (request == request_queue_head) { if (obj_req == object_queue_head) {
request_queue_head = request->next; object_queue_head = obj_req->next;
} else { } else {
while (entry->next != NULL && entry->next != request) while (entry->next != NULL && entry->next != obj_req)
entry = entry->next; entry = entry->next;
if (entry->next == request) if (entry->next == obj_req)
entry->next = entry->next->next; entry->next = entry->next->next;
} }
free(request->url); free(obj_req->url);
free(request); free(obj_req);
} }
#ifdef USE_CURL_MULTI #ifdef USE_CURL_MULTI
void fill_active_slots(void) void fill_active_slots(void)
{ {
struct transfer_request *request = request_queue_head; struct object_request *obj_req = object_queue_head;
struct active_request_slot *slot = active_queue_head; struct active_request_slot *slot = active_queue_head;
int num_transfers; int num_transfers;
while (active_requests < max_requests && request != NULL) { while (active_requests < max_requests && obj_req != NULL) {
if (request->state == WAITING) { if (obj_req->state == WAITING) {
if (has_sha1_file(request->sha1)) if (has_sha1_file(obj_req->sha1))
release_request(request); release_object_request(obj_req);
else else
start_request(request); start_object_request(obj_req);
curl_multi_perform(curlm, &num_transfers); curl_multi_perform(curlm, &num_transfers);
} }
request = request->next; obj_req = obj_req->next;
} }
while (slot != NULL) { while (slot != NULL) {
@ -329,8 +331,8 @@ void fill_active_slots(void)
void prefetch(unsigned char *sha1) void prefetch(unsigned char *sha1)
{ {
struct transfer_request *newreq; struct object_request *newreq;
struct transfer_request *tail; struct object_request *tail;
char *filename = sha1_file_name(sha1); char *filename = sha1_file_name(sha1);
newreq = xmalloc(sizeof(*newreq)); newreq = xmalloc(sizeof(*newreq));
@ -344,10 +346,10 @@ void prefetch(unsigned char *sha1)
"%s.temp", filename); "%s.temp", filename);
newreq->next = NULL; newreq->next = NULL;
if (request_queue_head == NULL) { if (object_queue_head == NULL) {
request_queue_head = newreq; object_queue_head = newreq;
} else { } else {
tail = request_queue_head; tail = object_queue_head;
while (tail->next != NULL) { while (tail->next != NULL) {
tail = tail->next; tail = tail->next;
} }
@ -441,9 +443,10 @@ static int setup_index(struct alt_base *repo, unsigned char *sha1)
return 0; return 0;
} }
static void process_alternates(void *callback_data) static void process_alternates_response(void *callback_data)
{ {
struct alt_request *alt_req = (struct alt_request *)callback_data; struct alternates_request *alt_req =
(struct alternates_request *)callback_data;
struct active_request_slot *slot = alt_req->slot; struct active_request_slot *slot = alt_req->slot;
struct alt_base *tail = alt; struct alt_base *tail = alt;
char *base = alt_req->base; char *base = alt_req->base;
@ -548,7 +551,7 @@ static void fetch_alternates(char *base)
char *url; char *url;
char *data; char *data;
struct active_request_slot *slot; struct active_request_slot *slot;
static struct alt_request alt_req; static struct alternates_request alt_req;
/* If another request has already started fetching alternates, /* If another request has already started fetching alternates,
wait for them to arrive and return to processing this request's wait for them to arrive and return to processing this request's
@ -580,7 +583,7 @@ static void fetch_alternates(char *base)
/* Use a callback to process the result, since another request /* Use a callback to process the result, since another request
may fail and need to have alternates loaded before continuing */ may fail and need to have alternates loaded before continuing */
slot = get_active_slot(); slot = get_active_slot();
slot->callback_func = process_alternates; slot->callback_func = process_alternates_response;
slot->callback_data = &alt_req; slot->callback_data = &alt_req;
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
@ -760,54 +763,54 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
{ {
char *hex = sha1_to_hex(sha1); char *hex = sha1_to_hex(sha1);
int ret = 0; int ret = 0;
struct transfer_request *request = request_queue_head; struct object_request *obj_req = object_queue_head;
while (request != NULL && memcmp(request->sha1, sha1, 20)) while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
request = request->next; obj_req = obj_req->next;
if (request == NULL) if (obj_req == NULL)
return error("Couldn't find request for %s in the queue", hex); return error("Couldn't find request for %s in the queue", hex);
if (has_sha1_file(request->sha1)) { if (has_sha1_file(obj_req->sha1)) {
release_request(request); release_object_request(obj_req);
return 0; return 0;
} }
#ifdef USE_CURL_MULTI #ifdef USE_CURL_MULTI
while (request->state == WAITING) { while (obj_req->state == WAITING) {
step_active_slots(); step_active_slots();
} }
#else #else
start_request(request); start_object_request(obj_req);
#endif #endif
while (request->state == ACTIVE) { while (obj_req->state == ACTIVE) {
run_active_slot(request->slot); run_active_slot(obj_req->slot);
} }
if (request->local != -1) { if (obj_req->local != -1) {
close(request->local); request->local = -1; close(obj_req->local); obj_req->local = -1;
} }
if (request->state == ABORTED) { if (obj_req->state == ABORTED) {
ret = error("Request for %s aborted", hex); ret = error("Request for %s aborted", hex);
} else if (request->curl_result != CURLE_OK && } else if (obj_req->curl_result != CURLE_OK &&
request->http_code != 416) { obj_req->http_code != 416) {
if (request->http_code == 404) if (obj_req->http_code == 404)
ret = -1; /* Be silent, it is probably in a pack. */ ret = -1; /* Be silent, it is probably in a pack. */
else else
ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)", ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
request->errorstr, request->curl_result, obj_req->errorstr, obj_req->curl_result,
request->http_code, hex); obj_req->http_code, hex);
} else if (request->zret != Z_STREAM_END) { } else if (obj_req->zret != Z_STREAM_END) {
ret = error("File %s (%s) corrupt\n", hex, request->url); ret = error("File %s (%s) corrupt\n", hex, obj_req->url);
} else if (memcmp(request->sha1, request->real_sha1, 20)) { } else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
ret = error("File %s has bad hash\n", hex); ret = error("File %s has bad hash\n", hex);
} else if (request->rename < 0) { } else if (obj_req->rename < 0) {
ret = error("unable to write sha1 filename %s: %s", ret = error("unable to write sha1 filename %s: %s",
request->filename, obj_req->filename,
strerror(request->rename)); strerror(obj_req->rename));
} }
release_request(request); release_object_request(obj_req);
return ret; return ret;
} }