summaryrefslogtreecommitdiff
path: root/lib/libalpm/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/server.c')
-rw-r--r--lib/libalpm/server.c123
1 files changed, 64 insertions, 59 deletions
diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c
index 5b41a808..5fdc41a6 100644
--- a/lib/libalpm/server.c
+++ b/lib/libalpm/server.c
@@ -41,20 +41,6 @@
#include "handle.h"
#include "package.h"
-/** Fetch a remote pkg.
- * @param url
- * @return the downloaded filename on success, NULL on error
- * @addtogroup alpm_misc
- */
-char SYMEXPORT *alpm_fetch_pkgurl(const char *url)
-{
- ALPM_LOG_FUNC;
-
- ASSERT(strstr(url, "://"), return(NULL));
-
- return(_alpm_fetch_pkgurl(url));
-}
-
pmserver_t *_alpm_server_new(const char *url)
{
struct url *u;
@@ -64,18 +50,18 @@ pmserver_t *_alpm_server_new(const char *url)
server = malloc(sizeof(pmserver_t));
if(server == NULL) {
- _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmserver_t));
+ _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes\n"), sizeof(pmserver_t));
RET_ERR(PM_ERR_MEMORY, NULL);
}
memset(server, 0, sizeof(pmserver_t));
u = downloadParseURL(url);
if(!u) {
- _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring"), url);
+ _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring\n"), url);
RET_ERR(PM_ERR_SERVER_BAD_URL, NULL);
}
if(strlen(u->scheme) == 0) {
- _alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming http"));
+ _alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming http\n"));
strcpy(u->scheme, "http");
}
@@ -117,7 +103,8 @@ static char *strip_filename(pmserver_t *server)
p = strrchr(server->s_url->doc, '/');
if(p && *(++p)) {
fname = strdup(p);
- _alpm_log(PM_LOG_DEBUG, _("stripping '%s' from '%s'"), fname, server->s_url->doc);
+ _alpm_log(PM_LOG_DEBUG, "stripping '%s' from '%s'\n",
+ fname, server->s_url->doc);
*p = 0;
}
@@ -146,7 +133,7 @@ static struct url *url_for_file(pmserver_t *server, const char *filename)
doc,
server->s_url->user,
server->s_url->pwd);
- free(doc);
+ FREE(doc);
return(ret);
}
@@ -156,9 +143,9 @@ static struct url *url_for_file(pmserver_t *server, const char *filename)
*
* RETURN: 0 for successful download, 1 on error
*/
-int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath, alpm_list_t *files)
+int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
+ alpm_list_t *files)
{
- ALPM_LOG_FUNC;
return(_alpm_downloadfiles_forreal(servers, localpath, files, NULL, NULL));
}
@@ -216,7 +203,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
/* just use the raw filename if we can't find crap */
strncpy(pkgname, fn, PKG_NAME_LEN);
}
- _alpm_log(PM_LOG_DEBUG, _("using '%s' for download progress"), pkgname);
+ _alpm_log(PM_LOG_DEBUG, "using '%s' for download progress\n", pkgname);
snprintf(realfile, PATH_MAX, "%s%s", localpath, fn);
snprintf(output, PATH_MAX, "%s%s.part", localpath, fn);
@@ -232,7 +219,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
int chk_resume = 0;
if(stat(output, &st) == 0 && st.st_size > 0) {
- _alpm_log(PM_LOG_DEBUG, _("existing file found, using it"));
+ _alpm_log(PM_LOG_DEBUG, "existing file found, using it\n");
fileurl->offset = (off_t)st.st_size;
dltotal_bytes = st.st_size;
localf = fopen(output, "a");
@@ -251,22 +238,23 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
dlf = downloadXGet(fileurl, &ust, (handle->nopassiveftp ? "" : "p"));
if(downloadLastErrCode != 0 || dlf == NULL) {
- _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s"),
+ _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from %s : %s\n"),
fn, fileurl->host, downloadLastErrString);
if(localf != NULL) {
fclose(localf);
}
/* try the next server */
+ downloadFreeURL(fileurl);
continue;
} else {
- _alpm_log(PM_LOG_DEBUG, _("connected to %s successfully"), fileurl->host);
+ _alpm_log(PM_LOG_DEBUG, "connected to %s successfully\n", fileurl->host);
}
if(ust.mtime && mtime1) {
char strtime[15];
_alpm_time2string(ust.mtime, strtime);
if(strcmp(mtime1, strtime) == 0) {
- _alpm_log(PM_LOG_DEBUG, _("mtimes are identical, skipping %s"), fn);
+ _alpm_log(PM_LOG_DEBUG, "mtimes are identical, skipping %s\n", fn);
complete = alpm_list_add(complete, fn);
if(localf != NULL) {
fclose(localf);
@@ -274,6 +262,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
if(dlf != NULL) {
fclose(dlf);
}
+ downloadFreeURL(fileurl);
return(1);
}
}
@@ -283,7 +272,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
}
if(chk_resume && fileurl->offset == 0) {
- _alpm_log(PM_LOG_WARNING, _("cannot resume download, starting over"));
+ _alpm_log(PM_LOG_WARNING, _("cannot resume download, starting over\n"));
if(localf != NULL) {
fclose(localf);
localf = NULL;
@@ -296,11 +285,12 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
dltotal_bytes = 0;
localf = fopen(output, "w");
if(localf == NULL) { /* still null? */
- _alpm_log(PM_LOG_ERROR, _("cannot write to file '%s'"), output);
+ _alpm_log(PM_LOG_ERROR, _("cannot write to file '%s'\n"), output);
if(dlf != NULL) {
fclose(dlf);
}
- return -1;
+ downloadFreeURL(fileurl);
+ return(-1);
}
}
@@ -311,10 +301,11 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
char buffer[PM_DLBUF_LEN];
while((nread = fread(buffer, 1, PM_DLBUF_LEN, dlf)) > 0) {
if(ferror(dlf)) {
- _alpm_log(PM_LOG_ERROR, _("error downloading '%s': %s"),
+ _alpm_log(PM_LOG_ERROR, _("error downloading '%s': %s\n"),
fn, downloadLastErrString);
fclose(localf);
fclose(dlf);
+ downloadFreeURL(fileurl);
return(-1);
}
@@ -322,10 +313,11 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
while(nwritten < nread) {
nwritten += fwrite(buffer, 1, (nread - nwritten), localf);
if(ferror(localf)) {
- _alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s"),
+ _alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
realfile, strerror(errno));
fclose(localf);
fclose(dlf);
+ downloadFreeURL(fileurl);
return(-1);
}
}
@@ -337,7 +329,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
if(handle->dlcb) handle->dlcb(pkgname, dltotal_bytes, ust.size);
}
-
+ downloadFreeURL(fileurl);
fclose(localf);
fclose(dlf);
rename(output, realfile);
@@ -352,7 +344,10 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
char cwd[PATH_MAX];
/* build the full download url */
- snprintf(url, PATH_MAX, "%s://%s%s", fileurl->scheme, fileurl->host, fileurl->doc);
+ snprintf(url, PATH_MAX, "%s://%s%s", fileurl->scheme,
+ fileurl->host, fileurl->doc);
+ /* we don't need this anymore */
+ downloadFreeURL(fileurl);
/* replace all occurrences of %o with fn.part */
strncpy(origCmd, handle->xfercommand, sizeof(origCmd));
@@ -379,18 +374,18 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
/* cwd to the download directory */
getcwd(cwd, PATH_MAX);
if(chdir(localpath)) {
- _alpm_log(PM_LOG_WARNING, _("could not chdir to %s"), localpath);
+ _alpm_log(PM_LOG_WARNING, _("could not chdir to %s\n"), localpath);
return(PM_ERR_CONNECT_FAILED);
}
/* execute the parsed command via /bin/sh -c */
- _alpm_log(PM_LOG_DEBUG, _("running command: %s"), parsedCmd);
+ _alpm_log(PM_LOG_DEBUG, "running command: %s\n", parsedCmd);
ret = system(parsedCmd);
if(ret == -1) {
- _alpm_log(PM_LOG_WARNING, _("running XferCommand: fork failed!"));
+ _alpm_log(PM_LOG_WARNING, _("running XferCommand: fork failed!\n"));
return(PM_ERR_FORK_FAILED);
} else if(ret != 0) {
/* download failed */
- _alpm_log(PM_LOG_DEBUG, _("XferCommand command returned non-zero status code (%d)"), ret);
+ _alpm_log(PM_LOG_DEBUG, "XferCommand command returned non-zero status code (%d)\n", ret);
} else {
/* download was successful */
complete = alpm_list_add(complete, fn);
@@ -400,26 +395,36 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
}
chdir(cwd);
}
- downloadFreeURL(fileurl);
}
if(alpm_list_count(complete) == alpm_list_count(files)) {
done = 1;
}
+ alpm_list_free(complete);
}
return(done ? 0 : -1);
}
-char *_alpm_fetch_pkgurl(const char *target)
+/** Fetch a remote pkg.
+ * @param url URL of the package to download
+ * @return the downloaded filepath on success, NULL on error
+ * @addtogroup alpm_misc
+ */
+char SYMEXPORT *alpm_fetch_pkgurl(const char *url)
{
pmserver_t *server;
- char *filename;
- struct stat st;
+ char *filename, *filepath;
+ const char *cachedir;
ALPM_LOG_FUNC;
- server = _alpm_server_new(target);
+ if(strstr(url, "://") == NULL) {
+ _alpm_log(PM_LOG_DEBUG, "Invalid URL passed to alpm_fetch_pkgurl\n");
+ return(NULL);
+ }
+
+ server = _alpm_server_new(url);
if(!server) {
return(NULL);
}
@@ -427,30 +432,30 @@ char *_alpm_fetch_pkgurl(const char *target)
/* strip path information from the filename */
filename = strip_filename(server);
if(!filename) {
- _alpm_log(PM_LOG_ERROR, _("URL does not contain a file for download"));
+ _alpm_log(PM_LOG_ERROR, _("URL does not contain a file for download\n"));
return(NULL);
}
- /* do not download the file if it exists in the current dir */
- if(stat(filename, &st) == 0) {
- _alpm_log(PM_LOG_DEBUG, _("%s has already been downloaded"), filename);
- } else {
- alpm_list_t *servers = alpm_list_add(NULL, server);
- alpm_list_t *files = alpm_list_add(NULL, filename);
+ /* find a valid cache dir to download to */
+ cachedir = _alpm_filecache_setup();
- if(_alpm_downloadfiles(servers, "./", files)) {
- _alpm_log(PM_LOG_WARNING, _("failed to download %s"), target);
- return(NULL);
- }
- _alpm_log(PM_LOG_DEBUG, _("successfully downloaded %s"), filename);
- alpm_list_free(files);
- alpm_list_free(servers);
- }
+ /* TODO this seems like needless complexity just to download one file */
+ alpm_list_t *servers = alpm_list_add(NULL, server);
+ alpm_list_t *files = alpm_list_add(NULL, filename);
+ /* download the file */
+ if(_alpm_downloadfiles(servers, cachedir, files)) {
+ _alpm_log(PM_LOG_WARNING, _("failed to download %s\n"), url);
+ return(NULL);
+ }
+ _alpm_log(PM_LOG_DEBUG, "successfully downloaded %s\n", filename);
+ alpm_list_free(files);
+ alpm_list_free(servers);
_alpm_server_free(server);
- /* return the target with the raw filename, no URL */
- return(filename);
+ /* we should be able to find the file the second time around */
+ filepath = _alpm_filecache_find(filename);
+ return(filepath);
}
/* vim: set ts=2 sw=2 noet: */