Btrfs-progs: fix closing of opendir()

valgrind complains open_file_or_dir() causes a memory leak.That is because
if we open a directoy by opendir(), and then we should call closedir()
to free memory.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
master
Wang Shilong 2013-07-15 19:36:50 +08:00 committed by David Sterba
parent c118c21b3e
commit c125b7cf43
12 changed files with 148 additions and 114 deletions

View File

@ -403,6 +403,7 @@ int main(int argc, char **argv)
int ret; int ret;
u64 flags = 0; u64 flags = 0;
char *dir = "html"; char *dir = "html";
DIR *dirstream = NULL;
while (1) { while (1) {
int c = getopt(argc, argv, "cmso:h"); int c = getopt(argc, argv, "cmso:h");
@ -437,7 +438,7 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
exit(1); exit(1);
@ -447,6 +448,7 @@ int main(int argc, char **argv)
flags = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA; flags = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
ret = list_fragments(fd, flags, dir); ret = list_fragments(fd, flags, dir);
close_file_or_dir(fd, dirstream);
if (ret) if (ret)
exit(1); exit(1);

View File

@ -294,8 +294,9 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
int fd; int fd;
int ret; int ret;
int e; int e;
DIR *dirstream = NULL;
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -339,7 +340,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
} }
out: out:
close(fd); close_file_or_dir(fd, dirstream);
return ret; return ret;
} }
@ -487,13 +488,14 @@ static int cmd_balance_pause(int argc, char **argv)
int fd; int fd;
int ret; int ret;
int e; int e;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_balance_pause_usage); usage(cmd_balance_pause_usage);
path = argv[1]; path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -501,7 +503,7 @@ static int cmd_balance_pause(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE); ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n", fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n",
@ -524,13 +526,14 @@ static int cmd_balance_cancel(int argc, char **argv)
int fd; int fd;
int ret; int ret;
int e; int e;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_balance_cancel_usage); usage(cmd_balance_cancel_usage);
path = argv[1]; path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -538,7 +541,7 @@ static int cmd_balance_cancel(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL); ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n", fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n",
@ -559,6 +562,7 @@ static int cmd_balance_resume(int argc, char **argv)
{ {
struct btrfs_ioctl_balance_args args; struct btrfs_ioctl_balance_args args;
const char *path; const char *path;
DIR *dirstream = NULL;
int fd; int fd;
int ret; int ret;
int e; int e;
@ -568,7 +572,7 @@ static int cmd_balance_resume(int argc, char **argv)
path = argv[1]; path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -579,7 +583,7 @@ static int cmd_balance_resume(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args); ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
if (e == ECANCELED) { if (e == ECANCELED) {
@ -626,6 +630,7 @@ static int cmd_balance_status(int argc, char **argv)
{ {
struct btrfs_ioctl_balance_args args; struct btrfs_ioctl_balance_args args;
const char *path; const char *path;
DIR *dirstream = NULL;
int fd; int fd;
int verbose = 0; int verbose = 0;
int ret; int ret;
@ -657,7 +662,7 @@ static int cmd_balance_status(int argc, char **argv)
path = argv[optind]; path = argv[optind];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 2; return 2;
@ -665,7 +670,7 @@ static int cmd_balance_status(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args); ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
if (e == ENOTCONN) { if (e == ENOTCONN) {

View File

@ -54,13 +54,14 @@ static int cmd_add_dev(int argc, char **argv)
{ {
char *mntpnt; char *mntpnt;
int i, fdmnt, ret=0, e; int i, fdmnt, ret=0, e;
DIR *dirstream = NULL;
if (check_argc_min(argc, 3)) if (check_argc_min(argc, 3))
usage(cmd_add_dev_usage); usage(cmd_add_dev_usage);
mntpnt = argv[argc - 1]; mntpnt = argv[argc - 1];
fdmnt = open_file_or_dir(mntpnt); fdmnt = open_file_or_dir(mntpnt, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt); fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
return 12; return 12;
@ -127,7 +128,7 @@ static int cmd_add_dev(int argc, char **argv)
} }
close(fdmnt); close_file_or_dir(fdmnt, dirstream);
if (ret) if (ret)
return ret+20; return ret+20;
else else
@ -144,13 +145,14 @@ static int cmd_rm_dev(int argc, char **argv)
{ {
char *mntpnt; char *mntpnt;
int i, fdmnt, ret=0, e; int i, fdmnt, ret=0, e;
DIR *dirstream = NULL;
if (check_argc_min(argc, 3)) if (check_argc_min(argc, 3))
usage(cmd_rm_dev_usage); usage(cmd_rm_dev_usage);
mntpnt = argv[argc - 1]; mntpnt = argv[argc - 1];
fdmnt = open_file_or_dir(mntpnt); fdmnt = open_file_or_dir(mntpnt, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt); fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
return 12; return 12;
@ -170,7 +172,7 @@ static int cmd_rm_dev(int argc, char **argv)
} }
} }
close(fdmnt); close_file_or_dir(fdmnt, dirstream);
if( ret) if( ret)
return ret+20; return ret+20;
else else
@ -297,6 +299,7 @@ static int cmd_dev_stats(int argc, char **argv)
int c; int c;
int err = 0; int err = 0;
__u64 flags = 0; __u64 flags = 0;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while ((c = getopt(argc, argv, "z")) != -1) { while ((c = getopt(argc, argv, "z")) != -1) {
@ -321,7 +324,7 @@ static int cmd_dev_stats(int argc, char **argv)
path = argv[optind]; path = argv[optind];
fdmnt = open_path_or_dev_mnt(path); fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
@ -389,7 +392,7 @@ static int cmd_dev_stats(int argc, char **argv)
out: out:
free(di_args); free(di_args);
close(fdmnt); close_file_or_dir(fdmnt, dirstream);
return err; return err;
} }

View File

@ -52,13 +52,14 @@ static int cmd_df(int argc, char **argv)
int fd; int fd;
int e; int e;
char *path; char *path;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_df_usage); usage(cmd_df_usage);
path = argv[1]; path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -76,14 +77,11 @@ static int cmd_df(int argc, char **argv)
if (ret) { if (ret) {
fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
path, strerror(e)); path, strerror(e));
close(fd); goto out;
free(sargs);
return ret;
} }
if (!sargs->total_spaces) { if (!sargs->total_spaces) {
close(fd); ret = 0;
free(sargs); goto out;
return 0;
} }
count = sargs->total_spaces; count = sargs->total_spaces;
@ -91,9 +89,9 @@ static int cmd_df(int argc, char **argv)
sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) + sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
(count * sizeof(struct btrfs_ioctl_space_info))); (count * sizeof(struct btrfs_ioctl_space_info)));
if (!sargs) { if (!sargs) {
close(fd); sargs = sargs_orig;
free(sargs_orig); ret = -ENOMEM;
return -ENOMEM; goto out;
} }
sargs->space_slots = count; sargs->space_slots = count;
@ -104,9 +102,7 @@ static int cmd_df(int argc, char **argv)
if (ret) { if (ret) {
fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
path, strerror(e)); path, strerror(e));
close(fd); goto out;
free(sargs);
return ret;
} }
for (i = 0; i < sargs->total_spaces; i++) { for (i = 0; i < sargs->total_spaces; i++) {
@ -157,7 +153,8 @@ static int cmd_df(int argc, char **argv)
pretty_size(sargs->spaces[i].total_bytes), pretty_size(sargs->spaces[i].total_bytes),
pretty_size(sargs->spaces[i].used_bytes)); pretty_size(sargs->spaces[i].used_bytes));
} }
close(fd); out:
close_file_or_dir(fd, dirstream);
free(sargs); free(sargs);
return 0; return 0;
@ -282,13 +279,14 @@ static int cmd_sync(int argc, char **argv)
{ {
int fd, res, e; int fd, res, e;
char *path; char *path;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_sync_usage); usage(cmd_sync_usage);
path = argv[1]; path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -297,7 +295,7 @@ static int cmd_sync(int argc, char **argv)
printf("FSSync '%s'\n", path); printf("FSSync '%s'\n", path);
res = ioctl(fd, BTRFS_IOC_SYNC); res = ioctl(fd, BTRFS_IOC_SYNC);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if( res < 0 ){ if( res < 0 ){
fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
path, strerror(e)); path, strerror(e));
@ -347,6 +345,7 @@ static int cmd_defrag(int argc, char **argv)
struct btrfs_ioctl_defrag_range_args range; struct btrfs_ioctl_defrag_range_args range;
int e=0; int e=0;
int compress_type = BTRFS_COMPRESS_NONE; int compress_type = BTRFS_COMPRESS_NONE;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while(1) { while(1) {
@ -402,7 +401,7 @@ static int cmd_defrag(int argc, char **argv)
for (i = optind; i < argc; i++) { for (i = optind; i < argc; i++) {
if (verbose) if (verbose)
printf("%s\n", argv[i]); printf("%s\n", argv[i]);
fd = open_file_or_dir(argv[i]); fd = open_file_or_dir(argv[i], &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "failed to open %s\n", argv[i]); fprintf(stderr, "failed to open %s\n", argv[i]);
perror("open:"); perror("open:");
@ -429,7 +428,7 @@ static int cmd_defrag(int argc, char **argv)
argv[i], strerror(e)); argv[i], strerror(e));
errors++; errors++;
} }
close(fd); close_file_or_dir(fd, dirstream);
} }
if (verbose) if (verbose)
printf("%s\n", BTRFS_BUILD_VERSION); printf("%s\n", BTRFS_BUILD_VERSION);
@ -454,6 +453,7 @@ static int cmd_resize(int argc, char **argv)
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
int fd, res, len, e; int fd, res, len, e;
char *amount, *path; char *amount, *path;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 3)) if (check_argc_exact(argc, 3))
usage(cmd_resize_usage); usage(cmd_resize_usage);
@ -468,7 +468,7 @@ static int cmd_resize(int argc, char **argv)
return 14; return 14;
} }
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12; return 12;
@ -478,7 +478,7 @@ static int cmd_resize(int argc, char **argv)
strncpy_null(args.name, amount); strncpy_null(args.name, amount);
res = ioctl(fd, BTRFS_IOC_RESIZE, &args); res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if( res < 0 ){ if( res < 0 ){
fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
path, strerror(e)); path, strerror(e));

