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