LibCore: Use a StringView for the file path in File::remove

This allows us to use our nice syscall wrappers, avoids some accidental
string copying, and starts to unlink us from the old DeprecatedString.
This commit is contained in:
Tim Schumacher 2022-12-23 14:33:05 +01:00 committed by Tim Flynn
parent 9805f73704
commit 8455d58a44
4 changed files with 8 additions and 14 deletions

View file

@ -133,7 +133,7 @@ static TitleAndText build_cpu_registers(const ELF::Core::ThreadInfo& thread_info
};
}
static void unlink_coredump(StringView const& coredump_path)
static void unlink_coredump(StringView coredump_path)
{
if (Core::File::remove(coredump_path, Core::File::RecursionMode::Disallowed).is_error())
dbgln("Failed deleting coredump file");

View file

@ -549,11 +549,9 @@ ErrorOr<void> File::link_file(DeprecatedString const& dst_path, DeprecatedString
return {};
}
ErrorOr<void> File::remove(DeprecatedString const& path, RecursionMode mode)
ErrorOr<void> File::remove(StringView path, RecursionMode mode)
{
struct stat path_stat;
if (lstat(path.characters(), &path_stat) < 0)
return Error::from_errno(errno);
auto path_stat = TRY(Core::System::lstat(path));
if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) {
auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir);
@ -561,16 +559,12 @@ ErrorOr<void> File::remove(DeprecatedString const& path, RecursionMode mode)
return Error::from_errno(di.error());
while (di.has_next()) {
auto result = remove(di.next_full_path(), RecursionMode::Allowed);
if (result.is_error())
return result.error();
TRY(remove(di.next_full_path(), RecursionMode::Allowed));
}
if (rmdir(path.characters()) < 0)
return Error::from_errno(errno);
TRY(Core::System::rmdir(path));
} else {
if (unlink(path.characters()) < 0)
return Error::from_errno(errno);
TRY(Core::System::unlink(path));
}
return {};

View file

@ -93,7 +93,7 @@ public:
static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
static ErrorOr<void> link_file(DeprecatedString const& dst_path, DeprecatedString const& src_path);
static ErrorOr<void> remove(DeprecatedString const& path, RecursionMode);
static ErrorOr<void> remove(StringView path, RecursionMode);
virtual bool open(OpenMode) override;

View file

@ -49,7 +49,7 @@ TempFile::~TempFile()
if (m_type == Type::Directory)
recursion_allowed = File::RecursionMode::Allowed;
auto rc = File::remove(m_path.characters(), recursion_allowed);
auto rc = File::remove(m_path, recursion_allowed);
if (rc.is_error()) {
warnln("File::remove failed: {}", rc.error().string_literal());
}