File 0002-btrfs-progs-add-sysfs-file-reading-helpers.patch of Package btrfsprogs.24931
From 34f0594bcaafc1dcfe22bfae05f649a7366801cc Mon Sep 17 00:00:00 2001
From: Goldwyn Rodrigues <rgoldwyn@suse.com>
Date: Tue, 25 Aug 2020 10:03:36 -0500
Subject: [PATCH] btrfs-progs: add sysfs file reading helpers
Add helpers to open and read sysfs files from the per-fs directory.
Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
---
utils.c | 33 +++++++++++++++++++++++++++++++++
utils.h | 3 +++
2 files changed, 36 insertions(+)
--- a/utils.c
+++ b/utils.c
@@ -2598,3 +2598,36 @@
print_device_info(dev, "\t");
printf("\n");
}
+
+/*
+ * Open a file in fsid directory in sysfs and return the file descriptor or
+ * error
+ */
+int sysfs_open_fsid_file(int fd, const char *filename)
+{
+ u8 fsid[BTRFS_UUID_SIZE];
+ char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
+ char sysfs_file[PATH_MAX];
+ int ret;
+
+ ret = get_fsid_fd(fd, fsid);
+ if (ret < 0)
+ return ret;
+ uuid_unparse(fsid, fsid_str);
+
+ ret = path_cat3_out(sysfs_file, "/sys/fs/btrfs", fsid_str, filename);
+ if (ret < 0)
+ return ret;
+
+ return open(sysfs_file, O_RDONLY);
+}
+
+/*
+ * Read up to @size bytes to @buf from @fd
+ */
+int sysfs_read_file(int fd, char *buf, size_t size)
+{
+ lseek(fd, 0, SEEK_SET);
+ memset(buf, 0, size);
+ return read(fd, buf, size);
+}
--- a/utils.h
+++ b/utils.h
@@ -191,4 +191,7 @@
unsigned int rand_range(unsigned int upper);
void init_rand_seed(u64 seed);
+int sysfs_open_fsid_file(int fd, const char *filename);
+int sysfs_read_file(int fd, char *buf, size_t size);
+
#endif