dir_access.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**************************************************************************/
  2. /* dir_access.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef DIR_ACCESS_H
  31. #define DIR_ACCESS_H
  32. #include "core/object/ref_counted.h"
  33. #include "core/string/ustring.h"
  34. #include "core/typedefs.h"
  35. //@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
  36. class DirAccess : public RefCounted {
  37. GDCLASS(DirAccess, RefCounted);
  38. public:
  39. enum AccessType {
  40. ACCESS_RESOURCES,
  41. ACCESS_USERDATA,
  42. ACCESS_FILESYSTEM,
  43. ACCESS_MAX
  44. };
  45. typedef Ref<DirAccess> (*CreateFunc)();
  46. private:
  47. AccessType _access_type = ACCESS_FILESYSTEM;
  48. static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
  49. static Ref<DirAccess> _open(const String &p_path);
  50. Error _copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links);
  51. PackedStringArray _get_contents(bool p_directories);
  52. thread_local static Error last_dir_open_error;
  53. bool include_navigational = false;
  54. bool include_hidden = false;
  55. protected:
  56. static void _bind_methods();
  57. String _get_root_path() const;
  58. virtual String _get_root_string() const;
  59. AccessType get_access_type() const;
  60. virtual String fix_path(const String &p_path) const;
  61. template <typename T>
  62. static Ref<DirAccess> _create_builtin() {
  63. return memnew(T);
  64. }
  65. public:
  66. virtual Error list_dir_begin() = 0; ///< This starts dir listing
  67. virtual String get_next() = 0;
  68. virtual bool current_is_dir() const = 0;
  69. virtual bool current_is_hidden() const = 0;
  70. virtual void list_dir_end() = 0; ///<
  71. virtual int get_drive_count() = 0;
  72. virtual String get_drive(int p_drive) = 0;
  73. virtual int get_current_drive();
  74. virtual bool drives_are_shortcuts();
  75. virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
  76. virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
  77. virtual Error make_dir(String p_dir) = 0;
  78. virtual Error make_dir_recursive(const String &p_dir);
  79. virtual Error erase_contents_recursive(); //super dangerous, use with care!
  80. virtual bool file_exists(String p_file) = 0;
  81. virtual bool dir_exists(String p_dir) = 0;
  82. virtual bool is_readable(String p_dir) { return true; }
  83. virtual bool is_writable(String p_dir) { return true; }
  84. static bool exists(const String &p_dir);
  85. virtual uint64_t get_space_left() = 0;
  86. Error copy_dir(const String &p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false);
  87. virtual Error copy(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  88. virtual Error rename(String p_from, String p_to) = 0;
  89. virtual Error remove(String p_name) = 0;
  90. virtual bool is_link(String p_file) = 0;
  91. virtual String read_link(String p_file) = 0;
  92. virtual Error create_link(String p_source, String p_target) = 0;
  93. // Meant for editor code when we want to quickly remove a file without custom
  94. // handling (e.g. removing a cache file).
  95. static void remove_file_or_error(const String &p_path) {
  96. Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
  97. if (da->file_exists(p_path)) {
  98. if (da->remove(p_path) != OK) {
  99. ERR_FAIL_MSG(vformat("Cannot remove file or directory: '%s'.", p_path));
  100. }
  101. } else {
  102. ERR_FAIL_MSG(vformat("Cannot remove non-existent file or directory: '%s'.", p_path));
  103. }
  104. }
  105. virtual String get_filesystem_type() const = 0;
  106. static String get_full_path(const String &p_path, AccessType p_access);
  107. static Ref<DirAccess> create_for_path(const String &p_path);
  108. static Ref<DirAccess> create(AccessType p_access);
  109. static Error get_open_error();
  110. template <typename T>
  111. static void make_default(AccessType p_access) {
  112. create_func[p_access] = _create_builtin<T>;
  113. }
  114. static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
  115. static int _get_drive_count();
  116. static String get_drive_name(int p_idx);
  117. static Error make_dir_absolute(const String &p_dir);
  118. static Error make_dir_recursive_absolute(const String &p_dir);
  119. static bool dir_exists_absolute(const String &p_dir);
  120. static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  121. static Error rename_absolute(const String &p_from, const String &p_to);
  122. static Error remove_absolute(const String &p_path);
  123. PackedStringArray get_files();
  124. static PackedStringArray get_files_at(const String &p_path);
  125. PackedStringArray get_directories();
  126. static PackedStringArray get_directories_at(const String &p_path);
  127. String _get_next();
  128. void set_include_navigational(bool p_enable);
  129. bool get_include_navigational() const;
  130. void set_include_hidden(bool p_enable);
  131. bool get_include_hidden() const;
  132. virtual bool is_case_sensitive(const String &p_path) const;
  133. DirAccess() {}
  134. virtual ~DirAccess() {}
  135. };
  136. #endif // DIR_ACCESS_H