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.

191 lines
6.2KB

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