Skip to content

Commit

Permalink
Merge pull request #82347 from SaracenOne/dir_access_checks
Browse files Browse the repository at this point in the history
Add error checks for DirAccess creation
  • Loading branch information
akien-mga committed Sep 26, 2023
2 parents 9b0b441 + 3f4513d commit da91cf9
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other par
#ifdef TOOLS_ENABLED
bool _csproj_exists(String p_root_dir) {
Ref<DirAccess> dir = DirAccess::open(p_root_dir);
ERR_FAIL_COND_V(dir.is_null(), false);

dir->list_dir_begin();
String file_name = dir->_get_next();
Expand Down
3 changes: 3 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5963,6 +5963,7 @@ void EditorNode::_dropped_files(const Vector<String> &p_files) {

void EditorNode::_add_dropped_files_recursive(const Vector<String> &p_files, String to_path) {
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_FAIL_COND(dir.is_null());

for (int i = 0; i < p_files.size(); i++) {
String from = p_files[i];
Expand All @@ -5972,6 +5973,8 @@ void EditorNode::_add_dropped_files_recursive(const Vector<String> &p_files, Str
Vector<String> sub_files;

Ref<DirAccess> sub_dir = DirAccess::open(from);
ERR_FAIL_COND(sub_dir.is_null());

sub_dir->list_dir_begin();

String next_file = sub_dir->get_next();
Expand Down
2 changes: 2 additions & 0 deletions editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ void EditorSettings::create() {
if (EditorPaths::get_singleton()->are_paths_valid()) {
// Validate editor config file.
Ref<DirAccess> dir = DirAccess::open(EditorPaths::get_singleton()->get_config_dir());
ERR_FAIL_COND(dir.is_null());

String config_file_name = "editor_settings-" + itos(VERSION_MAJOR) + ".tres";
config_file_path = EditorPaths::get_singleton()->get_config_dir().path_join(config_file_name);
if (!dir->file_exists(config_file_name)) {
Expand Down
2 changes: 2 additions & 0 deletions editor/export/editor_export_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,8 @@ void EditorExportPlatform::zip_folder_recursive(zipFile &p_zip, const String &p_
String dir = p_folder.is_empty() ? p_root_path : p_root_path.path_join(p_folder);

Ref<DirAccess> da = DirAccess::open(dir);
ERR_FAIL_COND(da.is_null());

da->list_dir_begin();
String f = da->get_next();
while (!f.is_empty()) {
Expand Down
4 changes: 4 additions & 0 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,8 @@ void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const Strin
} else {
// Recursively duplicate all files inside the folder.
Ref<DirAccess> old_dir = DirAccess::open(old_path);
ERR_FAIL_COND(old_dir.is_null());

Ref<FileAccess> file_access = FileAccess::create(FileAccess::ACCESS_RESOURCES);
old_dir->set_include_navigational(false);
old_dir->list_dir_begin();
Expand Down Expand Up @@ -3308,6 +3310,8 @@ bool FileSystemDock::_get_imported_files(const String &p_path, String &r_extensi
}

Ref<DirAccess> da = DirAccess::open(p_path);
ERR_FAIL_COND_V(da.is_null(), false);

da->list_dir_begin();
String n = da->get_next();
while (!n.is_empty()) {
Expand Down
4 changes: 4 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,8 @@ Error Main::setup2() {
// Editor setting class is not available, load config directly.
if (!init_use_custom_screen && (editor || project_manager) && EditorPaths::get_singleton()->are_paths_valid()) {
Ref<DirAccess> dir = DirAccess::open(EditorPaths::get_singleton()->get_config_dir());
ERR_FAIL_COND_V(dir.is_null(), FAILED);

String config_file_name = "editor_settings-" + itos(VERSION_MAJOR) + ".tres";
String config_file_path = EditorPaths::get_singleton()->get_config_dir().path_join(config_file_name);
if (dir->file_exists(config_file_name)) {
Expand Down Expand Up @@ -3302,6 +3304,8 @@ bool Main::start() {

if (sep == -1) {
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_FAIL_COND_V(da.is_null(), false);

local_game_path = da->get_current_dir().path_join(local_game_path);
} else {
Ref<DirAccess> da = DirAccess::open(local_game_path.substr(0, sep));
Expand Down
2 changes: 2 additions & 0 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3051,6 +3051,8 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> p_state) {
String relative_texture_dir = "textures";
String full_texture_dir = p_state->base_path.path_join(relative_texture_dir);
Ref<DirAccess> da = DirAccess::open(p_state->base_path);
ERR_FAIL_COND_V(da.is_null(), FAILED);

if (!da->dir_exists(full_texture_dir)) {
da->make_dir(full_texture_dir);
}
Expand Down
4 changes: 4 additions & 0 deletions platform/android/export/export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,8 @@ void EditorExportPlatformAndroid::_clear_assets_directory() {
if (da_res->dir_exists(APK_ASSETS_DIRECTORY)) {
print_verbose("Clearing APK assets directory...");
Ref<DirAccess> da_assets = DirAccess::open(APK_ASSETS_DIRECTORY);
ERR_FAIL_COND(da_assets.is_null());

da_assets->erase_contents_recursive();
da_res->remove(APK_ASSETS_DIRECTORY);
}
Expand All @@ -2643,6 +2645,8 @@ void EditorExportPlatformAndroid::_clear_assets_directory() {
if (da_res->dir_exists(AAB_ASSETS_DIRECTORY)) {
print_verbose("Clearing AAB assets directory...");
Ref<DirAccess> da_assets = DirAccess::open(AAB_ASSETS_DIRECTORY);
ERR_FAIL_COND(da_assets.is_null());

da_assets->erase_contents_recursive();
da_res->remove(AAB_ASSETS_DIRECTORY);
}
Expand Down
2 changes: 2 additions & 0 deletions scene/resources/resource_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,8 @@ Error ResourceLoaderText::save_as_binary(const String &p_path) {
wf->store_buffer(data.ptr(), data.size());
{
Ref<DirAccess> dar = DirAccess::open(temp_file.get_base_dir());
ERR_FAIL_COND_V(dar.is_null(), FAILED);

dar->remove(temp_file);
}

Expand Down
2 changes: 2 additions & 0 deletions servers/movie_writer/movie_writer_pngwav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Error MovieWriterPNGWAV::write_begin(const Size2i &p_movie_size, uint32_t p_fps,
//Remove existing files before writing anew
uint32_t idx = 0;
Ref<DirAccess> d = DirAccess::open(base_path.get_base_dir());
ERR_FAIL_COND_V(d.is_null(), FAILED);

String file = base_path.get_file();
while (true) {
String path = file + zeros_str(idx) + ".png";
Expand Down

0 comments on commit da91cf9

Please sign in to comment.