libbtrfsutil: add btrfs_util_[gs]et_read_only()

In the future, btrfs_util_[gs]et_subvolume_flags() might be useful, but
since these are the only subvolume flags we've defined in all this time,
this will do for now.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Omar Sandoval 2018-01-18 13:39:57 -08:00 committed by David Sterba
parent 0d36261bd5
commit 172c0d1a12
6 changed files with 186 additions and 0 deletions

View File

@ -279,6 +279,39 @@ enum btrfs_util_error btrfs_util_subvolume_info(const char *path, uint64_t id,
enum btrfs_util_error btrfs_util_subvolume_info_fd(int fd, uint64_t id,
struct btrfs_util_subvolume_info *subvol);
/**
* btrfs_util_get_subvolume_read_only() - Get whether a subvolume is read-only.
* @path: Subvolume path.
* @ret: Returned read-only flag.
*
* Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
*/
enum btrfs_util_error btrfs_util_get_subvolume_read_only(const char *path,
bool *ret);
/**
* btrfs_util_get_subvolume_read_only_fd() - See
* btrfs_util_get_subvolume_read_only().
*/
enum btrfs_util_error btrfs_util_get_subvolume_read_only_fd(int fd, bool *ret);
/**
* btrfs_util_set_subvolume_read_only() - Set whether a subvolume is read-only.
* @path: Subvolume path.
* @read_only: New value of read-only flag.
*
* Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
*/
enum btrfs_util_error btrfs_util_set_subvolume_read_only(const char *path,
bool read_only);
/**
* btrfs_util_set_subvolume_read_only_fd() - See
* btrfs_util_set_subvolume_read_only().
*/
enum btrfs_util_error btrfs_util_set_subvolume_read_only_fd(int fd,
bool read_only);
struct btrfs_util_qgroup_inherit;
/**

View File

@ -67,6 +67,8 @@ PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *subvolume_path(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *subvolume_info(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *get_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *set_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *create_subvolume(PyObject *self, PyObject *args, PyObject *kwds);
void add_module_constants(PyObject *m);

View File

@ -181,6 +181,19 @@ static PyMethodDef btrfsutil_methods[] = {
"path -- string, bytes, path-like object, or open file descriptor\n"
"id -- if not zero, instead of returning information about the\n"
"given path, return information about the subvolume with this ID"},
{"get_subvolume_read_only", (PyCFunction)get_subvolume_read_only,
METH_VARARGS | METH_KEYWORDS,
"get_subvolume_read_only(path) -> bool\n\n"
"Get whether a subvolume is read-only.\n\n"
"Arguments:\n"
"path -- string, bytes, path-like object, or open file descriptor"},
{"set_subvolume_read_only", (PyCFunction)set_subvolume_read_only,
METH_VARARGS | METH_KEYWORDS,
"set_subvolume_read_only(path, read_only=True)\n\n"
"Set whether a subvolume is read-only.\n\n"
"Arguments:\n"
"path -- string, bytes, path-like object, or open file descriptor\n"
"read_only -- bool flag value"},
{"create_subvolume", (PyCFunction)create_subvolume,
METH_VARARGS | METH_KEYWORDS,
"create_subvolume(path, async=False)\n\n"

View File

@ -215,6 +215,61 @@ PyStructSequence_Desc SubvolumeInfo_desc = {
PyTypeObject SubvolumeInfo_type;
PyObject *get_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
bool read_only;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&:get_subvolume_read_only",
keywords, &path_converter, &path))
return NULL;
if (path.path) {
err = btrfs_util_get_subvolume_read_only(path.path, &read_only);
} else {
err = btrfs_util_get_subvolume_read_only_fd(path.fd,
&read_only);
}
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
return PyBool_FromLong(read_only);
}
PyObject *set_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "read_only", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
int read_only = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&|p:set_subvolume_read_only",
keywords, &path_converter, &path,
&read_only))
return NULL;
if (path.path)
err = btrfs_util_set_subvolume_read_only(path.path, read_only);
else
err = btrfs_util_set_subvolume_read_only_fd(path.fd, read_only);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
Py_RETURN_NONE;
}
PyObject *create_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "async", "qgroup_inherit", NULL};

View File

@ -137,6 +137,23 @@ class TestSubvolume(BtrfsTestCase):
# BTRFS_EXTENT_TREE_OBJECTID
btrfsutil.subvolume_info(arg, 2)
def test_read_only(self):
for arg in self.path_or_fd(self.mountpoint):
with self.subTest(type=type(arg)):
btrfsutil.set_subvolume_read_only(arg)
self.assertTrue(btrfsutil.get_subvolume_read_only(arg))
self.assertTrue(btrfsutil.subvolume_info(arg).flags & 1)
btrfsutil.set_subvolume_read_only(arg, False)
self.assertFalse(btrfsutil.get_subvolume_read_only(arg))
self.assertFalse(btrfsutil.subvolume_info(arg).flags & 1)
btrfsutil.set_subvolume_read_only(arg, True)
self.assertTrue(btrfsutil.get_subvolume_read_only(arg))
self.assertTrue(btrfsutil.subvolume_info(arg).flags & 1)
btrfsutil.set_subvolume_read_only(arg, False)
def test_create_subvolume(self):
subvol = os.path.join(self.mountpoint, 'subvol')

View File

@ -399,6 +399,72 @@ PUBLIC enum btrfs_util_error btrfs_util_subvolume_info_fd(int fd, uint64_t id,
return BTRFS_UTIL_OK;
}
PUBLIC enum btrfs_util_error btrfs_util_get_subvolume_read_only_fd(int fd,
bool *read_only_ret)
{
uint64_t flags;
int ret;
ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
if (ret == -1)
return BTRFS_UTIL_ERROR_SUBVOL_GETFLAGS_FAILED;
*read_only_ret = flags & BTRFS_SUBVOL_RDONLY;
return BTRFS_UTIL_OK;
}
PUBLIC enum btrfs_util_error btrfs_util_get_subvolume_read_only(const char *path,
bool *ret)
{
enum btrfs_util_error err;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
return BTRFS_UTIL_ERROR_OPEN_FAILED;
err = btrfs_util_get_subvolume_read_only_fd(fd, ret);
SAVE_ERRNO_AND_CLOSE(fd);
return err;
}
PUBLIC enum btrfs_util_error btrfs_util_set_subvolume_read_only(const char *path,
bool read_only)
{
enum btrfs_util_error err;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
return BTRFS_UTIL_ERROR_OPEN_FAILED;
err = btrfs_util_set_subvolume_read_only_fd(fd, read_only);
SAVE_ERRNO_AND_CLOSE(fd);
return err;
}
PUBLIC enum btrfs_util_error btrfs_util_set_subvolume_read_only_fd(int fd,
bool read_only)
{
uint64_t flags;
int ret;
ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
if (ret == -1)
return BTRFS_UTIL_ERROR_SUBVOL_GETFLAGS_FAILED;
if (read_only)
flags |= BTRFS_SUBVOL_RDONLY;
else
flags &= ~BTRFS_SUBVOL_RDONLY;
ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
if (ret == -1)
return BTRFS_UTIL_ERROR_SUBVOL_SETFLAGS_FAILED;
return BTRFS_UTIL_OK;
}
static enum btrfs_util_error openat_parent_and_name(int dirfd, const char *path,
char *name, size_t name_len,
int *fd)