aboutsummaryrefslogtreecommitdiff
path: root/src/core/file.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-01-10 16:14:54 -0800
committerGarrett D'Amore <garrett@damore.org>2018-01-11 06:42:19 -0800
commit3dd4cbf8efcfd574e2244798a86edd2f10c9cb45 (patch)
treea5087862282f1ed950cf3daf3c3e45078b9e4216 /src/core/file.c
parent282da09430fc39d8f93d78b828d3653e95318255 (diff)
downloadnng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.tar.gz
nng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.tar.bz2
nng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.zip
Refactored file API.
This refactor of the file API provides a simpler and easier to use interface for our needs (and simpler to implement) in both the ZeroTier transport and the HTTP/TLS file accesses. It also removes some restrictions present on the old one, although it is still not suitable for working with large files. (It will work, just be very inefficient as the entire file must be loaded into memory.)
Diffstat (limited to 'src/core/file.c')
-rw-r--r--src/core/file.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/core/file.c b/src/core/file.c
new file mode 100644
index 00000000..d7000a6a
--- /dev/null
+++ b/src/core/file.c
@@ -0,0 +1,111 @@
+//
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#include "core/nng_impl.h"
+
+int
+nni_file_put(const char *name, const void *data, size_t sz)
+{
+ return (nni_plat_file_put(name, data, sz));
+}
+
+int
+nni_file_get(const char *name, void **datap, size_t *szp)
+{
+ return (nni_plat_file_get(name, datap, szp));
+}
+
+int
+nni_file_delete(const char *name)
+{
+ return (nni_plat_file_delete(name));
+}
+
+struct walkdata {
+ nni_file_walker fn;
+ void * arg;
+};
+
+static int
+plat_walker(const char *name, void *arg)
+{
+ struct walkdata *w = arg;
+ int rv;
+
+ rv = w->fn(name, w->arg);
+ switch (rv) {
+ case NNI_FILE_WALK_CONTINUE:
+ return (NNI_PLAT_FILE_WALK_CONTINUE);
+ case NNI_FILE_WALK_STOP:
+ return (NNI_PLAT_FILE_WALK_STOP);
+ case NNI_FILE_WALK_PRUNE_CHILD:
+ return (NNI_PLAT_FILE_WALK_PRUNE_CHILD);
+ case NNI_FILE_WALK_PRUNE_SIB:
+ return (NNI_PLAT_FILE_WALK_PRUNE_SIB);
+ }
+ // We treat any other value as a stop condition. The program
+ // is returning something invalid.
+ return (NNI_PLAT_FILE_WALK_STOP);
+}
+
+int
+nni_file_walk(const char *name, nni_file_walker walker, void *arg, int flags)
+{
+ struct walkdata w;
+ int wflags = 0;
+
+ w.fn = walker;
+ w.arg = arg;
+
+ if (flags & NNI_FILE_WALK_FILES_ONLY) {
+ wflags |= NNI_PLAT_FILE_WALK_FILES_ONLY;
+ }
+ if (flags & NNI_FILE_WALK_SHALLOW) {
+ wflags |= NNI_PLAT_FILE_WALK_SHALLOW;
+ }
+
+ return (nni_plat_file_walk(name, plat_walker, &w, wflags));
+}
+
+int
+nni_file_type(const char *name, int *ftype)
+{
+ int rv;
+ int t;
+
+ if ((rv = nni_plat_file_type(name, &t)) != 0) {
+ return (rv);
+ }
+
+ switch (t) {
+ case NNI_PLAT_FILE_TYPE_FILE:
+ *ftype = NNI_FILE_TYPE_FILE;
+ break;
+ case NNI_PLAT_FILE_TYPE_DIR:
+ *ftype = NNI_FILE_TYPE_DIR;
+ break;
+ default:
+ *ftype = NNI_FILE_TYPE_OTHER;
+ break;
+ }
+ return (0);
+}
+
+char *
+nni_file_join(const char *dir, const char *file)
+{
+ return (nni_plat_join_dir(dir, file));
+}
+
+const char *
+nni_file_basename(const char *path)
+{
+ return (nni_plat_file_basename(path));
+} \ No newline at end of file