Date: Thu, 13 Jul 2017 11:39:39 GMT From: kneitinger@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r324565 - soc2017/kneitinger/libbe-head/lib/libbe Message-ID: <201707131139.v6DBddo1090078@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kneitinger Date: Thu Jul 13 11:39:37 2017 New Revision: 324565 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=324565 Log: libbe(3): add rename function Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.c soc2017/kneitinger/libbe-head/lib/libbe/be.h soc2017/kneitinger/libbe-head/lib/libbe/be_access.c soc2017/kneitinger/libbe-head/lib/libbe/be_error.c soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h soc2017/kneitinger/libbe-head/lib/libbe/be_info.c Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.c Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.c Thu Jul 13 11:39:37 2017 (r324565) @@ -352,9 +352,73 @@ // TODO: beadm allows commas...they seem like a bad idea though if (isalnum(c) || (c == '-') || (c == '_') || (c == '.') || (c == ',')) { - return (i); + continue; } + return (name[i]); } return (BE_ERR_SUCCESS); } + + +/* + * usage + */ +int +be_rename(libbe_handle_t *lbh, char *old, char *new) +{ + char full_old[MAXPATHLEN]; + char full_new[MAXPATHLEN]; + zfs_handle_t *zfs_hdl; + int err; + + + if (err = be_root_concat(lbh, old, full_old)) { + return (set_error(lbh, err)); + } + if (err = be_root_concat(lbh, new, full_new)) { + return (set_error(lbh, err)); + } + + if (be_validate_name(lbh, new)) { + return (BE_ERR_UNKNOWN); + // TODO set and return correct error + } + + // check if old is active be + if (strcmp(full_new, be_active_path(lbh)) == 0) { + return (BE_ERR_UNKNOWN); + // TODO set and return correct error + } + + if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) { + return (BE_ERR_UNKNOWN); + // TODO set and return correct error + } + + if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) { + return (BE_ERR_UNKNOWN); + // TODO set and return correct error + } + + // TODO: what about mounted bes? + // - if mounted error out unless a force flag is set? + + + if ((zfs_hdl = zfs_open(lbh->lzh, full_old, + ZFS_TYPE_FILESYSTEM)) == NULL) { + return (BE_ERR_UNKNOWN); + // TODO set and return correct error + } + + + // recurse, nounmount, forceunmount + struct renameflags flags = { 0, 0, 0 }; + + // TODO: error log on this call + err = zfs_rename(zfs_hdl, NULL, full_new, flags); + + zfs_close(zfs_hdl); + + return (set_error(lbh, err)); +} Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.h ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.h Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.h Thu Jul 13 11:39:37 2017 (r324565) @@ -61,7 +61,6 @@ const char *be_active_path(libbe_handle_t *); const char *be_root_path(libbe_handle_t *); nvlist_t *be_get_bootenv_props(libbe_handle_t *); -bool be_exists(libbe_handle_t, char *); /* Bootenv creation functions */ int be_create(libbe_handle_t *, char *); Modified: soc2017/kneitinger/libbe-head/lib/libbe/be_access.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be_access.c Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_access.c Thu Jul 13 11:39:37 2017 (r324565) @@ -35,36 +35,36 @@ int be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags) { - char be[MAXPATHLEN]; - zfs_handle_t *zfs_hdl; - char *path; - int mntflags; - int err; + char be[MAXPATHLEN]; + zfs_handle_t *zfs_hdl; + char *path; + int mntflags; + int err; - // TODO: handle deep bes + // TODO: handle deep bes - if (err = be_root_concat(lbh, bootenv, be)) { - return (set_error(lbh, err)); - } + if (err = be_root_concat(lbh, bootenv, be)) { + return (set_error(lbh, err)); + } - // TODO: make sure it exists (in a be_exists fn)! + // TODO: make sure it exists (in a be_exists fn)! - if (is_mounted(lbh->lzh, be, &path)) { - return (set_error(lbh, BE_ERR_MOUNTED)); - } + if (is_mounted(lbh->lzh, be, &path)) { + return (set_error(lbh, BE_ERR_MOUNTED)); + } - mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; + mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; - char opt = '\0'; - if (err = zmount(be, mountpoint, mntflags, MNTTYPE_ZFS, - NULL, 0, &opt, 1)) { - // TODO: zmount returns the nmount error, look into what kind of - // errors we can report from that - return (set_error(lbh, BE_ERR_UNKNOWN)); - } + char opt = '\0'; + if (err = zmount(be, mountpoint, mntflags, MNTTYPE_ZFS, + NULL, 0, &opt, 1)) { + // TODO: zmount returns the nmount error, look into what kind of + // errors we can report from that + return (set_error(lbh, BE_ERR_UNKNOWN)); + } - return (set_error(lbh, BE_ERR_SUCCESS)); + return (set_error(lbh, BE_ERR_SUCCESS)); } @@ -74,46 +74,46 @@ int be_unmount(libbe_handle_t *lbh, char *bootenv, int flags) { - int err, mntflags; - char be[MAXPATHLEN]; - struct statfs *mntbuf; - int mntsize; - char *mntpath; - - if (err = be_root_concat(lbh, bootenv, be)) { - return (set_error(lbh, err)); - } - - if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) { - // TODO correct error - return (set_error(lbh, BE_ERR_NOMOUNT)); - } - - mntpath = NULL; - for (int i = 0; i < mntsize; ++i) { - /* 0x000000de is the type number of zfs */ - if (mntbuf[i].f_type != 0x000000de) { - continue; - } - - if (strcmp(mntbuf[i].f_mntfromname, be) == 0) { - mntpath = mntbuf[i].f_mntonname; - break; - } - } - - if (mntpath == NULL) { - return (set_error(lbh, BE_ERR_NOMOUNT)); - } - - mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; - - if (err = unmount(mntpath, mntflags)) { - // TODO correct error - return (set_error(lbh, BE_ERR_NOMOUNT)); - } + int err, mntflags; + char be[MAXPATHLEN]; + struct statfs *mntbuf; + int mntsize; + char *mntpath; + + if (err = be_root_concat(lbh, bootenv, be)) { + return (set_error(lbh, err)); + } + + if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) { + // TODO correct error + return (set_error(lbh, BE_ERR_NOMOUNT)); + } + + mntpath = NULL; + for (int i = 0; i < mntsize; ++i) { + /* 0x000000de is the type number of zfs */ + if (mntbuf[i].f_type != 0x000000de) { + continue; + } + + if (strcmp(mntbuf[i].f_mntfromname, be) == 0) { + mntpath = mntbuf[i].f_mntonname; + break; + } + } + + if (mntpath == NULL) { + return (set_error(lbh, BE_ERR_NOMOUNT)); + } + + mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; + + if (err = unmount(mntpath, mntflags)) { + // TODO correct error + return (set_error(lbh, BE_ERR_NOMOUNT)); + } - return (set_error(lbh, BE_ERR_SUCCESS)); + return (set_error(lbh, BE_ERR_SUCCESS)); } @@ -123,5 +123,5 @@ int be_jail(libbe_handle_t *lbh, char *bootenv) { - return (BE_ERR_SUCCESS); + return (BE_ERR_SUCCESS); } Modified: soc2017/kneitinger/libbe-head/lib/libbe/be_error.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Thu Jul 13 11:39:37 2017 (r324565) @@ -35,78 +35,78 @@ int libbe_errno(libbe_handle_t *lbh) { - return (lbh->error); + return (lbh->error); } const char * libbe_error_description(libbe_handle_t *lbh) { - switch (lbh->error) { - case BE_ERR_INVALIDNAME: - return ("invalid boot environment name"); + switch (lbh->error) { + case BE_ERR_INVALIDNAME: + return ("invalid boot environment name"); - case BE_ERR_EXISTS: - return ("boot environment name already taken"); + case BE_ERR_EXISTS: + return ("boot environment name already taken"); - case BE_ERR_NOENT: - return ("specified boot environment does not exist"); + case BE_ERR_NOENT: + return ("specified boot environment does not exist"); - case BE_ERR_PERMS: - return ("insufficient permissions"); + case BE_ERR_PERMS: + return ("insufficient permissions"); - case BE_ERR_DESTROYACT: - return ("cannot destroy active boot environment"); + case BE_ERR_DESTROYACT: + return ("cannot destroy active boot environment"); - case BE_ERR_DESTROYMNT: - return ("cannot destroy mounted boot env unless forced"); + case BE_ERR_DESTROYMNT: + return ("cannot destroy mounted boot env unless forced"); - case BE_ERR_PATHLEN: - return ("provided path name exceeds maximum length limit"); + case BE_ERR_PATHLEN: + return ("provided path name exceeds maximum length limit"); - case BE_ERR_INVORIGIN: - return ("snapshot origin's mountpoint is not \"/\""); + case BE_ERR_INVORIGIN: + return ("snapshot origin's mountpoint is not \"/\""); - case BE_ERR_NOORIGIN: - return ("could not open snapshot's origin"); + case BE_ERR_NOORIGIN: + return ("could not open snapshot's origin"); - case BE_ERR_MOUNTED: - return ("boot environment is already mounted"); + case BE_ERR_MOUNTED: + return ("boot environment is already mounted"); - case BE_ERR_NOMOUNT: - return ("boot environment is not mounted"); + case BE_ERR_NOMOUNT: + return ("boot environment is not mounted"); - case BE_ERR_ZFSOPEN: - return ("calling zfs_open() failed"); + case BE_ERR_ZFSOPEN: + return ("calling zfs_open() failed"); - case BE_ERR_UNKNOWN: - return ("unknown error"); + case BE_ERR_UNKNOWN: + return ("unknown error"); - default: - assert(lbh->error == BE_ERR_SUCCESS); - return ("no error"); - } + default: + assert(lbh->error == BE_ERR_SUCCESS); + return ("no error"); + } } void libbe_print_on_error(libbe_handle_t *lbh, bool val) { - lbh->print_on_err = val; - libzfs_print_on_error(lbh->lzh, val); + lbh->print_on_err = val; + libzfs_print_on_error(lbh->lzh, val); } int set_error(libbe_handle_t *lbh, be_error_t err) { - // TODO: should the old error be overwritten or no? + // TODO: should the old error be overwritten or no? - lbh->error = err; + lbh->error = err; - if (lbh->print_on_err) { - fprintf(stderr, "%s\n", libbe_error_description(lbh)); - } + if (lbh->print_on_err) { + fprintf(stderr, "%s\n", libbe_error_description(lbh)); + } - return (err); + return (err); } Modified: soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h Thu Jul 13 11:39:37 2017 (r324565) @@ -35,12 +35,12 @@ struct libbe_handle { - libzfs_handle_t *lzh; - zfs_handle_t *be_root; - zfs_handle_t *be_active; - be_error_t error; - nvlist_t *list; - bool print_on_err; + libzfs_handle_t *lzh; + zfs_handle_t *be_root; + zfs_handle_t *be_active; + be_error_t error; + nvlist_t *list; + bool print_on_err; }; int prop_list_builder(libbe_handle_t *); Modified: soc2017/kneitinger/libbe-head/lib/libbe/be_info.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be_info.c Thu Jul 13 09:27:11 2017 (r324564) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_info.c Thu Jul 13 11:39:37 2017 (r324565) @@ -37,9 +37,9 @@ const char * be_active_name(libbe_handle_t *lbh) { - const char *full_path = zfs_get_name(lbh->be_active); + const char *full_path = zfs_get_name(lbh->be_active); - return (strrchr(full_path, '/') + 1); + return (strrchr(full_path, '/') + 1); } @@ -49,7 +49,7 @@ const char * be_active_path(libbe_handle_t *lbh) { - return (zfs_get_name(lbh->be_active)); + return (zfs_get_name(lbh->be_active)); } @@ -59,7 +59,7 @@ const char * be_root_path(libbe_handle_t *lbh) { - return (zfs_get_name(lbh->be_root)); + return (zfs_get_name(lbh->be_root)); } @@ -70,9 +70,9 @@ nvlist_t * be_get_bootenv_props(libbe_handle_t *lbh) { - // TODO: Should there be a dirty flag that re-calcs the list if an op - // has changed it? - return (lbh->list); + // TODO: Should there be a dirty flag that re-calcs the list if an op + // has changed it? + return (lbh->list); } @@ -84,83 +84,83 @@ static int prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data) { - /* - * TODO: - * some system for defining constants for the nvlist keys - * error checking - */ + /* + * TODO: + * some system for defining constants for the nvlist keys + * error checking + */ - boolean_t mounted, active, nextboot; + boolean_t mounted, active, nextboot; - char buf[512]; + char buf[512]; - nvlist_t *props; + nvlist_t *props; - libbe_handle_t *lbh = (libbe_handle_t *)data; + libbe_handle_t *lbh = (libbe_handle_t *)data; - nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); + nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); - const char *dataset = zfs_get_name(zfs_hdl); - nvlist_add_string(props, "dataset", dataset); + const char *dataset = zfs_get_name(zfs_hdl); + nvlist_add_string(props, "dataset", dataset); - const char *name = strrchr(dataset, '/') + 1; - nvlist_add_string(props, "name", name); + const char *name = strrchr(dataset, '/') + 1; + nvlist_add_string(props, "name", name); - mounted = zfs_prop_get_int(zfs_hdl, ZFS_PROP_MOUNTED); - nvlist_add_boolean_value(props, "mounted", mounted); + mounted = zfs_prop_get_int(zfs_hdl, ZFS_PROP_MOUNTED); + nvlist_add_boolean_value(props, "mounted", mounted); - // TODO: NOT CORRECT! Must use is_mounted - if (mounted) { - zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512, - NULL, NULL, 0, 1); - nvlist_add_string(props, "mountpoint", buf); - } + // TODO: NOT CORRECT! Must use is_mounted + if (mounted) { + zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512, + NULL, NULL, 0, 1); + nvlist_add_string(props, "mountpoint", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "origin", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "origin", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "creation", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "creation", buf); + } - nvlist_add_boolean_value(props, "active", - (strcmp(be_active_path(lbh), dataset) == 0)); + nvlist_add_boolean_value(props, "active", + (strcmp(be_active_path(lbh), dataset) == 0)); - if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "used", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "used", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "usedds", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "usedds", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "usedsnap", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "usedsnap", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "usedrefreserv", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "usedrefreserv", buf); + } - if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512, - NULL, NULL, 0, 1)) { - nvlist_add_string(props, "referenced", buf); - } + if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512, + NULL, NULL, 0, 1)) { + nvlist_add_string(props, "referenced", buf); + } - /* TODO figure out how to read nextboot (set in libzfs_pool.c) */ + /* TODO figure out how to read nextboot (set in libzfs_pool.c) */ - nvlist_add_nvlist(lbh->list, name, props); + nvlist_add_nvlist(lbh->list, name, props); - return (0); + return (0); } @@ -173,20 +173,20 @@ int prop_list_builder(libbe_handle_t *lbh) { - if (lbh->list != NULL) { - /* TODO: should actually call prop_list_free */ - nvlist_free(lbh->list); - return (1); - } - - if (nvlist_alloc(&lbh->list, NV_UNIQUE_NAME, KM_SLEEP) != 0) { - /* TODO: actually handle error */ - return (1); - } + if (lbh->list != NULL) { + /* TODO: should actually call prop_list_free */ + nvlist_free(lbh->list); + return (1); + } + + if (nvlist_alloc(&lbh->list, NV_UNIQUE_NAME, KM_SLEEP) != 0) { + /* TODO: actually handle error */ + return (1); + } - zfs_iter_filesystems(lbh->be_root, prop_list_builder_cb, lbh); + zfs_iter_filesystems(lbh->be_root, prop_list_builder_cb, lbh); - return (0); + return (0); } @@ -196,24 +196,24 @@ void prop_list_free(libbe_handle_t *lbh) { - nvlist_t *be_list; - nvlist_t *prop_list; + nvlist_t *be_list; + nvlist_t *prop_list; - if ((be_list = lbh->list) == 0) { - return; - } - - nvpair_t *be_pair = nvlist_next_nvpair(be_list, NULL); - - if (nvpair_value_nvlist(be_pair, &prop_list) == 0) { - nvlist_free(prop_list); - } - - while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) { - if (nvpair_value_nvlist(be_pair, &prop_list) == 0) { - nvlist_free(prop_list); - } - } + if ((be_list = lbh->list) == 0) { + return; + } + + nvpair_t *be_pair = nvlist_next_nvpair(be_list, NULL); + + if (nvpair_value_nvlist(be_pair, &prop_list) == 0) { + nvlist_free(prop_list); + } + + while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) { + if (nvpair_value_nvlist(be_pair, &prop_list) == 0) { + nvlist_free(prop_list); + } + } } @@ -221,8 +221,8 @@ * Usage */ bool -be_exists(libbe_handle_t lbh, char *be) +be_exists(libbe_handle_t *lbh, char *be) { - // TODO - return (true); + // TODO + return (true); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201707131139.v6DBddo1090078>
