filesys.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <set>
  18. #include <string>
  19. #include <string_view>
  20. #include <vector>
  21. #ifdef _WIN32
  22. #define DIR_DELIM "\\"
  23. #define DIR_DELIM_CHAR '\\'
  24. #define FILESYS_CASE_INSENSITIVE true
  25. #define PATH_DELIM ";"
  26. #else
  27. #define DIR_DELIM "/"
  28. #define DIR_DELIM_CHAR '/'
  29. #define FILESYS_CASE_INSENSITIVE false
  30. #define PATH_DELIM ":"
  31. #endif
  32. namespace irr::io {
  33. class IFileSystem;
  34. }
  35. namespace fs
  36. {
  37. struct DirListNode
  38. {
  39. std::string name;
  40. bool dir;
  41. };
  42. std::vector<DirListNode> GetDirListing(const std::string &path);
  43. // Returns true if already exists
  44. bool CreateDir(const std::string &path);
  45. bool PathExists(const std::string &path);
  46. bool IsPathAbsolute(const std::string &path);
  47. bool IsDir(const std::string &path);
  48. bool IsExecutable(const std::string &path);
  49. inline bool IsFile(const std::string &path)
  50. {
  51. return PathExists(path) && !IsDir(path);
  52. }
  53. bool IsDirDelimiter(char c);
  54. // Only pass full paths to this one. True on success.
  55. // NOTE: The WIN32 version returns always true.
  56. bool RecursiveDelete(const std::string &path);
  57. bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
  58. /// Returns path to temp directory.
  59. /// You probably don't want to use this directly, see `CreateTempFile` or `CreateTempDir`.
  60. /// @return path or "" on error
  61. std::string TempPath();
  62. /// Returns path to securely-created temporary file (will already exist when this function returns).
  63. /// @return path or "" on error
  64. std::string CreateTempFile();
  65. /// Returns path to securely-created temporary directory (will already exist when this function returns).
  66. /// @return path or "" on error
  67. std::string CreateTempDir();
  68. /* Returns a list of subdirectories, including the path itself, but excluding
  69. hidden directories (whose names start with . or _)
  70. */
  71. void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
  72. std::vector<std::string> GetRecursiveDirs(const std::string &dir);
  73. /* Multiplatform */
  74. /* The path itself not included, returns a list of all subpaths.
  75. dst - vector that contains all the subpaths.
  76. list files - include files in the list of subpaths.
  77. ignore - paths that start with these charcters will not be listed.
  78. */
  79. void GetRecursiveSubPaths(const std::string &path,
  80. std::vector<std::string> &dst,
  81. bool list_files,
  82. const std::set<char> &ignore = {});
  83. // Only pass full paths to this one. True on success.
  84. bool RecursiveDeleteContent(const std::string &path);
  85. // Create all directories on the given path that don't already exist.
  86. bool CreateAllDirs(const std::string &path);
  87. // Copy a regular file
  88. bool CopyFileContents(const std::string &source, const std::string &target);
  89. // Copy directory and all subdirectories
  90. // Omits files and subdirectories that start with a period
  91. bool CopyDir(const std::string &source, const std::string &target);
  92. // Move directory and all subdirectories
  93. // Behavior with files/subdirs that start with a period is undefined
  94. bool MoveDir(const std::string &source, const std::string &target);
  95. // Check if one path is prefix of another
  96. // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
  97. // Ignores case differences and '/' vs. '\\' on Windows
  98. bool PathStartsWith(const std::string &path, const std::string &prefix);
  99. // Remove last path component and the dir delimiter before and/or after it,
  100. // returns "" if there is only one path component.
  101. // removed: If non-NULL, receives the removed component(s).
  102. // count: Number of components to remove
  103. std::string RemoveLastPathComponent(const std::string &path,
  104. std::string *removed = NULL, int count = 1);
  105. // Remove "." and ".." path components and for every ".." removed, remove
  106. // the last normal path component before it. Unlike AbsolutePath,
  107. // this does not resolve symlinks and check for existence of directories.
  108. std::string RemoveRelativePathComponents(std::string path);
  109. // Returns the absolute path for the passed path, with "." and ".." path
  110. // components and symlinks removed. Returns "" on error.
  111. std::string AbsolutePath(const std::string &path);
  112. // Returns the filename from a path or the entire path if no directory
  113. // delimiter is found.
  114. const char *GetFilenameFromPath(const char *path);
  115. bool safeWriteToFile(const std::string &path, std::string_view content);
  116. #ifndef SERVER
  117. bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
  118. #endif
  119. bool ReadFile(const std::string &path, std::string &out);
  120. bool Rename(const std::string &from, const std::string &to);
  121. } // namespace fs