mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00: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
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue