dir_access.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #pragma once
  31. #include "core/object/ref_counted.h"
  32. #include "core/string/ustring.h"
  33. #include "core/typedefs.h"
  34. //@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies
  35. class DirAccess : public RefCounted {
  36. GDCLASS(DirAccess, RefCounted);
  37. public:
  38. enum AccessType : int32_t {
  39. ACCESS_RESOURCES,
  40. ACCESS_USERDATA,
  41. ACCESS_FILESYSTEM,
  42. ACCESS_MAX
  43. };
  44. typedef Ref<DirAccess> (*CreateFunc)();
  45. private:
  46. AccessType _access_type = ACCESS_FILESYSTEM;
  47. static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object
  48. static Ref<DirAccess> _open(const String &p_path);
  49. Error _copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links);
  50. PackedStringArray _get_contents(bool p_directories);
  51. static inline thread_local Error last_dir_open_error = OK;
  52. bool include_navigational = false;
  53. bool include_hidden = false;
  54. bool _is_temp = false;
  55. bool _temp_keep_after_free = false;
  56. String _temp_path;
  57. void _delete_temp();
  58. static Ref<DirAccess> _create_temp(const String &p_prefix = "", bool p_keep = false);
  59. protected:
  60. static void _bind_methods();
  61. String _get_root_path() const;
  62. virtual String _get_root_string() const;
  63. AccessType get_access_type() const;
  64. virtual String fix_path(const String &p_path) const;
  65. template <typename T>
  66. static Ref<DirAccess> _create_builtin() {
  67. return memnew(T);
  68. }
  69. public:
  70. virtual Error list_dir_begin() = 0; ///< This starts dir listing
  71. virtual String get_next() = 0;
  72. virtual bool current_is_dir() const = 0;
  73. virtual bool current_is_hidden() const = 0;
  74. virtual void list_dir_end() = 0; ///<
  75. virtual int get_drive_count() = 0;
  76. virtual String get_drive(int p_drive) = 0;
  77. virtual int get_current_drive();
  78. virtual bool drives_are_shortcuts();
  79. virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
  80. virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
  81. virtual Error make_dir(String p_dir) = 0;
  82. virtual Error make_dir_recursive(const String &p_dir);
  83. virtual Error erase_contents_recursive(); //super dangerous, use with care!
  84. virtual bool file_exists(String p_file) = 0;
  85. virtual bool dir_exists(String p_dir) = 0;
  86. virtual bool is_readable(String p_dir) { return true; }
  87. virtual bool is_writable(String p_dir) { return true; }
  88. static bool exists(const String &p_dir);
  89. virtual uint64_t get_space_left() = 0;
  90. Error copy_dir(const String &p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false);
  91. virtual Error copy(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  92. virtual Error rename(String p_from, String p_to) = 0;
  93. virtual Error remove(String p_name) = 0;
  94. virtual bool is_link(String p_file) = 0;
  95. virtual String read_link(String p_file) = 0;
  96. virtual Error create_link(String p_source, String p_target) = 0;
  97. // Meant for editor code when we want to quickly remove a file without custom
  98. // handling (e.g. removing a cache file).
  99. static void remove_file_or_error(const String &p_path) {
  100. Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
  101. if (da->file_exists(p_path)) {
  102. if (da->remove(p_path) != OK) {
  103. ERR_FAIL_MSG(vformat("Cannot remove file or directory: '%s'.", p_path));
  104. }
  105. } else {
  106. ERR_FAIL_MSG(vformat("Cannot remove non-existent file or directory: '%s'.", p_path));
  107. }
  108. }
  109. virtual String get_filesystem_type() const = 0;
  110. static String get_full_path(const String &p_path, AccessType p_access);
  111. static Ref<DirAccess> create_for_path(const String &p_path);
  112. static Ref<DirAccess> create(AccessType p_access);
  113. static Error get_open_error();
  114. template <typename T>
  115. static void make_default(AccessType p_access) {
  116. create_func[p_access] = _create_builtin<T>;
  117. }
  118. static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
  119. static Ref<DirAccess> create_temp(const String &p_prefix = "", bool p_keep = false, Error *r_error = nullptr);
  120. static int _get_drive_count();
  121. static String get_drive_name(int p_idx);
  122. static Error make_dir_absolute(const String &p_dir);
  123. static Error make_dir_recursive_absolute(const String &p_dir);
  124. static bool dir_exists_absolute(const String &p_dir);
  125. static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1);
  126. static Error rename_absolute(const String &p_from, const String &p_to);
  127. static Error remove_absolute(const String &p_path);
  128. PackedStringArray get_files();
  129. static PackedStringArray get_files_at(const String &p_path);
  130. PackedStringArray get_directories();
  131. static PackedStringArray get_directories_at(const String &p_path);
  132. String _get_next();
  133. void set_include_navigational(bool p_enable);
  134. bool get_include_navigational() const;
  135. void set_include_hidden(bool p_enable);
  136. bool get_include_hidden() const;
  137. virtual bool is_case_sensitive(const String &p_path) const;
  138. virtual bool is_bundle(const String &p_file) const { return false; }
  139. virtual bool is_equivalent(const String &p_path_a, const String &p_path_b) const;
  140. public:
  141. DirAccess() {}
  142. virtual ~DirAccess();
  143. };