btrfs-progs: replace open_file_or_dir3 with btrfs_open_fd2

For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace open_file_or_dir3() with btrfs_open_fd2() removing any reference
to the unused/useless dirstream variables.  btrfs_open_fd2() is needed
because sometime the callers need to set the RDONLY/RDWRITE mode, and to
avoid spurious messages.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Goffredo Baroncelli 2024-02-08 21:19:23 +01:00 committed by David Sterba
parent 900f21b4f1
commit 50770da81e
2 changed files with 5 additions and 8 deletions

View File

@ -1013,7 +1013,6 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
bool recursive = false;
int ret = 0;
int compress_type = BTRFS_COMPRESS_NONE;
DIR *dirstream;
/*
* Kernel 4.19+ supports defragmention of files open read-only,
@ -1151,8 +1150,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
struct stat st;
int defrag_err = 0;
dirstream = NULL;
fd = open_file_or_dir3(argv[i], &dirstream, defrag_open_mode);
fd = btrfs_open_fd2(argv[i], false, defrag_open_mode == O_RDWR, false);
if (fd < 0) {
error("cannot open %s: %m", argv[i]);
ret = -errno;
@ -1186,7 +1184,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
error(
"defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
defrag_global_errors++;
close_file_or_dir(fd, dirstream);
close(fd);
break;
}
if (ret) {
@ -1198,7 +1196,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
next:
if (ret)
defrag_global_errors++;
close_file_or_dir(fd, dirstream);
close(fd);
}
if (defrag_global_errors)

View File

@ -175,12 +175,11 @@ static int prop_compression(enum prop_object_type type,
int ret;
ssize_t sret;
int fd = -1;
DIR *dirstream = NULL;
char *buf = NULL;
char *xattr_name = NULL;
int open_flags = value ? O_RDWR : O_RDONLY;
fd = open_file_or_dir3(object, &dirstream, open_flags);
fd = btrfs_open_fd2(object, false, open_flags == O_RDWR, false);
if (fd == -1) {
ret = -errno;
error("failed to open %s: %m", object);
@ -232,7 +231,7 @@ out:
free(xattr_name);
free(buf);
if (fd >= 0)
close_file_or_dir(fd, dirstream);
close(fd);
return ret;
}