btrfs-progs: alias btrfs device delete to btrfs device remove
[platform/upstream/btrfs-progs.git] / send-utils.c
index 1772d2c..fa09bb4 100644 (file)
@@ -20,6 +20,8 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <uuid/uuid.h>
+#include <limits.h>
+#include <errno.h>
 
 #include "ctree.h"
 #include "send-utils.h"
@@ -159,6 +161,7 @@ static int btrfs_read_root_item(int mnt_fd, u64 root_id,
        return 0;
 }
 
+#ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
 static struct rb_node *tree_insert(struct rb_root *root,
                                   struct subvol_info *si,
                                   enum subvol_search_type type)
@@ -223,6 +226,7 @@ static struct rb_node *tree_insert(struct rb_root *root,
        }
        return NULL;
 }
+#endif
 
 int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id)
 {
@@ -320,6 +324,7 @@ static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
        return 0;
 }
 
+#ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
 static int count_bytes(void *buf, int len, char b)
 {
        int cnt = 0;
@@ -416,6 +421,16 @@ static struct subvol_info *subvol_uuid_search_old(struct subvol_uuid_search *s,
                return NULL;
        return tree_search(root, root_id, uuid, transid, path, type);
 }
+#else
+void subvol_uuid_search_add(struct subvol_uuid_search *s,
+                           struct subvol_info *si)
+{
+       if (si) {
+               free(si->path);
+               free(si);
+       }
+}
+#endif
 
 struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
                                       u64 root_id, const u8 *uuid, u64 transid,
@@ -426,9 +441,11 @@ struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
        struct btrfs_root_item root_item;
        struct subvol_info *info = NULL;
 
+#ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
        if (!s->uuid_tree_existed)
                return subvol_uuid_search_old(s, root_id, uuid, transid,
                                             path, type);
+#endif
        switch (type) {
        case subvol_search_by_received_uuid:
                ret = btrfs_lookup_uuid_received_subvol_item(s->mnt_fd, uuid,
@@ -466,9 +483,9 @@ struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
        if (type == subvol_search_by_path) {
                info->path = strdup(path);
        } else {
-               info->path = malloc(BTRFS_PATH_NAME_MAX);
+               info->path = malloc(PATH_MAX);
                ret = btrfs_subvolid_resolve(s->mnt_fd, info->path,
-                                            BTRFS_PATH_NAME_MAX, root_id);
+                                            PATH_MAX, root_id);
        }
 
 out:
@@ -481,6 +498,7 @@ out:
        return info;
 }
 
+#ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
 static int is_uuid_tree_supported(int fd)
 {
        int ret;
@@ -522,7 +540,7 @@ int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
        struct btrfs_ioctl_search_key *sk = &args.key;
        struct btrfs_ioctl_search_header *sh;
        struct btrfs_root_item *root_item_ptr;
-       struct btrfs_root_item root_item;
+       struct btrfs_root_item root_item = {};
        struct subvol_info *si = NULL;
        int root_item_valid = 0;
        unsigned long off = 0;
@@ -540,7 +558,7 @@ int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
        ret = is_uuid_tree_supported(mnt_fd);
        if (ret < 0) {
                fprintf(stderr,
-                       "ERROR: check if we support uuid tree fails- %s\n",
+                       "ERROR: check if we support uuid tree fails - %s\n",
                        strerror(errno));
                return ret;
        } else if (ret) {
@@ -563,7 +581,7 @@ int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
                ret = ioctl(mnt_fd, BTRFS_IOC_TREE_SEARCH, &args);
                e = errno;
                if (ret < 0) {
-                       fprintf(stderr, "ERROR: can't perform the search- %s\n",
+                       fprintf(stderr, "ERROR: can't perform the search - %s\n",
                                strerror(e));
                        return ret;
                }
@@ -679,27 +697,56 @@ void subvol_uuid_search_finit(struct subvol_uuid_search *s)
        s->received_subvols = RB_ROOT;
        s->path_subvols = RB_ROOT;
 }
+#else
+int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
+{
+       s->mnt_fd = mnt_fd;
 
-char *path_cat(const char *p1, const char *p2)
+       return 0;
+}
+
+void subvol_uuid_search_finit(struct subvol_uuid_search *s)
+{
+}
+#endif
+
+int path_cat_out(char *out, const char *p1, const char *p2)
 {
        int p1_len = strlen(p1);
        int p2_len = strlen(p2);
-       char *new = malloc(p1_len + p2_len + 2);
+
+       if (p1_len + p2_len + 2 >= PATH_MAX)
+               return -ENAMETOOLONG;
 
        if (p1_len && p1[p1_len - 1] == '/')
                p1_len--;
        if (p2_len && p2[p2_len - 1] == '/')
                p2_len--;
-       sprintf(new, "%.*s/%.*s", p1_len, p1, p2_len, p2);
+       sprintf(out, "%.*s/%.*s", p1_len, p1, p2_len, p2);
+
+       return 0;
+}
+
+__attribute__((deprecated("please use path_cat_out")))
+char *path_cat(const char *p1, const char *p2)
+{
+       int p1_len = strlen(p1);
+       int p2_len = strlen(p2);
+       char *new = malloc(p1_len + p2_len + 2);
+
+       path_cat_out(new, p1, p2);
+
        return new;
 }
 
-char *path_cat3(const char *p1, const char *p2, const char *p3)
+int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3)
 {
        int p1_len = strlen(p1);
        int p2_len = strlen(p2);
        int p3_len = strlen(p3);
-       char *new = malloc(p1_len + p2_len + p3_len + 3);
+
+       if (p1_len + p2_len + p3_len + 3 >= PATH_MAX)
+               return -ENAMETOOLONG;
 
        if (p1_len && p1[p1_len - 1] == '/')
                p1_len--;
@@ -707,6 +754,20 @@ char *path_cat3(const char *p1, const char *p2, const char *p3)
                p2_len--;
        if (p3_len && p3[p3_len - 1] == '/')
                p3_len--;
-       sprintf(new, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);
+       sprintf(out, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);
+
+       return 0;
+}
+
+__attribute__((deprecated("please use path_cat3_out")))
+char *path_cat3(const char *p1, const char *p2, const char *p3)
+{
+       int p1_len = strlen(p1);
+       int p2_len = strlen(p2);
+       int p3_len = strlen(p3);
+       char *new = malloc(p1_len + p2_len + p3_len + 3);
+
+       path_cat3_out(new, p1, p2, p3);
+
        return new;
 }