aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/file.c111
-rw-r--r--src/core/file.h79
-rw-r--r--src/core/nng_impl.h1
-rw-r--r--src/core/platform.h45
4 files changed, 232 insertions, 4 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
diff --git a/src/core/file.h b/src/core/file.h
new file mode 100644
index 00000000..b45aae61
--- /dev/null
+++ b/src/core/file.h
@@ -0,0 +1,79 @@
+//
+// 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.
+//
+
+#ifndef CORE_FILE_H
+#define CORE_FILE_H
+
+// File/Store Support
+//
+// Some transports require a persistent storage for things like configs,
+// key material, etc. Generally, these are all going to be relatively
+// small objects (such as certificates), so we only require a synchronous
+// implementation from platforms. We provide a very limited and simple
+// file API for these purposes; basic CRUD operations only, plus a way
+// to iterate over names. These are adequate for NNG's internal uses;
+// applications should use normal platform-specific APIs or those in the
+// standard C library.
+
+// nni_file_put writes the named file, with the provided data,
+// and the given size. If the file already exists it is overwritten.
+// The permissions on the file will allow the application to read and
+// write the file, but may (should) restrict anything else beyond that
+// where they can. If the name contains platform specific directory
+// separators, then any missing parent directories will be created if
+// possible.
+extern int nni_file_put(const char *, const void *, size_t);
+
+// nni_plat_file_get reads the entire named file, allocating storage
+// to receive the data and returning the data and the size in the
+// reference arguments. The data pointer should be freed with nni_free
+// using the supplied size when no longer needed.
+extern int nni_file_get(const char *, void **, size_t *);
+
+// nni_file_delete deletes the named file.
+extern int nni_file_delete(const char *);
+
+enum nni_file_type_val {
+ NNI_FILE_TYPE_FILE,
+ NNI_FILE_TYPE_DIR,
+ NNI_FILE_TYPE_OTHER,
+};
+
+// nni_file_exists checks if the named file exists.
+extern int nni_file_type(const char *, int *);
+
+// nni_file_walk walks a list of files.
+enum nni_file_walk_result {
+ NNI_FILE_WALK_CONTINUE,
+ NNI_FILE_WALK_STOP,
+ NNI_FILE_WALK_PRUNE_SIB,
+ NNI_FILE_WALK_PRUNE_CHILD,
+};
+
+enum nni_file_walk_flags {
+ NNI_FILE_WALK_DEPTH_FIRST = 0, // get children first
+ NNI_FILE_WALK_BREADTH_FIRST = 1, // get siblings first (later)
+ NNI_FILE_WALK_SHALLOW = 2, // do not descend into subdirectories
+ NNI_FILE_WALK_FILES_ONLY = 4, // directory names are not reported
+};
+
+typedef int (*nni_file_walker)(const char *, void *);
+extern int nni_file_walk(const char *, nni_file_walker, void *, int);
+
+// nni_file_join joins two path components to make a path name.
+// For example. on UNIX systems nni_file_join("/tmp", "a") returns
+// "/tmp/a". The pathname returned should be freed with nni_strfree().
+extern char *nni_file_join(const char *, const char *);
+
+// nni_file_basename returns the "file" name, without the parent directory.
+// The returned value generally is within the supplied path name.
+extern const char *nni_file_basename(const char *);
+
+#endif // CORE_FILE_H
diff --git a/src/core/nng_impl.h b/src/core/nng_impl.h
index bee6ae41..ea8512bb 100644
--- a/src/core/nng_impl.h
+++ b/src/core/nng_impl.h
@@ -29,6 +29,7 @@
#include "core/aio.h"
#include "core/clock.h"
#include "core/device.h"
+#include "core/file.h"
#include "core/idhash.h"
#include "core/init.h"
#include "core/list.h"
diff --git a/src/core/platform.h b/src/core/platform.h
index 9e193175..73b6785b 100644
--- a/src/core/platform.h
+++ b/src/core/platform.h
@@ -1,6 +1,6 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
-// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+// 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
@@ -370,7 +370,7 @@ extern void nni_plat_pipe_close(int, int);
//
// Some transports require a persistent storage for things like
// key material, etc. Generally, these are all going to be relatively
-// small objects (such as certificates), so we ony require a synchronous
+// small objects (such as certificates), so we only require a synchronous
// implementation from platforms. This Key-Value API is intended to
// to support using the Key's as filenames, and keys will consist of
// only these characters: [0-9a-zA-Z._-]. The directory used should be
@@ -393,9 +393,41 @@ extern int nni_plat_file_put(const char *, const void *, size_t);
// using the supplied size when no longer needed.
extern int nni_plat_file_get(const char *, void **, size_t *);
-// nni_plat_file_delete deletes the named file.
+// nni_plat_file_delete deletes the named file. If the name refers to
+// a directory, then that will be removed only if empty.
extern int nni_plat_file_delete(const char *);
+// nni_plat_file_check checks the file path to determine its type.
+// If the path does not exist, then NNG_ENOENT is returned.
+enum nni_plat_file_type_val {
+ NNI_PLAT_FILE_TYPE_FILE, // normal file
+ NNI_PLAT_FILE_TYPE_DIR, // normal directory
+ NNI_PLAT_FILE_TYPE_OTHER, // something else (pipe, device node, etc.)
+};
+extern int nni_plat_file_type(const char *, int *);
+
+enum nni_plat_file_walk_result {
+ NNI_PLAT_FILE_WALK_CONTINUE,
+ NNI_PLAT_FILE_WALK_STOP, // stop walking (all done)
+ NNI_PLAT_FILE_WALK_PRUNE_SIB, // skip siblings and their children
+ NNI_PLAT_FILE_WALK_PRUNE_CHILD, // skip children
+};
+
+enum nni_plat_file_walk_flags {
+ NNI_PLAT_FILE_WALK_DEPTH_FIRST = 0, // get children first
+ NNI_PLAT_FILE_WALK_BREADTH_FIRST = 1, // get siblings first (later)
+ NNI_PLAT_FILE_WALK_SHALLOW = 2, // do not descend into subdirectories
+ NNI_PLAT_FILE_WALK_FILES_ONLY = 4, // directory names are not reported
+};
+
+// nni_plat_file_walker is called for each pathname found by walking a
+// directory tree. It returns one of the nni_plat_file_walk_result values.
+typedef int (*nni_plat_file_walker)(const char *, void *);
+
+// nni_plat_file_walk walks a directory tree, calling the walker function
+// with the path name, and the supplied void * argument.
+extern int nni_plat_file_walk(const char *, nni_plat_file_walker, void *, int);
+
// nni_plat_dir_open attempts to "open a directory" for listing. The
// handle for further operations is returned in the first argument, and
// the directory name is supplied in the second.
@@ -435,6 +467,11 @@ extern char *nni_plat_home_dir(void);
// "/tmp/a". The pathname returned should be freed with nni_strfree().
extern char *nni_plat_join_dir(const char *, const char *);
+// nni_plat_file_basename returns the "file" part of the file name.
+// The returned pointer will usually reference the end of the supplied
+// string, and may not be altered.
+extern const char *nni_plat_file_basename(const char *);
+
//
// Actual platforms we support. This is included up front so that we can
// get the specific types that are supplied by the platform.