diff options
author | Adam Spragg <adam@spra.gg> | 2022-05-18 16:35:35 +0100 |
---|---|---|
committer | Adam Spragg <adam@spra.gg> | 2022-05-24 10:12:33 +0100 |
commit | 7292804c1bafd43389defc0d7b4352ada666d626 (patch) | |
tree | 9e0390c7f45dd20b7db92d3f64bdcecaa3d9c1dc /src/metaentry.c | |
parent | ce62a76e4570b6368384b3995c1ba106389df454 (diff) |
Add ability to not save mtime in metadata files
If you're storing metadata in a version control system with multiple
branches, mtime differences are going to produce a whole bunch of
conflicts that you likely don't care about. This allows you to not
save mtime and avoid those.
Note that we use a sentinel value of -1 for the mentry `mtimensec` field
to indicate this in the data, as all values of `mtime` are theoretically
valid, but `mtimensec` must always be between 0 and 999,999,999 in the
real world.
I'm not 100% sure about the mechanism for selecting this feature. The
legacy behaviour for metastore was to save mtimes in the metadata files,
but ignore them for compare/apply by default, with a `--mtime` option to
use the mtime data.
Keeping the legacy behaviour for backwards compatibility, but adding a
`--no-mtime` option to ignore mtimes when saving felt like a reasonable
way of making this happen, but something about it doesn't feel great.
Maybe I just didn't figure out how to make the documentation clear
enough. ¯\_(ツ)_/¯
Diffstat (limited to 'src/metaentry.c')
-rw-r--r-- | src/metaentry.c | 145 |
1 files changed, 86 insertions, 59 deletions
diff --git a/src/metaentry.c b/src/metaentry.c index 7f1012f..9956f7f 100644 --- a/src/metaentry.c +++ b/src/metaentry.c @@ -193,7 +193,7 @@ mentries_print(const struct metahash *mhash) /* Creates a metaentry for the file/dir/etc at path */ struct metaentry * -mentry_create(const char *path) +mentry_create(const char *path, int with_mtime) { #if !defined(NO_XATTR) || !(NO_XATTR+0) ssize_t lsize, vsize; @@ -233,8 +233,14 @@ mentry_create(const char *path) mentry->owner = xstrdup(pbuf->pw_name); mentry->group = xstrdup(gbuf->gr_name); mentry->mode = sbuf.st_mode & 0177777; - mentry->mtime = sbuf.st_mtim.tv_sec; - mentry->mtimensec = sbuf.st_mtim.tv_nsec; + if (with_mtime) { + mentry->mtime = sbuf.st_mtim.tv_sec; + mentry->mtimensec = sbuf.st_mtim.tv_nsec; + } + else { + mentry->mtime = 0; + mentry->mtimensec = MTIME_NONE; + } /* symlinks have no xattrs */ if (S_ISLNK(mentry->mode)) @@ -364,7 +370,7 @@ mentries_recurse(const char *path, struct metahash *mhash, msettings *st) return; } - mentry = mentry_create(path); + mentry = mentry_create(path, st->do_mtime >= 0); if (!mentry) return; @@ -491,12 +497,17 @@ mentries_tofile_v1(const struct metahash *mhash, FILE * to) fprintf(to, "%.6o", mentry->mode); fputc('\t', to); - gmtime_r(&mentry->mtime, &tm); - strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%dT%H:%M:%S", &tm); - fputs(tmbuf, to); + if (mentry->mtimensec != MTIME_NONE) { + gmtime_r(&mentry->mtime, &tm); + strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%dT%H:%M:%S", &tm); + fputs(tmbuf, to); - fputc('.', to); - fprintf(to, "%.9ldZ", mentry->mtimensec); + fputc('.', to); + fprintf(to, "%.9ldZ", mentry->mtimensec); + } + else { + fputc('0', to); + } for (i = 0; i < mentry->xattrs; i++) { fputc('\t', to); @@ -625,50 +636,55 @@ mentries_fromfile_v1(struct metahash **mhash, const char *ptr, const char *max) goto err; if ((mtime = read_string_url(&ptr, max)) == NULL) goto err; - - /* Get the time_t part of `mtime` into mentry->mtime */ - if ((nsec = strptime(mtime, "%Y-%m-%dT%H:%M:%S", &tm)) == NULL) - goto err; - tm.tm_isdst = 0; - mentry->mtime = timegm(&tm); - - /* Check if there's a nanosecond portion of `mtime` */ - if (*nsec == '.') { - /* There is a decimal point in mtime. Get nanoseconds. */ - ++nsec; - mentry->mtimensec = strtol(nsec, &tz, 10); - i = tz - nsec; - while (i < 9) { - /* Too few digits for nanosecond precision. - * e.g. "...T10:15:23.5", which we've parsed as - * 5 nanoseconds, but is actually half a second - * or 500000000 nanoseconds. - * Scale to correct value. - */ - mentry->mtimensec *= 10; - ++i; - } - while (i > 9) { - /* Too many digits for nanosecond precision. - * Scale to correct value - */ - mentry->mtimensec /= 10; - --i; - } + if (strcmp(mtime, "0") == 0) { + mentry->mtime = 0; + mentry->mtimensec = MTIME_NONE; } else { - /* No decimal point. Set nanoseconds to zero */ - mentry->mtimensec = 0; - tz = nsec; - } + /* Get the time_t part of `mtime` into mentry->mtime */ + if ((nsec = strptime(mtime, "%Y-%m-%dT%H:%M:%S", &tm)) == NULL) + goto err; + tm.tm_isdst = 0; + mentry->mtime = timegm(&tm); + + /* Check if there's a nanosecond portion of `mtime` */ + if (*nsec == '.') { + /* There is a decimal point in mtime. Get nanoseconds. */ + ++nsec; + mentry->mtimensec = strtol(nsec, &tz, 10); + i = tz - nsec; + while (i < 9) { + /* Too few digits for nanosecond precision. + * e.g. "...T10:15:23.5", which we've parsed as + * 5 nanoseconds, but is actually half a second + * or 500000000 nanoseconds. + * Scale to correct value. + */ + mentry->mtimensec *= 10; + ++i; + } + while (i > 9) { + /* Too many digits for nanosecond precision. + * Scale to correct value + */ + mentry->mtimensec /= 10; + --i; + } + } + else { + /* No decimal point. Set nanoseconds to zero */ + mentry->mtimensec = 0; + tz = nsec; + } - /* Check for a 'Z' (Zulu/UTC) timesone specifier in `mtime`. */ - if (*tz == 'Z') - ++tz; + /* Check for a 'Z' (Zulu/UTC) timesone specifier in `mtime`. */ + if (*tz == 'Z') + ++tz; - /* Check that we've reached the end of `mtime` */ - if (*tz != '\0') - goto err; + /* Check that we've reached the end of `mtime` */ + if (*tz != '\0') + goto err; + } free(mtime); mtime = NULL; @@ -842,10 +858,12 @@ mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st) if ((left->mode & S_IFMT) != (right->mode & S_IFMT)) retval |= DIFF_TYPE; - if (st->do_mtime && strcmp(left->path, st->metafile) && - ( left->mtime != right->mtime - || left->mtimensec != right->mtimensec) - ) + if (st->do_mtime > 0 + && left->mtimensec != MTIME_NONE + && right->mtimensec != MTIME_NONE + && strcmp(left->path, st->metafile) + && (left->mtime != right->mtime + || left->mtimensec != right->mtimensec)) retval |= DIFF_MTIME; if (mentry_compare_xattr(left, right)) { @@ -898,19 +916,28 @@ mentries_dump(struct metahash *mhash) const struct metaentry *mentry; char mode[11 + 1] = ""; char date[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = ""; - char zone[5 + 1] = ""; + char nsec[1 + 9 + 1] = ""; + char zone[1 + 5 + 1] = ""; struct tm cal; for (int key = 0; key < HASH_INDEXES; key++) { for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) { strmode(mentry->mode, mode); - localtime_r(&mentry->mtime, &cal); - strftime(date, sizeof(date), "%F %T", &cal); - strftime(zone, sizeof(zone), "%z", &cal); - printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n", + if (mentry->mtimensec == MTIME_NONE) { + snprintf(date, sizeof(date), "%19s", ""); + snprintf(nsec, sizeof(nsec), "%10s", ""); + snprintf(zone, sizeof(zone), "%6s", ""); + } + else { + localtime_r(&mentry->mtime, &cal); + strftime(date, sizeof(date), "%F %T", &cal); + snprintf(nsec, sizeof(nsec), ".%09ld", mentry->mtimensec); + strftime(zone, sizeof(zone), " %z", &cal); + } + printf("%s\t%s\t%s\t%s%s%s\t%s%s\n", mode, mentry->owner, mentry->group, - date, mentry->mtimensec, zone, + date, nsec, zone, mentry->path, S_ISDIR(mentry->mode) ? "/" : ""); for (unsigned i = 0; i < mentry->xattrs; i++) { printf("\t\t\t\t%s%s\t%s=", |