mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-07-06 04:51:46 -07:00
clang-format
This commit is contained in:
parent
d45f280cb7
commit
ba2a4a605c
140 changed files with 19214 additions and 17403 deletions
|
@ -14,29 +14,28 @@
|
|||
#ifndef ZT_OSUTILS_HPP
|
||||
#define ZT_OSUTILS_HPP
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/InetAddress.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <shlwapi.h>
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#ifdef __LINUX__
|
||||
#include <sys/syscall.h>
|
||||
#endif
|
||||
|
@ -51,9 +50,8 @@ namespace ZeroTier {
|
|||
/**
|
||||
* Miscellaneous utility functions and global constants
|
||||
*/
|
||||
class OSUtils
|
||||
{
|
||||
public:
|
||||
class OSUtils {
|
||||
public:
|
||||
/**
|
||||
* Variant of snprintf that is portable and throws an exception
|
||||
*
|
||||
|
@ -66,11 +64,11 @@ public:
|
|||
* @param ... Format arguments
|
||||
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
||||
*/
|
||||
static unsigned int ztsnprintf(char *buf,unsigned int len,const char *fmt,...);
|
||||
static unsigned int ztsnprintf(char* buf, unsigned int len, const char* fmt, ...);
|
||||
|
||||
/**
|
||||
* Converts a uint64_t network ID into a string
|
||||
*
|
||||
*
|
||||
* @param nwid network ID
|
||||
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
||||
*/
|
||||
|
@ -78,7 +76,7 @@ public:
|
|||
|
||||
/**
|
||||
* Converts a uint64_t node ID into a string
|
||||
*
|
||||
*
|
||||
* @param nid node ID
|
||||
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
||||
*/
|
||||
|
@ -95,9 +93,8 @@ public:
|
|||
* @param stderrPath Path to file to use for stderr, or NULL for same as stdout (default)
|
||||
* @return True on success
|
||||
*/
|
||||
static bool redirectUnixOutputs(const char *stdoutPath,const char *stderrPath = (const char *)0)
|
||||
throw();
|
||||
#endif // __UNIX_LIKE__
|
||||
static bool redirectUnixOutputs(const char* stdoutPath, const char* stderrPath = (const char*)0) throw();
|
||||
#endif // __UNIX_LIKE__
|
||||
|
||||
/**
|
||||
* Delete a file
|
||||
|
@ -105,7 +102,7 @@ public:
|
|||
* @param path Path to delete
|
||||
* @return True if delete was successful
|
||||
*/
|
||||
static inline bool rm(const char *path)
|
||||
static inline bool rm(const char* path)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return (DeleteFileA(path) != FALSE);
|
||||
|
@ -113,29 +110,35 @@ public:
|
|||
return (unlink(path) == 0);
|
||||
#endif
|
||||
}
|
||||
static inline bool rm(const std::string &path) { return rm(path.c_str()); }
|
||||
static inline bool rm(const std::string& path)
|
||||
{
|
||||
return rm(path.c_str());
|
||||
}
|
||||
|
||||
static inline bool mkdir(const char *path)
|
||||
static inline bool mkdir(const char* path)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
if (::PathIsDirectoryA(path))
|
||||
return true;
|
||||
return (::CreateDirectoryA(path,NULL) == TRUE);
|
||||
return (::CreateDirectoryA(path, NULL) == TRUE);
|
||||
#else
|
||||
if (::mkdir(path,0755) != 0)
|
||||
if (::mkdir(path, 0755) != 0)
|
||||
return (errno == EEXIST);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
static inline bool mkdir(const std::string &path) { return OSUtils::mkdir(path.c_str()); }
|
||||
static inline bool mkdir(const std::string& path)
|
||||
{
|
||||
return OSUtils::mkdir(path.c_str());
|
||||
}
|
||||
|
||||
static inline bool rename(const char *o,const char *n)
|
||||
static inline bool rename(const char* o, const char* n)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
DeleteFileA(n);
|
||||
return (::rename(o,n) == 0);
|
||||
return (::rename(o, n) == 0);
|
||||
#else
|
||||
return (::rename(o,n) == 0);
|
||||
return (::rename(o, n) == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -146,7 +149,7 @@ public:
|
|||
* @param includeDirectories If true, include directories as well as files
|
||||
* @return Names of files in directory (without path prepended)
|
||||
*/
|
||||
static std::vector<std::string> listDirectory(const char *path,bool includeDirectories = false);
|
||||
static std::vector<std::string> listDirectory(const char* path, bool includeDirectories = false);
|
||||
|
||||
/**
|
||||
* Clean a directory of files whose last modified time is older than this
|
||||
|
@ -156,7 +159,7 @@ public:
|
|||
* @param olderThan Last modified older than timestamp (ms since epoch)
|
||||
* @return Number of cleaned files or negative on fatal error
|
||||
*/
|
||||
static long cleanDirectory(const char *path,const int64_t olderThan);
|
||||
static long cleanDirectory(const char* path, const int64_t olderThan);
|
||||
|
||||
/**
|
||||
* Delete a directory and all its files and subdirectories recursively
|
||||
|
@ -164,7 +167,7 @@ public:
|
|||
* @param path Path to delete
|
||||
* @return True on success
|
||||
*/
|
||||
static bool rmDashRf(const char *path);
|
||||
static bool rmDashRf(const char* path);
|
||||
|
||||
/**
|
||||
* Set modes on a file to something secure
|
||||
|
@ -175,7 +178,7 @@ public:
|
|||
* @param path Path to lock
|
||||
* @param isDir True if this is a directory
|
||||
*/
|
||||
static void lockDownFile(const char *path,bool isDir);
|
||||
static void lockDownFile(const char* path, bool isDir);
|
||||
|
||||
/**
|
||||
* Get file last modification time
|
||||
|
@ -186,20 +189,20 @@ public:
|
|||
* @param path Path to file to get time
|
||||
* @return Last modification time in ms since epoch or 0 if not found
|
||||
*/
|
||||
static uint64_t getLastModified(const char *path);
|
||||
static uint64_t getLastModified(const char* path);
|
||||
|
||||
/**
|
||||
* @param path Path to check
|
||||
* @param followLinks Follow links (on platforms with that concept)
|
||||
* @return True if file or directory exists at path location
|
||||
*/
|
||||
static bool fileExists(const char *path,bool followLinks = true);
|
||||
static bool fileExists(const char* path, bool followLinks = true);
|
||||
|
||||
/**
|
||||
* @param path Path to file
|
||||
* @return File size or -1 if nonexistent or other failure
|
||||
*/
|
||||
static int64_t getFileSize(const char *path);
|
||||
static int64_t getFileSize(const char* path);
|
||||
|
||||
/**
|
||||
* Get IP (v4 and/or v6) addresses for a given host
|
||||
|
@ -209,7 +212,7 @@ public:
|
|||
* @param name Host name
|
||||
* @return IP addresses in InetAddress sort order or empty vector if not found
|
||||
*/
|
||||
static std::vector<InetAddress> resolve(const char *name);
|
||||
static std::vector<InetAddress> resolve(const char* name);
|
||||
|
||||
/**
|
||||
* @return Current time in milliseconds since epoch
|
||||
|
@ -221,14 +224,14 @@ public:
|
|||
SYSTEMTIME st;
|
||||
ULARGE_INTEGER tmp;
|
||||
GetSystemTime(&st);
|
||||
SystemTimeToFileTime(&st,&ft);
|
||||
SystemTimeToFileTime(&st, &ft);
|
||||
tmp.LowPart = ft.dwLowDateTime;
|
||||
tmp.HighPart = ft.dwHighDateTime;
|
||||
return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds );
|
||||
return (int64_t)(((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds);
|
||||
#else
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,(struct timezone *)0);
|
||||
return ( (1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000) );
|
||||
gettimeofday(&tv, (struct timezone*)0);
|
||||
return ((1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000));
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -242,7 +245,7 @@ public:
|
|||
* @param buf Buffer to fill
|
||||
* @return True if open and read successful
|
||||
*/
|
||||
static bool readFile(const char *path,std::string &buf);
|
||||
static bool readFile(const char* path, std::string& buf);
|
||||
|
||||
/**
|
||||
* Write a block of data to disk, replacing any current file contents
|
||||
|
@ -252,7 +255,7 @@ public:
|
|||
* @param len Length of buffer
|
||||
* @return True if entire file was successfully written
|
||||
*/
|
||||
static bool writeFile(const char *path,const void *buf,unsigned int len);
|
||||
static bool writeFile(const char* path, const void* buf, unsigned int len);
|
||||
|
||||
/**
|
||||
* Split a string by delimiter, with optional escape and quote characters
|
||||
|
@ -263,7 +266,7 @@ public:
|
|||
* @param quot Zero or more quote characters
|
||||
* @return Vector of tokens
|
||||
*/
|
||||
static std::vector<std::string> split(const char *s,const char *const sep,const char *esc,const char *quot);
|
||||
static std::vector<std::string> split(const char* s, const char* const sep, const char* esc, const char* quot);
|
||||
|
||||
/**
|
||||
* Write a block of data to disk, replacing any current file contents
|
||||
|
@ -272,13 +275,19 @@ public:
|
|||
* @param s Data to write
|
||||
* @return True if entire file was successfully written
|
||||
*/
|
||||
static inline bool writeFile(const char *path,const std::string &s) { return writeFile(path,s.data(),(unsigned int)s.length()); }
|
||||
static inline bool writeFile(const char* path, const std::string& s)
|
||||
{
|
||||
return writeFile(path, s.data(), (unsigned int)s.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param c ASCII character to convert
|
||||
* @return Lower case ASCII character or unchanged if not a letter
|
||||
*/
|
||||
static inline char toLower(char c) throw() { return (char)OSUtils::TOLOWER_TABLE[(unsigned long)c]; }
|
||||
static inline char toLower(char c) throw()
|
||||
{
|
||||
return (char)OSUtils::TOLOWER_TABLE[(unsigned long)c];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Platform default ZeroTier One home path
|
||||
|
@ -286,20 +295,20 @@ public:
|
|||
static std::string platformDefaultHomePath();
|
||||
|
||||
#ifndef OMIT_JSON_SUPPORT
|
||||
static nlohmann::json jsonParse(const std::string &buf);
|
||||
static std::string jsonDump(const nlohmann::json &j,int indentation = 1);
|
||||
static uint64_t jsonInt(const nlohmann::json &jv,const uint64_t dfl);
|
||||
static double jsonDouble(const nlohmann::json &jv,const double dfl);
|
||||
static uint64_t jsonIntHex(const nlohmann::json &jv,const uint64_t dfl);
|
||||
static bool jsonBool(const nlohmann::json &jv,const bool dfl);
|
||||
static std::string jsonString(const nlohmann::json &jv,const char *dfl);
|
||||
static std::string jsonBinFromHex(const nlohmann::json &jv);
|
||||
#endif // OMIT_JSON_SUPPORT
|
||||
static nlohmann::json jsonParse(const std::string& buf);
|
||||
static std::string jsonDump(const nlohmann::json& j, int indentation = 1);
|
||||
static uint64_t jsonInt(const nlohmann::json& jv, const uint64_t dfl);
|
||||
static double jsonDouble(const nlohmann::json& jv, const double dfl);
|
||||
static uint64_t jsonIntHex(const nlohmann::json& jv, const uint64_t dfl);
|
||||
static bool jsonBool(const nlohmann::json& jv, const bool dfl);
|
||||
static std::string jsonString(const nlohmann::json& jv, const char* dfl);
|
||||
static std::string jsonBinFromHex(const nlohmann::json& jv);
|
||||
#endif // OMIT_JSON_SUPPORT
|
||||
|
||||
private:
|
||||
private:
|
||||
static const unsigned char TOLOWER_TABLE[256];
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue