From 3dd4cbf8efcfd574e2244798a86edd2f10c9cb45 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 10 Jan 2018 16:14:54 -0800 Subject: 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.) --- src/core/file.h | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/core/file.h (limited to 'src/core/file.h') 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. +// Copyright 2018 Capitar IT Group BV +// +// 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 -- cgit v1.2.3-70-g09d2