btrfs-progs: ci: add dockerfile for a musl build test
[platform/upstream/btrfs-progs.git] / cmds-subvolume.c
index e7caa05..8a473f7 100644 (file)
@@ -210,7 +210,7 @@ static int cmd_subvol_create(int argc, char **argv)
        }
 
        if (res < 0) {
-               error("cannot create subvolume: %s", strerror(errno));
+               error("cannot create subvolume: %m");
                goto out;
        }
 
@@ -325,8 +325,7 @@ again:
        cpath = realpath(path, NULL);
        if (!cpath) {
                ret = errno;
-               error("cannot find real path for '%s': %s",
-                       path, strerror(errno));
+               error("cannot find real path for '%s': %m", path);
                goto out;
        }
        dupdname = strdup(cpath);
@@ -348,8 +347,7 @@ again:
        strncpy_null(args.name, vname);
        res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
        if(res < 0 ){
-               error("cannot delete '%s/%s': %s", dname, vname,
-                       strerror(errno));
+               error("cannot delete '%s/%s': %m", dname, vname);
                ret = 1;
                goto out;
        }
@@ -357,8 +355,7 @@ again:
        if (commit_mode == COMMIT_EACH) {
                res = wait_for_commit(fd);
                if (res < 0) {
-                       error("unable to wait for commit after '%s': %s",
-                               path, strerror(errno));
+                       error("unable to wait for commit after '%s': %m", path);
                        ret = 1;
                }
        } else if (commit_mode == COMMIT_AFTER) {
@@ -415,8 +412,8 @@ keep_fd:
                                if (res < 0) {
                                        uuid_unparse(seen->fsid, uuidbuf);
                                        error(
-                       "unable to do final sync after deletion: %s, fsid: %s",
-                                               strerror(errno), uuidbuf);
+                       "unable to do final sync after deletion: %m, fsid: %s",
+                                               uuidbuf);
                                        ret = 1;
                                } else if (verbose > 0) {
                                        uuid_unparse(seen->fsid, uuidbuf);
@@ -776,7 +773,7 @@ static int cmd_subvol_snapshot(int argc, char **argv)
        res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
 
        if (res < 0) {
-               error("cannot snapshot '%s': %s", subvol, strerror(errno));
+               error("cannot snapshot '%s': %m", subvol);
                goto out;
        }
 
@@ -819,8 +816,7 @@ static int cmd_subvol_get_default(int argc, char **argv)
 
        ret = btrfs_list_get_default_subvolume(fd, &default_id);
        if (ret) {
-               error("failed to look up default subvolume: %s",
-                       strerror(errno));
+               error("failed to look up default subvolume: %m");
                goto out;
        }
 
@@ -858,14 +854,17 @@ out:
 }
 
 static const char * const cmd_subvol_set_default_usage[] = {
+       "btrfs subvolume set-default <subvolume>\n"
        "btrfs subvolume set-default <subvolid> <path>",
-       "Set the default subvolume of a filesystem",
+       "Set the default subvolume of the filesystem mounted as default.",
+       "The subvolume can be specified by its path,",
+       "or the pair of subvolume id and path to the filesystem.",
        NULL
 };
 
 static int cmd_subvol_set_default(int argc, char **argv)
 {
-       int     ret=0, fd, e;
+       int     ret=0, fd;
        u64     objectid;
        char    *path;
        char    *subvolid;
@@ -873,24 +872,48 @@ static int cmd_subvol_set_default(int argc, char **argv)
 
        clean_args_no_options(argc, argv, cmd_subvol_set_default_usage);
 
-       if (check_argc_exact(argc - optind, 2))
+       if (check_argc_min(argc - optind, 1) ||
+                       check_argc_max(argc - optind, 2))
                usage(cmd_subvol_set_default_usage);
 
-       subvolid = argv[optind];
-       path = argv[optind + 1];
+       if (argc - optind == 1) {
+               /* path to the subvolume is specified */
+               path = argv[optind];
 
-       objectid = arg_strtou64(subvolid);
+               ret = test_issubvolume(path);
+               if (ret < 0) {
+                       error("stat error: %s", strerror(-ret));
+                       return 1;
+               } else if (!ret) {
+                       error("'%s' is not a subvolume", path);
+                       return 1;
+               }
 
-       fd = btrfs_open_dir(path, &dirstream, 1);
-       if (fd < 0)
-               return 1;
+               fd = btrfs_open_dir(path, &dirstream, 1);
+               if (fd < 0)
+                       return 1;
+
+               ret = lookup_path_rootid(fd, &objectid);
+               if (ret) {
+                       error("unable to get subvol id: %s", strerror(-ret));
+                       close_file_or_dir(fd, dirstream);
+                       return 1;
+               }
+       } else {
+               /* subvol id and path to the filesystem are specified */
+               subvolid = argv[optind];
+               path = argv[optind + 1];
+               objectid = arg_strtou64(subvolid);
+
+               fd = btrfs_open_dir(path, &dirstream, 1);
+               if (fd < 0)
+                       return 1;
+       }
 
        ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
-       e = errno;
        close_file_or_dir(fd, dirstream);
        if (ret < 0) {
-               error("unable to set a new default subvolume: %s",
-                       strerror(e));
+               error("unable to set a new default subvolume: %m");
                return 1;
        }
        return 0;
@@ -934,8 +957,7 @@ static int cmd_subvol_find_new(int argc, char **argv)
 
        ret = ioctl(fd, BTRFS_IOC_SYNC);
        if (ret < 0) {
-               error("sync ioctl failed on '%s': %s",
-                       subvol, strerror(errno));
+               error("sync ioctl failed on '%s': %m", subvol);
                close_file_or_dir(fd, dirstream);
                return 1;
        }
@@ -1010,8 +1032,7 @@ static int cmd_subvol_show(int argc, char **argv)
        memset(&get_ri, 0, sizeof(get_ri));
        fullpath = realpath(argv[optind], NULL);
        if (!fullpath) {
-               error("cannot find real path for '%s': %s",
-                       argv[optind], strerror(errno));
+               error("cannot find real path for '%s': %m", argv[optind]);
                goto out;
        }