Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 5 Aug 2018 04:40:14 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r337343 - in projects/bectl: lib/libbe sbin/bectl
Message-ID:  <201808050440.w754eEGF057808@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Sun Aug  5 04:40:13 2018
New Revision: 337343
URL: https://svnweb.freebsd.org/changeset/base/337343

Log:
  bectl: Implement -D ("space if origin datasets were deleted")
  
  This also accomplishes the following:
  
  - Proxy through zfs_nicenum as be_nicenum, because it looks better than
    humanize_number and would presumably be useful to other libbe consumers.
  
  - Rename be_get_snapshot_props to be_get_dataset_props, make it more useful

Modified:
  projects/bectl/lib/libbe/be.c
  projects/bectl/lib/libbe/be.h
  projects/bectl/lib/libbe/be_info.c
  projects/bectl/lib/libbe/libbe.3
  projects/bectl/sbin/bectl/bectl.c

Modified: projects/bectl/lib/libbe/be.c
==============================================================================
--- projects/bectl/lib/libbe/be.c	Sun Aug  5 04:20:52 2018	(r337342)
+++ projects/bectl/lib/libbe/be.c	Sun Aug  5 04:40:13 2018	(r337343)
@@ -171,6 +171,15 @@ libbe_close(libbe_handle_t *lbh)
 	free(lbh);
 }
 
+/*
+ * Proxy through to libzfs for the moment.
+ */
+void
+be_nicenum(uint64_t num, char *buf, size_t buflen)
+{
+
+	zfs_nicenum(num, buf, buflen);
+}
 
 /*
  * Destroy the boot environment or snapshot specified by the name

Modified: projects/bectl/lib/libbe/be.h
==============================================================================
--- projects/bectl/lib/libbe/be.h	Sun Aug  5 04:20:52 2018	(r337342)
+++ projects/bectl/lib/libbe/be.h	Sun Aug  5 04:40:13 2018	(r337343)
@@ -67,7 +67,7 @@ const char *be_nextboot_path(libbe_handle_t *);
 const char *be_root_path(libbe_handle_t *);
 
 int be_get_bootenv_props(libbe_handle_t *, nvlist_t *);
-int be_get_snapshot_props(libbe_handle_t *, const char *, nvlist_t *);
+int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *);
 int be_prop_list_alloc(nvlist_t **be_list);
 void be_prop_list_free(nvlist_t *be_list);
 
@@ -116,5 +116,6 @@ int be_export(libbe_handle_t *, char *, int fd);
 int be_import(libbe_handle_t *, char *, int fd);
 
 int be_add_child(libbe_handle_t *, char *, bool);
+void be_nicenum(uint64_t num, char *buf, size_t buflen);
 
 #endif  /* _LIBBE_H */

Modified: projects/bectl/lib/libbe/be_info.c
==============================================================================
--- projects/bectl/lib/libbe/be_info.c	Sun Aug  5 04:20:52 2018	(r337342)
+++ projects/bectl/lib/libbe/be_info.c	Sun Aug  5 04:40:13 2018	(r337343)
@@ -99,7 +99,7 @@ be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *ds
 }
 
 int
