2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-09-10 09:57:38 -03:00
|
|
|
#include "FileUtils.h"
|
2020-05-26 08:52:44 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-08-17 06:30:30 -03:00
|
|
|
#include <LibCore/File.h>
|
2020-09-13 16:38:44 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2019-09-10 09:57:38 -03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
namespace FileUtils {
|
|
|
|
|
|
2020-12-28 12:29:25 -03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 21:56:28 -03:00
|
|
|
bool is_directory = S_ISDIR(st.st_mode);
|
|
|
|
|
auto result = Core::File::remove(path, Core::File::RecursionMode::Allowed, false);
|
2020-12-28 12:29:25 -03:00
|
|
|
|
2021-02-20 21:56:28 -03:00
|
|
|
if (result.is_error()) {
|
|
|
|
|
auto& error = result.error();
|
|
|
|
|
if (is_directory) {
|
2020-12-28 12:29:25 -03:00
|
|
|
GUI::MessageBox::show(parent_window,
|
2021-02-20 21:56:28 -03:00
|
|
|
String::formatted("Failed to delete directory \"{}\": {}", error.file, error.error_code),
|
|
|
|
|
"Delete failed",
|
|
|
|
|
GUI::MessageBox::Type::Error);
|
|
|
|
|
} else {
|
|
|
|
|
GUI::MessageBox::show(parent_window,
|
|
|
|
|
String::formatted("Failed to delete file \"{}\": {}", error.file, error.error_code),
|
2020-12-28 12:29:25 -03:00
|
|
|
"Delete failed",
|
|
|
|
|
GUI::MessageBox::Type::Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 16:38:44 -03:00
|
|
|
void delete_paths(const Vector<String>& paths, bool should_confirm, GUI::Window* parent_window)
|
|
|
|
|
{
|
|
|
|
|
String message;
|
|
|
|
|
if (paths.size() == 1) {
|
2020-10-05 09:59:50 -03:00
|
|
|
message = String::formatted("Really delete {}?", LexicalPath(paths[0]).basename());
|
2020-09-13 16:38:44 -03:00
|
|
|
} else {
|
2020-10-05 09:59:50 -03:00
|
|
|
message = String::formatted("Really delete {} files?", paths.size());
|
2020-09-13 16:38:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (should_confirm) {
|
|
|
|
|
auto result = GUI::MessageBox::show(parent_window,
|
|
|
|
|
message,
|
|
|
|
|
"Confirm deletion",
|
|
|
|
|
GUI::MessageBox::Type::Warning,
|
|
|
|
|
GUI::MessageBox::InputType::OKCancel);
|
|
|
|
|
if (result == GUI::MessageBox::ExecCancel)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& path : paths) {
|
2020-12-28 12:29:25 -03:00
|
|
|
delete_path(path, parent_window);
|
2020-09-13 16:38:44 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 12:34:25 -03:00
|
|
|
}
|