Skip to content

Commit 08bf9b3

Browse files
strssndktnbapt
authored andcommitted
pkgdb: track mtime in pkgdb
Store the file mtime in package manifests in order to be agnostic of the container format (tar).
1 parent 699f92e commit 08bf9b3

18 files changed

+126
-41
lines changed

docs/pkg-query.8

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ for the package origin, and
223223
for the package version.
224224
.It Cm \&%C
225225
Expands to the list of categories the matched package belongs to.
226-
.It Cm \&%F Ns Op psugmft
226+
.It Cm \&%F Ns Op psugmftl
227227
Expands to the list of files of the matched package, where
228228
.Cm p
229229
stands for path,
@@ -236,7 +236,9 @@ for group,
236236
.Cm m
237237
for mode (permissions),
238238
.Cm f
239-
for file flags, and
239+
for file flags,
240+
.Cm l
241+
for last modification time, and
240242
.Cm t
241243
for the symlink target (or empty string if no symlink).
242244
.It Cm \&%D

docs/pkg_printf.3

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ File flags [string]
512512
.It Cm %Fg
513513
File ownership: group name [string]
514514
.Vt struct pkg_file *
515+
.It Cm %Fl
516+
File last modification time [epoch]
517+
.Vt struct pkg_file *
515518
.It Cm %\^Fn
516519
File path name [string]
517520
.Vt struct pkg_file *

libpkg/pkg.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,13 @@ pkg_addrdep(struct pkg *pkg, const char *name, const char *origin, const char *v
493493
int
494494
pkg_addfile(struct pkg *pkg, const char *path, const char *sum, bool check_duplicates)
495495
{
496-
return (pkg_addfile_attr(pkg, path, sum, NULL, NULL, 0, 0, NULL, check_duplicates));
496+
return (pkg_addfile_attr(pkg, path, sum, NULL, NULL, 0, 0, 0, NULL, check_duplicates));
497497
}
498498

499499
int
500500
pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum,
501-
const char *uname, const char *gname, mode_t perm, u_long fflags,
501+
const char *uname, const char *gname, mode_t perm,
502+
u_long fflags, time_t mtime,
502503
const char *symlink_target, bool check_duplicates)
503504
{
504505
struct pkg_file *f = NULL;
@@ -541,6 +542,9 @@ pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sum,
541542
if (symlink_target != NULL)
542543
strlcpy(f->symlink_target, symlink_target, sizeof(f->symlink_target));
543544

545+
if (mtime > 0)
546+
f->time[1].tv_sec = mtime;
547+
544548
pkghash_safe_add(pkg->filehash, f->path, f, NULL);
545549
DL_APPEND(pkg->files, f);
546550

libpkg/pkg_add.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,15 @@ fill_timespec_buf(const struct stat *aest, struct timespec tspec[2])
374374
{
375375
#ifdef HAVE_STRUCT_STAT_ST_MTIM
376376
tspec[0].tv_sec = aest->st_atim.tv_sec;
377-
tspec[0].tv_nsec = aest->st_atim.tv_nsec;
377+
tspec[0].tv_nsec = 0;
378378
tspec[1].tv_sec = aest->st_mtim.tv_sec;
379-
tspec[1].tv_nsec = aest->st_mtim.tv_nsec;
379+
tspec[1].tv_nsec = 0;
380380
#else
381381
# if defined(_DARWIN_C_SOURCE) || defined(__APPLE__)
382382
tspec[0].tv_sec = aest->st_atimespec.tv_sec;
383-
tspec[0].tv_nsec = aest->st_atimespec.tv_nsec;
383+
tspec[0].tv_nsec = 0;
384384
tspec[1].tv_sec = aest->st_mtimespec.tv_sec;
385-
tspec[1].tv_nsec = aest->st_mtimespec.tv_nsec;
385+
tspec[1].tv_nsec = 0;
386386
# else
387387
/* Portable unix version */
388388
tspec[0].tv_sec = aest->st_atime;

libpkg/pkg_create.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
132132
}
133133
file->symlink_target[linklen] = '\0';
134134
}
135+
136+
file->time[0] = st.st_atim;
137+
file->time[1] = st.st_mtim;
135138
}
136139

137140
counter_count();

libpkg/pkg_manifest.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ pkg_set_files_from_object(struct pkg *pkg, const ucl_object_t *obj)
580580
u_long fflags = 0;
581581
char *fname = NULL;
582582
const char *key, *okey;
583+
time_t mtime = 0;
583584

584585
okey = ucl_object_key(obj);
585586
if (okey == NULL)
@@ -618,14 +619,23 @@ pkg_set_files_from_object(struct pkg *pkg, const ucl_object_t *obj)
618619
#endif
619620
} else if (STRIEQ(key, "symlink_target") && cur->type == UCL_STRING) {
620621
symlink_target = ucl_object_tostring(cur);
622+
} else if (STRIEQ(key, "mtime") &&
623+
(cur->type == UCL_STRING || cur->type == UCL_INT)) {
624+
errno = 0;
625+
mtime = strtoll(ucl_object_tostring_forced(cur), NULL, 10);
626+
if (mtime == 0 && errno != 0) {
627+
pkg_emit_errno("strtoll: invalid epoch value",
628+
ucl_object_tostring_forced(cur));
629+
continue;
630+
}
621631
} else {
622632
dbg(1, "Skipping unknown key for file(%s): %s",
623633
fname, key);
624634
}
625635
}
626636

627637
pkg_addfile_attr(pkg, fname, sum, uname, gname, perm, fflags,
628-
symlink_target, false);
638+
mtime, symlink_target, false);
629639
free(fname);
630640

631641
return (EPKG_OK);
@@ -1177,6 +1187,11 @@ pkg_emit_object(struct pkg *pkg, short flags)
11771187
ucl_object_fromstring(file->symlink_target),
11781188
"symlink_target", 0, false);
11791189
}
1190+
if (file->time[1].tv_sec > 0) {
1191+
ucl_object_insert_key(file_attrs,
1192+
ucl_object_fromint(file->time[1].tv_sec),
1193+
"mtime", 0, false);
1194+
}
11801195

11811196
urlencode(dp, &tmpsbuf);
11821197
if (map == NULL)

libpkg/pkg_ports.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,12 @@ meta_file(struct plist *p, char *line, struct file_attr *a, bool is_config)
407407
a->owner ? a->owner : p->uname,
408408
a->group ? a->group : p->gname,
409409
a->mode ? a->mode : p->perm,
410-
a->fflags,
410+
a->fflags, st.st_mtim.tv_sec,
411411
linklen > 0 ? symlink_target : NULL,
412412
true);
413413
} else {
414414
ret = pkg_addfile_attr(p->pkg, path, buf, p->uname,
415-
p->gname, p->perm, 0,
415+
p->gname, p->perm, 0, st.st_mtim.tv_sec,
416416
linklen > 0 ? symlink_target : NULL,
417417
true);
418418
}

libpkg/pkg_printf.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* Ff pkg_file File flags of file
7777
* Fg pkg_file Group owner of file
7878
* Fk pkg_file Keep flag
79+
* Fl pkg_file File modification time
7980
* Fn pkg_file File path name
8081
* Fp pkg_file File permissions
8182
* Fs pkg_file File SHA256 checksum
@@ -320,6 +321,15 @@ static const struct pkg_printf_fmt fmt[] = {
320321
PP_PKG|PP_F,
321322
&format_file_group,
322323
},
324+
[PP_PKG_FILE_MTIME] =
325+
{
326+
'F',
327+
'l',
328+
false,
329+
false,
330+
PP_PKG|PP_F,
331+
&format_file_mtime,
332+
},
323333
[PP_PKG_FILE_PATH] =
324334
{
325335
'F',
@@ -885,6 +895,24 @@ format_fflags(xstring *buf, u_long fflags, struct percent_esc *p)
885895
return (ret);
886896
}
887897

898+
static xstring *
899+
format_time_t(xstring *buf, time_t timestamp, struct percent_esc *p)
900+
{
901+
fflush(p->item_fmt->fp);
902+
if (strlen(p->item_fmt->buf) == 0)
903+
return (int_val(buf, timestamp, p));
904+
else {
905+
char buffer[1024];
906+
time_t tsv;
907+
908+
tsv = timestamp;
909+
strftime(buffer, sizeof(buffer), p->item_fmt->buf,
910+
localtime(&tsv));
911+
fprintf(buf->fp, "%s", buffer);
912+
}
913+
return (buf);
914+
}
915+
888916
/*
889917
* Note: List values -- special behaviour with ? and # modifiers.
890918
* Affects %A %B %C %D %F %G %L %O %U %b %d %r
@@ -1167,6 +1195,17 @@ format_files(xstring *buf, const void *data, struct percent_esc *p)
11671195
return (buf);
11681196
}
11691197

1198+
/*
1199+
* %Fm -- File modification time.
1200+
*/
1201+
xstring *
1202+
format_file_mtime(xstring *buf, const void *data, struct percent_esc *p)
1203+
{
1204+
const struct pkg_file *file = data;
1205+
1206+
return (format_time_t(buf, file->time[1].tv_sec, p));
1207+
}
1208+
11701209
/*
11711210
* %Fg -- File group.
11721211
*/
@@ -1930,19 +1969,7 @@ format_install_tstamp(xstring *buf, const void *data, struct percent_esc *p)
19301969
{
19311970
const struct pkg *pkg = data;
19321971

1933-
fflush(p->item_fmt->fp);
1934-
if (strlen(p->item_fmt->buf) == 0)
1935-
return (int_val(buf, pkg->timestamp, p));
1936-
else {
1937-
char buffer[1024];
1938-
time_t tsv;
1939-
1940-
tsv = (time_t)pkg->timestamp;
1941-
strftime(buffer, sizeof(buffer), p->item_fmt->buf,
1942-
localtime(&tsv));
1943-
fprintf(buf->fp, "%s", buffer);
1944-
}
1945-
return (buf);
1972+
return (format_time_t(buf, pkg->timestamp, p));
19461973
}
19471974

