aboutsummaryrefslogtreecommitdiffstats
path: root/http.c
diff options
context:
space:
mode:
Diffstat (limited to 'http.c')
-rw-r--r--http.c276
1 files changed, 140 insertions, 136 deletions
diff --git a/http.c b/http.c
index d59e59f66b..9b62f627dc 100644
--- a/http.c
+++ b/http.c
@@ -1,4 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE
+#define DISABLE_SIGN_COMPARE_WARNINGS
#include "git-compat-util.h"
#include "git-curl-compat.h"
@@ -18,7 +19,8 @@
#include "packfile.h"
#include "string-list.h"
#include "object-file.h"
-#include "object-store-ll.h"
+#include "odb.h"
+#include "tempfile.h"
static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
static int trace_curl_data = 1;
@@ -52,22 +54,16 @@ static struct {
{ "sslv2", CURL_SSLVERSION_SSLv2 },
{ "sslv3", CURL_SSLVERSION_SSLv3 },
{ "tlsv1", CURL_SSLVERSION_TLSv1 },
-#ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
{ "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
{ "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
{ "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
-#endif
-#ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
{ "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
-#endif
};
static char *ssl_key;
static char *ssl_key_type;
static char *ssl_capath;
static char *curl_no_proxy;
-#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
static char *ssl_pinnedkey;
-#endif
static char *ssl_cainfo;
static long curl_low_speed_limit = -1;
static long curl_low_speed_time = -1;
@@ -108,6 +104,10 @@ static struct {
};
#endif
+static long curl_tcp_keepidle = -1;
+static long curl_tcp_keepintvl = -1;
+static long curl_tcp_keepcnt = -1;
+
enum proactive_auth {
PROACTIVE_AUTH_NONE = 0,
PROACTIVE_AUTH_IF_CREDENTIALS,
@@ -442,11 +442,11 @@ static int http_options(const char *var, const char *value,
return 0;
}
if (!strcmp("http.lowspeedlimit", var)) {
- curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
+ curl_low_speed_limit = git_config_int(var, value, ctx->kvi);
return 0;
}
if (!strcmp("http.lowspeedtime", var)) {
- curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
+ curl_low_speed_time = git_config_int(var, value, ctx->kvi);
return 0;
}
@@ -511,12 +511,7 @@ static int http_options(const char *var, const char *value,
}
if (!strcmp("http.pinnedpubkey", var)) {
-#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
return git_config_pathname(&ssl_pinnedkey, var, value);
-#else
- warning(_("Public key pinning not supported with cURL < 7.39.0"));
- return 0;
-#endif
}
if (!strcmp("http.extraheader", var)) {
@@ -566,6 +561,19 @@ static int http_options(const char *var, const char *value,
return 0;
}
+ if (!strcmp("http.keepaliveidle", var)) {
+ curl_tcp_keepidle = git_config_int(var, value, ctx->kvi);
+ return 0;
+ }
+ if (!strcmp("http.keepaliveinterval", var)) {
+ curl_tcp_keepintvl = git_config_int(var, value, ctx->kvi);
+ return 0;
+ }
+ if (!strcmp("http.keepalivecount", var)) {
+ curl_tcp_keepcnt = git_config_int(var, value, ctx->kvi);
+ return 0;
+ }
+
/* Fall back on the default ones */
return git_default_config(var, value, ctx, data);
}
@@ -607,8 +615,7 @@ static void init_curl_http_auth(CURL *result)
{
if ((!http_auth.username || !*http_auth.username) &&
(!http_auth.credential || !*http_auth.credential)) {
- int empty_auth = curl_empty_auth_enabled();
- if ((empty_auth != -1 && !always_auth_proactively()) || empty_auth == 1) {
+ if (!always_auth_proactively() && curl_empty_auth_enabled()) {
curl_easy_setopt(result, CURLOPT_USERPWD, ":");
return;
} else if (!always_auth_proactively()) {
@@ -618,7 +625,7 @@ static void init_curl_http_auth(CURL *result)
}
}
- credential_fill(&http_auth, 1);
+ credential_fill(the_repository, &http_auth, 1);
if (http_auth.password) {
if (always_auth_proactively()) {
@@ -661,7 +668,7 @@ static void init_curl_proxy_auth(CURL *result)
{
if (proxy_auth.username) {
if (!proxy_auth.password && !proxy_auth.credential)
- credential_fill(&proxy_auth, 1);
+ credential_fill(the_repository, &proxy_auth, 1);
set_proxyauth_name_password(result);
}
@@ -695,12 +702,11 @@ static int has_cert_password(void)
cert_auth.host = xstrdup("");
cert_auth.username = xstrdup("");
cert_auth.path = xstrdup(ssl_cert);
- credential_fill(&cert_auth, 0);
+ credential_fill(the_repository, &cert_auth, 0);
}
return 1;
}
-#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
static int has_proxy_cert_password(void)
{
if (http_proxy_ssl_cert == NULL || proxy_ssl_cert_password_required != 1)
@@ -710,40 +716,10 @@ static int has_proxy_cert_password(void)
proxy_cert_auth.host = xstrdup("");
proxy_cert_auth.username = xstrdup("");
proxy_cert_auth.path = xstrdup(http_proxy_ssl_cert);
- credential_fill(&proxy_cert_auth, 0);
+ credential_fill(the_repository, &proxy_cert_auth, 0);
}
return 1;
}
-#endif
-
-#ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
-static void set_curl_keepalive(CURL *c)
-{
- curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
-}
-
-#else
-static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
-{
- int ka = 1;
- int rc;
- socklen_t len = (socklen_t)sizeof(ka);
-
- if (type != CURLSOCKTYPE_IPCXN)
- return 0;
-
- rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
- if (rc < 0)
- warning_errno("unable to set SO_KEEPALIVE on socket");
-
- return CURL_SOCKOPT_OK;
-}
-
-static void set_curl_keepalive(CURL *c)
-{
- curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
-}
-#endif
/* Return 1 if redactions have been made, 0 otherwise. */
static int redact_sensitive_header(struct strbuf *header, size_t offset)
@@ -1013,7 +989,6 @@ static long get_curl_allowed_protocols(int from_user, struct strbuf *list)
return bits;
}
-#ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
static int get_curl_http_version_opt(const char *version_string, long *opt)
{
int i;
@@ -1036,8 +1011,6 @@ static int get_curl_http_version_opt(const char *version_string, long *opt)
return -1; /* not found */
}
-#endif
-
static CURL *get_curl_handle(void)
{
CURL *result = curl_easy_init();
@@ -1046,16 +1019,15 @@ static CURL *get_curl_handle(void)
die("curl_easy_init failed");
if (!curl_ssl_verify) {
- curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
- curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
+ curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0L);
+ curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0L);
} else {
/* Verify authenticity of the peer's certificate */
- curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
+ curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1L);
/* The name in the cert must match whom we tried to connect */
- curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
+ curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2L);
}
-#ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
if (curl_http_version) {
long opt;
if (!get_curl_http_version_opt(curl_http_version, &opt)) {
@@ -1063,7 +1035,6 @@ static CURL *get_curl_handle(void)
curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
}
}
-#endif
curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
@@ -1086,11 +1057,7 @@ static CURL *get_curl_handle(void)
if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
!http_schannel_check_revoke) {
-#ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
- curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
-#else
- warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
-#endif
+ curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_NO_REVOKE);
}
if (http_proactive_auth != PROACTIVE_AUTH_NONE)
@@ -1130,23 +1097,17 @@ static CURL *get_curl_handle(void)
curl_easy_setopt(result, CURLOPT_SSLKEYTYPE, ssl_key_type);
if (ssl_capath)
curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
-#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
if (ssl_pinnedkey)
curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
-#endif
if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
!http_schannel_use_ssl_cainfo) {
curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
-#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
-#endif
} else if (ssl_cainfo != NULL || http_proxy_ssl_ca_info != NULL) {
if (ssl_cainfo)
curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
-#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
if (http_proxy_ssl_ca_info)
curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, http_proxy_ssl_ca_info);
-#endif
}
if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
@@ -1156,8 +1117,8 @@ static CURL *get_curl_handle(void)
curl_low_speed_time);
}
- curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
- curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
+ curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20L);
+ curl_easy_setopt(result, CURLOPT_POSTREDIR, (long)CURL_REDIR_POST_ALL);
#ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
{
@@ -1190,7 +1151,7 @@ static CURL *get_curl_handle(void)
user_agent ? user_agent : git_user_agent());
if (curl_ftp_no_epsv)
- curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
+ curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0L);
if (curl_ssl_try)
curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
@@ -1232,19 +1193,18 @@ static CURL *get_curl_handle(void)
if (starts_with(curl_http_proxy, "socks5h"))
curl_easy_setopt(result,
- CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
+ CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS5_HOSTNAME);
else if (starts_with(curl_http_proxy, "socks5"))
curl_easy_setopt(result,
- CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
+ CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS5);
else if (starts_with(curl_http_proxy, "socks4a"))
curl_easy_setopt(result,
- CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
+ CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS4A);
else if (starts_with(curl_http_proxy, "socks"))
curl_easy_setopt(result,
- CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
-#ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
+ CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS4);
else if (starts_with(curl_http_proxy, "https")) {
- curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
+ curl_easy_setopt(result, CURLOPT_PROXYTYPE, (long)CURLPROXY_HTTPS);
if (http_proxy_ssl_cert)
curl_easy_setopt(result, CURLOPT_PROXY_SSLCERT, http_proxy_ssl_cert);
@@ -1255,7 +1215,6 @@ static CURL *get_curl_handle(void)
if (has_proxy_cert_password())
curl_easy_setopt(result, CURLOPT_PROXY_KEYPASSWD, proxy_cert_auth.password);
}
-#endif
if (strstr(curl_http_proxy, "://"))
credential_from_url(&proxy_auth, curl_http_proxy);
else {
@@ -1295,7 +1254,18 @@ static CURL *get_curl_handle(void)
}
init_curl_proxy_auth(result);
- set_curl_keepalive(result);
+ curl_easy_setopt(result, CURLOPT_TCP_KEEPALIVE, 1L);
+
+ if (curl_tcp_keepidle > -1)
+ curl_easy_setopt(result, CURLOPT_TCP_KEEPIDLE,
+ curl_tcp_keepidle);
+ if (curl_tcp_keepintvl > -1)
+ curl_easy_setopt(result, CURLOPT_TCP_KEEPINTVL,
+ curl_tcp_keepintvl);
+#ifdef GIT_CURL_HAVE_CURLOPT_TCP_KEEPCNT
+ if (curl_tcp_keepcnt > -1)
+ curl_easy_setopt(result, CURLOPT_TCP_KEEPCNT, curl_tcp_keepcnt);
+#endif
return result;
}
@@ -1309,10 +1279,30 @@ static void set_from_env(char **var, const char *envname)
}
}
+static void set_long_from_env(long *var, const char *envname)
+{
+ const char *val = getenv(envname);
+ if (val) {
+ long tmp;
+ char *endp;
+ int saved_errno = errno;
+
+ errno = 0;
+ tmp = strtol(val, &endp, 10);
+
+ if (errno)
+ warning_errno(_("failed to parse %s"), envname);
+ else if (*endp || endp == val)
+ warning(_("failed to parse %s"), envname);
+ else
+ *var = tmp;
+
+ errno = saved_errno;
+ }
+}
+
void http_init(struct remote *remote, const char *url, int proactive_auth)
{
- char *low_speed_limit;
- char *low_speed_time;
char *normalized_url;
struct urlmatch_config config = URLMATCH_CONFIG_INIT;
@@ -1329,7 +1319,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
free(normalized_url);
string_list_clear(&config.vars, 1);
-#ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
if (http_ssl_backend) {
const curl_ssl_backend **backends;
struct strbuf buf = STRBUF_INIT;
@@ -1354,7 +1343,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
break; /* Okay! */
}
}
-#endif
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
die("curl_global_init failed");
@@ -1393,12 +1381,8 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
- low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
- if (low_speed_limit)
- curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
- low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
- if (low_speed_time)
- curl_low_speed_time = strtol(low_speed_time, NULL, 10);
+ set_long_from_env(&curl_low_speed_limit, "GIT_HTTP_LOW_SPEED_LIMIT");
+ set_long_from_env(&curl_low_speed_time, "GIT_HTTP_LOW_SPEED_TIME");
if (curl_ssl_verify == -1)
curl_ssl_verify = 1;
@@ -1425,6 +1409,10 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
ssl_cert_password_required = 1;
}
+ set_long_from_env(&curl_tcp_keepidle, "GIT_TCP_KEEPIDLE");
+ set_long_from_env(&curl_tcp_keepintvl, "GIT_TCP_KEEPINTVL");
+ set_long_from_env(&curl_tcp_keepcnt, "GIT_TCP_KEEPCNT");
+
curl_default = get_curl_handle();
}
@@ -1552,9 +1540,9 @@ struct active_request_slot *get_active_slot(void)
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, -1L);
- curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
- curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
+ curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0L);
+ curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1L);
+ curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
/*
@@ -1563,9 +1551,9 @@ struct active_request_slot *get_active_slot(void)
* HTTP_FOLLOW_* cases themselves.
*/
if (http_follow_config == HTTP_FOLLOW_ALWAYS)
- curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
+ curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1L);
else
- curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
+ curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
@@ -1838,9 +1826,9 @@ static int handle_curl_result(struct slot_results *results)
curl_errorstr, sizeof(curl_errorstr));
if (results->curl_result == CURLE_OK) {
- credential_approve(&http_auth);
- credential_approve(&proxy_auth);
- credential_approve(&cert_auth);
+ credential_approve(the_repository, &http_auth);
+ credential_approve(the_repository, &proxy_auth);
+ credential_approve(the_repository, &cert_auth);
return HTTP_OK;
} else if (results->curl_result == CURLE_SSL_CERTPROBLEM) {
/*
@@ -1849,12 +1837,10 @@ static int handle_curl_result(struct slot_results *results)
* with the certificate. So we reject the credential to
* avoid caching or saving a bad password.
*/
- credential_reject(&cert_auth);
+ credential_reject(the_repository, &cert_auth);
return HTTP_NOAUTH;
-#ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
} else if (results->curl_result == CURLE_SSL_PINNEDPUBKEYNOTMATCH) {
return HTTP_NOMATCHPUBLICKEY;
-#endif
} else if (missing_target(results))
return HTTP_MISSING_TARGET;
else if (results->http_code == 401) {
@@ -1864,7 +1850,7 @@ static int handle_curl_result(struct slot_results *results)
credential_clear_secrets(&http_auth);
return HTTP_REAUTH;
}
- credential_reject(&http_auth);
+ credential_reject(the_repository, &http_auth);
if (always_auth_proactively())
http_proactive_auth = PROACTIVE_AUTH_NONE;
return HTTP_NOAUTH;
@@ -1878,7 +1864,7 @@ static int handle_curl_result(struct slot_results *results)
}
} else {
if (results->http_connectcode == 407)
- credential_reject(&proxy_auth);
+ credential_reject(the_repository, &proxy_auth);
if (!curl_errorstr[0])
strlcpy(curl_errorstr,
curl_easy_strerror(results->curl_result),
@@ -2134,12 +2120,12 @@ static int http_request(const char *url,
int ret;
slot = get_active_slot();
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
+ curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1L);
if (!result) {
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
+ curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1L);
} else {
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
+ curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0L);
curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result);
if (target == HTTP_REQUEST_FILE) {
@@ -2165,7 +2151,7 @@ static int http_request(const char *url,
strbuf_addstr(&buf, " no-cache");
if (options && options->initial_request &&
http_follow_config == HTTP_FOLLOW_INITIAL)
- curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
+ curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1L);
headers = curl_slist_append(headers, buf.buf);
@@ -2184,7 +2170,7 @@ static int http_request(const char *url,
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
- curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
+ curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0L);
ret = run_one_slot(slot, &results);
@@ -2266,7 +2252,7 @@ static int http_request_reauth(const char *url,
int ret;
if (always_auth_proactively())
- credential_fill(&http_auth, 1);
+ credential_fill(the_repository, &http_auth, 1);
ret = http_request(url, result, target, options);
@@ -2290,22 +2276,24 @@ static int http_request_reauth(const char *url,
case HTTP_REQUEST_STRBUF:
strbuf_reset(result);
break;
- case HTTP_REQUEST_FILE:
- if (fflush(result)) {
+ case HTTP_REQUEST_FILE: {
+ FILE *f = result;
+ if (fflush(f)) {
error_errno("unable to flush a file");
return HTTP_START_FAILED;
}
- rewind(result);
- if (ftruncate(fileno(result), 0) < 0) {
+ rewind(f);
+ if (ftruncate(fileno(f), 0) < 0) {
error_errno("unable to truncate a file");
return HTTP_START_FAILED;
}
break;
+ }
default:
BUG("Unknown http_request target");
}
- credential_fill(&http_auth, 1);
+ credential_fill(the_repository, &http_auth, 1);
ret = http_request(url, result, target, options);
}
@@ -2388,8 +2376,24 @@ static char *fetch_pack_index(unsigned char *hash, const char *base_url)
strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
url = strbuf_detach(&buf, NULL);
- strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
- tmp = strbuf_detach(&buf, NULL);
+ /*
+ * Don't put this into packs/, since it's just temporary and we don't
+ * want to confuse it with our local .idx files. We'll generate our
+ * own index if we choose to download the matching packfile.
+ *
+ * It's tempting to use xmks_tempfile() here, but it's important that
+ * the file not exist, otherwise http_get_file() complains. So we
+ * create a filename that should be unique, and then just register it
+ * as a tempfile so that it will get cleaned up on exit.
+ *
+ * In theory we could hold on to the tempfile and delete these as soon
+ * as we download the matching pack, but it would take a bit of
+ * refactoring. Leaving them until the process ends is probably OK.
+ */
+ tmp = xstrfmt("%s/tmp_pack_%s.idx",
+ repo_get_object_directory(the_repository),
+ hash_to_hex(hash));
+ register_tempfile(tmp);
if (http_get_file(url, tmp, NULL) != HTTP_OK) {
error("Unable to get pack index %s", url);
@@ -2403,22 +2407,24 @@ static char *fetch_pack_index(unsigned char *hash, const char *base_url)
static int fetch_and_setup_pack_index(struct packed_git **packs_head,
unsigned char *sha1, const char *base_url)
{
- struct packed_git *new_pack;
+ struct packed_git *new_pack, *p;
char *tmp_idx = NULL;
int ret;
- if (has_pack_index(sha1)) {
- new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
- if (!new_pack)
- return -1; /* parse_pack_index() already issued error message */
- goto add_pack;
+ /*
+ * If we already have the pack locally, no need to fetch its index or
+ * even add it to list; we already have all of its objects.
+ */
+ for (p = get_all_packs(the_repository); p; p = p->next) {
+ if (hasheq(p->hash, sha1, the_repository->hash_algo))
+ return 0;
}
tmp_idx = fetch_pack_index(sha1, base_url);
if (!tmp_idx)
return -1;
- new_pack = parse_pack_index(sha1, tmp_idx);
+ new_pack = parse_pack_index(the_repository, sha1, tmp_idx);
if (!new_pack) {
unlink(tmp_idx);
free(tmp_idx);
@@ -2427,15 +2433,12 @@ static int fetch_and_setup_pack_index(struct packed_git **packs_head,
}
ret = verify_pack_index(new_pack);
- if (!ret) {
+ if (!ret)
close_pack_index(new_pack);
- ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
- }
free(tmp_idx);
if (ret)
return -1;
-add_pack:
new_pack->next = *packs_head;
*packs_head = new_pack;
return 0;
@@ -2563,7 +2566,8 @@ struct http_pack_request *new_direct_http_pack_request(
preq->url = url;
- strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
+ odb_pack_name(the_repository, &preq->tmpfile, packed_git_hash, "pack");
+ strbuf_addstr(&preq->tmpfile, ".temp");
preq->packfile = fopen(preq->tmpfile.buf, "a");
if (!preq->packfile) {
error("Unable to open local file %s for pack",
@@ -2635,8 +2639,8 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
freq->stream.next_out = expn;
freq->stream.avail_out = sizeof(expn);
freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
- the_hash_algo->update_fn(&freq->c, expn,
- sizeof(expn) - freq->stream.avail_out);
+ git_hash_update(&freq->c, expn,
+ sizeof(expn) - freq->stream.avail_out);
} while (freq->stream.avail_in && freq->zret == Z_OK);
return nmemb;
}
@@ -2658,7 +2662,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
oidcpy(&freq->oid, oid);
freq->localfile = -1;
- loose_object_path(the_repository, &filename, oid);
+ odb_loose_path(the_repository->objects->sources, &filename, oid);
strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
strbuf_addf(&prevfile, "%s.prev", filename.buf);
@@ -2746,7 +2750,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
freq->headers = object_request_headers();
curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
- curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
+ curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0L);
curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
@@ -2801,7 +2805,7 @@ int finish_http_object_request(struct http_object_request *freq)
return -1;
}
- the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
+ git_hash_final_oid(&freq->real_oid, &freq->c);
if (freq->zret != Z_STREAM_END) {
unlink_or_warn(freq->tmpfile.buf);
return -1;
@@ -2810,7 +2814,7 @@ int finish_http_object_request(struct http_object_request *freq)
unlink_or_warn(freq->tmpfile.buf);
return -1;
}
- loose_object_path(the_repository, &filename, &freq->oid);
+ odb_loose_path(the_repository->objects->sources, &filename, &freq->oid);
freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
strbuf_release(&filename);