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.

162 lines
5.4KB

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