1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

FileManager: Added support for deleting directories

Directories can now be deleted using the "Delete..." action from the
context menu
This commit is contained in:
Till Mayer 2019-11-21 21:43:02 +01:00 committed by Andreas Kling
parent 9009390f9c
commit 09189e34e3
3 changed files with 66 additions and 3 deletions

View file

@ -9,6 +9,42 @@
namespace FileUtils {
int delete_directory(String directory, String& file_that_caused_error)
{
CDirIterator iterator(directory, CDirIterator::SkipDots);
if (iterator.has_error()) {
file_that_caused_error = directory;
return -1;
}
while (iterator.has_next()) {
auto file_to_delete = String::format("%s/%s", directory.characters(), iterator.next_path().characters());
struct stat st;
if (lstat(file_to_delete.characters(), &st)) {
file_that_caused_error = file_to_delete;
return errno;
}
if (S_ISDIR(st.st_mode)) {
if (delete_directory(file_to_delete, file_to_delete)) {
file_that_caused_error = file_to_delete;
return errno;
}
} else if (unlink(file_to_delete.characters())) {
file_that_caused_error = file_to_delete;
return errno;
}
}
if (rmdir(directory.characters())) {
file_that_caused_error = directory;
return errno;
}
return 0;
}
bool copy_file_or_directory(const String& src_path, const String& dst_path)
{
int duplicate_count = 0;