View File

@ -92,6 +92,7 @@ static int cmd_inode_resolve(int argc, char **argv)
int fd; int fd;
int verbose = 0; int verbose = 0;
int ret; int ret;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while (1) { while (1) {
@ -111,7 +112,7 @@ static int cmd_inode_resolve(int argc, char **argv)
if (check_argc_exact(argc - optind, 2)) if (check_argc_exact(argc - optind, 2))
usage(cmd_inode_resolve_usage); usage(cmd_inode_resolve_usage);
fd = open_file_or_dir(argv[optind+1]); fd = open_file_or_dir(argv[optind+1], &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]); fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
return 12; return 12;
@ -119,7 +120,7 @@ static int cmd_inode_resolve(int argc, char **argv)
ret = __ino_to_path_fd(atoll(argv[optind]), fd, verbose, ret = __ino_to_path_fd(atoll(argv[optind]), fd, verbose,
argv[optind+1]); argv[optind+1]);
close(fd); close_file_or_dir(fd, dirstream);
return ret; return ret;
} }
@ -148,6 +149,7 @@ static int cmd_logical_resolve(int argc, char **argv)
u64 size = 4096; u64 size = 4096;
char full_path[4096]; char full_path[4096];
char *path_ptr; char *path_ptr;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while (1) { while (1) {
@ -183,7 +185,7 @@ static int cmd_logical_resolve(int argc, char **argv)
loi.size = size; loi.size = size;
loi.inodes = (uintptr_t)inodes; loi.inodes = (uintptr_t)inodes;
fd = open_file_or_dir(argv[optind+1]); fd = open_file_or_dir(argv[optind+1], &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]); fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
ret = 12; ret = 12;
@ -216,6 +218,7 @@ static int cmd_logical_resolve(int argc, char **argv)
u64 root = inodes->val[i+2]; u64 root = inodes->val[i+2];
int path_fd; int path_fd;
char *name; char *name;
DIR *dirs = NULL;
if (getpath) { if (getpath) {
name = btrfs_list_path_for_root(fd, root); name = btrfs_list_path_for_root(fd, root);
@ -232,7 +235,7 @@ static int cmd_logical_resolve(int argc, char **argv)
name); name);
BUG_ON(ret >= bytes_left); BUG_ON(ret >= bytes_left);
free(name); free(name);
path_fd = open_file_or_dir(full_path); path_fd = open_file_or_dir(full_path, &dirs);
if (path_fd < 0) { if (path_fd < 0) {
fprintf(stderr, "ERROR: can't access " fprintf(stderr, "ERROR: can't access "
"'%s'\n", full_path); "'%s'\n", full_path);
@ -241,7 +244,7 @@ static int cmd_logical_resolve(int argc, char **argv)
} }
__ino_to_path_fd(inum, path_fd, verbose, full_path); __ino_to_path_fd(inum, path_fd, verbose, full_path);
if (path_fd != fd) if (path_fd != fd)
close(path_fd); close_file_or_dir(path_fd, dirs);
} else { } else {
printf("inode %llu offset %llu root %llu\n", inum, printf("inode %llu offset %llu root %llu\n", inum,
offset, root); offset, root);
@ -249,8 +252,7 @@ static int cmd_logical_resolve(int argc, char **argv)
} }
out: out:
if (fd >= 0) close_file_or_dir(fd, dirstream);
close(fd);
free(inodes); free(inodes);
return ret; return ret;
} }
@ -267,11 +269,12 @@ static int cmd_subvolid_resolve(int argc, char **argv)
int fd = -1; int fd = -1;
u64 subvol_id; u64 subvol_id;
char path[BTRFS_PATH_NAME_MAX + 1]; char path[BTRFS_PATH_NAME_MAX + 1];
DIR *dirstream = NULL;
if (check_argc_exact(argc, 3)) if (check_argc_exact(argc, 3))
usage(cmd_subvolid_resolve_usage); usage(cmd_subvolid_resolve_usage);
fd = open_file_or_dir(argv[2]); fd = open_file_or_dir(argv[2], &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", argv[2]); fprintf(stderr, "ERROR: can't access '%s'\n", argv[2]);
ret = -ENOENT; ret = -ENOENT;
@ -292,8 +295,7 @@ static int cmd_subvolid_resolve(int argc, char **argv)
printf("%s\n", path); printf("%s\n", path);
out: out:
if (fd >= 0) close_file_or_dir(fd, dirstream);
close(fd);
return ret ? 1 : 0; return ret ? 1 : 0;
} }

