From f14cdb705cc11f39a4702c28f22007ffaaf7c9e1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 6 Oct 2017 18:25:56 -0700 Subject: New platform API for storage methods. This includes converting the ZeroTier transport to use these. The new API supports file creation, retrieval, and deletion. It also supports directory methods for traversal, creation, and deletion. It also has a few methods to obtain well-known directories like $TMPDIR and $HOME. A rich test suite for this functionality is added as well. --- src/transport/zerotier/zerotier.c | 53 +++++++-------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) (limited to 'src/transport') diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index 56acce03..28b253d0 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -17,10 +17,6 @@ #include "core/nng_impl.h" #include "zerotier.h" -#ifndef _WIN32 -#include -#endif - #include // These values are supplied to help folks checking status. They are the @@ -1187,7 +1183,6 @@ zt_state_put(ZT_Node *node, void *userptr, void *thr, enum ZT_StateObjectType objtype, const uint64_t objid[2], const void *data, int len) { - FILE * file; zt_node * ztn = userptr; char path[NNG_MAXADDRLEN + 1]; const char *fname; @@ -1224,27 +1219,12 @@ zt_state_put(ZT_Node *node, void *userptr, void *thr, return; } - // We assume that everyone can do standard C I/O. - // This may be a bad assumption. If that's the case, - // the platform should supply an alternative - // implementation. We are also assuming that we don't - // need to worry about atomic updates. As these items - // (keys, etc.) pretty much don't change, this should - // be fine. - if (len < 0) { - (void) unlink(path); - return; - } - - if ((file = fopen(path, "wb")) == NULL) { + nni_plat_file_delete(path); return; } - if (fwrite(data, 1, len, file) != len) { - (void) unlink(path); - } - (void) fclose(file); + (void) nni_plat_file_put(path, data, len); } static int @@ -1252,12 +1232,11 @@ zt_state_get(ZT_Node *node, void *userptr, void *thr, enum ZT_StateObjectType objtype, const uint64_t objid[2], void *data, unsigned int len) { - FILE * file; zt_node * ztn = userptr; char path[NNG_MAXADDRLEN + 1]; const char *fname; - int nread; size_t sz; + void * buf; NNI_ARG_UNUSED(objid); // we only use global files @@ -1285,30 +1264,16 @@ zt_state_get(ZT_Node *node, void *userptr, void *thr, return (-1); } - // We assume that everyone can do standard C I/O. - // This may be a bad assumption. If that's the case, - // the platform should supply an alternative - // implementation. We are also assuming that we don't - // need to worry about atomic updates. As these items - // (keys, etc.) pretty much don't change, this should - // be fine. - - if ((file = fopen(path, "rb")) == NULL) { + if (nni_plat_file_get(path, &buf, &sz) != 0) { return (-1); } - - // seek to end of file - (void) fseek(file, 0, SEEK_END); - if (ftell(file) > len) { - fclose(file); + if (sz > len) { + nni_free(buf, sz); return (-1); } - (void) fseek(file, 0, SEEK_SET); - - nread = (int) fread(data, 1, len, file); - (void) fclose(file); - - return (nread); + memcpy(data, buf, sz); + nni_free(buf, sz); + return ((int) sz); } typedef struct zt_send_hdr { -- cgit v1.2.3-70-g09d2