summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaulo Zanoni <paulo.r.zanoni@intel.com>2015-07-13 14:04:25 -0300
committerPaulo Zanoni <paulo.r.zanoni@intel.com>2015-08-05 17:30:58 -0300
commit995f2738adece4f7423f9ce9ac34ab0477844840 (patch)
tree8b3bb6bee7d4c6229324d049e15911913413b6e2 /lib
parentffd7321c701411c6ffb4aa5b983aacb92289908c (diff)
lib: add igt_debugfs_read()
A helpful function for when you want to read a whole debugfs file to a string and don't want to worry about opening and closing file descriptors and asserting buffer sizes. We've been using this already for kms_frontbuffer_tracking and kms_fbcon_fbt, so the only test with new code here is kms_fbc_crc. Also notice that for kms_fbc_crc we had to increase the buffer size since the file can sometimes be bigger than 64 bytes - depending on the reason why FBC is disabled. Of course, there are probably many other programs we can patch, but I'm not doing this now. v2: Add the macro to wrap sizeof() (Daniel). v3: Add documentation for the macro too (Daniel). Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/igt_debugfs.c27
-rw-r--r--lib/igt_debugfs.h12
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/igt_debugfs.c b/lib/igt_debugfs.c
index a2c6fe29..568154ac 100644
--- a/lib/igt_debugfs.c
+++ b/lib/igt_debugfs.c
@@ -185,6 +185,33 @@ FILE *igt_debugfs_fopen(const char *filename,
return fopen(buf, mode);
}
+/**
+ * __igt_debugfs_read:
+ * @filename: file name
+ * @buf: buffer where the contents will be stored, allocated by the caller
+ * @buf_size: size of the buffer
+ *
+ * This function opens the debugfs file, reads it, stores the content in the
+ * provided buffer, then closes the file. Users should make sure that the buffer
+ * provided is big enough to fit the whole file, plus one byte.
+ */
+void __igt_debugfs_read(const char *filename, char *buf, int buf_size)
+{
+ FILE *file;
+ size_t n_read;
+
+ file = igt_debugfs_fopen(filename, "r");
+ igt_assert(file);
+
+ n_read = fread(buf, 1, buf_size - 1, file);
+ igt_assert(n_read > 0);
+ igt_assert(feof(file));
+
+ buf[n_read] = '\0';
+
+ igt_assert(fclose(file) == 0);
+}
+
/*
* Pipe CRC
*/
diff --git a/lib/igt_debugfs.h b/lib/igt_debugfs.h
index ece71486..78cb5e1c 100644
--- a/lib/igt_debugfs.h
+++ b/lib/igt_debugfs.h
@@ -34,6 +34,18 @@ enum pipe;
int igt_debugfs_open(const char *filename, int mode);
FILE *igt_debugfs_fopen(const char *filename,
const char *mode);
+void __igt_debugfs_read(const char *filename, char *buf, int buf_size);
+
+/**
+ * igt_debugfs_read:
+ * @filename: name of the debugfs file
+ * @buf: buffer where the contents will be stored, allocated by the caller.
+ *
+ * This is just a convenience wrapper for __igt_debugfs_read. See its
+ * documentation.
+ */
+#define igt_debugfs_read(filename, buf) \
+ __igt_debugfs_read((filename), (buf), sizeof(buf))
/*
* Pipe CRC