View File

@ -39,6 +39,7 @@ static int qgroup_assign(int assign, int argc, char **argv)
int e; int e;
char *path = argv[3]; char *path = argv[3];
struct btrfs_ioctl_qgroup_assign_args args; struct btrfs_ioctl_qgroup_assign_args args;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 4)) if (check_argc_exact(argc, 4))
return -1; return -1;
@ -55,7 +56,7 @@ static int qgroup_assign(int assign, int argc, char **argv)
fprintf(stderr, "ERROR: bad relation requested '%s'\n", path); fprintf(stderr, "ERROR: bad relation requested '%s'\n", path);
return 12; return 12;
} }
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -63,7 +64,7 @@ static int qgroup_assign(int assign, int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args); ret = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: unable to assign quota group: %s\n", fprintf(stderr, "ERROR: unable to assign quota group: %s\n",
strerror(e)); strerror(e));
@ -79,6 +80,7 @@ static int qgroup_create(int create, int argc, char **argv)
int e; int e;
char *path = argv[2]; char *path = argv[2];
struct btrfs_ioctl_qgroup_create_args args; struct btrfs_ioctl_qgroup_create_args args;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 3)) if (check_argc_exact(argc, 3))
return -1; return -1;
@ -87,7 +89,7 @@ static int qgroup_create(int create, int argc, char **argv)
args.create = create; args.create = create;
args.qgroupid = parse_qgroupid(argv[1]); args.qgroupid = parse_qgroupid(argv[1]);
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -95,7 +97,7 @@ static int qgroup_create(int create, int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args); ret = ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: unable to create quota group: %s\n", fprintf(stderr, "ERROR: unable to create quota group: %s\n",
strerror(e)); strerror(e));
@ -300,11 +302,12 @@ static int cmd_qgroup_show(int argc, char **argv)
int fd; int fd;
int e; int e;
char *path = argv[1]; char *path = argv[1];
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_qgroup_show_usage); usage(cmd_qgroup_show_usage);
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -312,7 +315,7 @@ static int cmd_qgroup_show(int argc, char **argv)
ret = list_qgroups(fd); ret = list_qgroups(fd);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: can't list qgroups: %s\n", fprintf(stderr, "ERROR: can't list qgroups: %s\n",
strerror(e)); strerror(e));
@ -342,6 +345,7 @@ static int cmd_qgroup_limit(int argc, char **argv)
unsigned long long size; unsigned long long size;
int compressed = 0; int compressed = 0;
int exclusive = 0; int exclusive = 0;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while (1) { while (1) {
@ -405,7 +409,7 @@ static int cmd_qgroup_limit(int argc, char **argv)
} else } else
usage(cmd_qgroup_limit_usage); usage(cmd_qgroup_limit_usage);
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -413,7 +417,7 @@ static int cmd_qgroup_limit(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args); ret = ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: unable to limit requested quota group: " fprintf(stderr, "ERROR: unable to limit requested quota group: "
"%s\n", strerror(e)); "%s\n", strerror(e));

