dir_access.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************************************/
  2. /* dir_access.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. Error _copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod_flags, bool p_copy_links);
  50. thread_local static Error last_dir_open_error;
  51. bool include_navigational = false;
  52. bool include_hidden = false;
  53. protected:
  54. static void _bind_methods();
  55. String _get_root_path() const;
  56. virtual String _get_root_string() const;
  57. AccessType get_access_type() const;
  58. String fix_path(String p_path) const;
  59. template <class T>
  60. static Ref<DirAccess> _create_builtin() {
  61. return memnew(T);
  62. }
  63. public:
  64. virtual Error list_dir_begin() = 0; ///< This starts dir listing
  65. virtual String get_next() = 0;
  66. virtual bool current_is_dir() const = 0;
  67. virtual bool current_is_hidden() const = 0;
  68. virtual void list_dir_end() = 0; ///<
  69. virtual int get_drive_count() = 0;
  70. virtual String get_drive(int p_drive) = 0;
  71. virtual int get_current_drive();
  72. virtual bool drives_are_shortcuts();
  73. virtual Error change_dir(String p_dir) = 0; ///< can be relative or absolute, return false on success
  74. virtual String get_current_dir(bool p_include_drive = true) const = 0; ///< return current dir location
  75. virtual Error make_dir(String p_dir) = 0;
  76. virtual Error make_dir_recursive(String p_dir);
  77. virtual Error erase_contents_recursive(); //super dangerous, use with care!
  78. virtual bool file_exists(String p_file) = 0;
  79. virtual bool dir_exists(String p_dir) = 0;
  80. virtual bool is_readable(String p_dir) { return true; };
  81. virtual bool is_writable(String p_dir) { return true; };
  82. static bool exists(String p_dir);
  83. virtual uint64_t get_space_left() = 0;
  84. Error copy_dir(String p_from, String p_to, int p_chmod_flags = -1, bool p_copy_links = false);
  85. virtual Error copy(String p_from, String p_to, int p_chmod_flags = -1);
  86. virtual Error rename(String p_from, String p_to) = 0;
  87. virtual Error remove(String p_name) = 0;
  88. virtual bool is_link(String p_file) = 0;
  89. virtual String read_link(String p_file) = 0;
  90. virtual Error create_link(String p_source, String p_target) = 0;
  91. // Meant for editor code when we want to quickly remove a file without custom
  92. // handling (e.g. removing a cache file).
  93. static void remove_file_or_error(String p_path) {
  94. Ref<DirAccess> da = create(ACCESS_FILESYSTEM);
  95. if (da->file_exists(p_path)) {
  96. if (da->remove(p_path) != OK) {
  97. ERR_FAIL_MSG("Cannot remove file or directory: " + p_path);
  98. }
  99. } else {
  100. ERR_FAIL_MSG("Cannot remove non-existent file or directory: " + p_path);
  101. }
  102. }
  103. virtual String get_filesystem_type() const = 0;
  104. static String get_full_path(const String &p_path, AccessType p_access);
  105. static Ref<DirAccess> create_for_path(const String &p_path);
  106. static Ref<DirAccess> create(AccessType p_access);
  107. static Error get_open_error();
  108. template <class T>
  109. static void make_default(AccessType p_access) {
  110. create_func[p_access] = _create_builtin<T>;
  111. }
  112. static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr);
  113. static Ref<DirAccess> _open(const String &p_path);
  114. PackedStringArray get_files();
  115. PackedStringArray get_directories();
  116. PackedStringArray _get_contents(bool p_directories);
  117. String _get_next();
  118. void set_include_navigational(bool p_enable);
  119. bool get_include_navigational() const;
  120. void set_include_hidden(bool p_enable);
  121. bool get_include_hidden() const;
  122. DirAccess() {}
  123. virtual ~DirAccess() {}
  124. };
  125. #endif // DIR_ACCESS_H