diff options
Diffstat (limited to 'src/pacman/db.c')
-rw-r--r-- | src/pacman/db.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/pacman/db.c b/src/pacman/db.c index e16bd901..42b93d05 100644 --- a/src/pacman/db.c +++ b/src/pacman/db.c @@ -34,6 +34,73 @@ #include "sync.h" #include "db.h" +/* 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 + * + */ +int db_getlastupdate(PM_DB *db, char *ts) +{ + FILE *fp; + char *root, *dbpath, *treename; + char file[PATH_MAX]; + + if(db == NULL || ts == NULL) { + return(-1); + } + + alpm_get_option(PM_OPT_ROOT, (long *)&root); + alpm_get_option(PM_OPT_DBPATH, (long *)&dbpath); + treename = alpm_db_getinfo(db, PM_DB_TREENAME); + snprintf(file, PATH_MAX, "%s%s/%s/.lastupdate", root, dbpath, treename); + + /* get the last update time, if it's there */ + if((fp = fopen(file, "r")) == NULL) { + return(-1); + } else { + char line[256]; + if(fgets(line, sizeof(line), fp)) { + STRNCPY(ts, line, 15); /* YYYYMMDDHHMMSS */ + ts[14] = '\0'; + } else { + fclose(fp); + return(-1); + } + } + fclose(fp); + return(0); +} + +/* writes the dbpath/.lastupdate with the contents of *ts + */ +int db_setlastupdate(PM_DB *db, char *ts) +{ + FILE *fp; + char *root, *dbpath, *treename; + char file[PATH_MAX]; + + if(db == NULL || ts == NULL || strlen(ts) == 0) { + return(-1); + } + + alpm_get_option(PM_OPT_ROOT, (long *)&root); + alpm_get_option(PM_OPT_DBPATH, (long *)&dbpath); + treename = alpm_db_getinfo(db, PM_DB_TREENAME); + snprintf(file, PATH_MAX, "%s%s/%s/.lastupdate", root, dbpath, treename); + + if((fp = fopen(file, "w")) == NULL) { + return(-1); + } + if(fputs(ts, fp) <= 0) { + fclose(fp); + return(-1); + } + fclose(fp); + + return(0); +} + int db_search(PM_DB *db, const char *treename, list_t *needles) { list_t *i; |