OSUtils.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_OSUTILS_HPP
  28. #define ZT_OSUTILS_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <string>
  35. #include <stdexcept>
  36. #include <vector>
  37. #include <map>
  38. #include "../node/Constants.hpp"
  39. #ifdef __WINDOWS__
  40. #include <WinSock2.h>
  41. #include <Windows.h>
  42. #else
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #include <sys/time.h>
  46. #include <sys/stat.h>
  47. #include <arpa/inet.h>
  48. #endif
  49. namespace ZeroTier {
  50. /**
  51. * Miscellaneous utility functions and global constants
  52. */
  53. class OSUtils
  54. {
  55. public:
  56. #ifdef __UNIX_LIKE__
  57. /**
  58. * Close STDOUT_FILENO and STDERR_FILENO and replace them with output to given path
  59. *
  60. * This can be called after fork() and prior to exec() to suppress output
  61. * from a subprocess, such as auto-update.
  62. *
  63. * @param stdoutPath Path to file to use for stdout
  64. * @param stderrPath Path to file to use for stderr, or NULL for same as stdout (default)
  65. * @return True on success
  66. */
  67. static bool redirectUnixOutputs(const char *stdoutPath,const char *stderrPath = (const char *)0)
  68. throw();
  69. #endif // __UNIX_LIKE__
  70. /**
  71. * Delete a file
  72. *
  73. * @param path Path to delete
  74. * @return True if delete was successful
  75. */
  76. static inline bool rm(const char *path)
  77. throw()
  78. {
  79. #ifdef __WINDOWS__
  80. return (DeleteFileA(path) != FALSE);
  81. #else
  82. return (unlink(path) == 0);
  83. #endif
  84. }
  85. static inline bool rm(const std::string &path) throw() { return rm(path.c_str()); }
  86. static inline bool mkdir(const char *path)
  87. throw()
  88. {
  89. if (::mkdir(path,0755) != 0)
  90. return (errno == EEXIST);
  91. return true;
  92. }
  93. static inline bool mkdir(const std::string &path) throw() { return OSUtils::mkdir(path.c_str()); }
  94. /**
  95. * List a directory's contents
  96. *
  97. * This returns only files, not sub-directories.
  98. *
  99. * @param path Path to list
  100. * @return Names of files in directory
  101. */
  102. static std::vector<std::string> listDirectory(const char *path);
  103. /**
  104. * Set modes on a file to something secure
  105. *
  106. * This locks a file so that only the owner can access it. What it actually
  107. * does varies by platform.
  108. *
  109. * @param path Path to lock
  110. * @param isDir True if this is a directory
  111. */
  112. static void lockDownFile(const char *path,bool isDir);
  113. /**
  114. * Get file last modification time
  115. *
  116. * Resolution is often only second, not millisecond, but the return is
  117. * always in ms for comparison against now().
  118. *
  119. * @param path Path to file to get time
  120. * @return Last modification time in ms since epoch or 0 if not found
  121. */
  122. static uint64_t getLastModified(const char *path);
  123. /**
  124. * @param path Path to check
  125. * @param followLinks Follow links (on platforms with that concept)
  126. * @return True if file or directory exists at path location
  127. */
  128. static bool fileExists(const char *path,bool followLinks = true);
  129. /**
  130. * @param path Path to file
  131. * @return File size or -1 if nonexistent or other failure
  132. */
  133. static int64_t getFileSize(const char *path);
  134. /**
  135. * @return Current time in milliseconds since epoch
  136. */
  137. static inline uint64_t now()
  138. throw()
  139. {
  140. #ifdef __WINDOWS__
  141. FILETIME ft;
  142. SYSTEMTIME st;
  143. ULARGE_INTEGER tmp;
  144. GetSystemTime(&st);
  145. SystemTimeToFileTime(&st,&ft);
  146. tmp.LowPart = ft.dwLowDateTime;
  147. tmp.HighPart = ft.dwHighDateTime;
  148. return ( ((tmp.QuadPart - 116444736000000000ULL) / 10000L) + st.wMilliseconds );
  149. #else
  150. struct timeval tv;
  151. gettimeofday(&tv,(struct timezone *)0);
  152. return ( (1000ULL * (uint64_t)tv.tv_sec) + (uint64_t)(tv.tv_usec / 1000) );
  153. #endif
  154. };
  155. /**
  156. * @return Current time in seconds since epoch, to the highest available resolution
  157. */
  158. static inline double nowf()
  159. throw()
  160. {
  161. #ifdef __WINDOWS__
  162. FILETIME ft;
  163. SYSTEMTIME st;
  164. ULARGE_INTEGER tmp;
  165. GetSystemTime(&st);
  166. SystemTimeToFileTime(&st,&ft);
  167. tmp.LowPart = ft.dwLowDateTime;
  168. tmp.HighPart = ft.dwHighDateTime;
  169. return (((double)(tmp.QuadPart - 116444736000000000ULL)) / 10000000.0);
  170. #else
  171. struct timeval tv;
  172. gettimeofday(&tv,(struct timezone *)0);
  173. return ( ((double)tv.tv_sec) + (((double)tv.tv_usec) / 1000000.0) );
  174. #endif
  175. }
  176. /**
  177. * Read the full contents of a file into a string buffer
  178. *
  179. * The buffer isn't cleared, so if it already contains data the file's data will
  180. * be appended.
  181. *
  182. * @param path Path of file to read
  183. * @param buf Buffer to fill
  184. * @return True if open and read successful
  185. */
  186. static bool readFile(const char *path,std::string &buf);
  187. /**
  188. * Write a block of data to disk, replacing any current file contents
  189. *
  190. * @param path Path to write
  191. * @param buf Buffer containing data
  192. * @param len Length of buffer
  193. * @return True if entire file was successfully written
  194. */
  195. static bool writeFile(const char *path,const void *buf,unsigned int len);
  196. /**
  197. * Write a block of data to disk, replacing any current file contents
  198. *
  199. * @param path Path to write
  200. * @param s Data to write
  201. * @return True if entire file was successfully written
  202. */
  203. static inline bool writeFile(const char *path,const std::string &s) { return writeFile(path,s.data(),(unsigned int)s.length()); }
  204. };
  205. } // namespace ZeroTier
  206. #endif