Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbtrfsutil: add btrfs_util_get_label #736

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libbtrfsutil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ everywhere. The following checklist should help to make sure nothing is missing:
### API summary

* filesystem
* get_label
* sync
* wait for sync
* subvolume
Expand Down
19 changes: 19 additions & 0 deletions libbtrfsutil/btrfsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum btrfs_util_error {
BTRFS_UTIL_ERROR_GET_SUBVOL_ROOTREF_FAILED,
BTRFS_UTIL_ERROR_INO_LOOKUP_USER_FAILED,
BTRFS_UTIL_ERROR_FS_INFO_FAILED,
BTRFS_UTIL_ERROR_FS_GET_LABEL_FAILED,
};

/**
Expand All @@ -78,6 +79,24 @@ enum btrfs_util_error {
*/
const char *btrfs_util_strerror(enum btrfs_util_error err);

/**
* btrfs_util_get_label() - Get the filesystem label for a
* filesystem.
* @path: Path on a Btrfs filesystem.
* @label: Returned filesytem label.
*
* Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
*/
enum btrfs_util_error btrfs_util_get_label(const char *path,
char **label);

/**
* btrfs_util_get_label_fd() - See
* btrfs_util_get_label().
*/
enum btrfs_util_error btrfs_util_get_label_fd(int fd,
char **label);

/**
* btrfs_util_sync() - Force a sync on a specific Btrfs filesystem.
* @path: Path on a Btrfs filesystem.
Expand Down
2 changes: 2 additions & 0 deletions libbtrfsutil/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static const char * const error_messages[] = {
"Could not resolve subvolume path with BTRFS_IOC_INO_LOOKUP_USER",
[BTRFS_UTIL_ERROR_FS_INFO_FAILED] =
"Could not get filesystem information",
[BTRFS_UTIL_ERROR_FS_GET_LABEL_FAILED] =
"Could not get filesystem label",
};

PUBLIC const char *btrfs_util_strerror(enum btrfs_util_error err)
Expand Down
41 changes: 41 additions & 0 deletions libbtrfsutil/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,52 @@

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include "btrfsutil_internal.h"


PUBLIC enum btrfs_util_error btrfs_util_get_label(const char *path,
char **label_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_label_fd(fd, label_ret);
if (err)
return err;

return BTRFS_UTIL_OK;
}

PUBLIC enum btrfs_util_error btrfs_util_get_label_fd(int fd,
char **label_ret)
{
int ret;
size_t len;

char label[BTRFS_PATH_NAME_MAX];
ret = ioctl(fd, BTRFS_IOC_GET_FSLABEL, label);
if (ret == -1)
return BTRFS_UTIL_ERROR_SUBVOL_SETFLAGS_FAILED;

len = strlen(label);
*label_ret = malloc(len + 1);
if (!*label_ret)
return BTRFS_UTIL_ERROR_NO_MEMORY;

memcpy(*label_ret, label, len);
label_ret[len] = '\0';

return BTRFS_UTIL_OK;
}

PUBLIC enum btrfs_util_error btrfs_util_sync(const char *path)
{
enum btrfs_util_error err;
Expand Down
2 changes: 2 additions & 0 deletions libbtrfsutil/libbtrfsutil.sym
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ global:
btrfs_util_destroy_subvolume_iterator;
btrfs_util_get_default_subvolume;
btrfs_util_get_default_subvolume_fd;
btrfs_util_get_label;
btrfs_util_get_label_fd;
btrfs_util_get_subvolume_read_only;
btrfs_util_get_subvolume_read_only_fd;
btrfs_util_is_subvolume;
Expand Down
1 change: 1 addition & 0 deletions libbtrfsutil/python/btrfsutilpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void SetFromBtrfsUtilErrorWithPaths(enum btrfs_util_error err,
struct path_arg *path1,
struct path_arg *path2);

PyObject *filesystem_get_label(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *filesystem_sync(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *start_sync(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *wait_sync(PyObject *self, PyObject *args, PyObject *kwds);
Expand Down
25 changes: 25 additions & 0 deletions libbtrfsutil/python/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@

#include "btrfsutilpy.h"

PyObject *filesystem_get_label(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
char *label;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:get_label", keywords,
&path_converter, &path))
return NULL;

if (path.path)
err = btrfs_util_get_label(path.path, &label);
else
err = btrfs_util_get_label_fd(path.fd, &label);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}

path_cleanup(&path);
return PyUnicode_FromString(label);
}

PyObject *filesystem_sync(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
Expand Down
6 changes: 6 additions & 0 deletions libbtrfsutil/python/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ void path_cleanup(struct path_arg *path)
}

static PyMethodDef btrfsutil_methods[] = {
{"get_label", (PyCFunction)filesystem_get_label,
METH_VARARGS | METH_KEYWORDS,
"get_label(path)\n\n"
"Get the filesystem label.\n\n"
"Arguments:\n"
"path -- string, bytes, path-like object, or open file descriptor"},
{"sync", (PyCFunction)filesystem_sync,
METH_VARARGS | METH_KEYWORDS,
"sync(path)\n\n"
Expand Down
4 changes: 4 additions & 0 deletions libbtrfsutil/python/tests/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ def test_wait_sync(self):
new_generation = self.super_generation()
self.assertGreater(new_generation, old_generation)
old_generation = new_generation

def test_get_label(self):
label = btrfsutil.get_label(self.mountpoint)
self.assertIsNotNone(label)