summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremie Koenig <jk@jk.fr.eu.org>2010-08-17 10:10:22 +0000
committerJeremie Koenig <jk@jk.fr.eu.org>2010-08-30 14:14:48 +0200
commit0a4c7a1ca8bff458eb11322d2c94ec9ffd832524 (patch)
tree645506d2f687121d88e8012d7d21f68b6de872b0
parentd938e96e59a41d5eaa11040513815b757e58eb0c (diff)
Add a helper module for simple regular files
* procfs_file.h: New file, declares procfs_file_make_node. * procfs_file.c: New file, implements procfs_file_make_node. * main.c: Use them. * Makefile: Add the procfs_file module.
-rw-r--r--Makefile2
-rw-r--r--main.c13
-rw-r--r--procfs_file.c59
-rw-r--r--procfs_file.h6
4 files changed, 68 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index 4ae336a..e7282ce 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
TARGET = procfs
-OBJS = procfs.o netfs.o main.o
+OBJS = procfs.o netfs.o procfs_file.o main.o
LIBS = -lnetfs
CC = gcc
diff --git a/main.c b/main.c
index cafd0c9..457cf6a 100644
--- a/main.c
+++ b/main.c
@@ -3,14 +3,7 @@
#include <argp.h>
#include <hurd/netfs.h>
#include "procfs.h"
-
-static error_t get_contents (void *hook, void **contents, size_t *contents_len)
-{
- static const char hello[] = "Hello, World!\n";
- *contents = (void *) hello;
- *contents_len = sizeof hello - 1;
- return 0;
-}
+#include "procfs_file.h"
static error_t get_entries (void *hook, void **contents, size_t *contents_len)
{
@@ -22,12 +15,10 @@ static error_t get_entries (void *hook, void **contents, size_t *contents_len)
static error_t lookup (void *hook, const char *name, struct node **np)
{
- static const struct procfs_node_ops ops = { .get_contents = get_contents };
-
if (strcmp (name, "hello"))
return ENOENT;
- *np = procfs_make_node (&ops, NULL);
+ *np = procfs_file_make_node ("Hello, World!\n", -1, NULL);
if (! *np)
return ENOMEM;
diff --git a/procfs_file.c b/procfs_file.c
new file mode 100644
index 0000000..62419ee
--- /dev/null
+++ b/procfs_file.c
@@ -0,0 +1,59 @@
+#include <hurd/hurd_types.h>
+#include <stdlib.h>
+#include <string.h>
+#include "procfs.h"
+#include "procfs_file.h"
+
+struct procfs_file
+{
+ void *contents;
+ size_t len;
+ void (*cleanup)(void *contents);
+};
+
+error_t
+procfs_file_getcontents (void *hook, void **contents, size_t *contents_len)
+{
+ struct procfs_file *f = hook;
+
+ *contents = f->contents;
+ *contents_len = f->len;
+ return 0;
+}
+
+void
+procfs_file_cleanup (void *hook)
+{
+ struct procfs_file *f = hook;
+
+ if (f->cleanup)
+ f->cleanup (f->contents);
+
+ free (f);
+}
+
+struct node *
+procfs_file_make_node (void *contents, ssize_t len, void (*cleanup)(void *))
+{
+ static const struct procfs_node_ops ops = {
+ .get_contents = procfs_file_getcontents,
+ .cleanup = procfs_file_cleanup,
+ };
+ struct procfs_file *f;
+ struct node *np;
+
+ f = malloc (sizeof *f);
+ if (! f)
+ return NULL;
+
+ f->contents = contents;
+ f->len = (len >= 0) ? len : strlen (f->contents);
+ f->cleanup = cleanup;
+
+ np = procfs_make_node (&ops, f);
+ if (! np)
+ free (f);
+
+ return np;
+}
+
diff --git a/procfs_file.h b/procfs_file.h
new file mode 100644
index 0000000..b615db9
--- /dev/null
+++ b/procfs_file.h
@@ -0,0 +1,6 @@
+/* Create a new regular file with the given CONTENTS. If LEN is negative,
+ CONTENTS is considered as a string and the file stops at the first
+ nul char. If CLEANUP is non-NULL, it is passed CONTENTS when the
+ node is destroyed. */
+struct node *
+procfs_file_make_node (void *contents, ssize_t len, void (*cleanup)(void *));