btrfs: fix get_btrfs_subvol_path()

parent 3893aee8
...@@ -41,6 +41,10 @@ ...@@ -41,6 +41,10 @@
#include "storage.h" #include "storage.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCAT
#include "include/strlcat.h"
#endif
#ifndef HAVE_STRLCPY #ifndef HAVE_STRLCPY
#include "include/strlcpy.h" #include "include/strlcpy.h"
#endif #endif
...@@ -58,11 +62,11 @@ extern char *dir_new_path(char *src, const char *oldname, const char *name, ...@@ -58,11 +62,11 @@ extern char *dir_new_path(char *src, const char *oldname, const char *name,
* simply return a. * simply return a.
*/ */
char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
int name_len) u16 name_len)
{ {
struct btrfs_ioctl_ino_lookup_args args; struct btrfs_ioctl_ino_lookup_args args;
int ret, e; int ret;
size_t len; size_t len, retlen;
char *retpath; char *retpath;
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
...@@ -70,17 +74,16 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, ...@@ -70,17 +74,16 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
args.objectid = objid; args.objectid = objid;
ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
e = errno;
if (ret) { if (ret) {
ERROR("Failed to lookup path for %llu %llu %s - %s\n", SYSERROR("Failed to lookup path for %llu %llu %s",
(unsigned long long) dir_id, (unsigned long long) dir_id,
(unsigned long long) objid, (unsigned long long) objid,
name, strerror(e)); name);
return NULL; return NULL;
} else } else
INFO("Got path for %llu %llu - %s\n", INFO("Got path for %llu %llu - %s",
(unsigned long long) objid, (unsigned long long) dir_id, (unsigned long long) objid, (unsigned long long) dir_id,
name); name);
if (args.name[0]) { if (args.name[0]) {
/* /*
...@@ -91,18 +94,33 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, ...@@ -91,18 +94,33 @@ char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
retpath = malloc(len); retpath = malloc(len);
if (!retpath) if (!retpath)
return NULL; return NULL;
(void)strlcpy(retpath, args.name, len); (void)strlcpy(retpath, args.name, len);
strncat(retpath, "/", 1); (void)strlcat(retpath, "/", len);
strncat(retpath, name, name_len);
retlen = strlcat(retpath, name, len);
if (retlen >= len) {
ERROR("Failed to append name - %s", name);
free(retpath);
return NULL;
}
} else { } else {
/* we're at the root of ref_tree */ /* we're at the root of ref_tree */
len = name_len + 1; len = name_len + 1;
retpath = malloc(len); retpath = malloc(len);
if (!retpath) if (!retpath)
return NULL; return NULL;
*retpath = '\0'; *retpath = '\0';
strncat(retpath, name, name_len);
retlen = strlcat(retpath, name, len);
if (retlen >= len) {
ERROR("Failed to append name - %s", name);
free(retpath);
return NULL;
}
} }
return retpath; return retpath;
} }
......
...@@ -396,7 +396,7 @@ extern int btrfs_mount(struct lxc_storage *bdev); ...@@ -396,7 +396,7 @@ extern int btrfs_mount(struct lxc_storage *bdev);
extern int btrfs_umount(struct lxc_storage *bdev); extern int btrfs_umount(struct lxc_storage *bdev);
extern char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, extern char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
int name_len); u16 name_len);
extern int btrfs_list_get_path_rootid(int fd, u64 *treeid); extern int btrfs_list_get_path_rootid(int fd, u64 *treeid);
extern bool is_btrfs_fs(const char *path); extern bool is_btrfs_fs(const char *path);
extern int is_btrfs_subvol(const char *path); extern int is_btrfs_subvol(const char *path);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment