| 1 | /**************************************************************************/ |
| 2 | /* dir_access.cpp */ |
| 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 | #include "dir_access.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "core/os/os.h" |
| 36 | #include "core/os/time.h" |
| 37 | #include "core/templates/local_vector.h" |
| 38 | |
| 39 | String DirAccess::_get_root_path() const { |
| 40 | switch (_access_type) { |
| 41 | case ACCESS_RESOURCES: |
| 42 | return ProjectSettings::get_singleton()->get_resource_path(); |
| 43 | case ACCESS_USERDATA: |
| 44 | return OS::get_singleton()->get_user_data_dir(); |
| 45 | default: |
| 46 | return ""; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | String DirAccess::_get_root_string() const { |
| 51 | switch (_access_type) { |
| 52 | case ACCESS_RESOURCES: |
| 53 | return "res://"; |
| 54 | case ACCESS_USERDATA: |
| 55 | return "user://"; |
| 56 | default: |
| 57 | return ""; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int DirAccess::get_current_drive() { |
| 62 | String path = get_current_dir().to_lower(); |
| 63 | for (int i = 0; i < get_drive_count(); i++) { |
| 64 | String d = get_drive(i).to_lower(); |
| 65 | if (path.begins_with(d)) { |
| 66 | return i; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | bool DirAccess::drives_are_shortcuts() { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | static Error _erase_recursive(DirAccess *da) { |
| 78 | List<String> dirs; |
| 79 | List<String> files; |
| 80 | |
| 81 | da->list_dir_begin(); |
| 82 | String n = da->get_next(); |
| 83 | while (!n.is_empty()) { |
| 84 | if (n != "." && n != "..") { |
| 85 | if (da->current_is_dir() && !da->is_link(n)) { |
| 86 | dirs.push_back(n); |
| 87 | } else { |
| 88 | files.push_back(n); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | n = da->get_next(); |
| 93 | } |
| 94 | |
| 95 | da->list_dir_end(); |
| 96 | |
| 97 | for (const String &E : dirs) { |
| 98 | Error err = da->change_dir(E); |
| 99 | if (err == OK) { |
| 100 | err = _erase_recursive(da); |
| 101 | if (err) { |
| 102 | da->change_dir(".."); |
| 103 | return err; |
| 104 | } |
| 105 | err = da->change_dir(".."); |
| 106 | if (err) { |
| 107 | return err; |
| 108 | } |
| 109 | err = da->remove(da->get_current_dir().path_join(E)); |
| 110 | if (err) { |
| 111 | return err; |
| 112 | } |
| 113 | } else { |
| 114 | return err; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (const String &E : files) { |
| 119 | Error err = da->remove(da->get_current_dir().path_join(E)); |
| 120 | if (err) { |
| 121 | return err; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return OK; |
| 126 | } |
| 127 | |
| 128 | Error DirAccess::erase_contents_recursive() { |
| 129 | return _erase_recursive(this); |
| 130 | } |
| 131 | |
| 132 | Error DirAccess::make_dir_recursive(const String &p_dir) { |
| 133 | if (p_dir.length() < 1) { |
| 134 | return OK; |
| 135 | } |
| 136 | |
| 137 | String full_dir; |
| 138 | |
| 139 | if (p_dir.is_relative_path()) { |
| 140 | //append current |
| 141 | full_dir = get_current_dir().path_join(p_dir); |
| 142 | |
| 143 | } else { |
| 144 | full_dir = p_dir; |
| 145 | } |
| 146 | |
| 147 | full_dir = full_dir.replace_char('\\', '/'); |
| 148 | |
| 149 | String base; |
| 150 | |
| 151 | if (full_dir.begins_with("res://")) { |
| 152 | base = "res://"; |
| 153 | } else if (full_dir.begins_with("user://")) { |
| 154 | base = "user://"; |
| 155 | } else if (full_dir.is_network_share_path()) { |
| 156 | int pos = full_dir.find_char('/', 2); |
| 157 | ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER); |
| 158 | pos = full_dir.find_char('/', pos + 1); |
| 159 | ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER); |
| 160 | base = full_dir.substr(0, pos + 1); |
| 161 | } else if (full_dir.begins_with("/")) { |
| 162 | base = "/"; |
| 163 | } else if (full_dir.contains(":/")) { |
| 164 | base = full_dir.substr(0, full_dir.find(":/") + 2); |
| 165 | } else { |
| 166 | ERR_FAIL_V(ERR_INVALID_PARAMETER); |
| 167 | } |
| 168 | |
| 169 | full_dir = full_dir.replace_first(base, "").simplify_path(); |
| 170 | |
| 171 | Vector<String> subdirs = full_dir.split("/"); |
| 172 | |
| 173 | String curpath = base; |
| 174 | for (int i = 0; i < subdirs.size(); i++) { |
| 175 | curpath = curpath.path_join(subdirs[i]); |
| 176 | Error err = make_dir(curpath); |
| 177 | if (err != OK && err != ERR_ALREADY_EXISTS) { |
| 178 | ERR_FAIL_V_MSG(err, vformat("Could not create directory: '%s'.", curpath)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return OK; |
| 183 | } |
| 184 | |
| 185 | DirAccess::AccessType DirAccess::get_access_type() const { |
| 186 | return _access_type; |
| 187 | } |
| 188 | |
| 189 | String DirAccess::fix_path(const String &p_path) const { |
| 190 | switch (_access_type) { |
| 191 | case ACCESS_RESOURCES: { |
| 192 | if (ProjectSettings::get_singleton()) { |
| 193 | if (p_path.begins_with("res://")) { |
| 194 | String resource_path = ProjectSettings::get_singleton()->get_resource_path(); |
| 195 | if (!resource_path.is_empty()) { |
| 196 | return p_path.replace_first("res:/", resource_path); |
| 197 | } |
| 198 | return p_path.replace_first("res://", ""); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | } break; |
| 203 | case ACCESS_USERDATA: { |
| 204 | if (p_path.begins_with("user://")) { |
| 205 | String data_dir = OS::get_singleton()->get_user_data_dir(); |
| 206 | if (!data_dir.is_empty()) { |
| 207 | return p_path.replace_first("user:/", data_dir); |
| 208 | } |
| 209 | return p_path.replace_first("user://", ""); |
| 210 | } |
| 211 | |
| 212 | } break; |
| 213 | case ACCESS_FILESYSTEM: { |
| 214 | return p_path; |
| 215 | } break; |
| 216 | case ACCESS_MAX: |
| 217 | break; // Can't happen, but silences warning |
| 218 | } |
| 219 | |
| 220 | return p_path; |
| 221 | } |
| 222 | |
| 223 | DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { nullptr, nullptr, nullptr }; |
| 224 | |
| 225 | Ref<DirAccess> DirAccess::create_for_path(const String &p_path) { |
| 226 | Ref<DirAccess> da; |
| 227 | if (p_path.begins_with("res://")) { |
| 228 | da = create(ACCESS_RESOURCES); |
| 229 | } else if (p_path.begins_with("user://")) { |
| 230 | da = create(ACCESS_USERDATA); |
| 231 | } else { |
| 232 | da = create(ACCESS_FILESYSTEM); |
| 233 | } |
| 234 | |
| 235 | return da; |
| 236 | } |
| 237 | |
| 238 | Ref<DirAccess> DirAccess::open(const String &p_path, Error *r_error) { |
| 239 | Ref<DirAccess> da = create_for_path(p_path); |
| 240 | ERR_FAIL_COND_V_MSG(da.is_null(), nullptr, vformat("Cannot create DirAccess for path '%s'.", p_path)); |
| 241 | Error err = da->change_dir(p_path); |
| 242 | if (r_error) { |
| 243 | *r_error = err; |
| 244 | } |
| 245 | if (err != OK) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | |
| 249 | return da; |
| 250 | } |
| 251 | |
| 252 | Ref<DirAccess> DirAccess::_open(const String &p_path) { |
| 253 | Error err = OK; |
| 254 | Ref<DirAccess> da = open(p_path, &err); |
| 255 | last_dir_open_error = err; |
| 256 | if (err) { |
| 257 | return Ref<DirAccess>(); |
| 258 | } |
| 259 | return da; |
| 260 | } |
| 261 | |
| 262 | int DirAccess::_get_drive_count() { |
| 263 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 264 | return d->get_drive_count(); |
| 265 | } |
| 266 | |
| 267 | String DirAccess::get_drive_name(int p_idx) { |
| 268 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 269 | return d->get_drive(p_idx); |
| 270 | } |
| 271 | |
| 272 | Error DirAccess::make_dir_absolute(const String &p_dir) { |
| 273 | Ref<DirAccess> d = DirAccess::create_for_path(p_dir); |
| 274 | return d->make_dir(p_dir); |
| 275 | } |
| 276 | |
| 277 | Error DirAccess::make_dir_recursive_absolute(const String &p_dir) { |
| 278 | Ref<DirAccess> d = DirAccess::create_for_path(p_dir); |
| 279 | return d->make_dir_recursive(p_dir); |
| 280 | } |
| 281 | |
| 282 | bool DirAccess::dir_exists_absolute(const String &p_dir) { |
| 283 | Ref<DirAccess> d = DirAccess::create_for_path(p_dir); |
| 284 | return d->dir_exists(p_dir); |
| 285 | } |
| 286 | |
| 287 | Error DirAccess::copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags) { |
| 288 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 289 | // Support copying from res:// to user:// etc. |
| 290 | String from = ProjectSettings::get_singleton()->globalize_path(p_from); |
| 291 | String to = ProjectSettings::get_singleton()->globalize_path(p_to); |
| 292 | return d->copy(from, to, p_chmod_flags); |
| 293 | } |
| 294 | |
| 295 | Error DirAccess::rename_absolute(const String &p_from, const String &p_to) { |
| 296 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 297 | String from = ProjectSettings::get_singleton()->globalize_path(p_from); |
| 298 | String to = ProjectSettings::get_singleton()->globalize_path(p_to); |
| 299 | return d->rename(from, to); |
| 300 | } |
| 301 | |
| 302 | Error DirAccess::remove_absolute(const String &p_path) { |
| 303 | Ref<DirAccess> d = DirAccess::create_for_path(p_path); |
| 304 | return d->remove(p_path); |
| 305 | } |
| 306 | |
| 307 | Ref<DirAccess> DirAccess::create(AccessType p_access) { |
| 308 | Ref<DirAccess> da = create_func[p_access] ? create_func[p_access]() : nullptr; |
| 309 | if (da.is_valid()) { |
| 310 | da->_access_type = p_access; |
| 311 | |
| 312 | // for ACCESS_RESOURCES and ACCESS_FILESYSTEM, current_dir already defaults to where game was started |
| 313 | // in case current directory is force changed elsewhere for ACCESS_RESOURCES |
| 314 | if (p_access == ACCESS_RESOURCES) { |
| 315 | da->change_dir("res://"); |
| 316 | } else if (p_access == ACCESS_USERDATA) { |
| 317 | da->change_dir("user://"); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return da; |
| 322 | } |
| 323 | |
| 324 | Ref<DirAccess> DirAccess::create_temp(const String &p_prefix, bool p_keep, Error *r_error) { |
| 325 | const String ERROR_COMMON_PREFIX = "Error while creating temporary directory"; |
| 326 | |
| 327 | if (!p_prefix.is_valid_filename()) { |
| 328 | *r_error = ERR_FILE_BAD_PATH; |
| 329 | ERR_FAIL_V_MSG(Ref<DirAccess>(), vformat(R"(%s: "%s" is not a valid prefix.)", ERROR_COMMON_PREFIX, p_prefix)); |
| 330 | } |
| 331 | |
| 332 | Ref<DirAccess> dir_access = DirAccess::open(OS::get_singleton()->get_temp_path()); |
| 333 | |
| 334 | uint32_t suffix_i = 0; |
| 335 | String path; |
| 336 | while (true) { |
| 337 | String datetime = Time::get_singleton()->get_datetime_string_from_system().remove_chars("-T:"); |
| 338 | datetime += itos(Time::get_singleton()->get_ticks_usec()); |
| 339 | String suffix = datetime + (suffix_i > 0 ? itos(suffix_i) : ""); |
| 340 | path = (p_prefix.is_empty() ? "" : p_prefix + "-") + suffix; |
| 341 | if (!path.is_valid_filename()) { |
| 342 | *r_error = ERR_FILE_BAD_PATH; |
| 343 | return Ref<DirAccess>(); |
| 344 | } |
| 345 | if (!DirAccess::exists(path)) { |
| 346 | break; |
| 347 | } |
| 348 | suffix_i += 1; |
| 349 | } |
| 350 | |
| 351 | Error err = dir_access->make_dir(path); |
| 352 | if (err != OK) { |
| 353 | *r_error = err; |
| 354 | ERR_FAIL_V_MSG(Ref<DirAccess>(), vformat(R"(%s: "%s" couldn't create directory "%s".)", ERROR_COMMON_PREFIX, path)); |
| 355 | } |
| 356 | err = dir_access->change_dir(path); |
| 357 | if (err != OK) { |
| 358 | *r_error = err; |
| 359 | return Ref<DirAccess>(); |
| 360 | } |
| 361 | |
| 362 | dir_access->_is_temp = true; |
| 363 | dir_access->_temp_keep_after_free = p_keep; |
| 364 | dir_access->_temp_path = dir_access->get_current_dir(); |
| 365 | |
| 366 | *r_error = OK; |
| 367 | return dir_access; |
| 368 | } |
| 369 | |
| 370 | Ref<DirAccess> DirAccess::_create_temp(const String &p_prefix, bool p_keep) { |
| 371 | return create_temp(p_prefix, p_keep, &last_dir_open_error); |
| 372 | } |
| 373 | |
| 374 | void DirAccess::_delete_temp() { |
| 375 | if (!_is_temp || _temp_keep_after_free) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | if (!DirAccess::exists(_temp_path)) { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | Error err; |
| 384 | { |
| 385 | Ref<DirAccess> dir_access = DirAccess::open(_temp_path, &err); |
| 386 | if (err != OK) { |
| 387 | return; |
| 388 | } |
| 389 | err = dir_access->erase_contents_recursive(); |
| 390 | if (err != OK) { |
| 391 | return; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | DirAccess::remove_absolute(_temp_path); |
| 396 | } |
| 397 | |
| 398 | Error DirAccess::get_open_error() { |
| 399 | return last_dir_open_error; |
| 400 | } |
| 401 | |
| 402 | String DirAccess::get_full_path(const String &p_path, AccessType p_access) { |
| 403 | Ref<DirAccess> d = DirAccess::create(p_access); |
| 404 | if (d.is_null()) { |
| 405 | return p_path; |
| 406 | } |
| 407 | |
| 408 | d->change_dir(p_path); |
| 409 | String full = d->get_current_dir(); |
| 410 | return full; |
| 411 | } |
| 412 | |
| 413 | Error DirAccess::copy(const String &p_from, const String &p_to, int p_chmod_flags) { |
| 414 | ERR_FAIL_COND_V_MSG(p_from == p_to, ERR_INVALID_PARAMETER, "Source and destination path are equal."); |
| 415 | |
| 416 | //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data()); |
| 417 | Error err; |
| 418 | { |
| 419 | Ref<FileAccess> fsrc = FileAccess::open(p_from, FileAccess::READ, &err); |
| 420 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to open '%s'.", p_from)); |
| 421 | |
| 422 | Ref<FileAccess> fdst = FileAccess::open(p_to, FileAccess::WRITE, &err); |
| 423 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to open '%s'.", p_to)); |
| 424 | |
| 425 | const size_t copy_buffer_limit = 65536; // 64 KB |
| 426 | |
| 427 | fsrc->seek_end(0); |
| 428 | uint64_t size = fsrc->get_position(); |
| 429 | fsrc->seek(0); |
| 430 | err = OK; |
| 431 | size_t buffer_size = MIN(size * sizeof(uint8_t), copy_buffer_limit); |
| 432 | LocalVector<uint8_t> buffer; |
| 433 | buffer.resize(buffer_size); |
| 434 | while (size > 0) { |
| 435 | if (fsrc->get_error() != OK) { |
| 436 | err = fsrc->get_error(); |
| 437 | break; |
| 438 | } |
| 439 | if (fdst->get_error() != OK) { |
| 440 | err = fdst->get_error(); |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | int bytes_read = fsrc->get_buffer(buffer.ptr(), buffer_size); |
| 445 | if (bytes_read <= 0) { |
| 446 | err = FAILED; |
| 447 | break; |
| 448 | } |
| 449 | fdst->store_buffer(buffer.ptr(), bytes_read); |
| 450 | |
| 451 | size -= bytes_read; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (err == OK && p_chmod_flags != -1) { |
| 456 | err = FileAccess::set_unix_permissions(p_to, p_chmod_flags); |
| 457 | // If running on a platform with no chmod support (i.e., Windows), don't fail |
| 458 | if (err == ERR_UNAVAILABLE) { |
| 459 | err = OK; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return err; |
| 464 | } |
| 465 | |
| 466 | // Changes dir for the current scope, returning back to the original dir |
| 467 | // when scope exits |
| 468 | class DirChanger { |
| 469 | DirAccess *da; |
| 470 | String original_dir; |
| 471 | |
| 472 | public: |
| 473 | DirChanger(DirAccess *p_da, const String &p_dir) : |
| 474 | da(p_da), |
| 475 | original_dir(p_da->get_current_dir()) { |
| 476 | p_da->change_dir(p_dir); |
| 477 | } |
| 478 | |
| 479 | ~DirChanger() { |
| 480 | da->change_dir(original_dir); |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | Error DirAccess::_copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links) { |
| 485 | List<String> dirs; |
| 486 | |
| 487 | String curdir = get_current_dir(); |
| 488 | list_dir_begin(); |
| 489 | String n = get_next(); |
| 490 | while (!n.is_empty()) { |
| 491 | if (n != "." && n != "..") { |
| 492 | if (p_copy_links && is_link(get_current_dir().path_join(n))) { |
| 493 | Error err = p_target_da->create_link(read_link(get_current_dir().path_join(n)), p_to + n); |
| 494 | if (err) { |
| 495 | ERR_PRINT(vformat("Failed to copy symlink \"%s\".", n)); |
| 496 | } |
| 497 | } else if (current_is_dir()) { |
| 498 | dirs.push_back(n); |
| 499 | } else { |
| 500 | const String &rel_path = n; |
| 501 | if (!n.is_relative_path()) { |
| 502 | list_dir_end(); |
| 503 | ERR_FAIL_V_MSG(ERR_BUG, vformat("BUG: \"%s\" is not a relative path.", n)); |
| 504 | } |
| 505 | Error err = copy(get_current_dir().path_join(n), p_to + rel_path, p_chmod_flags); |
| 506 | if (err) { |
| 507 | list_dir_end(); |
| 508 | ERR_FAIL_V_MSG(err, vformat("Failed to copy file \"%s\".", n)); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | n = get_next(); |
| 514 | } |
| 515 | |
| 516 | list_dir_end(); |
| 517 | |
| 518 | for (const String &rel_path : dirs) { |
| 519 | String target_dir = p_to + rel_path; |
| 520 | if (!p_target_da->dir_exists(target_dir)) { |
| 521 | Error err = p_target_da->make_dir(target_dir); |
| 522 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot create directory '%s'.", target_dir)); |
| 523 | } |
| 524 | |
| 525 | Error err = change_dir(rel_path); |
| 526 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot change current directory to '%s'.", rel_path)); |
| 527 | |
| 528 | err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags, p_copy_links); |
| 529 | if (err) { |
| 530 | change_dir(".."); |
| 531 | ERR_FAIL_V_MSG(err, "Failed to copy recursively."); |
| 532 | } |
| 533 | err = change_dir(".."); |
| 534 | ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to go back."); |
| 535 | } |
| 536 | |
| 537 | return OK; |
| 538 | } |
| 539 | |
| 540 | Error DirAccess::copy_dir(const String &p_from, String p_to, int p_chmod_flags, bool p_copy_links) { |
| 541 | ERR_FAIL_COND_V_MSG(!dir_exists(p_from), ERR_FILE_NOT_FOUND, "Source directory doesn't exist."); |
| 542 | |
| 543 | Ref<DirAccess> target_da = DirAccess::create_for_path(p_to); |
| 544 | ERR_FAIL_COND_V_MSG(target_da.is_null(), ERR_CANT_CREATE, vformat("Cannot create DirAccess for path '%s'.", p_to)); |
| 545 | |
| 546 | if (!target_da->dir_exists(p_to)) { |
| 547 | Error err = target_da->make_dir_recursive(p_to); |
| 548 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot create directory '%s'.", p_to)); |
| 549 | } |
| 550 | |
| 551 | if (!p_to.ends_with("/")) { |
| 552 | p_to = p_to + "/"; |
| 553 | } |
| 554 | |
| 555 | DirChanger dir_changer(this, p_from); |
| 556 | Error err = _copy_dir(target_da, p_to, p_chmod_flags, p_copy_links); |
| 557 | |
| 558 | return err; |
| 559 | } |
| 560 | |
| 561 | bool DirAccess::exists(const String &p_dir) { |
| 562 | Ref<DirAccess> da = DirAccess::create_for_path(p_dir); |
| 563 | return da->change_dir(p_dir) == OK; |
| 564 | } |
| 565 | |
| 566 | PackedStringArray DirAccess::get_files() { |
| 567 | return _get_contents(false); |
| 568 | } |
| 569 | |
| 570 | PackedStringArray DirAccess::get_files_at(const String &p_path) { |
| 571 | Ref<DirAccess> da = DirAccess::open(p_path); |
| 572 | ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); |
| 573 | return da->get_files(); |
| 574 | } |
| 575 | |
| 576 | PackedStringArray DirAccess::get_directories() { |
| 577 | return _get_contents(true); |
| 578 | } |
| 579 | |
| 580 | PackedStringArray DirAccess::get_directories_at(const String &p_path) { |
| 581 | Ref<DirAccess> da = DirAccess::open(p_path); |
| 582 | ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); |
| 583 | return da->get_directories(); |
| 584 | } |
| 585 | |
| 586 | PackedStringArray DirAccess::_get_contents(bool p_directories) { |
| 587 | PackedStringArray ret; |
| 588 | |
| 589 | list_dir_begin(); |
| 590 | String s = _get_next(); |
| 591 | while (!s.is_empty()) { |
| 592 | if (current_is_dir() == p_directories) { |
| 593 | ret.append(s); |
| 594 | } |
| 595 | s = _get_next(); |
| 596 | } |
| 597 | |
| 598 | ret.sort(); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | String DirAccess::_get_next() { |
| 603 | String next = get_next(); |
| 604 | while (!next.is_empty() && ((!include_navigational && (next == "." || next == "..")) || (!include_hidden && current_is_hidden()))) { |
| 605 | next = get_next(); |
| 606 | } |
| 607 | return next; |
| 608 | } |
| 609 | |
| 610 | void DirAccess::set_include_navigational(bool p_enable) { |
| 611 | include_navigational = p_enable; |
| 612 | } |
| 613 | |
| 614 | bool DirAccess::get_include_navigational() const { |
| 615 | return include_navigational; |
| 616 | } |
| 617 | |
| 618 | void DirAccess::set_include_hidden(bool p_enable) { |
| 619 | include_hidden = p_enable; |
| 620 | } |
| 621 | |
| 622 | bool DirAccess::get_include_hidden() const { |
| 623 | return include_hidden; |
| 624 | } |
| 625 | |
| 626 | bool DirAccess::is_case_sensitive(const String &p_path) const { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | bool DirAccess::is_equivalent(const String &p_path_a, const String &p_path_b) const { |
| 631 | return p_path_a == p_path_b; |
| 632 | } |
| 633 | |
| 634 | void DirAccess::_bind_methods() { |
| 635 | ClassDB::bind_static_method("DirAccess", D_METHOD("open", "path"), &DirAccess::_open); |
| 636 | ClassDB::bind_static_method("DirAccess", D_METHOD("get_open_error"), &DirAccess::get_open_error); |
| 637 | ClassDB::bind_static_method("DirAccess", D_METHOD("create_temp", "prefix", "keep"), &DirAccess::_create_temp, DEFVAL(""), DEFVAL(false)); |
| 638 | |
| 639 | ClassDB::bind_method(D_METHOD("list_dir_begin"), &DirAccess::list_dir_begin); |
| 640 | ClassDB::bind_method(D_METHOD("get_next"), &DirAccess::_get_next); |
| 641 | ClassDB::bind_method(D_METHOD("current_is_dir"), &DirAccess::current_is_dir); |
| 642 | ClassDB::bind_method(D_METHOD("list_dir_end"), &DirAccess::list_dir_end); |
| 643 | ClassDB::bind_method(D_METHOD("get_files"), &DirAccess::get_files); |
| 644 | ClassDB::bind_static_method("DirAccess", D_METHOD("get_files_at", "path"), &DirAccess::get_files_at); |
| 645 | ClassDB::bind_method(D_METHOD("get_directories"), &DirAccess::get_directories); |
| 646 | ClassDB::bind_static_method("DirAccess", D_METHOD("get_directories_at", "path"), &DirAccess::get_directories_at); |
| 647 | ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_count"), &DirAccess::_get_drive_count); |
| 648 | ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_name", "idx"), &DirAccess::get_drive_name); |
| 649 | ClassDB::bind_method(D_METHOD("get_current_drive"), &DirAccess::get_current_drive); |
| 650 | ClassDB::bind_method(D_METHOD("change_dir", "to_dir"), &DirAccess::change_dir); |
| 651 | ClassDB::bind_method(D_METHOD("get_current_dir", "include_drive"), &DirAccess::get_current_dir, DEFVAL(true)); |
| 652 | ClassDB::bind_method(D_METHOD("make_dir", "path"), &DirAccess::make_dir); |
| 653 | ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_absolute", "path"), &DirAccess::make_dir_absolute); |
| 654 | ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &DirAccess::make_dir_recursive); |
| 655 | ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_recursive_absolute", "path"), &DirAccess::make_dir_recursive_absolute); |
| 656 | ClassDB::bind_method(D_METHOD("file_exists", "path"), &DirAccess::file_exists); |
| 657 | ClassDB::bind_method(D_METHOD("dir_exists", "path"), &DirAccess::dir_exists); |
| 658 | ClassDB::bind_static_method("DirAccess", D_METHOD("dir_exists_absolute", "path"), &DirAccess::dir_exists_absolute); |
| 659 | ClassDB::bind_method(D_METHOD("get_space_left"), &DirAccess::get_space_left); |
| 660 | ClassDB::bind_method(D_METHOD("copy", "from", "to", "chmod_flags"), &DirAccess::copy, DEFVAL(-1)); |
| 661 | ClassDB::bind_static_method("DirAccess", D_METHOD("copy_absolute", "from", "to", "chmod_flags"), &DirAccess::copy_absolute, DEFVAL(-1)); |
| 662 | ClassDB::bind_method(D_METHOD("rename", "from", "to"), &DirAccess::rename); |
| 663 | ClassDB::bind_static_method("DirAccess", D_METHOD("rename_absolute", "from", "to"), &DirAccess::rename_absolute); |
| 664 | ClassDB::bind_method(D_METHOD("remove", "path"), &DirAccess::remove); |
| 665 | ClassDB::bind_static_method("DirAccess", D_METHOD("remove_absolute", "path"), &DirAccess::remove_absolute); |
| 666 | |
| 667 | ClassDB::bind_method(D_METHOD("is_link", "path"), &DirAccess::is_link); |
| 668 | ClassDB::bind_method(D_METHOD("read_link", "path"), &DirAccess::read_link); |
| 669 | ClassDB::bind_method(D_METHOD("create_link", "source", "target"), &DirAccess::create_link); |
| 670 | |
| 671 | ClassDB::bind_method(D_METHOD("is_bundle", "path"), &DirAccess::is_bundle); |
| 672 | |
| 673 | ClassDB::bind_method(D_METHOD("set_include_navigational", "enable"), &DirAccess::set_include_navigational); |
| 674 | ClassDB::bind_method(D_METHOD("get_include_navigational"), &DirAccess::get_include_navigational); |
| 675 | ClassDB::bind_method(D_METHOD("set_include_hidden", "enable"), &DirAccess::set_include_hidden); |
| 676 | ClassDB::bind_method(D_METHOD("get_include_hidden"), &DirAccess::get_include_hidden); |
| 677 | |
| 678 | ClassDB::bind_method(D_METHOD("get_filesystem_type"), &DirAccess::get_filesystem_type); |
| 679 | |
| 680 | ClassDB::bind_method(D_METHOD("is_case_sensitive", "path"), &DirAccess::is_case_sensitive); |
| 681 | ClassDB::bind_method(D_METHOD("is_equivalent", "path_a", "path_b"), &DirAccess::is_equivalent); |
| 682 | |
| 683 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_navigational"), "set_include_navigational", "get_include_navigational"); |
| 684 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_hidden"), "set_include_hidden", "get_include_hidden"); |
| 685 | } |
| 686 | |
| 687 | DirAccess::~DirAccess() { |
| 688 | _delete_temp(); |
| 689 | } |