commit 3c01aae015a3145a6fe3b1acec54dfce8f85ade6
parent 82f6a4a2048c18ec965e8b4d052fe20a38e64447
Author: Roberto Vargas <roberto.vargas@arm.com>
Date: Thu, 21 Feb 2019 22:01:00 +0000
[dev] Use posix directory interfaces
We were not using these interfaces because they required
static memory and it wasn't possible in the old world,
but it can be done in the new world.
Change-Id: I6756337b537acbe812e9380471b446b9a4b960b0
Diffstat:
9 files changed, 84 insertions(+), 60 deletions(-)
diff --git a/include/rcode/9p.h b/include/rcode/9p.h
@@ -31,8 +31,8 @@ typedef struct {
extern int dirtop9(Dir *dp, unsigned char *buf, int n);
extern int p9todir(Dir *dp, unsigned char *buf, int n);
-extern DIR *p9opendir(DIR *dir, const char *name);
-extern int p9readdir(DIR *dir, struct dirent *ent);
-extern int p9closedir(DIR *dir);
+extern DIR *opendir(const char *name);
+extern int readdir(DIR *dir, struct dirent *ent);
+extern int closedir(DIR *dir);
#endif
diff --git a/src/lib9p/Makefile b/src/lib9p/Makefile
@@ -8,9 +8,9 @@ OBJS = tobytes.o \
frombytes.o \
fromstring.o \
p9todir.o \
- p9opendir.o \
- p9readdir.o \
- p9closedir.o \
+ opendir.o \
+ readdir.o \
+ closedir.o \
TARGET = $(LIBDIR)/lib9p.a
diff --git a/src/lib9p/closedir.c b/src/lib9p/closedir.c
@@ -0,0 +1,14 @@
+#include <errno.h>
+#include <string.h>
+
+#include <rcode/9p.h>
+#include <rcode/rcode.h>
+
+int
+closedir(DIR *dir)
+{
+ int fd = dir->fd;
+
+ dir->fd = 0;
+ return close(fd);
+}
diff --git a/src/lib9p/opendir.c b/src/lib9p/opendir.c
@@ -0,0 +1,36 @@
+#include <errno.h>
+#include <string.h>
+
+#include <rcode/9p.h>
+#include <rcode/rcode.h>
+
+#define NR_DIRS 2
+
+
+/*
+ * This code assumes that open is not going to
+ * return 0 because it is the console fd.
+ */
+DIR *
+opendir(const char *name)
+{
+ int fd;
+ DIR *dir;
+ static DIR dirs[NR_DIRS];
+
+ for (dir = dirs; dir < &dirs[NR_DIRS] && dir->fd; ++dir)
+ ;
+
+ if (dir == &dirs[NR_DIRS]) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ if ((fd = open(name, O_READ)) < 0)
+ return NULL;
+ dir->fd = fd;
+
+ /* TODO: check that it is actually a directory */
+
+ return dir;
+}
diff --git a/src/lib9p/p9closedir.c b/src/lib9p/p9closedir.c
@@ -1,8 +0,0 @@
-#include <rcode/9p.h>
-#include <rcode/rcode.h>
-
-int
-p9closedir(DIR *dir)
-{
- return close(dir->fd);
-}
diff --git a/src/lib9p/p9opendir.c b/src/lib9p/p9opendir.c
@@ -1,18 +0,0 @@
-#include <errno.h>
-
-#include <rcode/9p.h>
-#include <rcode/rcode.h>
-
-DIR *
-p9opendir(DIR *dir, const char *name)
-{
- int fd;
-
- if ((fd = open(name, O_READ)) < 0)
- return NULL;
- dir->fd = fd;
-
- /* TODO: check that it is actually a directory */
-
- return dir;
-}
diff --git a/src/lib9p/p9readdir.c b/src/lib9p/p9readdir.c
@@ -1,24 +0,0 @@
-#include <errno.h>
-#include <string.h>
-
-#include <rcode/9p.h>
-#include <rcode/rcode.h>
-
-int
-p9readdir(DIR *dir, struct dirent *ent)
-{
- int n;
- Dir dentry;
-
- if ((n = read(dir->fd, dir->buf, sizeof(dir->buf))) <= 0)
- return n;
-
- if (p9todir(&dentry, dir->buf, n) < 0) {
- errno = EINVAL;
- return -1;
- }
-
- strcpy(ent->d_name, dentry.name);
-
- return 1;
-}
diff --git a/src/lib9p/readdir.c b/src/lib9p/readdir.c
@@ -0,0 +1,24 @@
+#include <errno.h>
+#include <string.h>
+
+#include <rcode/9p.h>
+#include <rcode/rcode.h>
+
+int
+readdir(DIR *dir, struct dirent *ent)
+{
+ int n;
+ Dir dentry;
+
+ if ((n = read(dir->fd, dir->buf, sizeof(dir->buf))) <= 0)
+ return n;
+
+ if (p9todir(&dentry, dir->buf, n) < 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ strcpy(ent->d_name, dentry.name);
+
+ return 1;
+}
diff --git a/src/romfw/dlang.c b/src/romfw/dlang.c
@@ -235,16 +235,16 @@ static int
do_ls(const struct cmd *cmd, struct args *args)
{
int n;
- DIR dir;
+ DIR *dir;
struct dirent d;
- if (!p9opendir(&dir, args->argv[1]))
+ if ((dir = opendir(args->argv[1])) == NULL)
goto err;
- while ((n = p9readdir(&dir, &d)) > 0)
+ while ((n = readdir(dir, &d)) > 0)
kprint(PREFIX "%s\n", d.d_name);
- if (p9closedir(&dir) < 0 || n < 0)
+ if (closedir(dir) < 0 || n < 0)
goto err;
return 0;