[PATCH i-g-t 4/5] lib/igt_fs: Add helper functions to create and remove directories

Riana Tauro riana.tauro at intel.com
Tue Apr 22 09:56:00 UTC 2025


Add helper function to create directory and return fd of
the created directory. Also add a function to remove
directories. This will be used by configfs

Cc: Louis Chauvet <louis.chauvet at bootlin.com>
Cc: José Expósito <jose.exposito89 at gmail.com>
Signed-off-by: Riana Tauro <riana.tauro at intel.com>
---
 lib/igt_fs.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_fs.h |  2 ++
 2 files changed, 50 insertions(+)

diff --git a/lib/igt_fs.c b/lib/igt_fs.c
index f6a2530cd..1fa281315 100644
--- a/lib/igt_fs.c
+++ b/lib/igt_fs.c
@@ -21,8 +21,10 @@
  * IN THE SOFTWARE.
  *
  */
+#include <sys/stat.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -94,3 +96,49 @@ ssize_t igt_writen(int fd, const char *buf, size_t len)
 	} while (total != len);
 	return total ?: ret;
 }
+
+/**
+ * igt_fs_create_dir: creates and opens directory
+ * @fd: file descriptor of parent directory
+ * @name: name of the directory to create
+ * @mode: permissions for the directory
+ *
+ * creates a directory under parent directory and returns
+ * the fd
+ *
+ * Returns: directory fd on success, -errno otherwise
+ */
+int igt_fs_create_dir(int fd, const char *name, mode_t mode)
+{
+	int ret;
+	int dirfd;
+
+	ret = mkdirat(fd, name, mode);
+	if (ret)
+		return -errno;
+
+	dirfd = openat(fd, name, O_DIRECTORY);
+	if (dirfd < 0)
+		return -errno;
+
+	return dirfd;
+}
+
+/**
+ * igt_fs_remove_directory: removes directory
+ * @fd: fd of parent directory
+ * @name: name of directory to remove
+ *
+ * removes directory under parent directory
+ *
+ * Returns: 0 on success, -errno otherwise
+ */
+int igt_fs_remove_dir(int fd, const char *name)
+{
+	int ret = unlinkat(fd, name, AT_REMOVEDIR);
+
+	if (ret)
+		return -errno;
+
+	return 0;
+}
diff --git a/lib/igt_fs.h b/lib/igt_fs.h
index 84c3fed62..ee3e7593b 100644
--- a/lib/igt_fs.h
+++ b/lib/igt_fs.h
@@ -27,6 +27,8 @@
 
 #include <stdio.h>
 
+int igt_fs_create_dir(int fd, const char *name, mode_t mode);
+int igt_fs_remove_dir(int fd, const char *name);
 ssize_t igt_readn(int fd, char *buf, size_t len);
 ssize_t igt_writen(int fd, const char *buf, size_t len);
 
-- 
2.47.1



More information about the igt-dev mailing list