dir_access.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 : int32_t {
  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. bool _is_temp = false;
  56. bool _temp_keep_after_free = false;
  57. String _temp_path;
  58. void _delete_temp();
  59. static Ref<DirAccess> _create_temp(const String &p_prefix = "", bool p_keep = false);
  60. protected:
  61. static void _bind_methods();
  62. String _get_root_path() const;
  63. virtual String _get_root_string() const;
  64. AccessType get_access_type() const;
  65. virtual String fix_path(const String &p_path) const;
  66. template <typename T>
  67. static Ref<DirAccess> _create_builtin() {
  68. return memnew(T);
  69. }
  70. public:
  71. virtual Error list_dir_begin() = 0; ///< This starts dir listing
  72. virtual String get_next() = 0;
  73. virtual bool current_is_dir() const = 0;
  74. virtual bool current_is_hidden() const = 0;
  75. virtual void list_dir_end() = 0; ///<
  76. virtual int get_drive_count() = 0;
  77. virtual String get_drive(int p_drive) = 0;
  78. virtual int get_current_drive();
  79. virtual bool drives_are_shortcuts();
  80. virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
  81. virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
  82. virtual Error make_dir(String p_dir) = 0;
  83. virtual Error make_dir_recursive(const String &p_dir);
  84. virtual Error erase_contents_recursive(); //super dangerous, use with care!
  85. virtual bool file_exists(String p_file) = 0;
  86. virtual bool dir_exists(String p_dir) = 0;
  87. virtual bool is_readable(String p_dir) { return true; }
  88. virtual bool is_writable(String p_dir) { return true; }
  89. static bool exists(const String &p_dir);
  90. virtual uint64_t get_space_left() = 0;
  91. Error copy_dir(const String &p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false);
  92. virtual Error copy(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  93. virtual Error rename(String p_from, String p_to) = 0;
  94. virtual Error remove(String p_name) = 0;
  95. virtual bool is_link(String p_file) = 0;
  96. virtual String read_link(String p_file) = 0;
  97. virtual Error create_link(String p_source, String p_target) = 0;
  98. // Meant for editor code when we want to quickly remove a file without custom
  99. // handling (e.g. removing a cache file).
  100. static void remove_file_or_error(const String &p_path) {
  101. Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
  102. if (da->file_exists(p_path)) {
  103. if (da->remove(p_path) != OK) {
  104. ERR_FAIL_MSG(vformat("Cannot remove file or directory: '%s'.", p_path));
  105. }
  106. } else {
  107. ERR_FAIL_MSG(vformat("Cannot remove non-existent file or directory: '%s'.", p_path));
  108. }
  109. }
  110. virtual String get_filesystem_type() const = 0;
  111. static String get_full_path(const String &p_path, AccessType p_access);
  112. static Ref<DirAccess> create_for_path(const String &p_path);
  113. static Ref<DirAccess> create(AccessType p_access);
  114. static Error get_open_error();
  115. template <typename T>
  116. static void make_default(AccessType p_access) {
  117. create_func[p_access] = _create_builtin<T>;
  118. }
  119. static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
  120. static Ref<DirAccess> create_temp(const String &p_prefix = "", bool p_keep = false, Error *r_error = nullptr);
  121. static int _get_drive_count();
  122. static String get_drive_name(int p_idx);
  123. static Error make_dir_absolute(const String &p_dir);
  124. static Error make_dir_recursive_absolute(const String &p_dir);
  125. static bool dir_exists_absolute(const String &p_dir);
  126. static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  127. static Error rename_absolute(const String &p_from, const String &p_to);
  128. static Error remove_absolute(const String &p_path);
  129. PackedStringArray get_files();
  130. static PackedStringArray get_files_at(const String &p_path);
  131. PackedStringArray get_directories();
  132. static PackedStringArray get_directories_at(const String &p_path);
  133. String _get_next();
  134. void set_include_navigational(bool p_enable);
  135. bool get_include_navigational() const;
  136. void set_include_hidden(bool p_enable);
  137. bool get_include_hidden() const;
  138. virtual bool is_case_sensitive(const String &p_path) const;
  139. virtual bool is_bundle(const String &p_file) const { return false; }
  140. public:
  141. DirAccess() {}
  142. virtual ~DirAccess();
  143. };
  144. #endif // DIR_ACCESS_H