You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

system.hpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #include <vector>
  3. #include <common.hpp>
  4. namespace rack {
  5. /** Cross-platform functions for OS, file path, and filesystem routines */
  6. namespace system {
  7. // Filesystem
  8. /** Joins two paths with a directory separator.
  9. If `path2` is an empty string, returns `path1`.
  10. */
  11. std::string join(const std::string& path1, const std::string& path2 = "");
  12. /** Join an arbitrary number of paths, from left to right. */
  13. template <typename... Paths>
  14. std::string join(const std::string& path1, const std::string& path2, Paths... paths) {
  15. return join(join(path1, path2), paths...);
  16. }
  17. /** Returns all entries (directories, files, symbolic links, etc) in a directory.
  18. `depth` is the number of directories to recurse. 0 depth does not recurse. -1 depth recurses infinitely.
  19. */
  20. std::vector<std::string> getEntries(const std::string& dirPath, int depth = 0);
  21. /** Expands a glob pattern such as `dir/file*.txt` to a list of paths.
  22. Paths are sorted.
  23. */
  24. std::vector<std::string> glob(const std::string& pattern);
  25. bool exists(const std::string& path);
  26. /** Returns whether the given path is a file. */
  27. bool isFile(const std::string& path);
  28. /** Returns whether the given path is a directory. */
  29. bool isDirectory(const std::string& path);
  30. uint64_t getFileSize(const std::string& path);
  31. /** Moves a file or directory.
  32. Does not overwrite the destination. If this behavior is needed, use remove() or removeRecursively() before moving.
  33. Returns whether the rename was successful.
  34. */
  35. bool rename(const std::string& srcPath, const std::string& destPath);
  36. /** Copies a file or directory recursively.
  37. Overwrites destination if already exists.
  38. Returns whether the copy was successful.
  39. */
  40. bool copy(const std::string& srcPath, const std::string& destPath);
  41. /** Creates a directory.
  42. The parent directory must exist.
  43. Returns whether the creation was successful.
  44. */
  45. bool createDirectory(const std::string& path);
  46. /** Creates all directories up to the path.
  47. Returns whether the creation was successful.
  48. */
  49. bool createDirectories(const std::string& path);
  50. bool createSymbolicLink(const std::string& target, const std::string& link);
  51. /** Deletes a file or empty directory.
  52. Returns whether the deletion was successful.
  53. */
  54. bool remove(const std::string& path);
  55. /** Deletes a file or directory recursively.
  56. Returns the number of files and directories that were deleted.
  57. */
  58. int removeRecursively(const std::string& path);
  59. std::string getWorkingDirectory();
  60. void setWorkingDirectory(const std::string& path);
  61. std::string getTempDirectory();
  62. /** Returns the absolute path beginning with "/". */
  63. std::string getAbsolute(const std::string& path);
  64. /** Returns the canonical (unique) path, following symlinks and "." and ".." fake directories.
  65. The path must exist on the filesystem.
  66. Examples:
  67. getCanonical("/foo/./bar/.") // "/foo/bar"
  68. */
  69. std::string getCanonical(const std::string& path);
  70. /** Extracts the parent directory of the path.
  71. Examples:
  72. getDirectory("/var/tmp/example.txt") // "/var/tmp"
  73. getDirectory("/") // ""
  74. getDirectory("/var/tmp/.") // "/var/tmp"
  75. */
  76. std::string getDirectory(const std::string& path);
  77. /** Extracts the filename of the path.
  78. Examples:
  79. getFilename("/foo/bar.txt") // "bar.txt"
  80. getFilename("/foo/.bar") // ".bar"
  81. getFilename("/foo/bar/") // "."
  82. getFilename("/foo/.") // "."
  83. getFilename("/foo/..") // ".."
  84. getFilename(".") // "."
  85. getFilename("..") // ".."
  86. getFilename("/") // "/"
  87. */
  88. std::string getFilename(const std::string& path);
  89. /** Extracts the portion of a filename without the extension.
  90. Examples:
  91. getStem("/foo/bar.txt") // "bar"
  92. getStem("/foo/.bar") // ""
  93. getStem("/foo/foo.tar.ztd") // "foo.tar"
  94. */
  95. std::string getStem(const std::string& path);
  96. /** Extracts the extension of a filename, including the dot.
  97. Examples:
  98. getExtension("/foo/bar.txt") // ".txt"
  99. getExtension("/foo/bar.") // "."
  100. getExtension("/foo/bar") // ""
  101. getExtension("/foo/bar.txt/bar.cc") // ".cc"
  102. getExtension("/foo/bar.txt/bar.") // "."
  103. getExtension("/foo/bar.txt/bar") // ""
  104. getExtension("/foo/.") // ""
  105. getExtension("/foo/..") // ""
  106. getExtension("/foo/.hidden") // ".hidden"
  107. */
  108. std::string getExtension(const std::string& path);
  109. // File read/write
  110. /** Reads an entire file into a memory buffer.
  111. Throws on error.
  112. */
  113. std::vector<uint8_t> readFile(const std::string& path);
  114. uint8_t* readFile(const std::string& path, size_t* size);
  115. /** Writes a memory buffer to a file, overwriting if already exists.
  116. Throws on error.
  117. */
  118. void writeFile(const std::string& path, const std::vector<uint8_t>& data);
  119. /** Compresses the contents of a directory (recursively) to an archive.
  120. Uses the Unix Standard TAR + Zstandard format (.tar.zst).
  121. An equivalent shell command is
  122. tar -c -C dirPath . | zstd -1 -o archivePath
  123. or
  124. ZSTD_CLEVEL=1 tar -cf archivePath --zstd -C dirPath .
  125. Throws on error.
  126. */
  127. void archiveDirectory(const std::string& archivePath, const std::string& dirPath, int compressionLevel = 1);
  128. std::vector<uint8_t> archiveDirectory(const std::string& dirPath, int compressionLevel = 1);
  129. /** Extracts an archive into a directory.
  130. An equivalent shell command is
  131. zstd -d < archivePath | tar -x -C dirPath
  132. or
  133. tar -xf archivePath --zstd -C dirPath
  134. As a special case, zero-byte files in the archive cause the unarchiver to delete existing files instead of overwriting them.
  135. This is useful for removing presets in .vcvplugin packages, for example.
  136. Throws on error.
  137. */
  138. void unarchiveToDirectory(const std::string& archivePath, const std::string& dirPath);
  139. void unarchiveToDirectory(const std::vector<uint8_t>& archiveData, const std::string& dirPath);
  140. // Threading
  141. /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */
  142. int getLogicalCoreCount();
  143. /** Sets a name of the current thread for debuggers and OS-specific process viewers. */
  144. void setThreadName(const std::string& name);
  145. // Querying
  146. /** Returns the caller's human-readable stack trace with "\n"-separated lines. */
  147. std::string getStackTrace();
  148. /** Returns the number of seconds since application launch.
  149. Gives the most precise (fine-grained) monotonic (non-decreasing) time differences available on the OS for benchmarking purposes, while being fast to compute.
  150. */
  151. double getTime();
  152. /** Returns time since 1970-01-01 00:00:00 UTC in seconds.
  153. */
  154. double getUnixTime();
  155. double getThreadTime();
  156. void sleep(double time);
  157. std::string getOperatingSystemInfo();
  158. // Applications
  159. /** Opens a URL in a browser.
  160. Shell injection is possible, so make sure the URL is trusted or hard coded.
  161. Does nothing if string is blank.
  162. Does not block.
  163. */
  164. void openBrowser(const std::string& url);
  165. /** Opens Windows Explorer, Finder, etc at a directory location.
  166. Does nothing if string is blank.
  167. Does not block.
  168. */
  169. void openDirectory(const std::string& path);
  170. /** Runs an executable without blocking.
  171. The launched process will continue running if the current process is closed.
  172. */
  173. void runProcessDetached(const std::string& path);
  174. /** Returns the CPU's floating point unit control flags.
  175. MXCSR register on x64, and the FPCR register on ARM64.
  176. */
  177. uint32_t getFpuFlags();
  178. void setFpuFlags(uint32_t flags);
  179. /** Sets Rack-recommended FPU flags for the current thread.
  180. */
  181. void resetFpuFlags();
  182. PRIVATE void init();
  183. } // namespace system
  184. } // namespace rack