btrfs-progs: check: introduce function to check dir_item
[platform/upstream/btrfs-progs.git] / cmds-receive.c
index cbb1642..781dce9 100644 (file)
@@ -166,8 +166,18 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
        if (ret < 0)
                goto out;
 
-       BUG_ON(r->cur_subvol.path);
-       BUG_ON(r->cur_subvol_path[0]);
+       if (r->cur_subvol.path) {
+               error("subvol: another one already started, path ptr: %s",
+                               r->cur_subvol.path);
+               ret = -EINVAL;
+               goto out;
+       }
+       if (r->cur_subvol_path[0]) {
+               error("subvol: another one already started, path buf: %s",
+                               r->cur_subvol.path);
+               ret = -EINVAL;
+               goto out;
+       }
 
        if (*r->dest_dir_path == 0) {
                strncpy_null(r->cur_subvol_path, path);
@@ -224,8 +234,18 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
        if (ret < 0)
                goto out;
 
-       BUG_ON(r->cur_subvol.path);
-       BUG_ON(r->cur_subvol_path[0]);
+       if (r->cur_subvol.path) {
+               error("snapshot: another one already started, path ptr: %s",
+                               r->cur_subvol.path);
+               ret = -EINVAL;
+               goto out;
+       }
+       if (r->cur_subvol_path[0]) {
+               error("snapshot: another one already started, path buf: %s",
+                               r->cur_subvol.path);
+               ret = -EINVAL;
+               goto out;
+       }
 
        if (*r->dest_dir_path == 0) {
                strncpy_null(r->cur_subvol_path, path);
@@ -644,7 +664,7 @@ static int open_inode_for_write(struct btrfs_receive *r, const char *path)
        r->write_fd = open(path, O_RDWR);
        if (r->write_fd < 0) {
                ret = -errno;
-               error("cannont open %s: %s", path, strerror(-ret));
+               error("cannot open %s: %s", path, strerror(-ret));
                goto out;
        }
        strncpy_null(r->write_path, path);
@@ -1041,6 +1061,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
        char *dest_dir_full_path;
        char root_subvol_path[PATH_MAX];
        int end = 0;
+       int count;
 
        dest_dir_full_path = realpath(tomnt, NULL);
        if (!dest_dir_full_path) {
@@ -1086,11 +1107,8 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
         * subvols we want to receive in.
         */
        ret = btrfs_list_get_path_rootid(r->mnt_fd, &subvol_id);
-       if (ret) {
-               error("cannot resolve our subvolid: %d",
-                       ret);
+       if (ret)
                goto out;
-       }
 
        root_subvol_path[0] = 0;
        ret = btrfs_subvolid_resolve(r->mnt_fd, root_subvol_path,
@@ -1138,6 +1156,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
        if (ret < 0)
                goto out;
 
+       count = 0;
        while (!end) {
                if (r->cached_capabilities_len) {
                        if (g_verbose >= 3)
@@ -1152,6 +1171,13 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt,
                                                         max_errors);
                if (ret < 0)
                        goto out;
+               /* Empty stream is invalid */
+               if (ret && count == 0) {
+                       error("empty stream is not considered valid");
+                       ret = -EINVAL;
+                       goto out;
+               }
+               count++;
                if (ret)
                        end = 1;