Преглед на файлове

Clean OS-dep stuff out of node/Utils.

Adam Ierymenko преди 10 години
родител
ревизия
0751eaabd8
променени са 2 файла, в които са добавени 0 реда и са изтрити 385 реда
  1. 0 156
      node/Utils.cpp
  2. 0 229
      node/Utils.hpp

+ 0 - 156
node/Utils.cpp

@@ -54,30 +54,6 @@ namespace ZeroTier {
 
 const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
 
-#ifdef __UNIX_LIKE__
-bool Utils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
-	throw()
-{
-	int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT,0600);
-	if (fdout > 0) {
-		int fderr;
-		if (stderrPath) {
-			fderr = ::open(stderrPath,O_WRONLY|O_CREAT,0600);
-			if (fderr <= 0) {
-				::close(fdout);
-				return false;
-			}
-		} else fderr = fdout;
-		::close(STDOUT_FILENO);
-		::close(STDERR_FILENO);
-		::dup2(fdout,STDOUT_FILENO);
-		::dup2(fderr,STDERR_FILENO);
-		return true;
-	}
-	return false;
-}
-#endif // __UNIX_LIKE__
-
 static void _Utils_doBurn(char *ptr,unsigned int len)
 {
 	for(unsigned int i=0;i<len;++i)
@@ -94,42 +70,6 @@ void Utils::burn(void *ptr,unsigned int len)
 	(_Utils_doBurn_ptr)((char *)ptr,len);
 }
 
-std::map<std::string,bool> Utils::listDirectory(const char *path)
-{
-	std::map<std::string,bool> r;
-
-#ifdef __WINDOWS__
-	HANDLE hFind;
-	WIN32_FIND_DATAA ffd;
-	if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
-		do {
-			if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,"..")))
-				r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
-		} while (FindNextFileA(hFind,&ffd));
-		FindClose(hFind);
-	}
-#else
-	struct dirent de;
-	struct dirent *dptr;
-
-	DIR *d = opendir(path);
-	if (!d)
-		return r;
-
-	dptr = (struct dirent *)0;
-	for(;;) {
-		if (readdir_r(d,&de,&dptr))
-			break;
-		if (dptr) {
-			if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,"..")))
-				r[std::string(dptr->d_name)] = (dptr->d_type == DT_DIR);
-		} else break;
-	}
-#endif
-
-	return r;
-}
-
 std::string Utils::hex(const void *data,unsigned int len)
 {
 	std::string r;
@@ -268,102 +208,6 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 #endif // __WINDOWS__
 }
 
