mirror of
https://github.com/kdave/btrfs-progs
synced 2024-12-24 15:12:47 +00:00
btrfs-progs: move find_mount_root to utils.[ch]
Move find_mount_root to utils.[ch] for general use. Signed-off-by: Qu Wenruo <quwenruo@cn.fuijitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <clm@fb.com>
This commit is contained in:
parent
e7839bced7
commit
14ef4f5695
49
cmds-send.c
49
cmds-send.c
@ -39,6 +39,7 @@
|
|||||||
#include "ioctl.h"
|
#include "ioctl.h"
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include "send.h"
|
#include "send.h"
|
||||||
#include "send-utils.h"
|
#include "send-utils.h"
|
||||||
@ -57,54 +58,6 @@ struct btrfs_send {
|
|||||||
struct subvol_uuid_search sus;
|
struct subvol_uuid_search sus;
|
||||||
};
|
};
|
||||||
|
|
||||||
int find_mount_root(const char *path, char **mount_root)
|
|
||||||
{
|
|
||||||
FILE *mnttab;
|
|
||||||
int fd;
|
|
||||||
struct mntent *ent;
|
|
||||||
int len;
|
|
||||||
int ret;
|
|
||||||
int longest_matchlen = 0;
|
|
||||||
char *longest_match = NULL;
|
|
||||||
|
|
||||||
fd = open(path, O_RDONLY | O_NOATIME);
|
|
||||||
if (fd < 0)
|
|
||||||
return -errno;
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
mnttab = setmntent("/proc/self/mounts", "r");
|
|
||||||
if (!mnttab)
|
|
||||||
return -errno;
|
|
||||||
|
|
||||||
while ((ent = getmntent(mnttab))) {
|
|
||||||
len = strlen(ent->mnt_dir);
|
|
||||||
if (strncmp(ent->mnt_dir, path, len) == 0) {
|
|
||||||
/* match found */
|
|
||||||
if (longest_matchlen < len) {
|
|
||||||
free(longest_match);
|
|
||||||
longest_matchlen = len;
|
|
||||||
longest_match = strdup(ent->mnt_dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
endmntent(mnttab);
|
|
||||||
|
|
||||||
if (!longest_match) {
|
|
||||||
fprintf(stderr,
|
|
||||||
"ERROR: Failed to find mount root for path %s.\n",
|
|
||||||
path);
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
*mount_root = realpath(longest_match, NULL);
|
|
||||||
if (!*mount_root)
|
|
||||||
ret = -errno;
|
|
||||||
|
|
||||||
free(longest_match);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_root_id(struct btrfs_send *s, const char *path, u64 *root_id)
|
static int get_root_id(struct btrfs_send *s, const char *path, u64 *root_id)
|
||||||
{
|
{
|
||||||
struct subvol_info *si;
|
struct subvol_info *si;
|
||||||
|
@ -126,5 +126,4 @@ int cmd_rescue(int argc, char **argv);
|
|||||||
int test_issubvolume(char *path);
|
int test_issubvolume(char *path);
|
||||||
|
|
||||||
/* send.c */
|
/* send.c */
|
||||||
int find_mount_root(const char *path, char **mount_root);
|
|
||||||
char *get_subvol_name(char *mnt, char *full_path);
|
char *get_subvol_name(char *mnt, char *full_path);
|
||||||
|
48
utils.c
48
utils.c
@ -2110,3 +2110,51 @@ int lookup_ino_rootid(int fd, u64 *rootid)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int find_mount_root(const char *path, char **mount_root)
|
||||||
|
{
|
||||||
|
FILE *mnttab;
|
||||||
|
int fd;
|
||||||
|
struct mntent *ent;
|
||||||
|
int len;
|
||||||
|
int ret;
|
||||||
|
int longest_matchlen = 0;
|
||||||
|
char *longest_match = NULL;
|
||||||
|
|
||||||
|
fd = open(path, O_RDONLY | O_NOATIME);
|
||||||
|
if (fd < 0)
|
||||||
|
return -errno;
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
mnttab = setmntent("/proc/self/mounts", "r");
|
||||||
|
if (!mnttab)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
while ((ent = getmntent(mnttab))) {
|
||||||
|
len = strlen(ent->mnt_dir);
|
||||||
|
if (strncmp(ent->mnt_dir, path, len) == 0) {
|
||||||
|
/* match found */
|
||||||
|
if (longest_matchlen < len) {
|
||||||
|
free(longest_match);
|
||||||
|
longest_matchlen = len;
|
||||||
|
longest_match = strdup(ent->mnt_dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
endmntent(mnttab);
|
||||||
|
|
||||||
|
if (!longest_match) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"ERROR: Failed to find mount root for path %s.\n",
|
||||||
|
path);
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
*mount_root = realpath(longest_match, NULL);
|
||||||
|
if (!*mount_root)
|
||||||
|
ret = -errno;
|
||||||
|
|
||||||
|
free(longest_match);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
1
utils.h
1
utils.h
@ -96,5 +96,6 @@ int ask_user(char *question);
|
|||||||
int lookup_ino_rootid(int fd, u64 *rootid);
|
int lookup_ino_rootid(int fd, u64 *rootid);
|
||||||
int btrfs_scan_lblkid(int update_kernel);
|
int btrfs_scan_lblkid(int update_kernel);
|
||||||
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
|
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
|
||||||
|
int find_mount_root(const char *path, char **mount_root);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user