MfP4: set/unset tracking for atime, ctime, mtime, and size fields.

This generalizes the existing set/unset tracking for hardlink/symlink
fields and extends it to cover non-string fields.  Eventually, this
will be further extended to cover most fields.

In particular, this is needed to correctly detect when time fields
are missing (for example, reading ustar archives doesn't set atime or
ctime) for proper time restore and is helpful when trying to determine
whether to overwrite data when restoring hardlinks.

This commit updates the tests but not the docs.
This commit is contained in:
Tim Kientzle 2008-09-01 04:54:29 +00:00
parent c7715560f1
commit 32364a7ddb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=182590
4 changed files with 160 additions and 28 deletions

View file

@ -395,8 +395,7 @@ archive_entry_clone(struct archive_entry *entry)
aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
entry2->ae_hardlinkset = entry->ae_hardlinkset;
entry2->ae_symlinkset = entry->ae_symlinkset;
entry2->ae_set = entry->ae_set;
aes_copy(&entry2->ae_uname, &entry->ae_uname);
/* Copy ACL data over. */
@ -455,12 +454,24 @@ archive_entry_atime_nsec(struct archive_entry *entry)
return (entry->ae_stat.aest_atime_nsec);
}
int
archive_entry_atime_is_set(struct archive_entry *entry)
{
return (entry->ae_set & AE_SET_ATIME);
}
time_t
archive_entry_ctime(struct archive_entry *entry)
{
return (entry->ae_stat.aest_ctime);
}
int
archive_entry_ctime_is_set(struct archive_entry *entry)
{
return (entry->ae_set & AE_SET_CTIME);
}
long
archive_entry_ctime_nsec(struct archive_entry *entry)
{
@ -562,17 +573,17 @@ archive_entry_gname_w(struct archive_entry *entry)
const char *
archive_entry_hardlink(struct archive_entry *entry)
{
if (!entry->ae_hardlinkset)
return (NULL);
return (aes_get_mbs(&entry->ae_hardlink));
if (entry->ae_set & AE_SET_HARDLINK)
return (aes_get_mbs(&entry->ae_hardlink));
return (NULL);
}
const wchar_t *
archive_entry_hardlink_w(struct archive_entry *entry)
{
if (!entry->ae_hardlinkset)
return (NULL);
return (aes_get_wcs(&entry->ae_hardlink));
if (entry->ae_set & AE_SET_HARDLINK)
return (aes_get_wcs(&entry->ae_hardlink));
return (NULL);
}
ino_t
@ -599,6 +610,12 @@ archive_entry_mtime_nsec(struct archive_entry *entry)
return (entry->ae_stat.aest_mtime_nsec);
}
int
archive_entry_mtime_is_set(struct archive_entry *entry)
{
return (entry->ae_set & AE_SET_MTIME);
}
unsigned int
archive_entry_nlink(struct archive_entry *entry)
{
@ -651,6 +668,12 @@ archive_entry_size(struct archive_entry *entry)
return (entry->ae_stat.aest_size);
}
int
archive_entry_size_is_set(struct archive_entry *entry)
{
return (entry->ae_set & AE_SET_SIZE);
}
const char *
archive_entry_sourcepath(struct archive_entry *entry)
{
@ -660,17 +683,17 @@ archive_entry_sourcepath(struct archive_entry *entry)
const char *
archive_entry_symlink(struct archive_entry *entry)
{
if (!entry->ae_symlinkset)
return (NULL);
return (aes_get_mbs(&entry->ae_symlink));
if (entry->ae_set & AE_SET_SYMLINK)
return (aes_get_mbs(&entry->ae_symlink));
return (NULL);
}
const wchar_t *
archive_entry_symlink_w(struct archive_entry *entry)
{
if (!entry->ae_symlinkset)
return (NULL);
return (aes_get_wcs(&entry->ae_symlink));
if (entry->ae_set & AE_SET_SYMLINK)
return (aes_get_wcs(&entry->ae_symlink));
return (NULL);
}
uid_t
@ -773,7 +796,9 @@ archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
{
aes_set_mbs(&entry->ae_hardlink, target);
if (target != NULL)
entry->ae_hardlinkset = 1;
entry->ae_set |= AE_SET_HARDLINK;
else
entry->ae_set &= ~AE_SET_HARDLINK;
}
void
@ -781,7 +806,9 @@ archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
{
aes_copy_mbs(&entry->ae_hardlink, target);
if (target != NULL)
entry->ae_hardlinkset = 1;
entry->ae_set |= AE_SET_HARDLINK;
else
entry->ae_set &= ~AE_SET_HARDLINK;
}
void
@ -789,25 +816,43 @@ archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target
{
aes_copy_wcs(&entry->ae_hardlink, target);
if (target != NULL)
entry->ae_hardlinkset = 1;
entry->ae_set |= AE_SET_HARDLINK;
else
entry->ae_set &= ~AE_SET_HARDLINK;
}
void
archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
{
entry->stat_valid = 0;
entry->ae_set |= AE_SET_ATIME;
entry->ae_stat.aest_atime = t;
entry->ae_stat.aest_atime_nsec = ns;
}
void
archive_entry_unset_atime(struct archive_entry *entry)
{
archive_entry_set_atime(entry, 0, 0);
entry->ae_set &= ~AE_SET_ATIME;
}
void
archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
{
entry->stat_valid = 0;
entry->ae_set |= AE_SET_CTIME;
entry->ae_stat.aest_ctime = t;
entry->ae_stat.aest_ctime_nsec = ns;
}
void
archive_entry_unset_ctime(struct archive_entry *entry)
{
archive_entry_set_ctime(entry, 0, 0);
entry->ae_set &= ~AE_SET_CTIME;
}
void
archive_entry_set_dev(struct archive_entry *entry, dev_t d)
{
@ -836,7 +881,7 @@ archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
void
archive_entry_set_link(struct archive_entry *entry, const char *target)
{
if (entry->ae_symlinkset)
if (entry->ae_set & AE_SET_SYMLINK)
aes_set_mbs(&entry->ae_symlink, target);
else
aes_set_mbs(&entry->ae_hardlink, target);
@ -846,7 +891,7 @@ archive_entry_set_link(struct archive_entry *entry, const char *target)
void
archive_entry_copy_link(struct archive_entry *entry, const char *target)
{
if (entry->ae_symlinkset)
if (entry->ae_set & AE_SET_SYMLINK)
aes_copy_mbs(&entry->ae_symlink, target);
else
aes_copy_mbs(&entry->ae_hardlink, target);
@ -856,7 +901,7 @@ archive_entry_copy_link(struct archive_entry *entry, const char *target)
void
archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
{
if (entry->ae_symlinkset)
if (entry->ae_set & AE_SET_SYMLINK)
aes_copy_wcs(&entry->ae_symlink, target);
else
aes_copy_wcs(&entry->ae_hardlink, target);
@ -865,7 +910,7 @@ archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
int
archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
{
if (entry->ae_symlinkset)
if (entry->ae_set & AE_SET_SYMLINK)
return (aes_update_utf8(&entry->ae_symlink, target));
else
return (aes_update_utf8(&entry->ae_hardlink, target));
@ -882,10 +927,18 @@ void
archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
{
entry->stat_valid = 0;
entry->ae_set |= AE_SET_MTIME;
entry->ae_stat.aest_mtime = m;
entry->ae_stat.aest_mtime_nsec = ns;
}
void
archive_entry_unset_mtime(struct archive_entry *entry)
{
archive_entry_set_mtime(entry, 0, 0);
entry->ae_set &= ~AE_SET_MTIME;
}
void
archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
{
@ -954,6 +1007,14 @@ archive_entry_set_size(struct archive_entry *entry, int64_t s)
{
entry->stat_valid = 0;
entry->ae_stat.aest_size = s;
entry->ae_set |= AE_SET_SIZE;
}
void
archive_entry_unset_size(struct archive_entry *entry)
{
archive_entry_set_size(entry, 0);
entry->ae_set &= ~AE_SET_SIZE;
}
void
@ -967,7 +1028,9 @@ archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
{
aes_set_mbs(&entry->ae_symlink, linkname);
if (linkname != NULL)
entry->ae_symlinkset = 1;
entry->ae_set |= AE_SET_SYMLINK;
else
entry->ae_set &= ~AE_SET_SYMLINK;
}
void
@ -975,7 +1038,9 @@ archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
{
aes_copy_mbs(&entry->ae_symlink, linkname);
if (linkname != NULL)
entry->ae_symlinkset = 1;
entry->ae_set |= AE_SET_SYMLINK;
else
entry->ae_set &= ~AE_SET_SYMLINK;
}
void
@ -983,7 +1048,9 @@ archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linknam
{
aes_copy_wcs(&entry->ae_symlink, linkname);
if (linkname != NULL)
entry->ae_symlinkset = 1;
entry->ae_set |= AE_SET_SYMLINK;
else
entry->ae_set &= ~AE_SET_SYMLINK;
}
void

View file

@ -1,5 +1,5 @@
/*-
* Copyright (c) 2003-2007 Tim Kientzle
* Copyright (c) 2003-2008 Tim Kientzle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -152,12 +152,29 @@ __LA_DECL struct archive_entry *archive_entry_new(void);
/*
* Retrieve fields from an archive_entry.
*
* There are a number of implicit conversions among these fields. For
* example, if a regular string field is set and you read the _w wide
* character field, the entry will implicitly convert narrow-to-wide
* using the current locale. Similarly, dev values are automatically
* updated when you write devmajor or devminor and vice versa.
*
* In addition, fields can be "set" or "unset." Unset string fields
* return NULL, non-string fields have _is_set() functions to test
* whether they've been set. You can "unset" a string field by
* assigning NULL; non-string fields have _unset() functions to
* unset them.
*
* Note: There is one ambiguity in the above; string fields will
* also return NULL when implicit character set conversions fail.
* This is usually what you want.
*/
__LA_DECL time_t archive_entry_atime(struct archive_entry *);
__LA_DECL long archive_entry_atime_nsec(struct archive_entry *);
__LA_DECL int archive_entry_atime_is_set(struct archive_entry *);
__LA_DECL time_t archive_entry_ctime(struct archive_entry *);
__LA_DECL long archive_entry_ctime_nsec(struct archive_entry *);
__LA_DECL int archive_entry_ctime_is_set(struct archive_entry *);
__LA_DECL dev_t archive_entry_dev(struct archive_entry *);
__LA_DECL dev_t archive_entry_devmajor(struct archive_entry *);
__LA_DECL dev_t archive_entry_devminor(struct archive_entry *);
@ -175,6 +192,7 @@ __LA_DECL __LA_INO_T archive_entry_ino(struct archive_entry *);
__LA_DECL __LA_MODE_T archive_entry_mode(struct archive_entry *);
__LA_DECL time_t archive_entry_mtime(struct archive_entry *);
__LA_DECL long archive_entry_mtime_nsec(struct archive_entry *);
__LA_DECL int archive_entry_mtime_is_set(struct archive_entry *);
__LA_DECL unsigned int archive_entry_nlink(struct archive_entry *);
__LA_DECL const char *archive_entry_pathname(struct archive_entry *);
__LA_DECL const wchar_t *archive_entry_pathname_w(struct archive_entry *);
@ -183,6 +201,7 @@ __LA_DECL dev_t archive_entry_rdevmajor(struct archive_entry *);
__LA_DECL dev_t archive_entry_rdevminor(struct archive_entry *);
__LA_DECL const char *archive_entry_sourcepath(struct archive_entry *);
__LA_DECL int64_t archive_entry_size(struct archive_entry *);
__LA_DECL int archive_entry_size_is_set(struct archive_entry *);
__LA_DECL const char *archive_entry_strmode(struct archive_entry *);
__LA_DECL const char *archive_entry_symlink(struct archive_entry *);
__LA_DECL const wchar_t *archive_entry_symlink_w(struct archive_entry *);
@ -195,10 +214,16 @@ __LA_DECL const wchar_t *archive_entry_uname_w(struct archive_entry *);
*
* Note that string 'set' functions do not copy the string, only the pointer.
* In contrast, 'copy' functions do copy the object pointed to.
*
* Note: As of libarchive 2.4, 'set' functions do copy the string and
* are therefore exact synonyms for the 'copy' versions. The 'copy'
* names will be retired in libarchive 3.0.
*/
__LA_DECL void archive_entry_set_atime(struct archive_entry *, time_t, long);
__LA_DECL void archive_entry_unset_atime(struct archive_entry *);
__LA_DECL void archive_entry_set_ctime(struct archive_entry *, time_t, long);
__LA_DECL void archive_entry_unset_ctime(struct archive_entry *);
__LA_DECL void archive_entry_set_dev(struct archive_entry *, dev_t);
__LA_DECL void archive_entry_set_devmajor(struct archive_entry *, dev_t);
__LA_DECL void archive_entry_set_devminor(struct archive_entry *, dev_t);
@ -226,6 +251,7 @@ __LA_DECL void archive_entry_copy_link_w(struct archive_entry *, const wchar_t *
__LA_DECL int archive_entry_update_link_utf8(struct archive_entry *, const char *);
__LA_DECL void archive_entry_set_mode(struct archive_entry *, __LA_MODE_T);
__LA_DECL void archive_entry_set_mtime(struct archive_entry *, time_t, long);
__LA_DECL void archive_entry_unset_mtime(struct archive_entry *);
__LA_DECL void archive_entry_set_nlink(struct archive_entry *, unsigned int);
__LA_DECL void archive_entry_set_pathname(struct archive_entry *, const char *);
__LA_DECL void archive_entry_copy_pathname(struct archive_entry *, const char *);
@ -236,6 +262,7 @@ __LA_DECL void archive_entry_set_rdev(struct archive_entry *, dev_t);
__LA_DECL void archive_entry_set_rdevmajor(struct archive_entry *, dev_t);
__LA_DECL void archive_entry_set_rdevminor(struct archive_entry *, dev_t);
__LA_DECL void archive_entry_set_size(struct archive_entry *, int64_t);
__LA_DECL void archive_entry_unset_size(struct archive_entry *);
__LA_DECL void archive_entry_copy_sourcepath(struct archive_entry *, const char *);
__LA_DECL void archive_entry_set_symlink(struct archive_entry *, const char *);
__LA_DECL void archive_entry_copy_symlink(struct archive_entry *, const char *);
@ -257,6 +284,7 @@ __LA_DECL int archive_entry_update_uname_utf8(struct archive_entry *, const char
__LA_DECL const struct stat *archive_entry_stat(struct archive_entry *);
__LA_DECL void archive_entry_copy_stat(struct archive_entry *, const struct stat *);
/*
* ACL routines. This used to simply store and return text-format ACL
* strings, but that proved insufficient for a number of reasons:

View file

@ -136,6 +136,14 @@ struct archive_entry {
dev_t aest_rdevminor;
} ae_stat;
int ae_set; /* bitmap of fields that are currently set */
#define AE_SET_HARDLINK 1
#define AE_SET_SYMLINK 2
#define AE_SET_ATIME 4
#define AE_SET_CTIME 8
#define AE_SET_MTIME 16
#define AE_SET_SIZE 64
/*
* Use aes here so that we get transparent mbs<->wcs conversions.
*/
@ -147,8 +155,6 @@ struct archive_entry {
struct aes ae_pathname; /* Name of entry */
struct aes ae_symlink; /* symlink contents */
struct aes ae_uname; /* Name of owner */
unsigned char ae_hardlinkset;
unsigned char ae_symlinkset;
/* Not used within libarchive; useful for some clients. */
struct aes ae_sourcepath; /* Path this entry is sourced from. */

View file

@ -69,14 +69,25 @@ DEFINE_TEST(test_entry)
* The following tests are ordered alphabetically by the
* name of the field.
*/
/* atime */
archive_entry_set_atime(e, 13579, 24680);
assertEqualInt(archive_entry_atime(e), 13579);
assertEqualInt(archive_entry_atime_nsec(e), 24680);
archive_entry_unset_atime(e);
assertEqualInt(archive_entry_atime(e), 0);
assertEqualInt(archive_entry_atime_nsec(e), 0);
assert(!archive_entry_atime_is_set(e));
/* ctime */
archive_entry_set_ctime(e, 13580, 24681);
assertEqualInt(archive_entry_ctime(e), 13580);
assertEqualInt(archive_entry_ctime_nsec(e), 24681);
archive_entry_unset_ctime(e);
assertEqualInt(archive_entry_ctime(e), 0);
assertEqualInt(archive_entry_ctime_nsec(e), 0);
assert(!archive_entry_ctime_is_set(e));
#if ARCHIVE_VERSION_STAMP >= 1009000
/* dev */
archive_entry_set_dev(e, 235);
@ -85,6 +96,7 @@ DEFINE_TEST(test_entry)
skipping("archive_entry_dev()");
#endif
/* devmajor/devminor are tested specially below. */
#if ARCHIVE_VERSION_STAMP >= 1009000
/* filetype */
archive_entry_set_filetype(e, AE_IFREG);
@ -92,10 +104,13 @@ DEFINE_TEST(test_entry)
#else
skipping("archive_entry_filetype()");
#endif
/* fflags are tested specially below */
/* gid */
archive_entry_set_gid(e, 204);
assertEqualInt(archive_entry_gid(e), 204);
/* gname */
archive_entry_set_gname(e, "group");
assertEqualString(archive_entry_gname(e), "group");
@ -104,6 +119,7 @@ DEFINE_TEST(test_entry)
assertEqualWString(archive_entry_gname_w(e), L"wgroup");
memset(wbuff, 0, sizeof(wbuff));
assertEqualWString(archive_entry_gname_w(e), L"wgroup");
/* hardlink */
archive_entry_set_hardlink(e, "hardlinkname");
assertEqualString(archive_entry_hardlink(e), "hardlinkname");
@ -158,10 +174,16 @@ DEFINE_TEST(test_entry)
/* mode */
archive_entry_set_mode(e, 0123456);
assertEqualInt(archive_entry_mode(e), 0123456);
/* mtime */
archive_entry_set_mtime(e, 13581, 24682);
assertEqualInt(archive_entry_mtime(e), 13581);
assertEqualInt(archive_entry_mtime_nsec(e), 24682);
archive_entry_unset_mtime(e);
assertEqualInt(archive_entry_mtime(e), 0);
assertEqualInt(archive_entry_mtime_nsec(e), 0);
assert(!archive_entry_mtime_is_set(e));
#if ARCHIVE_VERSION_STAMP >= 1009000
/* nlink */
archive_entry_set_nlink(e, 736);
@ -169,6 +191,7 @@ DEFINE_TEST(test_entry)
#else
skipping("archive_entry_nlink()");
#endif
/* pathname */
archive_entry_set_pathname(e, "path");
assertEqualString(archive_entry_pathname(e), "path");
@ -184,6 +207,7 @@ DEFINE_TEST(test_entry)
assertEqualWString(archive_entry_pathname_w(e), L"wpath");
memset(wbuff, 0, sizeof(wbuff));
assertEqualWString(archive_entry_pathname_w(e), L"wpath");
#if ARCHIVE_VERSION_STAMP >= 1009000
/* rdev */
archive_entry_set_rdev(e, 532);
@ -192,9 +216,14 @@ DEFINE_TEST(test_entry)
skipping("archive_entry_rdev()");
#endif
/* rdevmajor/rdevminor are tested specially below. */
/* size */
archive_entry_set_size(e, 987654321);
assertEqualInt(archive_entry_size(e), 987654321);
archive_entry_unset_size(e);
assertEqualInt(archive_entry_size(e), 0);
assert(!archive_entry_size_is_set(e));
/* symlink */
archive_entry_set_symlink(e, "symlinkname");
assertEqualString(archive_entry_symlink(e), "symlinkname");
@ -207,9 +236,11 @@ DEFINE_TEST(test_entry)
#endif
archive_entry_copy_symlink_w(e, L"wsymlink");
assertEqualWString(archive_entry_symlink_w(e), L"wsymlink");
/* uid */
archive_entry_set_uid(e, 83);
assertEqualInt(archive_entry_uid(e), 83);
/* uname */
archive_entry_set_uname(e, "user");
assertEqualString(archive_entry_uname(e), "user");