diff options
Diffstat (limited to 'lib/libalpm/server.c')
-rw-r--r-- | lib/libalpm/server.c | 518 |
1 files changed, 153 insertions, 365 deletions
diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c index d86002d2..a41b9282 100644 --- a/lib/libalpm/server.c +++ b/lib/libalpm/server.c @@ -28,7 +28,6 @@ #include <unistd.h> #include <time.h> #include <sys/time.h> -#include <ftplib.h> /* pacman */ #include "server.h" @@ -37,20 +36,14 @@ #include "alpm.h" #include "util.h" #include "handle.h" +#include "log.h" -FtpCallback pm_dlcb = NULL; -/* progress bar */ -char *pm_dlfnm=NULL; -int *pm_dloffset=NULL; -struct timeval *pm_dlt0=NULL, *pm_dlt=NULL; -float *pm_dlrate=NULL; -int *pm_dlxfered1=NULL; -unsigned char *pm_dleta_h=NULL, *pm_dleta_m=NULL, *pm_dleta_s=NULL; +download_progress_cb pm_dlcb = NULL; pmserver_t *_alpm_server_new(char *url) { + struct url *u; pmserver_t *server; - char *ptr; server = (pmserver_t *)malloc(sizeof(pmserver_t)); if(server == NULL) { @@ -59,56 +52,28 @@ pmserver_t *_alpm_server_new(char *url) } memset(server, 0, sizeof(pmserver_t)); - - /* parse our special url */ - ptr = strstr(url, "://"); - if(ptr == NULL) { - RET_ERR(PM_ERR_SERVER_BAD_LOCATION, NULL); + u = fetchParseURL(url); + if(!u) { + _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring"), url); + return(NULL); } - *ptr = '\0'; - ptr++; ptr++; ptr++; - if(ptr == NULL || *ptr == '\0') { - RET_ERR(PM_ERR_SERVER_BAD_LOCATION, NULL); + if(strlen(u->scheme) == 0) { + _alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming http")); + strcpy(u->scheme, "http"); } - server->protocol = strdup(url); - if(!strcmp(server->protocol, "ftp") || !strcmp(server->protocol, "http")) { - char *slash; - /* split the url into domain and path */ - slash = strchr(ptr, '/'); - if(slash == NULL) { - /* no path included, default to / */ - server->path = strdup("/"); - } else { - /* add a trailing slash if we need to */ - if(slash[strlen(slash)-1] == '/') { - server->path = strdup(slash); - } else { - if((server->path = (char *)malloc(strlen(slash)+2)) == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(strlen(slash+2))); - RET_ERR(PM_ERR_MEMORY, NULL); - } - sprintf(server->path, "%s/", slash); - } - *slash = '\0'; - } - server->server = strdup(ptr); - } else if(!strcmp(server->protocol, "file")){ - /* add a trailing slash if we need to */ - if(ptr[strlen(ptr)-1] == '/') { - server->path = strdup(ptr); - } else { - server->path = (char *)malloc(strlen(ptr)+2); - if(server->path == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(strlen(ptr+2))); - RET_ERR(PM_ERR_MEMORY, NULL); - } - sprintf(server->path, "%s/", ptr); - } - } else { - RET_ERR(PM_ERR_SERVER_PROTOCOL_UNSUPPORTED, NULL); + + if(strcmp(u->scheme,"ftp") == 0 && strlen(u->user) == 0) { + strcpy(u->user, "anonymous"); + strcpy(u->pwd, "libalpm@guest"); } - return(server); + /* This isn't needed... we can actually kill the whole pmserver_t interface + * and replace it with libfetch's 'struct url' + */ + server->s_url = u; + server->path = strdup(u->doc); + + return server; } void _alpm_server_free(void *data) @@ -120,10 +85,9 @@ void _alpm_server_free(void *data) } /* free memory */ - FREE(server->protocol); - FREE(server->server); FREE(server->path); - free(server); + fetchFreeURL(server->s_url); + FREE(server); } /* @@ -134,7 +98,7 @@ void _alpm_server_free(void *data) */ int _alpm_downloadfiles(pmlist_t *servers, const char *localpath, pmlist_t *files) { - return(!!_alpm_downloadfiles_forreal(servers, localpath, files, NULL, NULL)); + return(_alpm_downloadfiles_forreal(servers, localpath, files, NULL, NULL)); } /* @@ -147,14 +111,13 @@ int _alpm_downloadfiles(pmlist_t *servers, const char *localpath, pmlist_t *file * of the remote file (from MDTM FTP cmd or Last-Modified HTTP header). * * RETURN: 0 for successful download - * -1 if the mtimes are identical - * 1 on error + * 1 if the mtimes are identical + * -1 on error */ int _alpm_downloadfiles_forreal(pmlist_t *servers, const char *localpath, pmlist_t *files, const char *mtime1, char *mtime2) { - int fsz; - netbuf *control = NULL; + int dltotal_bytes = 0; pmlist_t *lp; int done = 0; pmlist_t *complete = NULL; @@ -164,74 +127,122 @@ int _alpm_downloadfiles_forreal(pmlist_t *servers, const char *localpath, return(0); } - _alpm_log(PM_LOG_DEBUG, _("server check, %d\n"),servers); for(i = servers; i && !done; i = i->next) { - _alpm_log(PM_LOG_DEBUG, _("server check, done? %d\n"),done); pmserver_t *server = (pmserver_t*)i->data; - if(!handle->xfercommand && strcmp(server->protocol, "file")) { - if(!strcmp(server->protocol, "ftp") && !handle->proxyhost) { - FtpInit(); - _alpm_log(PM_LOG_DEBUG, _("connecting to %s:21\n"), server->server); - if(!FtpConnect(server->server, &control)) { - _alpm_log(PM_LOG_WARNING, _("cannot connect to %s\n"), server->server); - continue; + /* get each file in the list */ + for(lp = files; lp; lp = lp->next) { + char realfile[PATH_MAX]; + char output[PATH_MAX]; + char *fn = (char *)lp->data; + + snprintf(realfile, PATH_MAX, "%s/%s", localpath, fn); + snprintf(output, PATH_MAX, "%s/%s.part", localpath, fn); + + if(_alpm_list_is_strin(fn, complete)) { + continue; + } + + if(!handle->xfercommand) { + FILE *dlf, *localf = NULL; + struct url_stat ust; + struct stat st; + int chk_resume = 0; + + if(stat(output, &st) == 0 && st.st_size > 0) { + _alpm_log(PM_LOG_DEBUG, _("existing file found, using it\n")); + server->s_url->offset = (off_t)st.st_size; + dltotal_bytes = st.st_size; + localf = fopen(output, "a"); + chk_resume = 1; + } else { + server->s_url->offset = (off_t)0; + dltotal_bytes = 0; } - if(!FtpLogin("anonymous", "libalpm@guest", control)) { - _alpm_log(PM_LOG_WARNING, _("anonymous login failed\n")); - FtpQuit(control); - continue; - } - if(!FtpChdir(server->path, control)) { - _alpm_log(PM_LOG_WARNING, _("could not cwd to %s: %s\n"), server->path, FtpLastResponse(control)); - FtpQuit(control); - continue; + + FREE(server->s_url->doc); + int len = strlen(server->path) + strlen(fn) + 2; + server->s_url->doc = (char *)malloc(len); + snprintf(server->s_url->doc, len, "%s/%s", server->path, fn); + + /* libfetch does not reset the error code, reset it in the case of previous errors */ + fetchLastErrCode = 0; + + /* 10s timeout - TODO make a config option */ + fetchTimeout = 10000; + + /* Make libfetch super verbose... worthwhile for testing */ + if(pm_logmask & PM_LOG_DEBUG) { + fetchDebug = 1; + dlf = fetchXGet(server->s_url, &ust, (handle->nopassiveftp ? "v" : "vp")); + } else { + dlf = fetchXGet(server->s_url, &ust, (handle->nopassiveftp ? "" : "p")); } - if(!handle->nopassiveftp) { - if(!FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, control)) { - _alpm_log(PM_LOG_WARNING, _("failed to set passive mode\n")); + if(fetchLastErrCode != 0 || dlf == NULL) { + _alpm_log(PM_LOG_ERROR, _("failed retrieving file '%s' from '%s://%s%s', %d : %s\n"), fn, + server->s_url->scheme, server->s_url->host, server->s_url->doc, fetchLastErrCode, + fetchLastErrString); + if(localf != NULL) { + fclose(localf); } + return(-1); } else { - _alpm_log(PM_LOG_DEBUG, _("FTP passive mode not set\n")); + _alpm_log(PM_LOG_DEBUG, _("server connection to %s complete"), server->s_url->host); } - } else if(handle->proxyhost) { - char *host; - unsigned port; - host = (handle->proxyhost) ? handle->proxyhost : server->server; - port = (handle->proxyport) ? handle->proxyport : 80; - if(strchr(host, ':')) { - _alpm_log(PM_LOG_DEBUG, _("connecting to %s\n"), host); - } else { - _alpm_log(PM_LOG_DEBUG, _("connecting to %s:%u\n"), host, port); + + 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\n"), fn); + complete = _alpm_list_add(complete, fn); + if(localf != NULL) { + fclose(localf); + } + if(dlf != NULL) { + fclose(dlf); + } + return(1); + } } - if(!HttpConnect(host, port, &control)) { - _alpm_log(PM_LOG_WARNING, _("cannot connect to %s\n"), host); - continue; + + if(ust.mtime && mtime2) { + _alpm_time2string(ust.mtime, mtime2); } - } - /* set up our progress bar's callback (and idle timeout) */ - if(strcmp(server->protocol, "file") && control) { - if(pm_dlcb) { - FtpOptions(FTPLIB_CALLBACK, (long)pm_dlcb, control); - } else { - _alpm_log(PM_LOG_DEBUG, _("downloadfiles: progress bar's callback is not set\n")); + if(chk_resume && server->s_url->offset == 0) { + _alpm_log(PM_LOG_WARNING, _("cannot resume download, starting over")); + if(localf != NULL) { + fclose(localf); + localf = NULL; + } } - FtpOptions(FTPLIB_IDLETIME, (long)1000, control); - FtpOptions(FTPLIB_CALLBACKARG, (long)&fsz, control); - FtpOptions(FTPLIB_CALLBACKBYTES, (10*1024), control); - } - } - /* get each file in the list */ - for(lp = files; lp; lp = lp->next) { - char *fn = (char *)lp->data; + if(localf == NULL) { + _alpm_rmrf(output); + server->s_url->offset = (off_t)0; + dltotal_bytes = 0; + localf = fopen(output, "w"); + } - if(_alpm_list_is_strin(fn, complete)) { - continue; - } + /* Progress 0 - initialize */ + if(pm_dlcb) pm_dlcb(fn, 0, ust.size); - if(handle->xfercommand && strcmp(server->protocol, "file")) { + int nread = 0; + char buffer[PM_DLBUF_LEN]; + while((nread = fread(buffer, 1, PM_DLBUF_LEN, dlf)) > 0) { + int nwritten = 0; + while((nwritten += fwrite(buffer, 1, (nread - nwritten), localf)) < nread) ; + dltotal_bytes += nread; + + if(pm_dlcb) pm_dlcb(fn, dltotal_bytes, ust.size); + } + + fclose(localf); + fclose(dlf); + rename(output, realfile); + complete = _alpm_list_add(complete, fn); + } else { int ret; int usepart = 0; char *ptr1, *ptr2; @@ -240,8 +251,8 @@ int _alpm_downloadfiles_forreal(pmlist_t *servers, const char *localpath, char url[PATH_MAX]; char cwd[PATH_MAX]; /* build the full download url */ - snprintf(url, PATH_MAX, "%s://%s%s%s", server->protocol, server->server, - server->path, fn); + snprintf(url, PATH_MAX, "%s://%s%s/%s", server->s_url->scheme, server->s_url->host, + server->s_url->doc, fn); /* replace all occurrences of %o with fn.part */ strncpy(origCmd, handle->xfercommand, sizeof(origCmd)); ptr1 = origCmd; @@ -291,213 +302,6 @@ int _alpm_downloadfiles_forreal(pmlist_t *servers, const char *localpath, } } chdir(cwd); - } else { - char output[PATH_MAX]; - unsigned int j; - int filedone = 0; - char *ptr; - struct stat st; - snprintf(output, PATH_MAX, "%s/%s.part", localpath, fn); - if(pm_dlfnm) { - strncpy(pm_dlfnm, fn, PM_DLFNM_LEN); - } - /* drop filename extension */ - ptr = strstr(fn, PM_EXT_DB); - if(pm_dlfnm && ptr && (ptr-fn) < PM_DLFNM_LEN) { - pm_dlfnm[ptr-fn] = '\0'; - } - ptr = strstr(fn, PM_EXT_PKG); - if(ptr && (ptr-fn) < PM_DLFNM_LEN) { - pm_dlfnm[ptr-fn] = '\0'; - } - if(pm_dlfnm) { - for(j = strlen(pm_dlfnm); j < PM_DLFNM_LEN; j++) { - (pm_dlfnm)[j] = ' '; - } - pm_dlfnm[PM_DLFNM_LEN] = '\0'; - } - if(pm_dloffset) { - *pm_dloffset = 0; - } - - /* ETA setup */ - if(pm_dlt0 && pm_dlt && pm_dlrate && pm_dlxfered1 && pm_dleta_h && pm_dleta_m && pm_dleta_s) { - gettimeofday(pm_dlt0, NULL); - *pm_dlt = *pm_dlt0; - *pm_dlrate = 0; - *pm_dlxfered1 = 0; - *pm_dleta_h = 0; - *pm_dleta_m = 0; - *pm_dleta_s = 0; - } - - if(!strcmp(server->protocol, "ftp") && !handle->proxyhost) { - if(!FtpSize(fn, &fsz, FTPLIB_IMAGE, control)) { - _alpm_log(PM_LOG_WARNING, _("failed to get filesize for %s\n"), fn); - } - /* check mtimes */ - if(mtime1) { - char fmtime[64]; - if(!FtpModDate(fn, fmtime, sizeof(fmtime)-1, control)) { - _alpm_log(PM_LOG_WARNING, _("failed to get mtime for %s\n"), fn); - } else { - _alpm_strtrim(fmtime); - if(mtime1 && !strcmp(mtime1, fmtime)) { - /* mtimes are identical, skip this file */ - _alpm_log(PM_LOG_DEBUG, _("mtimes are identical, skipping %s\n"), fn); - filedone = -1; - complete = _alpm_list_add(complete, fn); - } else { - if(mtime2) { - strncpy(mtime2, fmtime, 15); /* YYYYMMDDHHMMSS (=14b) */ - mtime2[14] = '\0'; - } - } - } - } - if(!filedone) { - if(!stat(output, &st)) { - *pm_dloffset = (int)st.st_size; - if(!FtpRestart(*pm_dloffset, control)) { - _alpm_log(PM_LOG_WARNING, _("failed to resume download -- restarting\n")); - /* can't resume: */ - /* unlink the file in order to restart download from scratch */ - unlink(output); - } - } - if(!FtpGet(output, fn, FTPLIB_IMAGE, control)) { - _alpm_log(PM_LOG_WARNING, _("\nfailed downloading %s from %s: %s\n"), - fn, server->server, FtpLastResponse(control)); - /* we leave the partially downloaded file in place so it can be resumed later */ - } else { - _alpm_log(PM_LOG_DEBUG, _("downloaded %s from %s\n"), - fn, server->server); - filedone = 1; - } - } - } else if(!strcmp(server->protocol, "http") || (handle->proxyhost && strcmp(server->protocol, "file"))) { - char src[PATH_MAX]; - char *host; - unsigned port; - struct tm fmtime1; - struct tm fmtime2; - memset(&fmtime1, 0, sizeof(struct tm)); - memset(&fmtime2, 0, sizeof(struct tm)); - if(!strcmp(server->protocol, "http") && !handle->proxyhost) { - /* HTTP servers hang up after each request (but not proxies), so - * we have to re-connect for each file. - */ - host = (handle->proxyhost) ? handle->proxyhost : server->server; - port = (handle->proxyhost) ? handle->proxyport : 80; - if(strchr(host, ':')) { - _alpm_log(PM_LOG_DEBUG, _("connecting to %s\n"), host); - } else { - _alpm_log(PM_LOG_DEBUG, _("connecting to %s:%u\n"), host, port); - } - if(!HttpConnect(host, port, &control)) { - _alpm_log(PM_LOG_WARNING, _("cannot connect to %s\n"), host); - continue; - } - /* set up our progress bar's callback (and idle timeout) */ - if(strcmp(server->protocol, "file") && control) { - if(pm_dlcb) { - FtpOptions(FTPLIB_CALLBACK, (long)pm_dlcb, control); - } else { - _alpm_log(PM_LOG_DEBUG, _("downloadfiles: progress bar's callback is not set\n")); - } - FtpOptions(FTPLIB_IDLETIME, (long)1000, control); - FtpOptions(FTPLIB_CALLBACKARG, (long)&fsz, control); - FtpOptions(FTPLIB_CALLBACKBYTES, (10*1024), control); - } - } - - if(!stat(output, &st)) { - *pm_dloffset = (int)st.st_size; - } - if(!handle->proxyhost) { - snprintf(src, PATH_MAX, "%s%s", server->path, fn); - } else { - snprintf(src, PATH_MAX, "%s://%s%s%s", server->protocol, server->server, server->path, fn); - } - if(mtime1 && strlen(mtime1)) { - struct tm tmref; - time_t t, tref; - int diff; - /* date conversion from YYYYMMDDHHMMSS to "rfc1123-date" */ - sscanf(mtime1, "%4d%2d%2d%2d%2d%2d", - &fmtime1.tm_year, &fmtime1.tm_mon, &fmtime1.tm_mday, - &fmtime1.tm_hour, &fmtime1.tm_min, &fmtime1.tm_sec); - fmtime1.tm_year -= 1900; - fmtime1.tm_mon--; - /* compute the week day because some web servers (like lighttpd) need them. */ - /* we set tmref to "Thu, 01 Jan 1970 00:00:00" */ - memset(&tmref, 0, sizeof(struct tm)); - tmref.tm_mday = 1; - tref = mktime(&tmref); - /* then we compute the difference with mtime1 */ - t = mktime(&fmtime1); - diff = ((t-tref)/3600/24)%7; - fmtime1.tm_wday = diff+(diff >= 3 ? -3 : 4); - - } - fmtime2.tm_year = 0; - if(!HttpGet(server->server, output, src, &fsz, control, *pm_dloffset, - (mtime1) ? &fmtime1 : NULL, (mtime2) ? &fmtime2 : NULL)) { - if(strstr(FtpLastResponse(control), "304")) { - _alpm_log(PM_LOG_DEBUG, _("mtimes are identical, skipping %s\n"), fn); - filedone = -1; - complete = _alpm_list_add(complete, fn); - } else { - _alpm_log(PM_LOG_WARNING, _("\nfailed downloading %s from %s: %s\n"), - src, server->server, FtpLastResponse(control)); - /* we leave the partially downloaded file in place so it can be resumed later */ - } - } else { - if(mtime2) { - if(fmtime2.tm_year) { - /* date conversion from "rfc1123-date" to YYYYMMDDHHMMSS */ - sprintf(mtime2, "%4d%02d%02d%02d%02d%02d", - fmtime2.tm_year+1900, fmtime2.tm_mon+1, fmtime2.tm_mday, - fmtime2.tm_hour, fmtime2.tm_min, fmtime2.tm_sec); - } else { - _alpm_log(PM_LOG_WARNING, _("failed to get mtime for %s\n"), fn); - } - } - filedone = 1; - } - } else if(!strcmp(server->protocol, "file")) { - char src[PATH_MAX]; - snprintf(src, PATH_MAX, "%s%s", server->path, fn); - _alpm_log(PM_LOG_DEBUG, _("copying %s to %s/%s\n"), src, localpath, fn); - /* local repository, just copy the file */ - if(_alpm_copyfile(src, output)) { - _alpm_log(PM_LOG_WARNING, _("failed copying %s\n"), src); - } else { - filedone = 1; - } - } - - if(filedone > 0) { - char completefile[PATH_MAX]; - if(!strcmp(server->protocol, "file")) { - EVENT(handle->trans, PM_TRANS_EVT_RETRIEVE_LOCAL, pm_dlfnm, server->path); - } else if(pm_dlcb) { - pm_dlcb(control, fsz-*pm_dloffset, &fsz); - } - complete = _alpm_list_add(complete, fn); - /* rename "output.part" file to "output" file */ - snprintf(completefile, PATH_MAX, "%s/%s", localpath, fn); - rename(output, completefile); - } else if(filedone < 0) { - return(-1); - } - } - } - if(!handle->xfercommand) { - if(!strcmp(server->protocol, "ftp") && !handle->proxyhost) { - FtpQuit(control); - } else if(!strcmp(server->protocol, "http") || (handle->proxyhost && strcmp(server->protocol, "file"))) { - HttpQuit(control); } } @@ -506,42 +310,32 @@ int _alpm_downloadfiles_forreal(pmlist_t *servers, const char *localpath, } } - _alpm_log(PM_LOG_DEBUG, _("end _alpm_downloadfiles_forreal - return %d"),!done); return(!done); } char *_alpm_fetch_pkgurl(char *target) { - char spath[PATH_MAX]; - char url[PATH_MAX]; - char *host, *path, *fn; - struct stat buf; - - strncpy(url, target, PATH_MAX); - host = strstr(url, "://"); - *host = '\0'; - host += 3; - path = strchr(host, '/'); - *path = '\0'; - path++; - fn = strrchr(path, '/'); - if(fn) { - *fn = '\0'; - if(path[0] == '/') { - snprintf(spath, PATH_MAX, "%s/", path); - } else { - snprintf(spath, PATH_MAX, "/%s/", path); - } - fn++; - } else { - fn = path; - strcpy(spath, "/"); + struct stat st; + struct url *s_url; + + s_url = fetchParseURL(target); + if(!s_url) { + _alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring"), target); + return(NULL); + } + if(strlen(s_url->scheme) == 0) { + _alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming http")); + strcpy(s_url->scheme, "http"); } - /* do not download the file if it exists in the current dir - */ - if(stat(fn, &buf) == 0) { - _alpm_log(PM_LOG_DEBUG, _(" %s is already in the current directory\n"), fn); + if(strcmp(s_url->scheme,"ftp") == 0 && strlen(s_url->user) == 0) { + strcpy(s_url->user, "anonymous"); + strcpy(s_url->pwd, "libalpm@guest"); + } + + /* do not download the file if it exists in the current dir */ + if(stat(s_url->doc, &st) == 0) { + _alpm_log(PM_LOG_DEBUG, _(" %s is already in the current directory\n"), s_url->doc); } else { pmserver_t *server; pmlist_t *servers = NULL; @@ -551,27 +345,21 @@ char *_alpm_fetch_pkgurl(char *target) _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmserver_t)); return(NULL); } - server->protocol = url; - server->server = host; - server->path = spath; + server->s_url = s_url; + server->path = strdup(s_url->doc); servers = _alpm_list_add(servers, server); - files = _alpm_list_add(NULL, fn); + files = _alpm_list_add(NULL, s_url->doc); if(_alpm_downloadfiles(servers, ".", files)) { _alpm_log(PM_LOG_WARNING, _("failed to download %s\n"), target); return(NULL); } FREELISTPTR(files); - FREELIST(servers); } /* return the target with the raw filename, no URL */ - #if defined(__OpenBSD__) || defined(__APPLE__) - return(strdup(fn)); - #else - return(strndup(fn, PATH_MAX)); - #endif + return(strdup(s_url->doc)); } /* vim: set ts=2 sw=2 noet: */ |