19481975
/*

libpkg/pkgdb.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ pkgdb_init(sqlite3 *sdb)
420420
"gname TEXT,"
421421
"perm INTEGER,"
422422
"fflags INTEGER,"
423+
"mtime INTEGER,"
423424
"symlink_target TEXT,"
424425
"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
425426
" ON UPDATE CASCADE"
@@ -1366,22 +1367,22 @@ static sql_prstmt sql_prepared_statements[PRSTMT_LAST] = {
13661367
[FILES] = {
13671368
NULL,
13681369
"INSERT INTO files (path, sha256, uname, gname, "
1369-
"perm, fflags, symlink_target, package_id) "
1370-
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
1371-
"TTTTIITI",
1370+
"perm, fflags, symlink_target, mtime, package_id) "
1371+
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
1372+
"TTTTIITII",
13721373
},
13731374
[FILES_REPLACE] = {
13741375
NULL,
13751376
"INSERT OR REPLACE INTO files (path, sha256, uname, gname, "
1376-
"perm, fflags, symlink_target, package_id) "
1377-
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
1378-
"TTTTIITI",
1377+
"perm, fflags, symlink_target, mtime, package_id) "
1378+
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
1379+
"TTTTIITII",
13791380
},
13801381
[DIRS1] = {
13811382
NULL,
13821383
"INSERT OR IGNORE INTO directories(path, uname, gname, perm, fflags) "
13831384
"VALUES(?1,?2,?3,?4,?5)",
1384-
"TTTII",
1385+
"TTTIII",
13851386
},
13861387
[DIRS2] = {
13871388
NULL,
@@ -1770,6 +1771,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
17701771
_pkgdb_empty_str_null(file->gname),
17711772
file->perm, file->fflags,
17721773
_pkgdb_empty_str_null(file->symlink_target),
1774+
file->time[1].tv_sec,
17731775
package_id);
17741776
if (ret == SQLITE_DONE)
17751777
continue;
@@ -1792,6 +1794,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
17921794
_pkgdb_empty_str_null(file->gname),
17931795
file->perm, file->fflags,
17941796
_pkgdb_empty_str_null(file->symlink_target),
1797+
file->time[1].tv_sec,
17951798
package_id);
17961799
pkgdb_it_free(it);
17971800
if (ret == SQLITE_DONE)
@@ -1851,7 +1854,8 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
18511854
while (pkg_dirs(pkg, &dir) == EPKG_OK) {
18521855
if (run_prstmt(DIRS1, dir->path, _pkgdb_empty_str_null(dir->uname),
18531856
_pkgdb_empty_str_null(dir->gname),
1854-
dir->perm, dir->fflags) != SQLITE_DONE) {
1857+
dir->perm, dir->fflags,
1858+
dir->time[1].tv_sec) != SQLITE_DONE) {
18551859
ERROR_STMT_SQLITE(s, STMT(DIRS1));
18561860
goto cleanup;
18571861
}

libpkg/pkgdb_iterator.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pkgdb_load_files(sqlite3 *sqlite, struct pkg *pkg)
388388
sqlite3_stmt *stmt = NULL;
389389
int ret;
390390
const char sql[] = ""
391-
"SELECT path, sha256, uname, gname, perm, fflags, symlink_target "
391+
"SELECT path, sha256, uname, gname, perm, fflags, mtime, symlink_target "
392392
" FROM files"
393393
" WHERE package_id = ?1"
394394
" ORDER BY PATH ASC";
@@ -418,9 +418,10 @@ pkgdb_load_files(sqlite3 *sqlite, struct pkg *pkg)
418418
const char *gname = sqlite3_column_text(stmt, 3);
419419
mode_t perm = sqlite3_column_int64(stmt, 4);
420420
u_long fflags = sqlite3_column_int64(stmt, 5);
421-
const char *symlink_target = sqlite3_column_text(stmt, 6);
421+
time_t mtime = sqlite3_column_int64(stmt, 6);
422+
const char *symlink_target = sqlite3_column_text(stmt, 7);
422423
pkg_addfile_attr(pkg, path, sum, uname, gname, perm, fflags,
423-
symlink_target, false);
424+
mtime, symlink_target, false);
424425
}
425426
sqlite3_finalize(stmt);
426427

0 commit comments

Comments
 (0)