diff options
author | Omar Sandoval <osandov@fb.com> | 2018-01-18 12:49:55 -0800 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2018-03-06 11:28:36 +0100 |
commit | 92d4035074dd5234f1c3fd263947dde1f9bb288e (patch) | |
tree | d54816a235d76725e36e5efdebc67752ed51b25b /libbtrfsutil/python/subvolume.c | |
parent | 1b2775bdb068a1faee631a81aab72ba5aeda7d4f (diff) |
libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id()
These are the most trivial helpers in the library and will be used to
implement several of the more involved functions.
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'libbtrfsutil/python/subvolume.c')
-rw-r--r-- | libbtrfsutil/python/subvolume.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/libbtrfsutil/python/subvolume.c b/libbtrfsutil/python/subvolume.c new file mode 100644 index 00000000..4aab06a5 --- /dev/null +++ b/libbtrfsutil/python/subvolume.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018 Facebook + * + * This file is part of libbtrfsutil. + * + * libbtrfsutil is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libbtrfsutil is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "btrfsutilpy.h" + +PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *keywords[] = {"path", NULL}; + struct path_arg path = {.allow_fd = true}; + enum btrfs_util_error err; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:is_subvolume", + keywords, &path_converter, &path)) + return NULL; + + if (path.path) + err = btrfs_util_is_subvolume(path.path); + else + err = btrfs_util_is_subvolume_fd(path.fd); + if (err == BTRFS_UTIL_OK) { + path_cleanup(&path); + Py_RETURN_TRUE; + } else if (err == BTRFS_UTIL_ERROR_NOT_BTRFS || + err == BTRFS_UTIL_ERROR_NOT_SUBVOLUME) { + path_cleanup(&path); + Py_RETURN_FALSE; + } else { + SetFromBtrfsUtilErrorWithPath(err, &path); + path_cleanup(&path); + return NULL; + } +} + +PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *keywords[] = {"path", NULL}; + struct path_arg path = {.allow_fd = true}; + enum btrfs_util_error err; + uint64_t id; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:subvolume_id", + keywords, &path_converter, &path)) + return NULL; + + if (path.path) + err = btrfs_util_subvolume_id(path.path, &id); + else + err = btrfs_util_subvolume_id_fd(path.fd, &id); + if (err) { + SetFromBtrfsUtilErrorWithPath(err, &path); + path_cleanup(&path); + return NULL; + } + + path_cleanup(&path); + return PyLong_FromUnsignedLongLong(id); +} |