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.

188 lines
6.2KB

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