Browse Source

Fix uninstallation of mono templates directory

The previous code didn't take into account that templates directories
can have subdirectories like the mono templates have.
Also fix an unclosed dir handle.
Rémi Verschelde 6 years ago
parent
commit
47746e10cb
1 changed files with 15 additions and 31 deletions
  1. 15 31
      editor/export_template_manager.cpp

+ 15 - 31
editor/export_template_manager.cpp

@@ -155,38 +155,27 @@ void ExportTemplateManager::_uninstall_template(const String &p_version) {
 
 void ExportTemplateManager::_uninstall_template_confirm() {
 
-	DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
-	Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
-
-	ERR_FAIL_COND(err != OK);
-
-	err = d->change_dir(to_remove);
-
-	ERR_FAIL_COND(err != OK);
-
-	Vector<String> files;
-	d->list_dir_begin();
-	String c = d->get_next();
-	while (c != String()) {
-		if (!d->current_is_dir()) {
-			files.push_back(c);
-		}
-		c = d->get_next();
-	}
-	d->list_dir_end();
+	DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+	const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
+	Error err = da->change_dir(templates_dir);
+	ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
+	err = da->change_dir(to_remove);
+	ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
 
-	for (int i = 0; i < files.size(); i++) {
-		d->remove(files[i]);
-	}
+	err = da->erase_contents_recursive();
+	ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.plus_file(to_remove) + "'.");
 
-	d->change_dir("..");
-	d->remove(to_remove);
+	da->change_dir("..");
+	da->remove(to_remove);
+	ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
 
 	_update_template_list();
 }
 
 bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_progress) {
 
+	// unzClose() will take care of closing the file stored in the unzFile,
+	// so we don't need to `memdelete(fa)` in this method.
 	FileAccess *fa = NULL;
 	zlib_filefunc_def io = zipio_create_io_from_file(&fa);
 
@@ -251,7 +240,7 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
 
 	String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(version);
 
-	DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+	DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
 	Error err = d->make_dir_recursive(template_path);
 	if (err != OK) {
 		EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:") + "\n" + template_path);
@@ -259,8 +248,6 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
 		return false;
 	}
 
-	memdelete(d);
-
 	ret = unzGoToFirstFile(pkg);
 
 	EditorProgress *p = NULL;
@@ -316,7 +303,7 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
 		}
 
 		String to_write = template_path.plus_file(file);
-		FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
+		FileAccessRef f = FileAccess::open(to_write, FileAccess::WRITE);
 
 		if (!f) {
 			ret = unzGoToNextFile(pkg);
@@ -326,8 +313,6 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
 
 		f->store_buffer(data.ptr(), data.size());
 
-		memdelete(f);
-
 #ifndef WINDOWS_ENABLED
 		FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
 #endif
@@ -343,7 +328,6 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
 	unzClose(pkg);
 
 	_update_template_list();
-
 	return true;
 }