mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 09:46:04 -05:00
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:
parent
f30d4f22ef
commit
7450f20b81
Notes:
sideshowbarker
2024-07-19 00:29:42 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/7450f20b810 Pull-request: https://github.com/SerenityOS/serenity/pull/4602
2 changed files with 31 additions and 28 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue