Adds the -p option to zfs holds

This allows for printing a machine-readable, accurate to the second,
hold creation time in the form of a unix epoch timestamp.

Additionally, updates relevant documentation and man pages accordingly.

Reviewed-by: Allan Jude <allan@klarasystems.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Mohamed Tawfik <m_tawfik@aucegypt.edu>
Closes #13690
Closes #14152
This commit is contained in:
Mohamed Tawfik 2022-11-08 20:08:21 +02:00 committed by GitHub
parent ecbf02791f
commit 41715771b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 11 deletions

View File

@ -392,7 +392,7 @@ get_usage(zfs_help_t idx)
case HELP_HOLD: case HELP_HOLD:
return (gettext("\thold [-r] <tag> <snapshot> ...\n")); return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
case HELP_HOLDS: case HELP_HOLDS:
return (gettext("\tholds [-rH] <snapshot> ...\n")); return (gettext("\tholds [-rHp] <snapshot> ...\n"));
case HELP_RELEASE: case HELP_RELEASE:
return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
case HELP_DIFF: case HELP_DIFF:
@ -6456,7 +6456,8 @@ typedef struct holds_cbdata {
* *
*/ */
static void static void
print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl) print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl,
boolean_t parsable)
{ {
int i; int i;
nvpair_t *nvp = NULL; nvpair_t *nvp = NULL;
@ -6493,11 +6494,23 @@ print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
gettext(STRFTIME_FMT_STR), &t); gettext(STRFTIME_FMT_STR), &t);
if (scripted) { if (scripted) {
(void) printf("%s\t%s\t%s\n", zname, if (parsable) {
tagname, tsbuf); (void) printf("%s\t%s\t%ld\n", zname,
tagname, time);
} else {
(void) printf("%s\t%s\t%s\n", zname,
tagname, tsbuf);
}
} else { } else {
(void) printf("%-*s %-*s %s\n", nwidth, if (parsable) {
zname, tagwidth, tagname, tsbuf); (void) printf("%-*s %-*s %ld\n",
nwidth, zname, tagwidth,
tagname, time);
} else {
(void) printf("%-*s %-*s %s\n",
nwidth, zname, tagwidth,
tagname, tsbuf);
}
} }
} }
} }
@ -6544,10 +6557,11 @@ holds_callback(zfs_handle_t *zhp, void *data)
} }
/* /*
* zfs holds [-rH] <snap> ... * zfs holds [-rHp] <snap> ...
* *
* -r Lists holds that are set on the named snapshots recursively. * -r Lists holds that are set on the named snapshots recursively.
* -H Scripted mode; elide headers and separate columns by tabs. * -H Scripted mode; elide headers and separate columns by tabs.
* -p Display values in parsable (literal) format.
*/ */
static int static int
zfs_do_holds(int argc, char **argv) zfs_do_holds(int argc, char **argv)
@ -6556,6 +6570,7 @@ zfs_do_holds(int argc, char **argv)
boolean_t errors = B_FALSE; boolean_t errors = B_FALSE;
boolean_t scripted = B_FALSE; boolean_t scripted = B_FALSE;
boolean_t recursive = B_FALSE; boolean_t recursive = B_FALSE;
boolean_t parsable = B_FALSE;
int types = ZFS_TYPE_SNAPSHOT; int types = ZFS_TYPE_SNAPSHOT;
holds_cbdata_t cb = { 0 }; holds_cbdata_t cb = { 0 };
@ -6565,7 +6580,7 @@ zfs_do_holds(int argc, char **argv)
int flags = 0; int flags = 0;
/* check options */ /* check options */
while ((c = getopt(argc, argv, "rH")) != -1) { while ((c = getopt(argc, argv, "rHp")) != -1) {
switch (c) { switch (c) {
case 'r': case 'r':
recursive = B_TRUE; recursive = B_TRUE;
@ -6573,6 +6588,9 @@ zfs_do_holds(int argc, char **argv)
case 'H': case 'H':
scripted = B_TRUE; scripted = B_TRUE;
break; break;
case 'p':
parsable = B_TRUE;
break;
case '?': case '?':
(void) fprintf(stderr, gettext("invalid option '%c'\n"), (void) fprintf(stderr, gettext("invalid option '%c'\n"),
optopt); optopt);
@ -6626,7 +6644,8 @@ zfs_do_holds(int argc, char **argv)
/* /*
* 2. print holds data * 2. print holds data
*/ */
print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl); print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl,
parsable);
if (nvlist_empty(nvl)) if (nvlist_empty(nvl))
(void) fprintf(stderr, gettext("no datasets available\n")); (void) fprintf(stderr, gettext("no datasets available\n"));

View File

@ -43,7 +43,7 @@
.Ar tag Ar snapshot Ns .Ar tag Ar snapshot Ns
.Nm zfs .Nm zfs
.Cm holds .Cm holds
.Op Fl rH .Op Fl rHp
.Ar snapshot Ns .Ar snapshot Ns
.Nm zfs .Nm zfs
.Cm release .Cm release
@ -76,7 +76,7 @@ of all descendent file systems.
.It Xo .It Xo
.Nm zfs .Nm zfs
.Cm holds .Cm holds
.Op Fl rH .Op Fl rHp
.Ar snapshot Ns .Ar snapshot Ns
.Xc .Xc
Lists all existing user references for the given snapshot or snapshots. Lists all existing user references for the given snapshot or snapshots.
@ -86,6 +86,8 @@ Lists the holds that are set on the named descendent snapshots, in addition to
listing the holds on the named snapshot. listing the holds on the named snapshot.
.It Fl H .It Fl H
Do not print headers, use tab-delimited output. Do not print headers, use tab-delimited output.
.It Fl p
Prints holds timestamps as unix epoch timestamps.
.El .El
.It Xo .It Xo
.Nm zfs .Nm zfs