-be_get_snapshot_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
+be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
 {
 	zfs_handle_t *snap_hdl;
 	prop_data_t data;
@@ -109,7 +109,7 @@ be_get_snapshot_props(libbe_handle_t *lbh, const char 
 	data.list = props;
 	data.single_object = true;
 	if ((snap_hdl = zfs_open(lbh->lzh, name,
-	    ZFS_TYPE_SNAPSHOT)) == NULL)
+	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL)
 		return (BE_ERR_ZFSOPEN);
 
 	ret = prop_list_builder_cb(snap_hdl, &data);

Modified: projects/bectl/lib/libbe/libbe.3
==============================================================================
--- projects/bectl/lib/libbe/libbe.3	Sun Aug  5 04:20:52 2018	(r337342)
+++ projects/bectl/lib/libbe/libbe.3	Sun Aug  5 04:40:13 2018	(r337343)
@@ -88,6 +88,9 @@ of state to be retained, such as errors from previous 
 .Ft int
 .Fn be_destroy "libbe_handle_t *, char *, int" ;
 .Pp
+.Ft void
+.Fn be_nicenum "uint64_t, char *, size_t" ;
+.Pp
 .\" TODO: Write up of mount options
 .\" typedef enum {
 .\"	BE_MNT_FORCE		= 1 << 0,
@@ -139,7 +142,7 @@ of state to be retained, such as errors from previous 
 .Fn be_get_bootenv_props "libbe_handle_t *, nvlist_t *" ;
 .Pp
 .Ft int
-.Fn be_get_snapshot_props "libbe_handle_t *, const char *, nvlist_t *" ;
+.Fn be_get_dataset_props "libbe_handle_t *, const char *, nvlist_t *" ;
 .Pp
 .Ft void
 .Fn be_prop_list_free "nvlist_t *" ;

Modified: projects/bectl/sbin/bectl/bectl.c
==============================================================================
--- projects/bectl/sbin/bectl/bectl.c	Sun Aug  5 04:20:52 2018	(r337342)
+++ projects/bectl/sbin/bectl/bectl.c	Sun Aug  5 04:40:13 2018	(r337343)
@@ -445,7 +445,7 @@ get_origin_props(nvlist_t *dsprops, nvlist_t **originp
 			    "bectl list: failed to allocate origin prop nvlist\n");
 			return (NULL);
 		}
-		if (be_get_snapshot_props(be, propstr, *originprops) != 0) {
+		if (be_get_dataset_props(be, propstr, *originprops) != 0) {
 			/* XXX TODO: Real errors */
 			fprintf(stderr,
 			    "bectl list: failed to fetch origin properties\n");
@@ -471,6 +471,40 @@ print_padding(const char *fval, int colsz, struct prin
 	printf("%*s ", colsz, "");
 }
 
+static unsigned long long
+dataset_space(const char *oname)
+{
+	unsigned long long space;
+	char *dsname, *propstr, *sep;
+	nvlist_t *dsprops;
+
+	space = 0;
+	dsname = strdup(oname);
+	if (dsname == NULL)
+		return (0);
+
+	if ((sep = strchr(dsname, '@')) != NULL)
+		*sep = '\0';
+
+	if (be_prop_list_alloc(&dsprops) != 0) {
+		free(dsname);
+		return (0);
+	}
+
+	if (be_get_dataset_props(be, dsname, dsprops) != 0) {
+		nvlist_free(dsprops);
+		free(dsname);
+		return (0);
+	}
+
+	if (nvlist_lookup_string(dsprops, "used", &propstr) == 0)
+		space = strtoull(propstr, NULL, 10);
+
+	nvlist_free(dsprops);
+	free(dsname);
+	return (space);
+}
+
 static void
 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
 {
@@ -529,7 +563,7 @@ print_info(const char *name, nvlist_t *dsprops, struct
 		print_padding("-", pc->mount_colsz, pc);
 	}
 
-	get_origin_props(dsprops, &originprops);
+	oname = get_origin_props(dsprops, &originprops);
 
 	if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
 		space = strtoull(propstr, NULL, 10);
@@ -538,9 +572,12 @@ print_info(const char *name, nvlist_t *dsprops, struct
 		    nvlist_lookup_string(originprops, "used", &propstr) == 0)
 			space += strtoull(propstr, NULL, 10);
 
+		if (!pc->show_all_datasets && pc->show_space && oname != NULL)
+			/* Get the used space of the origin's dataset, too. */
+			space += dataset_space(oname);
+
 		/* Alas, there's more to it,. */
-		humanize_number(buf, 6, space, "", HN_AUTOSCALE,
-			HN_DECIMAL | HN_NOSPACE | HN_B);
+		be_nicenum(space, buf, 6);
 		printf("%s", buf);
 		print_padding(buf, pc->space_colsz, pc);
 	} else {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808050440.w754eEGF057808>