filesys.h 4.9 KB

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