Btrfs-progs: allow compression property gets for read-only subvolumes
authorFilipe David Borba Manana <fdmanana@gmail.com>
Tue, 15 Apr 2014 19:47:27 +0000 (20:47 +0100)
committerDavid Sterba <dsterba@suse.cz>
Fri, 2 May 2014 15:03:21 +0000 (17:03 +0200)
Because the function open_file_or_dir() always opened the input file in
read/write mode (O_RDWR), we were not able to due a compression property
get against a file living in a read-only subvolume/snapshot.
Fix this by opening the file with O_RDONLY mode if we're doing a property
get.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
props.c
utils.c
utils.h

diff --git a/props.c b/props.c
index c09865b..04b9e5f 100644 (file)
--- a/props.c
+++ b/props.c
@@ -119,8 +119,9 @@ static int prop_compression(enum prop_object_type type,
        DIR *dirstream = NULL;
        char *buf = NULL;
        char *xattr_name = NULL;
+       int open_flags = value ? O_RDWR : O_RDONLY;
 
-       fd = open_file_or_dir(object, &dirstream);
+       fd = open_file_or_dir3(object, &dirstream, open_flags);
        if (fd == -1) {
                ret = -errno;
                fprintf(stderr, "ERROR: open %s failed. %s\n",
diff --git a/utils.c b/utils.c
index 187ad3b..44c0e4a 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1622,7 +1622,7 @@ u64 parse_size(char *s)
        return strtoull(s, NULL, 10) * mult;
 }
 
-int open_file_or_dir(const char *fname, DIR **dirstream)
+int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags)
 {
        int ret;
        struct stat st;
@@ -1638,7 +1638,7 @@ int open_file_or_dir(const char *fname, DIR **dirstream)
                        return -1;
                fd = dirfd(*dirstream);
        } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
-               fd = open(fname, O_RDWR);
+               fd = open(fname, open_flags);
        } else {
                /*
                 * we set this on purpose, in case the caller output
@@ -1655,6 +1655,11 @@ int open_file_or_dir(const char *fname, DIR **dirstream)
        return fd;
 }
 
+int open_file_or_dir(const char *fname, DIR **dirstream)
+{
+       return open_file_or_dir3(fname, dirstream, O_RDWR);
+}
+
 void close_file_or_dir(int fd, DIR *dirstream)
 {
        if (dirstream)
diff --git a/utils.h b/utils.h
index 3c62066..db8d63c 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -72,6 +72,7 @@ int btrfs_scan_block_devices(int run_ioctl);
 u64 parse_size(char *s);
 u64 arg_strtou64(const char *str);
 int open_file_or_dir(const char *fname, DIR **dirstream);
+int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags);
 void close_file_or_dir(int fd, DIR *dirstream);
 int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
                struct btrfs_ioctl_dev_info_args **di_ret);