View File

@ -37,6 +37,7 @@ int quota_ctl(int cmd, int argc, char **argv)
int e; int e;
char *path = argv[1]; char *path = argv[1];
struct btrfs_ioctl_quota_ctl_args args; struct btrfs_ioctl_quota_ctl_args args;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
return -1; return -1;
@ -44,7 +45,7 @@ int quota_ctl(int cmd, int argc, char **argv)
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
args.cmd = cmd; args.cmd = cmd;
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -52,7 +53,7 @@ int quota_ctl(int cmd, int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args); ret = ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: quota command failed: %s\n", fprintf(stderr, "ERROR: quota command failed: %s\n",
strerror(e)); strerror(e));
@ -108,6 +109,7 @@ static int cmd_quota_rescan(int argc, char **argv)
char *path = NULL; char *path = NULL;
struct btrfs_ioctl_quota_rescan_args args; struct btrfs_ioctl_quota_rescan_args args;
int ioctlnum = BTRFS_IOC_QUOTA_RESCAN; int ioctlnum = BTRFS_IOC_QUOTA_RESCAN;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while (1) { while (1) {
@ -129,7 +131,7 @@ static int cmd_quota_rescan(int argc, char **argv)
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
path = argv[optind]; path = argv[optind];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);
return 12; return 12;
@ -137,7 +139,7 @@ static int cmd_quota_rescan(int argc, char **argv)
ret = ioctl(fd, ioctlnum, &args); ret = ioctl(fd, ioctlnum, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) { if (ioctlnum == BTRFS_IOC_QUOTA_RESCAN) {
if (ret < 0) { if (ret < 0) {

View File

@ -143,6 +143,7 @@ static int cmd_start_replace(int argc, char **argv)
u64 dstdev_block_count; u64 dstdev_block_count;
int do_not_background = 0; int do_not_background = 0;
int mixed = 0; int mixed = 0;
DIR *dirstream = NULL;
while ((c = getopt(argc, argv, "Brf")) != -1) { while ((c = getopt(argc, argv, "Brf")) != -1) {
switch (c) { switch (c) {
@ -169,7 +170,7 @@ static int cmd_start_replace(int argc, char **argv)
usage(cmd_start_replace_usage); usage(cmd_start_replace_usage);
path = argv[optind + 2]; path = argv[optind + 2];
fdmnt = open_path_or_dev_mnt(path); fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access \"%s\": %s\n", fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
@ -336,7 +337,7 @@ static int cmd_start_replace(int argc, char **argv)
goto leave_with_error; goto leave_with_error;
} }
} }
close(fdmnt); close_file_or_dir(fdmnt, dirstream);
return 0; return 0;
leave_with_error: leave_with_error:
@ -367,6 +368,7 @@ static int cmd_status_replace(int argc, char **argv)
char *path; char *path;
int once = 0; int once = 0;
int ret; int ret;
DIR *dirstream = NULL;
while ((c = getopt(argc, argv, "1")) != -1) { while ((c = getopt(argc, argv, "1")) != -1) {
switch (c) { switch (c) {
@ -383,7 +385,7 @@ static int cmd_status_replace(int argc, char **argv)
usage(cmd_status_replace_usage); usage(cmd_status_replace_usage);
path = argv[optind]; path = argv[optind];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
e = errno; e = errno;
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access \"%s\": %s\n", fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
@ -392,7 +394,7 @@ static int cmd_status_replace(int argc, char **argv)
} }
ret = print_replace_status(fd, path, once); ret = print_replace_status(fd, path, once);
close(fd); close_file_or_dir(fd, dirstream);
return ret; return ret;
} }
@ -533,6 +535,7 @@ static int cmd_cancel_replace(int argc, char **argv)
int fd; int fd;
int e; int e;
char *path; char *path;
DIR *dirstream = NULL;
while ((c = getopt(argc, argv, "")) != -1) { while ((c = getopt(argc, argv, "")) != -1) {
switch (c) { switch (c) {
@ -546,7 +549,7 @@ static int cmd_cancel_replace(int argc, char **argv)
usage(cmd_cancel_replace_usage); usage(cmd_cancel_replace_usage);
path = argv[optind]; path = argv[optind];
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access \"%s\": %s\n", fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
path, strerror(errno)); path, strerror(errno));
@ -556,7 +559,7 @@ static int cmd_cancel_replace(int argc, char **argv)
args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL; args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args); ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret) { if (ret) {
fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s, %s\n", fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s, %s\n",
path, strerror(e), path, strerror(e),

View File

@ -1095,6 +1095,7 @@ static int scrub_start(int argc, char **argv, int resume)
pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER;
void *terr; void *terr;
u64 devid; u64 devid;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while ((c = getopt(argc, argv, "BdqrRc:n:")) != -1) { while ((c = getopt(argc, argv, "BdqrRc:n:")) != -1) {
@ -1150,7 +1151,7 @@ static int scrub_start(int argc, char **argv, int resume)
path = argv[optind]; path = argv[optind];
fdmnt = open_path_or_dev_mnt(path); fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
ERR(!do_quiet, "ERROR: can't access '%s'\n", path); ERR(!do_quiet, "ERROR: can't access '%s'\n", path);
@ -1495,7 +1496,7 @@ out:
if (sock_path[0]) if (sock_path[0])
unlink(sock_path); unlink(sock_path);
} }
close(fdmnt); close_file_or_dir(fdmnt, dirstream);
if (err) if (err)
return 1; return 1;
@ -1535,13 +1536,14 @@ static int cmd_scrub_cancel(int argc, char **argv)
char *path; char *path;
int ret; int ret;
int fdmnt = -1; int fdmnt = -1;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_scrub_cancel_usage); usage(cmd_scrub_cancel_usage);
path = argv[1]; path = argv[1];
fdmnt = open_path_or_dev_mnt(path); fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: could not open %s: %s\n", fprintf(stderr, "ERROR: could not open %s: %s\n",
path, strerror(errno)); path, strerror(errno));
@ -1562,8 +1564,7 @@ static int cmd_scrub_cancel(int argc, char **argv)
printf("scrub cancelled\n"); printf("scrub cancelled\n");
out: out:
if (fdmnt != -1) close_file_or_dir(fdmnt, dirstream);
close(fdmnt);
return ret; return ret;
} }
@ -1614,6 +1615,7 @@ static int cmd_scrub_status(int argc, char **argv)
char fsid[37]; char fsid[37];
int fdres = -1; int fdres = -1;
int err = 0; int err = 0;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while ((c = getopt(argc, argv, "dR")) != -1) { while ((c = getopt(argc, argv, "dR")) != -1) {
@ -1635,7 +1637,7 @@ static int cmd_scrub_status(int argc, char **argv)
path = argv[optind]; path = argv[optind];
fdmnt = open_path_or_dev_mnt(path); fdmnt = open_path_or_dev_mnt(path, &dirstream);
if (fdmnt < 0) { if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
@ -1722,6 +1724,7 @@ out:
free(di_args); free(di_args);
if (fdres > -1) if (fdres > -1)
close(fdres); close(fdres);
close_file_or_dir(fdmnt, dirstream);
return err; return err;
} }

View File

@ -79,6 +79,7 @@ static int cmd_subvol_create(int argc, char **argv)
char *dstdir; char *dstdir;
char *dst; char *dst;
struct btrfs_qgroup_inherit *inherit = NULL; struct btrfs_qgroup_inherit *inherit = NULL;
DIR *dirstream = NULL;
optind = 1; optind = 1;
while (1) { while (1) {
@ -137,7 +138,7 @@ static int cmd_subvol_create(int argc, char **argv)
goto out; goto out;
} }
fddst = open_file_or_dir(dstdir); fddst = open_file_or_dir(dstdir, &dirstream);
if (fddst < 0) { if (fddst < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
goto out; goto out;
@ -171,8 +172,7 @@ static int cmd_subvol_create(int argc, char **argv)
retval = 0; /* success */ retval = 0; /* success */
out: out:
if (fddst != -1) close_file_or_dir(fddst, dirstream);
close(fddst);
free(inherit); free(inherit);
return retval; return retval;
@ -209,6 +209,7 @@ static int cmd_subvol_delete(int argc, char **argv)
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
char *dname, *vname, *cpath; char *dname, *vname, *cpath;
char *path; char *path;
DIR *dirstream = NULL;
if (argc < 2) if (argc < 2)
usage(cmd_subvol_delete_usage); usage(cmd_subvol_delete_usage);
@ -251,7 +252,7 @@ again:
goto out; goto out;
} }
fd = open_file_or_dir(dname); fd = open_file_or_dir(dname, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", dname); fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
ret = 12; ret = 12;
@ -263,7 +264,7 @@ again:
res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args); res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if(res < 0 ){ if(res < 0 ){
fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n", fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
@ -332,6 +333,7 @@ static int cmd_subvol_list(int argc, char **argv)
{"sort", 1, NULL, 'S'}, {"sort", 1, NULL, 'S'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
DIR *dirstream = NULL;
filter_set = btrfs_list_alloc_filter_set(); filter_set = btrfs_list_alloc_filter_set();
comparer_set = btrfs_list_alloc_comparer_set(); comparer_set = btrfs_list_alloc_comparer_set();
@ -424,7 +426,7 @@ static int cmd_subvol_list(int argc, char **argv)
} }
subvol = argv[optind]; subvol = argv[optind];
fd = open_file_or_dir(subvol); fd = open_file_or_dir(subvol, &dirstream);
if (fd < 0) { if (fd < 0) {
ret = -1; ret = -1;
fprintf(stderr, "ERROR: can't access '%s'\n", subvol); fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
@ -462,8 +464,7 @@ static int cmd_subvol_list(int argc, char **argv)
!is_list_all && !is_only_in_path, NULL); !is_list_all && !is_only_in_path, NULL);
out: out:
if (fd != -1) close_file_or_dir(fd, dirstream);
close(fd);
if (filter_set) if (filter_set)
btrfs_list_free_filter_set(filter_set); btrfs_list_free_filter_set(filter_set);
if (comparer_set) if (comparer_set)
@ -497,6 +498,7 @@ static int cmd_snapshot(int argc, char **argv)
char *dstdir; char *dstdir;
struct btrfs_ioctl_vol_args_v2 args; struct btrfs_ioctl_vol_args_v2 args;
struct btrfs_qgroup_inherit *inherit = NULL; struct btrfs_qgroup_inherit *inherit = NULL;
DIR *dirstream1 = NULL, *dirstream2 = NULL;
optind = 1; optind = 1;
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
@ -583,13 +585,13 @@ static int cmd_snapshot(int argc, char **argv)
goto out; goto out;
} }
fddst = open_file_or_dir(dstdir); fddst = open_file_or_dir(dstdir, &dirstream1);
if (fddst < 0) { if (fddst < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
goto out; goto out;
} }
fd = open_file_or_dir(subvol); fd = open_file_or_dir(subvol, &dirstream2);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir); fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
goto out; goto out;
@ -623,10 +625,8 @@ static int cmd_snapshot(int argc, char **argv)
retval = 0; /* success */ retval = 0; /* success */
out: out:
if (fd != -1) close_file_or_dir(fddst, dirstream1);
close(fd); close_file_or_dir(fd, dirstream2);
if (fddst != -1)
close(fddst);
free(inherit); free(inherit);
return retval; return retval;
@ -645,12 +645,13 @@ static int cmd_subvol_get_default(int argc, char **argv)
char *subvol; char *subvol;
struct btrfs_list_filter_set *filter_set; struct btrfs_list_filter_set *filter_set;
u64 default_id; u64 default_id;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_subvol_get_default_usage); usage(cmd_subvol_get_default_usage);
subvol = argv[1]; subvol = argv[1];
fd = open_file_or_dir(subvol); fd = open_file_or_dir(subvol, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", subvol); fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 1; return 1;
@ -691,8 +692,7 @@ static int cmd_subvol_get_default(int argc, char **argv)
if (filter_set) if (filter_set)
btrfs_list_free_filter_set(filter_set); btrfs_list_free_filter_set(filter_set);
out: out:
if (fd != -1) close_file_or_dir(fd, dirstream);
close(fd);
if (ret) if (ret)
return 1; return 1;
return 0; return 0;
@ -710,6 +710,7 @@ static int cmd_subvol_set_default(int argc, char **argv)
u64 objectid; u64 objectid;
char *path; char *path;
char *subvolid; char *subvolid;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 3)) if (check_argc_exact(argc, 3))
usage(cmd_subvol_set_default_usage); usage(cmd_subvol_set_default_usage);
@ -723,7 +724,7 @@ static int cmd_subvol_set_default(int argc, char **argv)
return 1; return 1;
} }
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path); fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 1; return 1;
@ -731,7 +732,7 @@ static int cmd_subvol_set_default(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid); ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
e = errno; e = errno;
close(fd); close_file_or_dir(fd, dirstream);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n", fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
strerror(e)); strerror(e));
@ -752,6 +753,7 @@ static int cmd_find_new(int argc, char **argv)
int ret; int ret;
char *subvol; char *subvol;
u64 last_gen; u64 last_gen;
DIR *dirstream = NULL;
if (check_argc_exact(argc, 3)) if (check_argc_exact(argc, 3))
usage(cmd_find_new_usage); usage(cmd_find_new_usage);
@ -769,13 +771,13 @@ static int cmd_find_new(int argc, char **argv)
return 13; return 13;
} }
fd = open_file_or_dir(subvol); fd = open_file_or_dir(subvol, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", subvol); fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
return 12; return 12;
} }
ret = btrfs_list_find_updated_files(fd, 0, last_gen); ret = btrfs_list_find_updated_files(fd, 0, last_gen);
close(fd); close_file_or_dir(fd, dirstream);
if (ret) if (ret)
return 19; return 19;
return 0; return 0;
@ -798,6 +800,7 @@ static int cmd_subvol_show(int argc, char **argv)
u64 sv_id, mntid; u64 sv_id, mntid;
int fd = -1, mntfd = -1; int fd = -1, mntfd = -1;
int ret = -1; int ret = -1;
DIR *dirstream1 = NULL, *dirstream2 = NULL;
if (check_argc_exact(argc, 2)) if (check_argc_exact(argc, 2))
usage(cmd_subvol_show_usage); usage(cmd_subvol_show_usage);
@ -829,7 +832,7 @@ static int cmd_subvol_show(int argc, char **argv)
ret = -1; ret = -1;
svpath = get_subvol_name(mnt, fullpath); svpath = get_subvol_name(mnt, fullpath);
fd = open_file_or_dir(fullpath); fd = open_file_or_dir(fullpath, &dirstream1);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", fullpath); fprintf(stderr, "ERROR: can't access '%s'\n", fullpath);
goto out; goto out;
@ -842,7 +845,7 @@ static int cmd_subvol_show(int argc, char **argv)
goto out; goto out;
} }
mntfd = open_file_or_dir(mnt); mntfd = open_file_or_dir(mnt, &dirstream2);
if (mntfd < 0) { if (mntfd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", mnt); fprintf(stderr, "ERROR: can't access '%s'\n", mnt);
goto out; goto out;
@ -925,10 +928,8 @@ static int cmd_subvol_show(int argc, char **argv)
btrfs_list_free_filter_set(filter_set); btrfs_list_free_filter_set(filter_set);
out: out:
if (mntfd >= 0) close_file_or_dir(fd, dirstream1);
close(mntfd); close_file_or_dir(mntfd, dirstream2);
if (fd >= 0)
close(fd);
if (mnt) if (mnt)
free(mnt); free(mnt);
if (fullpath) if (fullpath)

35
utils.c
View File

@ -31,7 +31,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <mntent.h> #include <mntent.h>
@ -728,7 +727,7 @@ out:
* *
* On error, return -1, errno should be set. * On error, return -1, errno should be set.
*/ */
int open_path_or_dev_mnt(const char *path) int open_path_or_dev_mnt(const char *path, DIR **dirstream)
{ {
char mp[BTRFS_PATH_NAME_MAX + 1]; char mp[BTRFS_PATH_NAME_MAX + 1];
int fdmnt; int fdmnt;
@ -742,9 +741,9 @@ int open_path_or_dev_mnt(const char *path)
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
fdmnt = open_file_or_dir(mp); fdmnt = open_file_or_dir(mp, dirstream);
} else { } else {
fdmnt = open_file_or_dir(path); fdmnt = open_file_or_dir(path, dirstream);
} }
return fdmnt; return fdmnt;
@ -1498,11 +1497,10 @@ u64 parse_size(char *s)
return strtoull(s, NULL, 10) * mult; return strtoull(s, NULL, 10) * mult;
} }
int open_file_or_dir(const char *fname) int open_file_or_dir(const char *fname, DIR **dirstream)
{ {
int ret; int ret;
struct stat st; struct stat st;
DIR *dirstream;
int fd; int fd;
ret = stat(fname, &st); ret = stat(fname, &st);
@ -1510,20 +1508,29 @@ int open_file_or_dir(const char *fname)
return -1; return -1;
} }
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
dirstream = opendir(fname); *dirstream = opendir(fname);
if (!dirstream) { if (!*dirstream)
return -2; return -2;
} fd = dirfd(*dirstream);
fd = dirfd(dirstream);
} else { } else {
fd = open(fname, O_RDWR); fd = open(fname, O_RDWR);
} }
if (fd < 0) { if (fd < 0) {
return -3; fd = -3;
if (*dirstream)
closedir(*dirstream);
} }
return fd; return fd;
} }
void close_file_or_dir(int fd, DIR *dirstream)
{
if (dirstream)
closedir(dirstream);
else if (fd >= 0)
close(fd);
}
int get_device_info(int fd, u64 devid, int get_device_info(int fd, u64 devid,
struct btrfs_ioctl_dev_info_args *di_args) struct btrfs_ioctl_dev_info_args *di_args)
{ {
@ -1556,6 +1563,7 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
struct btrfs_fs_devices *fs_devices_mnt = NULL; struct btrfs_fs_devices *fs_devices_mnt = NULL;
struct btrfs_ioctl_dev_info_args *di_args; struct btrfs_ioctl_dev_info_args *di_args;
char mp[BTRFS_PATH_NAME_MAX + 1]; char mp[BTRFS_PATH_NAME_MAX + 1];
DIR *dirstream = NULL;
memset(fi_args, 0, sizeof(*fi_args)); memset(fi_args, 0, sizeof(*fi_args));
@ -1586,7 +1594,7 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
} }
/* at this point path must not be for a block device */ /* at this point path must not be for a block device */
fd = open_file_or_dir(path); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
ret = -errno; ret = -errno;
goto out; goto out;
@ -1623,8 +1631,7 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
BUG_ON(ndevs == 0); BUG_ON(ndevs == 0);
ret = 0; ret = 0;
out: out:
if (fd != -1) close_file_or_dir(fd, dirstream);
close(fd);
return ret; return ret;
} }

