btrfs-progs: send: fix handling of -c option
authorTsutomu Itoh <t-itoh@jp.fujitsu.com>
Fri, 4 Nov 2016 08:33:58 +0000 (17:33 +0900)
committerDavid Sterba <dsterba@suse.com>
Fri, 11 Nov 2016 15:23:29 +0000 (16:23 +0100)
When two or more -c options are specified, cannot find a suitable
parent. So, output stream is bigger than correct one.

[before]
At subvol Snap1
At subvol Snap2
At subvol ../SnapY
-rw------- 1 root root 3153 Oct 19 10:37 /tmp/data1

[after]
At subvol Snap1
At subvol Snap2
At subvol ../SnapY
-rw------- 1 root root 1492 Oct 19 10:39 /tmp/data1

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
[ constify subvol argument, renamed single letter variables ]
Signed-off-by: David Sterba <dsterba@suse.com>
cmds-send.c

index ad06d5b..fbb2f80 100644 (file)
@@ -417,6 +417,37 @@ out:
        return ret;
 }
 
+static int set_root_info(struct btrfs_send *sctx, const char *subvol,
+               u64 *root_id)
+{
+       int ret;
+
+       ret = init_root_path(sctx, subvol);
+       if (ret < 0)
+               goto out;
+
+       ret = get_root_id(sctx, subvol_strip_mountpoint(sctx->root_path, subvol),
+               root_id);
+       if (ret < 0) {
+               error("cannot resolve rootid for %s", subvol);
+               goto out;
+       }
+
+out:
+       return ret;
+}
+
+static void free_send_info(struct btrfs_send *sctx)
+{
+       if (sctx->mnt_fd >= 0) {
+               close(sctx->mnt_fd);
+               sctx->mnt_fd = -1;
+       }
+       free(sctx->root_path);
+       sctx->root_path = NULL;
+       subvol_uuid_search_finit(&sctx->sus);
+}
+
 int cmd_send(int argc, char **argv)
 {
        char *subvol = NULL;
@@ -466,18 +497,10 @@ int cmd_send(int argc, char **argv)
                                goto out;
                        }
 
-                       ret = init_root_path(&send, subvol);
+                       ret = set_root_info(&send, subvol, &root_id);
                        if (ret < 0)
                                goto out;
 
-                       ret = get_root_id(&send,
-                               subvol_strip_mountpoint(send.root_path, subvol),
-                               &root_id);
-                       if (ret < 0) {
-                               error("cannot resolve rootid for %s", subvol);
-                               goto out;
-                       }
-
                        ret = is_subvol_ro(&send, subvol);
                        if (ret < 0)
                                goto out;
@@ -492,15 +515,9 @@ int cmd_send(int argc, char **argv)
                                error("cannot add clone source: %s", strerror(-ret));
                                goto out;
                        }
-                       subvol_uuid_search_finit(&send.sus);
                        free(subvol);
                        subvol = NULL;
-                       if (send.mnt_fd >= 0) {
-                               close(send.mnt_fd);
-                               send.mnt_fd = -1;
-                       }
-                       free(send.root_path);
-                       send.root_path = NULL;
+                       free_send_info(&send);
                        full_send = 0;
                        break;
                case 'f':
@@ -657,6 +674,10 @@ int cmd_send(int argc, char **argv)
                }
 
                if (!full_send && root_id) {
+                       ret = set_root_info(&send, subvol, &root_id);
+                       if (ret < 0)
+                               goto out;
+
                        ret = find_good_parent(&send, root_id, &parent_root_id);
                        if (ret < 0) {
                                error("parent determination failed for %lld",
@@ -686,6 +707,7 @@ int cmd_send(int argc, char **argv)
                                error("cannot add clone source: %s", strerror(-ret));
                                goto out;
                        }
+                       free_send_info(&send);
                }
        }
 
@@ -695,10 +717,7 @@ out:
        free(subvol);
        free(snapshot_parent);
        free(send.clone_sources);
-       if (send.mnt_fd >= 0)
-               close(send.mnt_fd);
-       free(send.root_path);
-       subvol_uuid_search_finit(&send.sus);
+       free_send_info(&send);
        return !!ret;
 }