btrfs-progs: Check if the FSID was seen by comparing full UUID
[platform/upstream/btrfs-progs.git] / send-utils.c
index cbaf2e9..3c369b8 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"
@@ -263,7 +265,7 @@ static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
        search_arg.key.max_transid = (u64)-1;
        search_arg.key.nr_items = 1;
        ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg);
-       if (ret) {
+       if (ret < 0) {
                fprintf(stderr,
                        "ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %s\n",
                        (unsigned long long)subvol_id, ret, strerror(errno));
@@ -300,7 +302,7 @@ static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
                ino_lookup_arg.objectid =
                        btrfs_stack_root_ref_dirid(backref_item);
                ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg);
-               if (ret) {
+               if (ret < 0) {
                        fprintf(stderr,
                                "ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %s\n",
                                ret, strerror(errno));
@@ -481,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:
@@ -543,7 +545,6 @@ int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
        int root_item_valid = 0;
        unsigned long off = 0;
        int i;
-       int e;
        char *path;
 
        s->mnt_fd = mnt_fd;
@@ -577,10 +578,9 @@ int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
 
        while (1) {
                ret = ioctl(mnt_fd, BTRFS_IOC_TREE_SEARCH, &args);
-               e = errno;
                if (ret < 0) {
                        fprintf(stderr, "ERROR: can't perform the search - %s\n",
-                               strerror(e));
+                               strerror(errno));
                        return ret;
                }
                if (sk->nr_items == 0)
@@ -708,26 +708,43 @@ void subvol_uuid_search_finit(struct subvol_uuid_search *s)
 }
 #endif
 
-char *path_cat(const char *p1, const char *p2)
+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))
+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--;
@@ -735,6 +752,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))
+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;
 }