btrfs-progs: add write helpers for sysfs files

Add convenience wrappers for writing a buffer or u64 to toplevel or FSID
file in sysfs.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-12-08 22:43:59 +01:00
parent b3b38751e9
commit 4576029dfd
2 changed files with 66 additions and 4 deletions

View File

@ -26,7 +26,7 @@
* Open a file in fsid directory in sysfs and return the file descriptor or * Open a file in fsid directory in sysfs and return the file descriptor or
* error * error
*/ */
int sysfs_open_fsid_file(int fd, const char *filename) static int sysfs_open_fsid_file_flags(int fd, const char *filename, int flags)
{ {
u8 fsid[BTRFS_UUID_SIZE]; u8 fsid[BTRFS_UUID_SIZE];
char fsid_str[BTRFS_UUID_UNPARSED_SIZE]; char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
@ -42,14 +42,24 @@ int sysfs_open_fsid_file(int fd, const char *filename)
if (ret < 0) if (ret < 0)
return ret; return ret;
return open(sysfs_file, O_RDONLY); return open(sysfs_file, flags);
}
int sysfs_open_fsid_file(int fd, const char *filename)
{
return sysfs_open_fsid_file_flags(fd, filename, O_RDONLY);
}
int sysfs_open_fsid_file_rw(int fd, const char *filename)
{
return sysfs_open_fsid_file_flags(fd, filename, O_RDWR);
} }
/* /*
* Open a file in the toplevel sysfs directory and return the file descriptor * Open a file in the toplevel sysfs directory and return the file descriptor
* or error. * or error.
*/ */
int sysfs_open_file(const char *name) static int sysfs_open_file_flags(const char *name, int flags)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
int ret; int ret;
@ -57,7 +67,17 @@ int sysfs_open_file(const char *name)
ret = path_cat_out(path, "/sys/fs/btrfs", name); ret = path_cat_out(path, "/sys/fs/btrfs", name);
if (ret < 0) if (ret < 0)
return ret; return ret;
return open(path, O_RDONLY); return open(path, flags);
}
int sysfs_open_file(const char *name)
{
return sysfs_open_file_flags(name, O_RDONLY);
}
int sysfs_open_file_rw(const char *name)
{
return sysfs_open_file_flags(name, O_RDWR);
} }
/* /*
@ -94,6 +114,12 @@ int sysfs_read_file(int fd, char *buf, size_t size)
return read(fd, buf, size); return read(fd, buf, size);
} }
int sysfs_write_file(int fd, const char *buf, size_t size)
{
lseek(fd, 0, SEEK_SET);
return write(fd, buf, size);
}
int sysfs_read_file_u64(const char *name, u64 *value) int sysfs_read_file_u64(const char *name, u64 *value)
{ {
int fd = -1; int fd = -1;
@ -115,6 +141,22 @@ out:
return ret; return ret;
} }
int sysfs_write_file_u64(const char *name, u64 value)
{
int fd;
int ret;
char str[32] = { 0 };
fd = sysfs_open_file_rw(name);
if (fd < 0)
return fd;
ret = snprintf(str, sizeof(str), "%llu", value);
ret = sysfs_write_file(fd, str, ret);
close(fd);
return ret;
}
int sysfs_read_fsid_file_u64(int fd, const char *name, u64 *value) int sysfs_read_fsid_file_u64(int fd, const char *name, u64 *value)
{ {
int ret; int ret;
@ -134,3 +176,18 @@ out:
close(fd); close(fd);
return ret; return ret;
} }
int sysfs_write_fsid_file_u64(int fd, const char *name, u64 value)
{
int ret;
char str[32] = { 0 };
fd = sysfs_open_fsid_file_rw(fd, name);
if (fd < 0)
return fd;
ret = snprintf(str, sizeof(str), "%llu", value);
ret = sysfs_write_file(fd, str, ret);
close(fd);
return ret;
}

View File

@ -20,10 +20,15 @@
#include <stddef.h> #include <stddef.h>
int sysfs_open_file(const char *name); int sysfs_open_file(const char *name);
int sysfs_open_file_rw(const char *name);
int sysfs_open_fsid_file(int fd, const char *filename); int sysfs_open_fsid_file(int fd, const char *filename);
int sysfs_open_fsid_file_rw(int fd, const char *filename);
int sysfs_open_fsid_dir(int fd, const char *dirname); int sysfs_open_fsid_dir(int fd, const char *dirname);
int sysfs_read_fsid_file_u64(int fd, const char *name, u64 *value); int sysfs_read_fsid_file_u64(int fd, const char *name, u64 *value);
int sysfs_write_fsid_file_u64(int fd, const char *name, u64 value);
int sysfs_read_file(int fd, char *buf, size_t size); int sysfs_read_file(int fd, char *buf, size_t size);
int sysfs_write_file(int fd, const char *buf, size_t size);
int sysfs_read_file_u64(const char *name, u64 *value); int sysfs_read_file_u64(const char *name, u64 *value);
int sysfs_write_file_u64(const char *name, u64 value);
#endif #endif