1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibCore: Add file management helpers to reduce code duplication

FileManager, cp, mv, rm had some duplicated code, implementing basic
file management operations. This patch creates adds functions that try
to provide an interface suited for all these programs, but this patch
does not make them be used throughout the Userland.

They are added to Core::File following the example of functions such as
read_link/real_path_for.
This commit is contained in:
Mițca Dumitru 2021-02-21 02:51:55 +02:00 committed by Andreas Kling
parent d934e77522
commit 919492945e
2 changed files with 238 additions and 1 deletions

View file

@ -26,9 +26,11 @@
#pragma once
#include <AK/OSError.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <LibCore/IODevice.h>
#include <sys/stat.h>
namespace Core {
@ -46,9 +48,41 @@ public:
static bool is_directory(const String& filename);
static bool exists(const String& filename);
static bool ensure_parent_directories(const String& path);
enum class RecursionMode {
Allowed,
Disallowed
};
enum class LinkMode {
Allowed,
Disallowed
};
enum class AddDuplicateFileMarker {
Yes,
No,
};
struct CopyError {
OSError error_code;
bool tried_recursing;
};
static Result<void, CopyError> copy_file(const String& dst_path, const struct stat& src_stat, File& source);
static Result<void, CopyError> copy_directory(const String& dst_path, const String& src_path, const struct stat& src_stat, LinkMode = LinkMode::Disallowed);
static Result<void, CopyError> copy_file_or_directory(const String& dst_path, const String& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes);
static String real_path_for(const String& filename);
static String read_link(const StringView& link_path);
static bool ensure_parent_directories(const String& path);
static Result<void, OSError> link_file(const String& dst_path, const String& src_path);
struct RemoveError {
String file;
OSError error_code;
};
static Result<void, RemoveError> remove(const String& path, RecursionMode, bool force);
virtual bool open(IODevice::OpenMode) override;