-void Utils::lockDownFile(const char *path,bool isDir)
-{
-#ifdef __UNIX_LIKE__
-	chmod(path,isDir ? 0700 : 0600);
-#else
-#ifdef __WINDOWS__
-	{
-		STARTUPINFOA startupInfo;
-		PROCESS_INFORMATION processInfo;
-
-		startupInfo.cb = sizeof(startupInfo);
-		memset(&startupInfo,0,sizeof(STARTUPINFOA));
-		memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
-		if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /inheritance:d /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
-			WaitForSingleObject(processInfo.hProcess,INFINITE);
-			CloseHandle(processInfo.hProcess);
-			CloseHandle(processInfo.hThread);
-		}
-
-		startupInfo.cb = sizeof(startupInfo);
-		memset(&startupInfo,0,sizeof(STARTUPINFOA));
-		memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
-		if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /remove *S-1-5-32-545 /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
-			WaitForSingleObject(processInfo.hProcess,INFINITE);
-			CloseHandle(processInfo.hProcess);
-			CloseHandle(processInfo.hThread);
-		}
-	}
-#endif
-#endif
-}
-
-uint64_t Utils::getLastModified(const char *path)
-{
-	struct stat s;
-	if (stat(path,&s))
-		return 0;
-	return (((uint64_t)s.st_mtime) * 1000ULL);
-}
-
-bool Utils::fileExists(const char *path,bool followLinks)
-{
-	struct stat s;
-#ifdef __UNIX_LIKE__
-	if (!followLinks)
-		return (lstat(path,&s) == 0);
-#endif
-	return (stat(path,&s) == 0);
-}
-
-int64_t Utils::getFileSize(const char *path)
-{
-	struct stat s;
-	if (stat(path,&s))
-		return -1;
-#ifdef __WINDOWS__
-	return s.st_size;
-#else
-	if (S_ISREG(s.st_mode))
-		return s.st_size;
-#endif
-	return -1;
-}
-
-bool Utils::readFile(const char *path,std::string &buf)
-{
-	char tmp[4096];
-	FILE *f = fopen(path,"rb");
-	if (f) {
-		for(;;) {
-			long n = (long)fread(tmp,1,sizeof(tmp),f);
-			if (n > 0)
-				buf.append(tmp,n);
-			else break;
-		}
-		fclose(f);
-		return true;
-	}
-	return false;
-}
-
-bool Utils::writeFile(const char *path,const void *buf,unsigned int len)
-{
-	FILE *f = fopen(path,"wb");
-	if (f) {
-		if ((long)fwrite(buf,1,len,f) != (long)len) {
-			fclose(f);
-			return false;
-		} else {
-			fclose(f);
-			return true;
-		}
-	}
-	return false;
-}
-
 std::vector<std::string> Utils::split(const char *s,const char *const sep,const char *esc,const char *quot)
 {
 	std::vector<std::string> fields;

+ 0 - 229
node/Utils.hpp

@@ -41,15 +41,6 @@
 
 #include "Constants.hpp"
 
-#ifdef __WINDOWS__
-#include <WinSock2.h>
-#include <Windows.h>
-#else
-#include <unistd.h>
-#include <sys/time.h>
-#include <arpa/inet.h>
-#endif
-
 namespace ZeroTier {
 
 /**
@@ -58,21 +49,6 @@ namespace ZeroTier {
 class Utils
 {
 public:
-#ifdef __UNIX_LIKE__
-	/**
-	 * Close STDOUT_FILENO and STDERR_FILENO and replace them with output to given path
-	 *
-	 * This can be called after fork() and prior to exec() to suppress output
-	 * from a subprocess, such as auto-update.
-	 *
-	 * @param stdoutPath Path to file to use for stdout
-	 * @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__
-
 	/**
 	 * Perform a time-invariant binary comparison
 	 *
@@ -106,36 +82,6 @@ public:
 	static void burn(void *ptr,unsigned int len)
 		throw();
 
-	/**
-	 * Delete a file
-	 *
-	 * @param path Path to delete
-	 * @return True if delete was successful
-	 */
-	static inline bool rm(const char *path)
-		throw()
-	{
-#ifdef __WINDOWS__
-		return (DeleteFileA(path) != FALSE);
-#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
-	 * 
-	 * Keys in returned map are filenames only and don't include the leading
-	 * path. Pseudo-paths like . and .. are not returned. Values are true if
-	 * the item is a directory, false if it's a file. More detailed attributes
-	 * aren't supported since the code that uses this doesn't need them.
-	 *
-	 * @param path Path to list
-	 * @return Map of entries and whether or not they are also directories (empty on failure)
-	 */
-	static std::map<std::string,bool> listDirectory(const char *path);
-
 	/**
 	 * Convert binary data to hexadecimal
 	 *
@@ -185,119 +131,6 @@ public:
 	 */
 	static void getSecureRandom(void *buf,unsigned int bytes);
 
-	/**
-	 * Set modes on a file to something secure
-	 * 
-	 * This locks a file so that only the owner can access it. What it actually
-	 * does varies by platform.
-	 * 
-	 * @param path Path to lock
-	 * @param isDir True if this is a directory
-	 */
-	static void lockDownFile(const char *path,bool isDir);
-
-	/**
-	 * Get file last modification time
-	 *
-	 * Resolution is often only second, not millisecond, but the return is
-	 * always in ms for comparison against now().
-	 *
-	 * @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);
-
-	/**
-	 * @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);
-
-	/**
-	 * @param path Path to file
-	 * @return File size or -1 if nonexistent or other failure
-	 */
-	static int64_t getFileSize(const char *path);
-
-	/**
-	 * @return Current time in milliseconds since epoch
-	 */
-	static inline uint64_t now()
-		throw()
-	{
-#ifdef __WINDOWS__
-		FILETIME ft;
-		SYSTEMTIME st;
-		ULARGE_INTEGER tmp;
-		GetSystemTime(&st);
-		SystemTimeToFileTime(&st,&ft);
-		tmp.LowPart = ft.dwLowDateTime;
-		tmp.HighPart = ft.dwHighDateTime;
-		return ( ((tmp.QuadPart - 116444736000000000ULL) / 10000L) + st.wMilliseconds );
-#else
-		struct timeval tv;
-		gettimeofday(&tv,(struct timezone *)0);
-		return ( (1000ULL * (uint64_t)tv.tv_sec) + (uint64_t)(tv.tv_usec / 1000) );
-#endif
-	};
-
-	/**
-	 * @return Current time in seconds since epoch, to the highest available resolution
-	 */
-	static inline double nowf()
-		throw()
-	{
-#ifdef __WINDOWS__
-		FILETIME ft;
-		SYSTEMTIME st;
-		ULARGE_INTEGER tmp;
-		GetSystemTime(&st);
-		SystemTimeToFileTime(&st,&ft);
-		tmp.LowPart = ft.dwLowDateTime;
-		tmp.HighPart = ft.dwHighDateTime;
-		return (((double)(tmp.QuadPart - 116444736000000000ULL)) / 10000000.0);
-#else
-		struct timeval tv;
-		gettimeofday(&tv,(struct timezone *)0);
-		return ( ((double)tv.tv_sec) + (((double)tv.tv_usec) / 1000000.0) );
-#endif
-	}
-
-	/**
-	 * Read the full contents of a file into a string buffer
-	 *
-	 * The buffer isn't cleared, so if it already contains data the file's data will
-	 * be appended.
-	 *
-	 * @param path Path of file to read
-	 * @param buf Buffer to fill
-	 * @return True if open and read successful
-	 */
-	static bool readFile(const char *path,std::string &buf);
-
-	/**
-	 * Write a block of data to disk, replacing any current file contents
-	 *
-	 * @param path Path to write
-	 * @param buf Buffer containing data
-	 * @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);
-
-	/**
-	 * Write a block of data to disk, replacing any current file contents
-	 *
-	 * @param path Path to write
-	 * @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());
-	}
-
 	/**
 	 * Split a string by delimiter, with optional escape and quote characters
 	 *
@@ -487,68 +320,6 @@ public:
 		return true;
 	}
 
-	/**
-	 * Compute SDBM hash of a binary string
-	 *
-	 * See: http://www.cse.yorku.ca/~oz/hash.html
-	 *
-	 * @param s Data to hash
-	 * @param l Length in bytes
-	 * @param h Previous hash value (use 0 initially)
-	 * @tparam H Hash integer type -- should be unsigned
-	 * @return New hash value
-	 */
-	template<typename H>
-	static inline H sdbmHash(const void *s,unsigned int l,H h)
-		throw()
-	{
-		for(unsigned int i=0;i<l;++i)
-			h = ((H)(((const unsigned char *)s)[i])) + (h << 6) + (h << 16) - h;
-		return h;
-	}
-
-	/**
-	 * Compute SDBM hash of a 0-terminated C string
-	 *
-	 * See: http://www.cse.yorku.ca/~oz/hash.html
-	 *
-	 * @param s C-string to hash
-	 * @param h Previous hash value (use 0 initially)
-	 * @tparam H Hash integer type -- should be unsigned
-	 * @return New hash value
-	 */
-	template<typename H>
-	static inline H sdbmHash(const char *s,H h)
-		throw()
-	{
-		char c;
-		while ((c = *(s++)))
-			h = ((H)c) + (h << 6) + (h << 16) - h;
-		return h;
-	}
-
-	/**
-	 * Compute SDBM hash of an integer's bytes in little-endian byte order
-	 *
-	 * See: http://www.cse.yorku.ca/~oz/hash.html
-	 *
-	 * @param n Integer to hash in LE byte order
-	 * @param h Previous hash value (use 0 initially)
-	 * @tparam I Integer type -- should be unsigned
-	 * @tparam H Hash integer type -- should be unsigned
-	 * @return New hash value
-	 */
-	template<typename I,typename H>
-	static inline H sdbmHash(I n,H h)
-		throw()
-	{
-		for(unsigned int i=0;i<(unsigned int)sizeof(n);++i) {
-			h = ((H)(n & 0xff)) + (h << 6) + (h << 16) - h;
-			n >>= 8;
-		}
-		return h;
-	}
-
 	// Byte swappers for big/little endian conversion
 	static inline uint8_t hton(uint8_t n) throw() { return n; }
 	static inline int8_t hton(int8_t n) throw() { return n; }