Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/openPMD/auxiliary/Filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
#pragma once

#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -66,6 +67,19 @@ namespace auxiliary
*/
std::vector<std::string> list_directory(std::string const &path);

/** List all contents of a directory at a given absolute or relative path.
*
* @note The equivalent of `ls path`
* @note Both contained files and directories are listed.
* `.` and `..` are not returned.
* @param path Absolute or relative path of directory to examine.
* @return Vector of all contained files and directories,
* or an empty option in case of an IO error, in which case the
* specific error will be stored in errno.
*/
std::optional<std::vector<std::string>>
list_directory_nothrow(std::string const &path);

/** Create all required directories to have a reachable given absolute or
* relative path.
*
Expand Down
38 changes: 30 additions & 8 deletions src/auxiliary/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ bool file_exists(std::string const &path)
}

std::vector<std::string> list_directory(std::string const &path)
{
auto res = list_directory_nothrow(path);
if (!res)
{
throw std::system_error(std::error_code(errno, std::system_category()));
}
else
{
return *res;
}
}

std::optional<std::vector<std::string>>
list_directory_nothrow(std::string const &path)
{
std::vector<std::string> ret;
#ifdef _WIN32
Expand All @@ -86,7 +100,9 @@ std::vector<std::string> list_directory(std::string const &path)
#else
auto directory = opendir(path.c_str());
if (!directory)
throw std::system_error(std::error_code(errno, std::system_category()));
{
return std::nullopt;
}
dirent *entry;
while ((entry = readdir(directory)) != nullptr)
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
Expand Down Expand Up @@ -150,14 +166,20 @@ bool remove_directory(std::string const &path)
return (0 == remove(p.c_str()));
};
#endif
for (auto const &entry : list_directory(path))
auto entries = list_directory_nothrow(path);
// Check if some other process was faster deleting this
if (entries)
{
auto partialPath = path;
partialPath.append(std::string(1, directory_separator)).append(entry);
if (directory_exists(partialPath))
success &= remove_directory(partialPath);
else if (file_exists(partialPath))
success &= remove_file(partialPath);
for (auto const &entry : *entries)
{
auto partialPath = path;
partialPath.append(std::string(1, directory_separator))
.append(entry);
if (directory_exists(partialPath))
success &= remove_directory(partialPath);
else if (file_exists(partialPath))
success &= remove_file(partialPath);
}
}
success &= del(path);
return success;
Expand Down
Loading