filesys.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <vector>
  20. #include "exceptions.h"
  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 { namespace 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. inline bool IsFile(const std::string &path)
  49. {
  50. return PathExists(path) && !IsDir(path);
  51. }
  52. bool IsDirDelimiter(char c);
  53. // Only pass full paths to this one. True on success.
  54. // NOTE: The WIN32 version returns always true.
  55. bool RecursiveDelete(const std::string &path);
  56. bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
  57. // Returns path to temp directory, can return "" on error
  58. std::string TempPath();
  59. // Returns path to securely-created temporary file (will already exist when this function returns)
  60. // can return "" on error
  61. std::string CreateTempFile();
  62. /* Returns a list of subdirectories, including the path itself, but excluding
  63. hidden directories (whose names start with . or _)
  64. */
  65. void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
  66. std::vector<std::string> GetRecursiveDirs(const std::string &dir);
  67. /* Multiplatform */
  68. /* The path itself not included, returns a list of all subpaths.
  69. dst - vector that contains all the subpaths.
  70. list files - include files in the list of subpaths.
  71. ignore - paths that start with these charcters will not be listed.
  72. */
  73. void GetRecursiveSubPaths(const std::string &path,
  74. std::vector<std::string> &dst,
  75. bool list_files,
  76. const std::set<char> &ignore = {});
  77. // Only pass full paths to this one. True on success.
  78. bool RecursiveDeleteContent(const std::string &path);
  79. // Create all directories on the given path that don't already exist.
  80. bool CreateAllDirs(const std::string &path);
  81. // Copy a regular file
  82. bool CopyFileContents(const std::string &source, const std::string &target);
  83. // Copy directory and all subdirectories
  84. // Omits files and subdirectories that start with a period
  85. bool CopyDir(const std::string &source, const std::string &target);
  86. // Move directory and all subdirectories
  87. // Behavior with files/subdirs that start with a period is undefined
  88. bool MoveDir(const std::string &source, const std::string &target);
  89. // Check if one path is prefix of another
  90. // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
  91. // Ignores case differences and '/' vs. '\\' on Windows
  92. bool PathStartsWith(const std::string &path, const std::string &prefix);
  93. // Remove last path component and the dir delimiter before and/or after it,
  94. // returns "" if there is only one path component.
  95. // removed: If non-NULL, receives the removed component(s).
  96. // count: Number of components to remove
  97. std::string RemoveLastPathComponent(const std::string &path,
  98. std::string *removed = NULL, int count = 1);
  99. // Remove "." and ".." path components and for every ".." removed, remove
  100. // the last normal path component before it. Unlike AbsolutePath,
  101. // this does not resolve symlinks and check for existence of directories.
  102. std::string RemoveRelativePathComponents(std::string path);
  103. // Returns the absolute path for the passed path, with "." and ".." path
  104. // components and symlinks removed. Returns "" on error.
  105. std::string AbsolutePath(const std::string &path);
  106. // Returns the filename from a path or the entire path if no directory
  107. // delimiter is found.
  108. const char *GetFilenameFromPath(const char *path);
  109. bool safeWriteToFile(const std::string &path, const std::string &content);
  110. #ifndef SERVER
  111. bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
  112. #endif
  113. bool ReadFile(const std::string &path, std::string &out);
  114. bool Rename(const std::string &from, const std::string &to);
  115. } // namespace fs