summaryrefslogtreecommitdiffstats
path: root/lib/libalpm
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2007-11-16 11:51:26 -0600
committerDan McGee <dan@archlinux.org>2007-11-16 11:51:26 -0600
commit3cd684b41dd606d42da76fcc3911be446dd3b78b (patch)
treef44f44424df6009153376b314e77facbd0f3f173 /lib/libalpm
parent6f2b43624915e3e1928cad2bbe14fd8b1ab21e12 (diff)
downloadpacman-3cd684b41dd606d42da76fcc3911be446dd3b78b.tar.xz
libalpm: simplify sync db lastupdate
Legacy code is hitting the trash here. Remove unnecessary _alpm_time2string time storage abstraction in favor of just writing the time_t value to the disk. The only drawback is that everyone's sync DBs will have to be updated at least once so that the lastupdate values are stored right. :) Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm')
-rw-r--r--lib/libalpm/be_files.c42
-rw-r--r--lib/libalpm/db.c20
-rw-r--r--lib/libalpm/db.h5
-rw-r--r--lib/libalpm/server.c42
-rw-r--r--lib/libalpm/server.h4
-rw-r--r--lib/libalpm/util.c14
-rw-r--r--lib/libalpm/util.h1
7 files changed, 54 insertions, 74 deletions
diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index ed8c8c14..c182e778 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -747,52 +747,49 @@ int _alpm_db_remove(pmdb_t *db, pmpkg_t *info)
return(0);
}
-/* reads dbpath/.lastupdate and populates *ts with the contents.
- * *ts should be malloc'ed and should be at least 15 bytes.
- *
- * Returns 0 on success, 1 on error
- *
+/*
+ * Return the last update time as number of seconds from the epoch.
+ * Returns 0 if the value is unknown or can't be read.
*/
-int _alpm_db_getlastupdate(const pmdb_t *db, char *ts)
+time_t _alpm_db_getlastupdate(const pmdb_t *db)
{
FILE *fp;
char file[PATH_MAX];
+ time_t ret = 0;
ALPM_LOG_FUNC;
- if(db == NULL || ts == NULL) {
- return(-1);
+ if(db == NULL) {
+ return(ret);
}
snprintf(file, PATH_MAX, "%s.lastupdate", db->path);
/* get the last update time, if it's there */
if((fp = fopen(file, "r")) == NULL) {
- return(-1);
+ return(ret);
} else {
- char line[256];
+ char line[64];
if(fgets(line, sizeof(line), fp)) {
- strncpy(ts, line, 14); /* YYYYMMDDHHMMSS */
- ts[14] = '\0';
- } else {
- fclose(fp);
- return(-1);
+ ret = atol(line);
}
}
fclose(fp);
- return(0);
+ return(ret);
}
-/* writes the dbpath/.lastupdate with the contents of *ts
+/*
+ * writes the dbpath/.lastupdate file with the value in time
*/
-int _alpm_db_setlastupdate(const pmdb_t *db, char *ts)
+int _alpm_db_setlastupdate(const pmdb_t *db, time_t time)
{
FILE *fp;
char file[PATH_MAX];
+ int ret = 0;
ALPM_LOG_FUNC;
- if(db == NULL || ts == NULL || strlen(ts) == 0) {
+ if(db == NULL || time == 0) {
return(-1);
}
@@ -801,13 +798,12 @@ int _alpm_db_setlastupdate(const pmdb_t *db, char *ts)
if((fp = fopen(file, "w")) == NULL) {
return(-1);
}
- if(fputs(ts, fp) <= 0) {
- fclose(fp);
- return(-1);
+ if(fprintf(fp, "%ju", (uintmax_t)time) <= 0) {
+ ret = -1;
}
fclose(fp);
- return(0);
+ return(ret);
}
/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 150b365a..8e4f3fa4 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -30,9 +30,11 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
+#include <stdint.h> /* uintmax_t */
#include <sys/stat.h>
#include <dirent.h>
#include <regex.h>
+#include <time.h>
/* libalpm */
#include "db.h"
@@ -221,8 +223,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
alpm_list_t *lp;
char path[PATH_MAX];
alpm_list_t *files = NULL;
- char newmtime[16] = "";
- char lastupdate[16] = "";
+ time_t newmtime = 0, lastupdate = 0;
const char *dbpath;
int ret;
@@ -245,9 +246,10 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
if(!force) {
/* get the lastupdate time */
- _alpm_db_getlastupdate(db, lastupdate);
- if(strlen(lastupdate) == 0) {
- _alpm_log(PM_LOG_DEBUG, "failed to get lastupdate time for %s (no big deal)\n", db->treename);
+ lastupdate = _alpm_db_getlastupdate(db);
+ if(lastupdate == 0) {
+ _alpm_log(PM_LOG_DEBUG, "failed to get lastupdate time for %s\n",
+ db->treename);
}
}
@@ -258,7 +260,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
dbpath = alpm_option_get_dbpath();
ret = _alpm_downloadfiles_forreal(db->servers, dbpath, files, lastupdate,
- newmtime, NULL, 0);
+ &newmtime, NULL, 0);
FREELIST(files);
if(ret == 1) {
/* mtimes match, do nothing */
@@ -271,9 +273,9 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
downloadLastErrString, downloadLastErrCode);
RET_ERR(PM_ERR_DB_SYNC, -1);
} else {
- if(strlen(newmtime)) {
- _alpm_log(PM_LOG_DEBUG, "sync: new mtime for %s: %s\n",
- db->treename, newmtime);
+ if(newmtime != 0) {
+ _alpm_log(PM_LOG_DEBUG, "sync: new mtime for %s: %ju\n",
+ db->treename, (uintmax_t)newmtime);
_alpm_db_setlastupdate(db, newmtime);
}
snprintf(path, PATH_MAX, "%s%s" DBEXT, dbpath, db->treename);
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index 16e12986..2549cdc8 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -25,6 +25,7 @@
#include "alpm.h"
#include <limits.h>
+#include <time.h>
/* Database entries */
typedef enum _pmdbinfrq_t {
@@ -65,8 +66,8 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target);
int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq);
int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq);
int _alpm_db_remove(pmdb_t *db, pmpkg_t *info);
-int _alpm_db_getlastupdate(const pmdb_t *db, char *ts);
-int _alpm_db_setlastupdate(const pmdb_t *db, char *ts);
+time_t _alpm_db_getlastupdate(const pmdb_t *db);
+int _alpm_db_setlastupdate(const pmdb_t *db, time_t time);
#endif /* _ALPM_DB_H */
diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c
index 7a3339e1..0ebf3123 100644
--- a/lib/libalpm/server.c
+++ b/lib/libalpm/server.c
@@ -144,18 +144,17 @@ static struct url *url_for_file(pmserver_t *server, const char *filename)
int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
alpm_list_t *files, int *dl_total, unsigned long totalsize)
{
- return(_alpm_downloadfiles_forreal(servers, localpath, files, NULL, NULL,
+ return(_alpm_downloadfiles_forreal(servers, localpath, files, 0, NULL,
dl_total, totalsize));
}
/*
* This is the real downloadfiles, used directly by sync_synctree() to check
* modtimes on remote files.
- * - if *mtime1 is non-NULL, then only download files
- * if they are different than *mtime1. String should be in the form
- * "YYYYMMDDHHMMSS" to match the form of ftplib's FtpModDate() function.
- * - if *mtime2 is non-NULL, then it will be filled with the mtime
- * of the remote file (from MDTM FTP cmd or Last-Modified HTTP header).
+ * - if mtime1 is non-NULL, then only download files if they are different
+ * than mtime1.
+ * - if *mtime2 is non-NULL, it will be filled with the mtime of the remote
+ * file.
* - if *dl_total is non-NULL, then it will be used as the starting
* download amount when TotalDownload is set. It will also be
* set to the final download amount for the calling function to use.
@@ -167,7 +166,7 @@ int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
* -1 on error
*/
int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
- alpm_list_t *files, const char *mtime1, char *mtime2, int *dl_total,
+ alpm_list_t *files, time_t mtime1, time_t *mtime2, int *dl_total,
unsigned long totalsize)
{
int dl_thisfile = 0;
@@ -229,7 +228,8 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
dl_thisfile = 0;
}
- /* libdownload does not reset the error code, reset it in the case of previous errors */
+ /* libdownload does not reset the error code, reset it in
+ * the case of previous errors */
downloadLastErrCode = 0;
/* 10s timeout - TODO make a config option */
@@ -250,25 +250,21 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
_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\n", fn);
- complete = alpm_list_add(complete, fn);
- if(localf != NULL) {
- fclose(localf);
- }
- if(dlf != NULL) {
- fclose(dlf);
- }
- downloadFreeURL(fileurl);
- return(1);
+ if(ust.mtime && mtime1 && ust.mtime == mtime1) {
+ _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);
+ }
+ downloadFreeURL(fileurl);
+ return(1);
}
if(ust.mtime && mtime2) {
- _alpm_time2string(ust.mtime, mtime2);
+ *mtime2 = ust.mtime;
}
if(chk_resume && fileurl->offset == 0) {
diff --git a/lib/libalpm/server.h b/lib/libalpm/server.h
index 914ee6da..f9e4155b 100644
--- a/lib/libalpm/server.h
+++ b/lib/libalpm/server.h
@@ -40,8 +40,8 @@ void _alpm_server_free(pmserver_t *server);
int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
alpm_list_t *files, int *dl_total, unsigned long totalsize);
int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
- alpm_list_t *files, const char *mtime1, char *mtime2,
- int *dl_total, unsigned long totalsize);
+ alpm_list_t *files, time_t mtime1, time_t *mtime2, int *dl_total,
+ unsigned long totalsize);
#endif /* _ALPM_SERVER_H */
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 2424e87d..95c12571 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -509,20 +509,6 @@ int _alpm_ldconfig(const char *root)
return(0);
}
-/* convert a time_t to a string - buffer MUST be large enough for
- * YYYYMMDDHHMMSS - 15 chars */
-void _alpm_time2string(time_t t, char *buffer)
-{
- if(buffer) {
- struct tm *lt;
- lt = localtime(&t);
- sprintf(buffer, "%4d%02d%02d%02d%02d%02d",
- lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
- lt->tm_hour, lt->tm_min, lt->tm_sec);
- buffer[14] = '\0';
- }
-}
-
/* Helper function for comparing strings using the
* alpm "compare func" signature */
int _alpm_str_cmp(const void *s1, const void *s2)
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index 85735287..def21047 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -61,7 +61,6 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn);
int _alpm_rmrf(const char *path);
int _alpm_logaction(unsigned short usesyslog, FILE *f, const char *fmt, va_list args);
int _alpm_ldconfig(const char *root);
-void _alpm_time2string(time_t t, char *buffer);
int _alpm_str_cmp(const void *s1, const void *s2);
char *_alpm_filecache_find(const char *filename);
const char *_alpm_filecache_setup(void);