aboutsummaryrefslogtreecommitdiff
path: root/src/transport
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-10-06 18:25:56 -0700
committerGarrett D'Amore <garrett@damore.org>2017-10-09 17:12:31 -0700
commitf14cdb705cc11f39a4702c28f22007ffaaf7c9e1 (patch)
tree8ca8b7b08e0ffc14a6c9283483bf1170fee31cd0 /src/transport
parent52d37858451ad23f077294fc78b1a3f56255c32f (diff)
downloadnng-f14cdb705cc11f39a4702c28f22007ffaaf7c9e1.tar.gz
nng-f14cdb705cc11f39a4702c28f22007ffaaf7c9e1.tar.bz2
nng-f14cdb705cc11f39a4702c28f22007ffaaf7c9e1.zip
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.
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/zerotier/zerotier.c53
1 files changed, 9 insertions, 44 deletions
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 <unistd.h>
-#endif
-
#include <ZeroTierOne.h>
// 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 {