ladybird/Userland/Applications/FileManager/FileUtils.cpp
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00

71 lines
2.1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "FileUtils.h"
#include <AK/LexicalPath.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibGUI/MessageBox.h>
#include <sys/stat.h>
#include <unistd.h>
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);
}
bool is_directory = S_ISDIR(st.st_mode);
auto result = Core::File::remove(path, Core::File::RecursionMode::Allowed, false);
if (result.is_error()) {
auto& error = result.error();
if (is_directory) {
GUI::MessageBox::show(parent_window,
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),
"Delete failed",
GUI::MessageBox::Type::Error);
}
}
}
void delete_paths(const Vector<String>& paths, bool should_confirm, GUI::Window* parent_window)
{
String message;
if (paths.size() == 1) {
message = String::formatted("Really delete {}?", LexicalPath(paths[0]).basename());
} else {
message = String::formatted("Really delete {} files?", paths.size());
}
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) {
delete_path(path, parent_window);
}
}
}