FileManager: Add a delete_path helper for singular paths

This is just the syscalls and error handling that was done for each
file in delete_paths.

This will be used to prevent the unneeded construction of vectors
when performing cut operations
This commit is contained in:
Idan Horowitz 2020-12-28 17:29:25 +02:00 committed by Andreas Kling
parent f30d4f22ef
commit 7450f20b81
Notes: sideshowbarker 2024-07-19 00:29:42 +09:00
2 changed files with 31 additions and 28 deletions

View file

@ -38,6 +38,35 @@
namespace FileUtils {
void delete_path(const String& path, GUI::Window* parent_window)
{
struct stat st;
if (lstat(path.characters(), &st)) {
GUI::MessageBox::show(parent_window,
String::formatted("lstat({}) failed: {}", path, strerror(errno)),
"Delete failed",
GUI::MessageBox::Type::Error);
}
if (S_ISDIR(st.st_mode)) {
String error_path;
int error = FileUtils::delete_directory(path, error_path);
if (error) {
GUI::MessageBox::show(parent_window,
String::formatted("Failed to delete directory \"{}\": {}", error_path, strerror(error)),
"Delete failed",
GUI::MessageBox::Type::Error);
}
} else if (unlink(path.characters()) < 0) {
int saved_errno = errno;
GUI::MessageBox::show(parent_window,
String::formatted("unlink(\"{}\") failed: {}", path, strerror(saved_errno)),
"Delete failed",
GUI::MessageBox::Type::Error);
}
}
void delete_paths(const Vector<String>& paths, bool should_confirm, GUI::Window* parent_window)
{
String message;
@ -58,34 +87,7 @@ void delete_paths(const Vector<String>& paths, bool should_confirm, GUI::Window*
}
for (auto& path : paths) {
struct stat st;
if (lstat(path.characters(), &st)) {
GUI::MessageBox::show(parent_window,
String::formatted("lstat({}) failed: {}", path, strerror(errno)),
"Delete failed",
GUI::MessageBox::Type::Error);
break;
}
if (S_ISDIR(st.st_mode)) {
String error_path;
int error = FileUtils::delete_directory(path, error_path);
if (error) {
GUI::MessageBox::show(parent_window,
String::formatted("Failed to delete directory \"{}\": {}", error_path, strerror(error)),
"Delete failed",
GUI::MessageBox::Type::Error);
break;
}
} else if (unlink(path.characters()) < 0) {
int saved_errno = errno;
GUI::MessageBox::show(parent_window,
String::formatted("unlink(\"{}\") failed: {}", path, strerror(saved_errno)),
"Delete failed",
GUI::MessageBox::Type::Error);
break;
}
delete_path(path, parent_window);
}
}

View file

@ -33,6 +33,7 @@
namespace FileUtils {
void delete_path(const String&, GUI::Window*);
void delete_paths(const Vector<String>&, bool should_confirm, GUI::Window*);
int delete_directory(String directory, String& file_that_caused_error);
bool copy_file_or_directory(const String& src_path, const String& dst_path);