diff options
-rw-r--r-- | lib/libftp/ftplib.c | 71 | ||||
-rw-r--r-- | lib/libftp/ftplib.h | 3 | ||||
-rw-r--r-- | src/pacman/download.c | 51 | ||||
-rw-r--r-- | src/pacman/sync.c | 6 |
4 files changed, 110 insertions, 21 deletions
diff --git a/lib/libftp/ftplib.c b/lib/libftp/ftplib.c index b69744eb..8be3cac1 100644 --- a/lib/libftp/ftplib.c +++ b/lib/libftp/ftplib.c @@ -32,6 +32,7 @@ #include <string.h> #include <errno.h> #include <ctype.h> +#include <time.h> #if defined(__unix__) #include <sys/time.h> #include <sys/types.h> @@ -1512,18 +1513,35 @@ static int HttpXfer(const char *localfile, const char *path, int *size, * return 1 if successful, 0 otherwise */ GLOBALREF int HttpGet(const char *host, const char *outputfile, const char *path, - int *size, netbuf *nControl, unsigned int offset) + int *size, netbuf *nControl, unsigned int offset, + const struct tm *mtime1, struct tm *mtime2) { - char buf[256]; + char buf[512]; - if(offset > 0) - sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%d-\r\n\r\n", path, host, offset); - else - sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); - if(!HttpSendCmd(buf,'2',nControl)) + sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n", path, host); + if (offset > 0) + sprintf(buf, "%sRange: bytes=%d-\r\n", buf, offset); + if (mtime1 && mtime1->tm_year) + { + char mtime[30]; + /* Format: + * "If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT\r\n" */ + strftime(mtime, sizeof(mtime), "%a, %d %b %Y %H:%M:%S GMT", mtime1); + sprintf(buf, "%sIf-Modified-Since: %s\r\n", buf, mtime); + } + sprintf(buf, "%s\r\n", buf); + + if (!HttpSendCmd(buf,'2',nControl)) { if (nControl->response[9] == '3') + { + /* If the answer from the server is 304, the requested file + * hasn't been modified: no need to retrieve it */ + if (mtime1 && mtime1->tm_year && nControl->response[11] == '4') + return 0; + /* otherwise, it is a redirection */ printf("redirection not supported\n"); + } return 0; } @@ -1545,9 +1563,46 @@ GLOBALREF int HttpGet(const char *host, const char *outputfile, const char *path if (strstr(nControl->response,"Content-Length")) { sscanf(nControl->response,"Content-Length: %d",size); - if(offset > 0) + if (offset > 0) *size += offset; } + else if (mtime2 && strstr(nControl->response,"Last-Modified")) + { + /* Format: + * "Last-Modified: Sat, 29 Oct 1994 19:43:31 GMT\r\n" */ + char *c = nControl->response+20; + int mint, j; + static const int months[12] = { + ('J'<<16)|('a'<<8)|'n', + ('F'<<16)|('e'<<8)|'b', + ('M'<<16)|('a'<<8)|'r', + ('A'<<16)|('p'<<8)|'r', + ('M'<<16)|('a'<<8)|'y', + ('J'<<16)|('u'<<8)|'n', + ('J'<<16)|('u'<<8)|'l', + ('A'<<16)|('u'<<8)|'g', + ('S'<<16)|('e'<<8)|'p', + ('O'<<16)|('c'<<8)|'t', + ('N'<<16)|('o'<<8)|'v', + ('D'<<16)|('e'<<8)|'c' + }; + mtime2->tm_mday = (c[0]-'0')*10+c[1]-'0'; + mint = (c[3]<<16)|(c[4]<<8)|c[5]; + mtime2->tm_mon = 0; + for(j = 0; j < 12; j++) + { + if (mint == months[j]) + { + mtime2->tm_mon = j; + break; + } + } + mtime2->tm_year = ((c[7]-'0')*10+(c[8]-'0')-19)*100; + mtime2->tm_year += (c[9]-'0')*10+c[10]-'0'; + mtime2->tm_hour = (c[12]-'0')*10+c[13]-'0'; + mtime2->tm_min = (c[15]-'0')*10+c[16]-'0'; + mtime2->tm_sec = (c[18]-'0')*10+c[19]-'0'; + } if (strlen(nControl->response) == 0) break; } diff --git a/lib/libftp/ftplib.h b/lib/libftp/ftplib.h index 49e0ccbf..4c2d95dd 100644 --- a/lib/libftp/ftplib.h +++ b/lib/libftp/ftplib.h @@ -121,7 +121,8 @@ GLOBALREF void FtpQuit(netbuf *nControl); GLOBALREF int HttpConnect(const char *host, unsigned short port, netbuf **nControl); GLOBALREF int HttpGet(const char *host, const char *output, const char *path, - int *size, netbuf *nControl, unsigned int offset); + int *size, netbuf *nControl, unsigned int offset, + const struct tm *mtime1, struct tm *mtime2); GLOBALREF void HttpQuit(netbuf *nControl); #ifdef __cplusplus diff --git a/src/pacman/download.c b/src/pacman/download.c index d75f6bd9..d93ea9fc 100644 --- a/src/pacman/download.c +++ b/src/pacman/download.c @@ -26,6 +26,7 @@ #include <string.h> #include <sys/stat.h> #include <unistd.h> +#include <time.h> #include <sys/time.h> #include <ftplib.h> @@ -366,10 +367,11 @@ int downloadfiles_forreal(list_t *servers, const char *localpath, vprint("mtimes are identical, skipping %s\n", fn); filedone = -1; complete = list_add(complete, fn); - } - if(mtime2) { - strncpy(mtime2, fmtime, 15); /* YYYYMMDDHHMMSS (=14b) */ - mtime2[14] = '\0'; + } else { + if(mtime2) { + strncpy(mtime2, fmtime, 15); /* YYYYMMDDHHMMSS (=14b) */ + mtime2[14] = '\0'; + } } } } @@ -395,6 +397,10 @@ int downloadfiles_forreal(list_t *servers, const char *localpath, 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") && !config->proxyhost) { /* HTTP servers hang up after each request (but not proxies), so * we have to re-connect for each file. @@ -402,9 +408,9 @@ int downloadfiles_forreal(list_t *servers, const char *localpath, host = (config->proxyhost) ? config->proxyhost : server->server; port = (config->proxyhost) ? config->proxyport : 80; if(strchr(host, ':')) { - vprint("Connecting to %s\n", host); + vprint("connecting to %s\n", host); } else { - vprint("Connecting to %s:%u\n", host, port); + vprint("connecting to %s:%u\n", host, port); } if(!HttpConnect(host, port, &control)) { fprintf(stderr, "error: cannot connect to %s\n", host); @@ -427,13 +433,38 @@ int downloadfiles_forreal(list_t *servers, const char *localpath, } else { snprintf(src, PATH_MAX, "%s://%s%s%s", server->protocol, server->server, server->path, fn); } - if(!HttpGet(server->server, output, src, &fsz, control, offset)) { - fprintf(stderr, "\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 */ + if(mtime1) { + /* 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--; + /* ORE - really compute the week day because some web servers (like lighttpd) + * need them. + * Fortunately, Apache does not use it: so our loosy implementation with a + * hardcoded week day will work in almost all cases. */ + fmtime1.tm_wday = 0; + } + fmtime2.tm_year = 0; + if(!HttpGet(server->server, output, src, &fsz, control, offset, &fmtime1, &fmtime2)) { + if(strstr(FtpLastResponse(control), "304")) { + vprint("mtimes are identical, skipping %s\n", fn); + filedone = -1; + complete = list_add(complete, fn); + } else { + fprintf(stderr, "\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 { filedone = 1; } + if(mtime2 && 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 if(!strcmp(server->protocol, "file")) { char src[PATH_MAX]; snprintf(src, PATH_MAX, "%s%s", server->path, fn); diff --git a/src/pacman/sync.c b/src/pacman/sync.c index 6c8d7454..b8c44f40 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -177,7 +177,9 @@ static int sync_synctree(list_t *syncs) snprintf(path, PATH_MAX, "%s%s", root, dbpath); ret = downloadfiles_forreal(sync->servers, path, files, lastupdate, newmtime); - vprint("sync: new mtime for %s: %s\n", sync->treename, newmtime); + if(strlen(newmtime)) { + vprint("sync: new mtime for %s: %s\n", sync->treename, newmtime); + } FREELIST(files); if(ret > 0) { ERR(NL, "failed to synchronize %s\n", sync->treename); @@ -190,7 +192,7 @@ static int sync_synctree(list_t *syncs) if(pm_errno != PM_ERR_DB_UPTODATE) { ERR(NL, "failed to synchronize %s (%s)\n", sync->treename, alpm_strerror(pm_errno)); success--; - } else { + } else if(!strlen(newmtime)){ MSG(NL, ":: %s is up to date\n", sync->treename); } } |