Generalize unlink to OS-dep code in Utils, just a little prep for Windows port.

This commit is contained in:
Adam Ierymenko 2013-08-08 10:06:39 -04:00
parent 8a46452a70
commit 86056fdbd9
3 changed files with 33 additions and 6 deletions

View file

@ -111,8 +111,8 @@ Network::~Network()
if (_destroyOnDelete) { if (_destroyOnDelete) {
std::string confPath(_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + toString() + ".conf"); std::string confPath(_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + toString() + ".conf");
std::string mcdbPath(_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + toString() + ".mcerts"); std::string mcdbPath(_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + toString() + ".mcerts");
unlink(confPath.c_str()); Utils::rm(confPath);
unlink(mcdbPath.c_str()); Utils::rm(mcdbPath);
} else { } else {
// Causes flush of membership certs to disk // Causes flush of membership certs to disk
clean(); clean();
@ -204,7 +204,7 @@ void Network::clean()
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
if (_configuration.isOpen()) { if (_configuration.isOpen()) {
_membershipCertificates.clear(); _membershipCertificates.clear();
unlink(mcdbPath.c_str()); Utils::rm(mcdbPath);
} else { } else {
FILE *mcdb = fopen(mcdbPath.c_str(),"wb"); FILE *mcdb = fopen(mcdbPath.c_str(),"wb");
bool writeError = false; bool writeError = false;
@ -236,7 +236,7 @@ void Network::clean()
if (mcdb) if (mcdb)
fclose(mcdb); fclose(mcdb);
if (writeError) { if (writeError) {
unlink(mcdbPath.c_str()); Utils::rm(mcdbPath);
LOG("error: unable to write to membership cert database at: %s",mcdbPath.c_str()); LOG("error: unable to write to membership cert database at: %s",mcdbPath.c_str());
} }
} }

View file

@ -339,8 +339,8 @@ Node::ReasonForTermination Node::run()
Utils::lockDownFile(identitySecretPath.c_str(),false); Utils::lockDownFile(identitySecretPath.c_str(),false);
// Clean up some obsolete files if present -- this will be removed later // Clean up some obsolete files if present -- this will be removed later
unlink((_r->homePath + ZT_PATH_SEPARATOR_S + "status").c_str()); Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "status"));
unlink((_r->homePath + ZT_PATH_SEPARATOR_S + "thisdeviceismine").c_str()); Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "thisdeviceismine"));
// Make sure networks.d exists // Make sure networks.d exists
mkdir((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),0700); mkdir((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),0700);

View file

@ -43,6 +43,12 @@
#include "../ext/lz4/lz4.h" #include "../ext/lz4/lz4.h"
#include "../ext/lz4/lz4hc.h" #include "../ext/lz4/lz4hc.h"
#ifdef __WINDOWS__
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "Constants.hpp" #include "Constants.hpp"
/** /**
@ -58,6 +64,27 @@ namespace ZeroTier {
class Utils class Utils
{ {
public: public:
/**
* Delete a file
*
* @param path Path to delete
* @return True if delete was successful
*/
static inline bool rm(const char *path)
throw()
{
#ifdef __WINDOWS__
foo;
#else
return (unlink(path) == 0);
#endif
}
static inline bool rm(const std::string &path)
throw()
{
return rm(path.c_str());
}
/** /**
* List a directory's contents * List a directory's contents
* *