View File

@ -21,6 +21,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "ctree.h" #include "ctree.h"
#include <dirent.h>
#define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024)
@ -56,7 +57,8 @@ void pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
int get_mountpt(char *dev, char *mntpt, size_t size); int get_mountpt(char *dev, char *mntpt, size_t size);
int btrfs_scan_block_devices(int run_ioctl); int btrfs_scan_block_devices(int run_ioctl);
u64 parse_size(char *s); u64 parse_size(char *s);
int open_file_or_dir(const char *fname); int open_file_or_dir(const char *fname, DIR **dirstream);
void close_file_or_dir(int fd, DIR *dirstream);
int get_device_info(int fd, u64 devid, int get_device_info(int fd, u64 devid,
struct btrfs_ioctl_dev_info_args *di_args); struct btrfs_ioctl_dev_info_args *di_args);
int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args, int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
@ -67,7 +69,7 @@ int set_label(const char *btrfs_dev, const char *label);
char *__strncpy__null(char *dest, const char *src, size_t n); char *__strncpy__null(char *dest, const char *src, size_t n);
int is_block_device(const char *file); int is_block_device(const char *file);
int get_btrfs_mount(const char *path, char *mp, size_t mp_size); int get_btrfs_mount(const char *path, char *mp, size_t mp_size);
int open_path_or_dev_mnt(const char *path); int open_path_or_dev_mnt(const char *path, DIR **dirstream);
int is_swap_device(const char *file); int is_swap_device(const char *file);
u64 btrfs_device_size(int fd, struct stat *st); u64 btrfs_device_size(int fd, struct stat *st);
/* Helper to always get proper size of the destination string */ /* Helper to always get proper size of the destination string */