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.

165 lines
5.5KB

  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 isDir(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. */
  32. void rename(const std::string& srcPath, const std::string& destPath);
  33. /** Copies a file or directory recursively. */
  34. void copy(const std::string& srcPath, const std::string& destPath);
  35. /** Creates a directory.
  36. The parent directory must exist.
  37. */
  38. bool createDir(const std::string& path);
  39. /** Creates all directories up to the path.
  40. */
  41. bool createDirs(const std::string& path);
  42. /** Deletes a file or empty directory.
  43. Returns whether the deletion was successful.
  44. */
  45. bool remove(const std::string& path);
  46. /** Deletes a file or directory recursively.
  47. Returns the number of files and directories that were deleted.
  48. */
  49. int removeRecursively(const std::string& path);
  50. std::string getWorkingDir();
  51. void setWorkingDir(const std::string& path);
  52. std::string getTempDir();
  53. /** Returns the absolute path beginning with "/". */
  54. std::string getAbsolute(const std::string& path);
  55. /** Returns the canonical (unique) path, following symlinks and "." and ".." fake directories.
  56. The path must exist on the filesystem.
  57. Examples:
  58. getCanonical("/foo/./bar/.") // "/foo/bar"
  59. */
  60. std::string getCanonical(const std::string& path);
  61. /** Extracts the parent directory of the path.
  62. Examples:
  63. getDir("/var/tmp/example.txt") // "/var/tmp"
  64. getDir("/") // ""
  65. getDir("/var/tmp/.") // "/var/tmp"
  66. */
  67. std::string getDir(const std::string& path);
  68. /** Extracts the filename of the path.
  69. Examples:
  70. getFilename("/foo/bar.txt") // "bar.txt"
  71. getFilename("/foo/.bar") // ".bar"
  72. getFilename("/foo/bar/") // "."
  73. getFilename("/foo/.") // "."
  74. getFilename("/foo/..") // ".."
  75. getFilename(".") // "."
  76. getFilename("..") // ".."
  77. getFilename("/") // "/"
  78. */
  79. std::string getFilename(const std::string& path);
  80. /** Extracts the portion of a filename without the extension.
  81. Examples:
  82. getStem("/foo/bar.txt") // "bar"
  83. getStem("/foo/.bar") // ""
  84. getStem("/foo/foo.tar.ztd") // "foo.tar"
  85. */
  86. std::string getStem(const std::string& path);
  87. /** Extracts the extension of a filename, including the dot.
  88. Examples:
  89. getExtension("/foo/bar.txt") // ".txt"
  90. getExtension("/foo/bar.") // "."
  91. getExtension("/foo/bar") // ""
  92. getExtension("/foo/bar.txt/bar.cc") // ".cc"
  93. getExtension("/foo/bar.txt/bar.") // "."
  94. getExtension("/foo/bar.txt/bar") // ""
  95. getExtension("/foo/.") // ""
  96. getExtension("/foo/..") // ""
  97. getExtension("/foo/.hidden") // ".hidden"
  98. */
  99. std::string getExtension(const std::string& path);
  100. /** Compresses the contents of a directory (recursively) to an archive.
  101. Uses the Unix Standard TAR + Zstandard format (.tar.zst).
  102. An equivalent shell command is
  103. ZSTD_CLEVEL=1 tar -cf archivePath --zstd -C dirPath .
  104. */
  105. void archiveDir(const std::string& archivePath, const std::string& dirPath, int compressionLevel = 1);
  106. std::vector<uint8_t> archiveDir(const std::string& dirPath, int compressionLevel = 1);
  107. /** Extracts an archive into a directory.
  108. An equivalent shell command is
  109. tar -xf archivePath --zstd -C dirPath
  110. */
  111. void unarchiveToDir(const std::string& archivePath, const std::string& dirPath);
  112. void unarchiveToDir(const std::vector<uint8_t>& archiveData, const std::string& dirPath);
  113. // Threading
  114. /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */
  115. int getLogicalCoreCount();
  116. /** Sets a name of the current thread for debuggers and OS-specific process viewers. */
  117. void setThreadName(const std::string& name);
  118. // Querying
  119. /** Returns the caller's human-readable stack trace with "\n"-separated lines. */
  120. std::string getStackTrace();
  121. /** Returns the number of seconds since application launch.
  122. Gives the most precise (fine-grained) time differences available on the OS for benchmarking purposes, while being fast to compute.
  123. */
  124. double getTime();
  125. /** Returns time since 1970-01-01 00:00:00 UTC in seconds.
  126. */
  127. double getUnixTime();
  128. std::string getOperatingSystemInfo();
  129. // Applications
  130. /** Opens a URL in a browser.
  131. Shell injection is possible, so make sure the URL is trusted or hard coded.
  132. May block, so open in a new thread.
  133. */
  134. void openBrowser(const std::string& url);
  135. /** Opens Windows Explorer, Finder, etc at a directory location. */
  136. void openDir(const std::string& path);
  137. /** Runs an executable without blocking.
  138. The launched process will continue running if the current process is closed.
  139. */
  140. void runProcessDetached(const std::string& path);
  141. void init();
  142. } // namespace system
  143. } // namespace rack