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.

189 lines
6.1KB

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