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/transport/zerotier/zerotier.c | 56 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'src/transport') diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c index f6594eb3..cef31a29 100644 --- a/src/transport/zerotier/zerotier.c +++ b/src/transport/zerotier/zerotier.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Garrett D'Amore -// Copyright 2017 Capitar IT Group BV +// 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 @@ -1212,17 +1212,14 @@ zt_state_put(ZT_Node *node, void *userptr, void *thr, return; } - if ((path = nni_plat_join_dir(ztn->zn_path, fname)) == NULL) { + if ((path = nni_file_join(ztn->zn_path, fname)) == NULL) { return; } if (len < 0) { - (void) nni_plat_file_delete(path); + (void) nni_file_delete(path); } else { - if (strlen(ztn->zn_path) > 0) { - (void) nni_plat_dir_create(ztn->zn_path); - } - (void) nni_plat_file_put(path, data, len); + (void) nni_file_put(path, data, len); } nni_strfree(path); } @@ -1260,11 +1257,11 @@ zt_state_get(ZT_Node *node, void *userptr, void *thr, return (len); } - if ((path = nni_plat_join_dir(ztn->zn_path, fname)) == NULL) { + if ((path = nni_file_join(ztn->zn_path, fname)) == NULL) { return (-1); } - if (nni_plat_file_get(path, &buf, &sz) != 0) { + if (nni_file_get(path, &buf, &sz) != 0) { nni_strfree(path); return (-1); } @@ -1475,9 +1472,6 @@ zt_node_create(zt_node **ztnp, const char *path) (nni_random() % (zt_max_port - zt_ephemeral)) + zt_ephemeral); nni_strlcpy(ztn->zn_path, path, sizeof(ztn->zn_path)); - if (strlen(ztn->zn_path) > 0) { - (void) nni_plat_dir_create(ztn->zn_path); - } zrv = ZT_Node_new(&ztn->zn_znode, ztn, NULL, &zt_callbacks, zt_now()); if (zrv != ZT_RESULT_OK) { zt_node_destroy(ztn); @@ -1512,6 +1506,24 @@ zt_node_create(zt_node **ztnp, const char *path) return (0); } +static int +zt_walk_moons(const char *path, void *arg) +{ + zt_node * ztn = arg; + const char *bn = nni_file_basename(path); + char * ep; + uint64_t moonid; + + if (strncmp(bn, "moon.", 5) != 0) { + return (NNI_FILE_WALK_CONTINUE); + } + moonid = strtoull(bn + 5, &ep, 16); + if (*ep == '\0') { + ZT_Node_orbit(ztn->zn_znode, NULL, moonid, 0); + } + return (NNI_FILE_WALK_CONTINUE); +} + static int zt_node_find(zt_ep *ep) { @@ -1519,7 +1531,6 @@ zt_node_find(zt_ep *ep) int rv; nng_sockaddr sa; ZT_VirtualNetworkConfig *cf; - void * dir; NNI_LIST_FOREACH (&zt_nodes, ztn) { if (strcmp(ep->ze_home, ztn->zn_path) == 0) { @@ -1534,20 +1545,9 @@ zt_node_find(zt_ep *ep) } // Load moons - if (nni_plat_dir_open(&dir, ep->ze_home) == 0) { - uint64_t moonid; - const char *fname; - char * ep; - while (nni_plat_dir_next(dir, &fname) == 0) { - if (strncmp(fname, "moon.", 5) != 0) { - continue; - } - moonid = strtoull(fname + 5, &ep, 16); - if (*ep == '\0') { - ZT_Node_orbit(ztn->zn_znode, NULL, moonid, 0); - } - } - nni_plat_dir_close(dir); + if (strlen(ep->ze_home) != 0) { + (void) nni_file_walk(ep->ze_home, zt_walk_moons, ztn, + NNI_FILE_WALK_FILES_ONLY | NNI_FILE_WALK_SHALLOW); } done: -- cgit v1.2.3-70-g09d2