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>
This commit is contained in:
parent
6f2b436249
commit
3cd684b41d
7 changed files with 54 additions and 74 deletions
|
@ -747,52 +747,49 @@ int _alpm_db_remove(pmdb_t *db, pmpkg_t *info)
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reads dbpath/.lastupdate and populates *ts with the contents.
|
/*
|
||||||
* *ts should be malloc'ed and should be at least 15 bytes.
|
* Return the last update time as number of seconds from the epoch.
|
||||||
*
|
* Returns 0 if the value is unknown or can't be read.
|
||||||
* Returns 0 on success, 1 on error
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
int _alpm_db_getlastupdate(const pmdb_t *db, char *ts)
|
time_t _alpm_db_getlastupdate(const pmdb_t *db)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char file[PATH_MAX];
|
char file[PATH_MAX];
|
||||||
|
time_t ret = 0;
|
||||||
|
|
||||||
ALPM_LOG_FUNC;
|
ALPM_LOG_FUNC;
|
||||||
|
|
||||||
if(db == NULL || ts == NULL) {
|
if(db == NULL) {
|
||||||
return(-1);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(file, PATH_MAX, "%s.lastupdate", db->path);
|
snprintf(file, PATH_MAX, "%s.lastupdate", db->path);
|
||||||
|
|
||||||
/* get the last update time, if it's there */
|
/* get the last update time, if it's there */
|
||||||
if((fp = fopen(file, "r")) == NULL) {
|
if((fp = fopen(file, "r")) == NULL) {
|
||||||
return(-1);
|
return(ret);
|
||||||
} else {
|
} else {
|
||||||
char line[256];
|
char line[64];
|
||||||
if(fgets(line, sizeof(line), fp)) {
|
if(fgets(line, sizeof(line), fp)) {
|
||||||
strncpy(ts, line, 14); /* YYYYMMDDHHMMSS */
|
ret = atol(line);
|
||||||
ts[14] = '\0';
|
|
||||||
} else {
|
|
||||||
fclose(fp);
|
|
||||||
return(-1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
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;
|
FILE *fp;
|
||||||
char file[PATH_MAX];
|
char file[PATH_MAX];
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
ALPM_LOG_FUNC;
|
ALPM_LOG_FUNC;
|
||||||
|
|
||||||
if(db == NULL || ts == NULL || strlen(ts) == 0) {
|
if(db == NULL || time == 0) {
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -801,13 +798,12 @@ int _alpm_db_setlastupdate(const pmdb_t *db, char *ts)
|
||||||
if((fp = fopen(file, "w")) == NULL) {
|
if((fp = fopen(file, "w")) == NULL) {
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
if(fputs(ts, fp) <= 0) {
|
if(fprintf(fp, "%ju", (uintmax_t)time) <= 0) {
|
||||||
fclose(fp);
|
ret = -1;
|
||||||
return(-1);
|
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
return(0);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vim: set ts=2 sw=2 noet: */
|
/* vim: set ts=2 sw=2 noet: */
|
||||||
|
|
|
@ -30,9 +30,11 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdint.h> /* uintmax_t */
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
/* libalpm */
|
/* libalpm */
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
@ -221,8 +223,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
|
||||||
alpm_list_t *lp;
|
alpm_list_t *lp;
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
alpm_list_t *files = NULL;
|
alpm_list_t *files = NULL;
|
||||||
char newmtime[16] = "";
|
time_t newmtime = 0, lastupdate = 0;
|
||||||
char lastupdate[16] = "";
|
|
||||||
const char *dbpath;
|
const char *dbpath;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
@ -245,9 +246,10 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
|
||||||
|
|
||||||
if(!force) {
|
if(!force) {
|
||||||
/* get the lastupdate time */
|
/* get the lastupdate time */
|
||||||
_alpm_db_getlastupdate(db, lastupdate);
|
lastupdate = _alpm_db_getlastupdate(db);
|
||||||
if(strlen(lastupdate) == 0) {
|
if(lastupdate == 0) {
|
||||||
_alpm_log(PM_LOG_DEBUG, "failed to get lastupdate time for %s (no big deal)\n", db->treename);
|
_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();
|
dbpath = alpm_option_get_dbpath();
|
||||||
|
|
||||||
ret = _alpm_downloadfiles_forreal(db->servers, dbpath, files, lastupdate,
|
ret = _alpm_downloadfiles_forreal(db->servers, dbpath, files, lastupdate,
|
||||||
newmtime, NULL, 0);
|
&newmtime, NULL, 0);
|
||||||
FREELIST(files);
|
FREELIST(files);
|
||||||
if(ret == 1) {
|
if(ret == 1) {
|
||||||
/* mtimes match, do nothing */
|
/* mtimes match, do nothing */
|
||||||
|
@ -271,9 +273,9 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
|
||||||
downloadLastErrString, downloadLastErrCode);
|
downloadLastErrString, downloadLastErrCode);
|
||||||
RET_ERR(PM_ERR_DB_SYNC, -1);
|
RET_ERR(PM_ERR_DB_SYNC, -1);
|
||||||
} else {
|
} else {
|
||||||
if(strlen(newmtime)) {
|
if(newmtime != 0) {
|
||||||
_alpm_log(PM_LOG_DEBUG, "sync: new mtime for %s: %s\n",
|
_alpm_log(PM_LOG_DEBUG, "sync: new mtime for %s: %ju\n",
|
||||||
db->treename, newmtime);
|
db->treename, (uintmax_t)newmtime);
|
||||||
_alpm_db_setlastupdate(db, newmtime);
|
_alpm_db_setlastupdate(db, newmtime);
|
||||||
}
|
}
|
||||||
snprintf(path, PATH_MAX, "%s%s" DBEXT, dbpath, db->treename);
|
snprintf(path, PATH_MAX, "%s%s" DBEXT, dbpath, db->treename);
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
#include "alpm.h"
|
#include "alpm.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
/* Database entries */
|
/* Database entries */
|
||||||
typedef enum _pmdbinfrq_t {
|
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_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_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq);
|
||||||
int _alpm_db_remove(pmdb_t *db, pmpkg_t *info);
|
int _alpm_db_remove(pmdb_t *db, pmpkg_t *info);
|
||||||
int _alpm_db_getlastupdate(const pmdb_t *db, char *ts);
|
time_t _alpm_db_getlastupdate(const pmdb_t *db);
|
||||||
int _alpm_db_setlastupdate(const pmdb_t *db, char *ts);
|
int _alpm_db_setlastupdate(const pmdb_t *db, time_t time);
|
||||||
|
|
||||||
#endif /* _ALPM_DB_H */
|
#endif /* _ALPM_DB_H */
|
||||||
|
|
||||||
|
|
|
@ -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,
|
int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
|
||||||
alpm_list_t *files, int *dl_total, unsigned long totalsize)
|
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));
|
dl_total, totalsize));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is the real downloadfiles, used directly by sync_synctree() to check
|
* This is the real downloadfiles, used directly by sync_synctree() to check
|
||||||
* modtimes on remote files.
|
* modtimes on remote files.
|
||||||
* - if *mtime1 is non-NULL, then only download files
|
* - if mtime1 is non-NULL, then only download files if they are different
|
||||||
* if they are different than *mtime1. String should be in the form
|
* than mtime1.
|
||||||
* "YYYYMMDDHHMMSS" to match the form of ftplib's FtpModDate() function.
|
* - if *mtime2 is non-NULL, it will be filled with the mtime of the remote
|
||||||
* - if *mtime2 is non-NULL, then it will be filled with the mtime
|
* file.
|
||||||
* of the remote file (from MDTM FTP cmd or Last-Modified HTTP header).
|
|
||||||
* - if *dl_total is non-NULL, then it will be used as the starting
|
* - if *dl_total is non-NULL, then it will be used as the starting
|
||||||
* download amount when TotalDownload is set. It will also be
|
* download amount when TotalDownload is set. It will also be
|
||||||
* set to the final download amount for the calling function to use.
|
* 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
|
* -1 on error
|
||||||
*/
|
*/
|
||||||
int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
|
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)
|
unsigned long totalsize)
|
||||||
{
|
{
|
||||||
int dl_thisfile = 0;
|
int dl_thisfile = 0;
|
||||||
|
@ -229,7 +228,8 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
|
||||||
dl_thisfile = 0;
|
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;
|
downloadLastErrCode = 0;
|
||||||
|
|
||||||
/* 10s timeout - TODO make a config option */
|
/* 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);
|
_alpm_log(PM_LOG_DEBUG, "connected to %s successfully\n", fileurl->host);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ust.mtime && mtime1) {
|
if(ust.mtime && mtime1 && ust.mtime == mtime1) {
|
||||||
char strtime[15];
|
_alpm_log(PM_LOG_DEBUG, "mtimes are identical, skipping %s\n", fn);
|
||||||
_alpm_time2string(ust.mtime, strtime);
|
complete = alpm_list_add(complete, fn);
|
||||||
if(strcmp(mtime1, strtime) == 0) {
|
if(localf != NULL) {
|
||||||
_alpm_log(PM_LOG_DEBUG, "mtimes are identical, skipping %s\n", fn);
|
fclose(localf);
|
||||||
complete = alpm_list_add(complete, fn);
|
|
||||||
if(localf != NULL) {
|
|
||||||
fclose(localf);
|
|
||||||
}
|
|
||||||
if(dlf != NULL) {
|
|
||||||
fclose(dlf);
|
|
||||||
}
|
|
||||||
downloadFreeURL(fileurl);
|
|
||||||
return(1);
|
|
||||||
}
|
}
|
||||||
|
if(dlf != NULL) {
|
||||||
|
fclose(dlf);
|
||||||
|
}
|
||||||
|
downloadFreeURL(fileurl);
|
||||||
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ust.mtime && mtime2) {
|
if(ust.mtime && mtime2) {
|
||||||
_alpm_time2string(ust.mtime, mtime2);
|
*mtime2 = ust.mtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chk_resume && fileurl->offset == 0) {
|
if(chk_resume && fileurl->offset == 0) {
|
||||||
|
|
|
@ -40,8 +40,8 @@ void _alpm_server_free(pmserver_t *server);
|
||||||
int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
|
int _alpm_downloadfiles(alpm_list_t *servers, const char *localpath,
|
||||||
alpm_list_t *files, int *dl_total, unsigned long totalsize);
|
alpm_list_t *files, int *dl_total, unsigned long totalsize);
|
||||||
int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
|
int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
|
||||||
alpm_list_t *files, const char *mtime1, char *mtime2,
|
alpm_list_t *files, time_t mtime1, time_t *mtime2, int *dl_total,
|
||||||
int *dl_total, unsigned long totalsize);
|
unsigned long totalsize);
|
||||||
|
|
||||||
#endif /* _ALPM_SERVER_H */
|
#endif /* _ALPM_SERVER_H */
|
||||||
|
|
||||||
|
|
|
@ -509,20 +509,6 @@ int _alpm_ldconfig(const char *root)
|
||||||
return(0);
|
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
|
/* Helper function for comparing strings using the
|
||||||
* alpm "compare func" signature */
|
* alpm "compare func" signature */
|
||||||
int _alpm_str_cmp(const void *s1, const void *s2)
|
int _alpm_str_cmp(const void *s1, const void *s2)
|
||||||
|
|
|
@ -61,7 +61,6 @@ int _alpm_unpack(const char *archive, const char *prefix, const char *fn);
|
||||||
int _alpm_rmrf(const char *path);
|
int _alpm_rmrf(const char *path);
|
||||||
int _alpm_logaction(unsigned short usesyslog, FILE *f, const char *fmt, va_list args);
|
int _alpm_logaction(unsigned short usesyslog, FILE *f, const char *fmt, va_list args);
|
||||||
int _alpm_ldconfig(const char *root);
|
int _alpm_ldconfig(const char *root);
|
||||||
void _alpm_time2string(time_t t, char *buffer);
|
|
||||||
int _alpm_str_cmp(const void *s1, const void *s2);
|
int _alpm_str_cmp(const void *s1, const void *s2);
|
||||||
char *_alpm_filecache_find(const char *filename);
|
char *_alpm_filecache_find(const char *filename);
|
||||||
const char *_alpm_filecache_setup(void);
|
const char *_alpm_filecache_setup(void);
|
||||||
|
|
Loading…
Add table
Reference in a new issue