[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH v2 2/2] lib/vfscore: Add scandir



From: Vlad-Andrei BĂDOIU <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>

We adapt the scandir implementation for musl to work
with our vfscore implementation.

Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxx>
---
 lib/vfscore/exportsyms.uk |  1 +
 lib/vfscore/main.c        | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/lib/vfscore/exportsyms.uk b/lib/vfscore/exportsyms.uk
index e863a544..223bb72b 100644
--- a/lib/vfscore/exportsyms.uk
+++ b/lib/vfscore/exportsyms.uk
@@ -131,3 +131,4 @@ futimens
 utimes
 lutimes
 posix_fadvise
+scandir
diff --git a/lib/vfscore/main.c b/lib/vfscore/main.c
index 8c80ea61..846e2efd 100644
--- a/lib/vfscore/main.c
+++ b/lib/vfscore/main.c
@@ -36,6 +36,7 @@
 #include <sys/stat.h>
 #include <limits.h>
 #include <string.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <vfscore/prex.h>
@@ -708,6 +709,53 @@ int closedir(DIR *dir)
        return 0;
 }
 
+int scandir(const char *path, struct dirent ***res,
+       int (*sel)(const struct dirent *),
+       int (*cmp)(const struct dirent **, const struct dirent **))
+{
+       DIR *d = opendir(path);
+       struct dirent *de, **names=0, **tmp;
+       size_t cnt=0, len=0;
+       int old_errno = errno;
+
+       if (!d)
+               return -1;
+
+       while ((errno=0), (de = readdir(d))) {
+               if (sel && !sel(de))
+                       continue;
+               if (cnt >= len) {
+                       len = 2*len+1;
+                       if (len > SIZE_MAX/sizeof(*names))
+                               break;
+                       tmp = realloc(names, len * sizeof(*names));
+                       if (!tmp)
+                               break;
+                       names = tmp;
+               }
+               names[cnt] = malloc(de->d_reclen);
+               if (!names[cnt])
+                       break;
+               memcpy(names[cnt++], de, de->d_reclen);
+       }
+
+       closedir(d);
+
+       if (errno) {
+               if (names)
+                       while (cnt-->0)
+                               free(names[cnt]);
+               free(names);
+               return -1;
+       }
+       errno = old_errno;
+
+       if (cmp)
+               qsort(names, cnt, sizeof *names, (int (*)(const void *, const 
void *))cmp);
+       *res = names;
+       return cnt;
+}
+
 struct dirent *readdir(DIR *dir)
 {
        static __thread struct dirent entry, *result;
-- 
2.20.1


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.