aboutsummaryrefslogtreecommitdiffstats
path: root/http-push.c
diff options
context:
space:
mode:
authorTay Ray Chuan <rctay89@gmail.com>2009-06-06 16:44:02 +0800
committerJunio C Hamano <gitster@pobox.com>2009-06-06 11:03:11 -0700
commit5424bc557fc6414660830b470dd45774b8f5f281 (patch)
tree418193e2e6ca3dce2ba599451a4981a4c368b2a5 /http-push.c
parent2264dfa5c4f11e2b0e2740072208186bee361afd (diff)
downloadgit-5424bc557fc6414660830b470dd45774b8f5f281.tar.gz
http*: add helper methods for fetching objects (loose)
The code handling the fetching of loose objects in http-push.c and http-walker.c have been refactored into new methods and a new struct (object_http_request) in http.c. They are not meant to be invoked elsewhere. The new methods in http.c are - new_http_object_request - process_http_object_request - finish_http_object_request - abort_http_object_request - release_http_object_request and the new struct is http_object_request. RANGER_HEADER_SIZE and no_pragma_header is no longer made available outside of http.c, since after the above changes, there are no other instances of usage outside of http.c. Remove members of the transfer_request struct in http-push.c and http-walker.c, including filename, real_sha1 and zret, as they are used no longer used. Move the methods append_remote_object_url() and get_remote_object_url() from http-push.c to http.c. Additionally, get_remote_object_url() is no longer defined only when USE_CURL_MULTI is defined, since non-USE_CURL_MULTI code in http.c uses it (namely, in new_http_object_request()). Refactor code from http-push.c::start_fetch_loose() and http-walker.c::start_object_fetch_request() that deals with the details of coming up with the filename to store the retrieved object, resuming a previously aborted request, and making a new curl request, into a new function, new_http_object_request(). Refactor code from http-walker.c::process_object_request() into the function, process_http_object_request(). Refactor code from http-push.c::finish_request() and http-walker.c::finish_object_request() into a new function, finish_http_object_request(). It returns the result of the move_temp_to_file() invocation. Add a function, release_http_object_request(), which cleans up object request data. http-push.c and http-walker.c invoke this function separately; http-push.c::release_request() and http-walker.c::release_object_request() do not invoke this function. Add a function, abort_http_object_request(), which unlink()s the object file and invokes release_http_object_request(). Update http-walker.c::abort_object_request() to use this. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'http-push.c')
-rw-r--r--http-push.c213
1 files changed, 15 insertions, 198 deletions
diff --git a/http-push.c b/http-push.c
index 8232cf488e..3439dcc5b8 100644
--- a/http-push.c
+++ b/http-push.c
@@ -115,18 +115,10 @@ struct transfer_request
struct remote_lock *lock;
struct curl_slist *headers;
struct buffer buffer;
- char filename[PATH_MAX];
- char tmpfile[PATH_MAX];
- int local_fileno;
enum transfer_state state;
CURLcode curl_result;
char errorstr[CURL_ERROR_SIZE];
long http_code;
- unsigned char real_sha1[20];
- git_SHA_CTX c;
- z_stream stream;
- int zret;
- int rename;
void *userData;
struct active_request_slot *slot;
struct transfer_request *next;
@@ -232,15 +224,6 @@ static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum d
return dav_headers;
}
-static void append_remote_object_url(struct strbuf *buf, const char *url,
- const char *hex,
- int only_two_digit_prefix)
-{
- strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
- if (!only_two_digit_prefix)
- strbuf_addf(buf, "%s", hex+2);
-}
-
static void finish_request(struct transfer_request *request);
static void release_request(struct transfer_request *request);
@@ -254,169 +237,29 @@ static void process_response(void *callback_data)
#ifdef USE_CURL_MULTI
-static char *get_remote_object_url(const char *url, const char *hex,
- int only_two_digit_prefix)
-{
- struct strbuf buf = STRBUF_INIT;
- append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
- return strbuf_detach(&buf, NULL);
-}
-
-static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
- void *data)
-{
- unsigned char expn[4096];
- size_t size = eltsize * nmemb;
- int posn = 0;
- struct transfer_request *request = (struct transfer_request *)data;
- do {
- ssize_t retval = xwrite(request->local_fileno,
- (char *) ptr + posn, size - posn);
- if (retval < 0)
- return posn;
- posn += retval;
- } while (posn < size);
-
- request->stream.avail_in = size;
- request->stream.next_in = ptr;
- do {
- request->stream.next_out = expn;
- request->stream.avail_out = sizeof(expn);
- request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
- git_SHA1_Update(&request->c, expn,
- sizeof(expn) - request->stream.avail_out);
- } while (request->stream.avail_in && request->zret == Z_OK);
- data_received++;
- return size;
-}
-
static void start_fetch_loose(struct transfer_request *request)
{
- char *hex = sha1_to_hex(request->obj->sha1);
- char *filename;
- char prevfile[PATH_MAX];
- char *url;
- int prevlocal;
- unsigned char prev_buf[PREV_BUF_SIZE];
- ssize_t prev_read = 0;
- long prev_posn = 0;
- char range[RANGE_HEADER_SIZE];
- struct curl_slist *range_header = NULL;
struct active_request_slot *slot;
+ struct http_object_request *obj_req;
- filename = sha1_file_name(request->obj->sha1);
- snprintf(request->filename, sizeof(request->filename), "%s", filename);
- snprintf(request->tmpfile, sizeof(request->tmpfile),
- "%s.temp", filename);
-
- snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
- unlink_or_warn(prevfile);
- rename(request->tmpfile, prevfile);
- unlink_or_warn(request->tmpfile);
-
- if (request->local_fileno != -1)
- error("fd leakage in start: %d", request->local_fileno);
- request->local_fileno = open(request->tmpfile,
- O_WRONLY | O_CREAT | O_EXCL, 0666);
- /*
- * This could have failed due to the "lazy directory creation";
- * try to mkdir the last path component.
- */
- if (request->local_fileno < 0 && errno == ENOENT) {
- char *dir = strrchr(request->tmpfile, '/');
- if (dir) {
- *dir = 0;
- mkdir(request->tmpfile, 0777);
- *dir = '/';
- }
- request->local_fileno = open(request->tmpfile,
- O_WRONLY | O_CREAT | O_EXCL, 0666);
- }
-
- if (request->local_fileno < 0) {
+ obj_req = new_http_object_request(repo->url, request->obj->sha1);
+ if (obj_req == NULL) {
request->state = ABORTED;
- error("Couldn't create temporary file %s for %s: %s",
- request->tmpfile, request->filename, strerror(errno));
return;
}
- memset(&request->stream, 0, sizeof(request->stream));
-
- git_inflate_init(&request->stream);
-
- git_SHA1_Init(&request->c);
-
- url = get_remote_object_url(repo->url, hex, 0);
- request->url = xstrdup(url);
-
- /*
- * If a previous temp file is present, process what was already
- * fetched.
- */
- prevlocal = open(prevfile, O_RDONLY);
- if (prevlocal != -1) {
- do {
- prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
- if (prev_read>0) {
- if (fwrite_sha1_file(prev_buf,
- 1,
- prev_read,
- request) == prev_read)
- prev_posn += prev_read;
- else
- prev_read = -1;
- }
- } while (prev_read > 0);
- close(prevlocal);
- }
- unlink_or_warn(prevfile);
-
- /*
- * Reset inflate/SHA1 if there was an error reading the previous temp
- * file; also rewind to the beginning of the local file.
- */
- if (prev_read == -1) {
- memset(&request->stream, 0, sizeof(request->stream));
- git_inflate_init(&request->stream);
- git_SHA1_Init(&request->c);
- if (prev_posn>0) {
- prev_posn = 0;
- lseek(request->local_fileno, 0, SEEK_SET);
- ftruncate(request->local_fileno, 0);
- }
- }
-
- slot = get_active_slot();
+ slot = obj_req->slot;
slot->callback_func = process_response;
slot->callback_data = request;
request->slot = slot;
-
- curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
- 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_URL, url);
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
-
- /*
- * If we have successfully processed data from a previous fetch
- * attempt, only fetch the data we don't already have.
- */
- if (prev_posn>0) {
- if (push_verbosely)
- fprintf(stderr,
- "Resuming fetch of object %s at byte %ld\n",
- hex, prev_posn);
- sprintf(range, "Range: bytes=%ld-", prev_posn);
- range_header = curl_slist_append(range_header, range);
- curl_easy_setopt(slot->curl,
- CURLOPT_HTTPHEADER, range_header);
- }
+ request->userData = obj_req;
/* Try to get the request started, abort the request on error */
request->state = RUN_FETCH_LOOSE;
if (!start_active_slot(slot)) {
fprintf(stderr, "Unable to start GET request\n");
repo->can_update_info_refs = 0;
+ release_http_object_request(obj_req);
release_request(request);
}
}
@@ -675,16 +518,14 @@ static void release_request(struct transfer_request *request)
entry->next = entry->next->next;
}
- if (request->local_fileno != -1)
- close(request->local_fileno);
free(request->url);
free(request);
}
static void finish_request(struct transfer_request *request)
{
- struct stat st;
struct http_pack_request *preq;
+ struct http_object_request *obj_req;
request->curl_result = request->slot->curl_result;
request->http_code = request->slot->http_code;
@@ -739,39 +580,17 @@ static void finish_request(struct transfer_request *request)
aborted = 1;
}
} else if (request->state == RUN_FETCH_LOOSE) {
- close(request->local_fileno);
- request->local_fileno = -1;
-
- if (request->curl_result != CURLE_OK &&
- request->http_code != 416) {
- if (stat(request->tmpfile, &st) == 0) {
- if (st.st_size == 0)
- unlink_or_warn(request->tmpfile);
- }
- } else {
- if (request->http_code == 416)
- warning("requested range invalid; we may already have all the data.");
-
- git_inflate_end(&request->stream);
- git_SHA1_Final(request->real_sha1, &request->c);
- if (request->zret != Z_STREAM_END) {
- unlink_or_warn(request->tmpfile);
- } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
- unlink_or_warn(request->tmpfile);
- } else {
- request->rename =
- move_temp_to_file(
- request->tmpfile,
- request->filename);
- if (request->rename == 0)
- request->obj->flags |= (LOCAL | REMOTE);
- }
- }
+ obj_req = (struct http_object_request *)request->userData;
+
+ if (finish_http_object_request(obj_req) == 0)
+ if (obj_req->rename == 0)
+ request->obj->flags |= (LOCAL | REMOTE);
/* Try fetching packed if necessary */
- if (request->obj->flags & LOCAL)
+ if (request->obj->flags & LOCAL) {
+ release_http_object_request(obj_req);
release_request(request);
- else
+ } else
start_fetch_packed(request);
} else if (request->state == RUN_FETCH_PACKED) {
@@ -843,7 +662,6 @@ static void add_fetch_request(struct object *obj)
request->url = NULL;
request->lock = NULL;
request->headers = NULL;
- request->local_fileno = -1;
request->state = NEED_FETCH;
request->next = request_queue_head;
request_queue_head = request;
@@ -882,7 +700,6 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
request->url = NULL;
request->lock = lock;
request->headers = NULL;
- request->local_fileno = -1;
request->state = NEED_PUSH;
request->next = request_queue_head;
request_queue_head = request;