From: Christian Brauner Date: Tue, 25 Jul 2017 11:02:30 +0000 (+0200) Subject: storage: make detect method return bool X-Git-Tag: lxc-2.1.0~32^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d2ae1e20ffd85c488a0b95d27bc716749a5de36;p=thirdparty%2Flxc.git storage: make detect method return bool Signed-off-by: Christian Brauner --- diff --git a/src/lxc/bdev/bdev.h b/src/lxc/bdev/bdev.h index 15d46502f..591ddb809 100644 --- a/src/lxc/bdev/bdev.h +++ b/src/lxc/bdev/bdev.h @@ -61,7 +61,7 @@ struct bdev; struct bdev_ops { /* detect whether path is of this bdev type */ - int (*detect)(const char *path); + bool (*detect)(const char *path); // mount requires src and dest to be set. int (*mount)(struct bdev *bdev); int (*umount)(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcaufs.c b/src/lxc/bdev/lxcaufs.c index 481b62ffe..a2122f236 100644 --- a/src/lxc/bdev/lxcaufs.c +++ b/src/lxc/bdev/lxcaufs.c @@ -227,12 +227,12 @@ int aufs_destroy(struct bdev *orig) return lxc_rmdir_onedev(upper, NULL); } -int aufs_detect(const char *path) +bool aufs_detect(const char *path) { if (!strncmp(path, "aufs:", 5)) - return 1; + return true; - return 0; + return false; } int aufs_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcaufs.h b/src/lxc/bdev/lxcaufs.h index fa623f712..a0dcac3eb 100644 --- a/src/lxc/bdev/lxcaufs.h +++ b/src/lxc/bdev/lxcaufs.h @@ -25,6 +25,7 @@ #define __LXC_AUFS_H #define _GNU_SOURCE +#include #include #if IS_BIONIC @@ -54,7 +55,7 @@ int aufs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int aufs_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int aufs_destroy(struct bdev *orig); -int aufs_detect(const char *path); +bool aufs_detect(const char *path); int aufs_mount(struct bdev *bdev); int aufs_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcbtrfs.c b/src/lxc/bdev/lxcbtrfs.c index dbb5ce672..0a3404279 100644 --- a/src/lxc/bdev/lxcbtrfs.c +++ b/src/lxc/bdev/lxcbtrfs.c @@ -166,26 +166,26 @@ int is_btrfs_subvol(const char *path) return stfs.f_type == BTRFS_SUPER_MAGIC; } -int btrfs_detect(const char *path) +bool btrfs_detect(const char *path) { struct stat st; int ret; if (!strncmp(path, "btrfs:", 6)) - return 1; + return true; if (!is_btrfs_fs(path)) - return 0; + return false; /* make sure it's a subvolume */ ret = stat(path, &st); if (ret < 0) - return 0; + return false; if (st.st_ino == 256 && S_ISDIR(st.st_mode)) - return 1; + return true; - return 0; + return false; } int btrfs_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcbtrfs.h b/src/lxc/bdev/lxcbtrfs.h index 0069245e8..d88e1be2c 100644 --- a/src/lxc/bdev/lxcbtrfs.h +++ b/src/lxc/bdev/lxcbtrfs.h @@ -397,7 +397,7 @@ int btrfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int btrfs_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int btrfs_destroy(struct bdev *orig); -int btrfs_detect(const char *path); +bool btrfs_detect(const char *path); int btrfs_mount(struct bdev *bdev); int btrfs_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcdir.c b/src/lxc/bdev/lxcdir.c index 610425aed..5b73df658 100644 --- a/src/lxc/bdev/lxcdir.c +++ b/src/lxc/bdev/lxcdir.c @@ -134,15 +134,15 @@ int dir_destroy(struct bdev *orig) return 0; } -int dir_detect(const char *path) +bool dir_detect(const char *path) { if (!strncmp(path, "dir:", 4)) - return 1; + return true; if (is_dir(path)) - return 1; + return true; - return 0; + return false; } int dir_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcdir.h b/src/lxc/bdev/lxcdir.h index f5cca9ada..d63c66900 100644 --- a/src/lxc/bdev/lxcdir.h +++ b/src/lxc/bdev/lxcdir.h @@ -25,6 +25,7 @@ #define __LXC_DIR_H #define _GNU_SOURCE +#include #include /* defined in bdev.h */ @@ -45,7 +46,7 @@ int dir_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int dir_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int dir_destroy(struct bdev *orig); -int dir_detect(const char *path); +bool dir_detect(const char *path); int dir_mount(struct bdev *bdev); int dir_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcloop.c b/src/lxc/bdev/lxcloop.c index 0da3e8187..7508cb62e 100644 --- a/src/lxc/bdev/lxcloop.c +++ b/src/lxc/bdev/lxcloop.c @@ -206,22 +206,22 @@ int loop_destroy(struct bdev *orig) { return unlink(orig->src + 5); } -int loop_detect(const char *path) +bool loop_detect(const char *path) { int ret; struct stat s; if (!strncmp(path, "loop:", 5)) - return 1; + return true; ret = stat(path, &s); if (ret < 0) - return 0; + return false; if (__S_ISTYPE(s.st_mode, S_IFREG)) - return 1; + return true; - return 0; + return false; } int loop_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcloop.h b/src/lxc/bdev/lxcloop.h index 5d33182a3..9c7baa2a7 100644 --- a/src/lxc/bdev/lxcloop.h +++ b/src/lxc/bdev/lxcloop.h @@ -25,6 +25,7 @@ #define __LXC_LOOP_H #define _GNU_SOURCE +#include #include /* defined in bdev.h */ @@ -45,7 +46,7 @@ int loop_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int loop_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int loop_destroy(struct bdev *orig); -int loop_detect(const char *path); +bool loop_detect(const char *path); int loop_mount(struct bdev *bdev); int loop_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxclvm.c b/src/lxc/bdev/lxclvm.c index aea605d04..0ed2446eb 100644 --- a/src/lxc/bdev/lxclvm.c +++ b/src/lxc/bdev/lxclvm.c @@ -193,7 +193,7 @@ static int do_lvm_create(const char *path, uint64_t size, const char *thinpool) /* Look at "/sys/dev/block/maj:min/dm/uuid". If it contains the hardcoded LVM * prefix "LVM-" then this is an lvm2 LV. */ -int lvm_detect(const char *path) +bool lvm_detect(const char *path) { int fd; ssize_t ret; @@ -201,35 +201,35 @@ int lvm_detect(const char *path) char devp[MAXPATHLEN], buf[4]; if (!strncmp(path, "lvm:", 4)) - return 1; + return true; ret = stat(path, &statbuf); if (ret < 0) - return 0; + return false; if (!S_ISBLK(statbuf.st_mode)) - return 0; + return false; ret = snprintf(devp, MAXPATHLEN, "/sys/dev/block/%d:%d/dm/uuid", major(statbuf.st_rdev), minor(statbuf.st_rdev)); if (ret < 0 || ret >= MAXPATHLEN) { ERROR("Failed to create string"); - return 0; + return false; } fd = open(devp, O_RDONLY); if (fd < 0) - return 0; + return false; ret = read(fd, buf, sizeof(buf)); close(fd); if (ret != sizeof(buf)) - return 0; + return false; if (strncmp(buf, "LVM-", 4)) - return 0; + return false; - return 1; + return true; } int lvm_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxclvm.h b/src/lxc/bdev/lxclvm.h index 07824fced..a89ca6693 100644 --- a/src/lxc/bdev/lxclvm.h +++ b/src/lxc/bdev/lxclvm.h @@ -40,7 +40,7 @@ struct lxc_conf; /* * Functions associated with an lvm bdev struct. */ -int lvm_detect(const char *path); +bool lvm_detect(const char *path); int lvm_mount(struct bdev *bdev); int lvm_umount(struct bdev *bdev); int lvm_compare_lv_attr(const char *path, int pos, const char expected); diff --git a/src/lxc/bdev/lxcnbd.c b/src/lxc/bdev/lxcnbd.c index e6ce59038..2c419527c 100644 --- a/src/lxc/bdev/lxcnbd.c +++ b/src/lxc/bdev/lxcnbd.c @@ -106,12 +106,12 @@ int nbd_destroy(struct bdev *orig) return -ENOSYS; } -int nbd_detect(const char *path) +bool nbd_detect(const char *path) { if (!strncmp(path, "nbd:", 4)) - return 1; + return true; - return 0; + return false; } int nbd_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcnbd.h b/src/lxc/bdev/lxcnbd.h index 1404d5a3b..52ce8cb05 100644 --- a/src/lxc/bdev/lxcnbd.h +++ b/src/lxc/bdev/lxcnbd.h @@ -46,7 +46,7 @@ int nbd_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int nbd_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int nbd_destroy(struct bdev *orig); -int nbd_detect(const char *path); +bool nbd_detect(const char *path); int nbd_mount(struct bdev *bdev); int nbd_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcoverlay.c b/src/lxc/bdev/lxcoverlay.c index 85f4650a2..5e4fde5e7 100644 --- a/src/lxc/bdev/lxcoverlay.c +++ b/src/lxc/bdev/lxcoverlay.c @@ -332,15 +332,15 @@ int ovl_destroy(struct bdev *orig) return lxc_rmdir_onedev(upper, NULL); } -int ovl_detect(const char *path) +bool ovl_detect(const char *path) { if (!strncmp(path, "overlayfs:", 10)) - return 1; + return true; if (!strncmp(path, "overlay:", 8)) - return 1; + return true; - return 0; + return false; } int ovl_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcoverlay.h b/src/lxc/bdev/lxcoverlay.h index fe70d3dbc..bc1edc792 100644 --- a/src/lxc/bdev/lxcoverlay.h +++ b/src/lxc/bdev/lxcoverlay.h @@ -25,6 +25,7 @@ #define __LXC_OVERLAY_H #include +#include #include #include #include @@ -56,7 +57,7 @@ int ovl_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int ovl_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int ovl_destroy(struct bdev *orig); -int ovl_detect(const char *path); +bool ovl_detect(const char *path); int ovl_mount(struct bdev *bdev); int ovl_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxcrbd.c b/src/lxc/bdev/lxcrbd.c index 7af4aaa24..3c2904e6e 100644 --- a/src/lxc/bdev/lxcrbd.c +++ b/src/lxc/bdev/lxcrbd.c @@ -219,15 +219,15 @@ int rbd_destroy(struct bdev *orig) return 0; } -int rbd_detect(const char *path) +bool rbd_detect(const char *path) { if (!strncmp(path, "rbd:", 4)) - return 1; + return true; if (!strncmp(path, "/dev/rbd/", 9)) - return 1; + return true; - return 0; + return false; } int rbd_mount(struct bdev *bdev) diff --git a/src/lxc/bdev/lxcrbd.h b/src/lxc/bdev/lxcrbd.h index 19fa026b7..731fc0a1b 100644 --- a/src/lxc/bdev/lxcrbd.h +++ b/src/lxc/bdev/lxcrbd.h @@ -25,6 +25,7 @@ #define __LXC_RDB_H #define _GNU_SOURCE +#include #include /* defined in bdev.h */ @@ -45,7 +46,7 @@ int rbd_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, int rbd_create(struct bdev *bdev, const char *dest, const char *n, struct bdev_specs *specs); int rbd_destroy(struct bdev *orig); -int rbd_detect(const char *path); +bool rbd_detect(const char *path); int rbd_mount(struct bdev *bdev); int rbd_umount(struct bdev *bdev); diff --git a/src/lxc/bdev/lxczfs.c b/src/lxc/bdev/lxczfs.c index 8fe74137d..253901cb6 100644 --- a/src/lxc/bdev/lxczfs.c +++ b/src/lxc/bdev/lxczfs.c @@ -22,12 +22,12 @@ */ #define _GNU_SOURCE -#include #include +#include #include #include -#include #include +#include #include "bdev.h" #include "config.h" @@ -37,50 +37,48 @@ lxc_log_define(lxczfs, lxc); -/* - * zfs ops: - * There are two ways we could do this. We could always specify the 'zfs device' +/* There are two ways we could do this. We could always specify the 'zfs device' * (i.e. tank/lxc lxc/container) as rootfs. But instead (at least right now) we - * have lxc-create specify $lxcpath/$lxcname/rootfs as the mountpoint, so that + * have lxc-create specify //rootfs as the mountpoint, so that * it is always mounted. That means 'mount' is really never needed and could be * noop, but for the sake of flexibility let's always bind-mount. */ -int zfs_list_entry(const char *path, char *output, size_t inlen) +static bool zfs_list_entry(const char *path, char *output, size_t inlen) { struct lxc_popen_FILE *f; - int found=0; + bool found = false; f = lxc_popen("zfs list 2> /dev/null"); if (f == NULL) { SYSERROR("popen failed"); - return 0; + return false; } while (fgets(output, inlen, f->f)) { if (strstr(output, path)) { - found = 1; + found = true; break; } } - (void) lxc_pclose(f); + (void)lxc_pclose(f); return found; } -int zfs_detect(const char *path) +bool zfs_detect(const char *path) { if (!strncmp(path, "zfs:", 4)) - return 1; + return true; char *output = malloc(LXC_LOG_BUFFER_SIZE); if (!output) { ERROR("out of memory"); - return 0; + return false; } - int found = zfs_list_entry(path, output, LXC_LOG_BUFFER_SIZE); + bool found = zfs_list_entry(path, output, LXC_LOG_BUFFER_SIZE); free(output); return found; @@ -104,7 +102,8 @@ int zfs_mount(struct bdev *bdev) } src = lxc_storage_get_path(bdev->src, bdev->type); - ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, mntdata); + ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, + mntdata); free(mntdata); return ret; @@ -122,7 +121,7 @@ int zfs_umount(struct bdev *bdev) } int zfs_clone(const char *opath, const char *npath, const char *oname, - const char *nname, const char *lxcpath, int snapshot) + const char *nname, const char *lxcpath, int snapshot) { // use the 'zfs list | grep opath' entry to get the zfsroot char output[MAXPATHLEN], option[MAXPATHLEN]; @@ -144,8 +143,9 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, zfsroot = lxc_global_config_value("lxc.bdev.zfs.root"); } - ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs", lxcpath, nname); - if (ret < 0 || ret >= MAXPATHLEN) + ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs", lxcpath, + nname); + if (ret < 0 || ret >= MAXPATHLEN) return -1; // zfs create -omountpoint=$lxcpath/$lxcname $zfsroot/$nname @@ -154,10 +154,12 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, return -1; if (!pid) { char dev[MAXPATHLEN]; - ret = snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, nname); - if (ret < 0 || ret >= MAXPATHLEN) + ret = + snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, nname); + if (ret < 0 || ret >= MAXPATHLEN) exit(EXIT_FAILURE); - execlp("zfs", "zfs", "create", option, dev, (char *)NULL); + execlp("zfs", "zfs", "create", option, dev, + (char *)NULL); exit(EXIT_FAILURE); } return wait_for_pid(pid); @@ -167,11 +169,11 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, // zfs clone zfsroot/oname@nname zfsroot/nname char path1[MAXPATHLEN], path2[MAXPATHLEN]; - ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", zfsroot, - oname, nname); + ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", zfsroot, oname, + nname); if (ret < 0 || ret >= MAXPATHLEN) return -1; - (void) snprintf(path2, MAXPATHLEN, "%s/%s", zfsroot, nname); + (void)snprintf(path2, MAXPATHLEN, "%s/%s", zfsroot, nname); // if the snapshot exists, delete it if ((pid = fork()) < 0) @@ -184,7 +186,7 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, exit(EXIT_FAILURE); } // it probably doesn't exist so destroy probably will fail. - (void) wait_for_pid(pid); + (void)wait_for_pid(pid); // run first (snapshot) command if ((pid = fork()) < 0) @@ -200,7 +202,8 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, if ((pid = fork()) < 0) return -1; if (!pid) { - execlp("zfs", "zfs", "clone", option, path1, path2, (char *)NULL); + execlp("zfs", "zfs", "clone", option, path1, path2, + (char *)NULL); exit(EXIT_FAILURE); } return wait_for_pid(pid); @@ -208,8 +211,8 @@ int zfs_clone(const char *opath, const char *npath, const char *oname, } int zfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, - const char *cname, const char *oldpath, const char *lxcpath, int snap, - uint64_t newsize, struct lxc_conf *conf) + const char *cname, const char *oldpath, const char *lxcpath, + int snap, uint64_t newsize, struct lxc_conf *conf) { char *origsrc, *newsrc; int len, ret; @@ -218,7 +221,8 @@ int zfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, return -1; if (snap && strcmp(orig->type, "zfs")) { - ERROR("zfs snapshot from %s backing store is not supported", orig->type); + ERROR("zfs snapshot from %s backing store is not supported", + orig->type); return -1; } @@ -286,7 +290,7 @@ int zfs_create_exec_wrapper(void *args) } int zfs_create(struct bdev *bdev, const char *dest, const char *n, - struct bdev_specs *specs) + struct bdev_specs *specs) { const char *zfsroot; char cmd_output[MAXPATHLEN], dev[MAXPATHLEN], option[MAXPATHLEN]; @@ -317,11 +321,11 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n, return -1; ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s", bdev->dest); - if (ret < 0 || ret >= MAXPATHLEN) + if (ret < 0 || ret >= MAXPATHLEN) return -1; ret = snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, n); - if (ret < 0 || ret >= MAXPATHLEN) + if (ret < 0 || ret >= MAXPATHLEN) return -1; cmd_args.options = option; @@ -329,6 +333,7 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n, ret = run_command(cmd_output, sizeof(cmd_output), zfs_create_exec_wrapper, (void *)&cmd_args); if (ret < 0) - ERROR("Failed to create zfs dataset \"%s\": %s", dev, cmd_output); + ERROR("Failed to create zfs dataset \"%s\": %s", dev, + cmd_output); return ret; } diff --git a/src/lxc/bdev/lxczfs.h b/src/lxc/bdev/lxczfs.h index 048461997..ea9ae87d7 100644 --- a/src/lxc/bdev/lxczfs.h +++ b/src/lxc/bdev/lxczfs.h @@ -53,8 +53,7 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n, * container busy. */ int zfs_destroy(struct bdev *orig); -int zfs_detect(const char *path); -int zfs_list_entry(const char *path, char *output, size_t inlen); +bool zfs_detect(const char *path); int zfs_mount(struct bdev *bdev); int zfs_umount(struct bdev *bdev);