LibCore: Completely drop DeprecatedFile
DeprecatedFile doesn't properly handle I/O or OOM errors, and only
provides a rudimentary interface to reasonably handle it. We have long
since learned how to do it "properly" with ErrorOr<> and similar
interfaces. See also d43a7eae54
.
Note that this leaves behind an invocation to DeprecatedFile in
Ladybird/AndroidPlatform.cpp. However, that part of the system has
compilation errors since at least January, and will get removed or
rewritten as part of issue #19085. Let's not wait for this Android port
to remove this deprecated API.
This commit is contained in:
parent
c5a89e7e6b
commit
1e1caed1f7
Notes:
sideshowbarker
2024-07-17 18:46:30 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/1e1caed1f7 Pull-request: https://github.com/SerenityOS/serenity/pull/19297 Issue: https://github.com/SerenityOS/serenity/issues/17129 Reviewed-by: https://github.com/gmta ✅
5 changed files with 0 additions and 278 deletions
|
@ -4,7 +4,6 @@ set(SOURCES
|
|||
Command.cpp
|
||||
ConfigFile.cpp
|
||||
DateTime.cpp
|
||||
DeprecatedFile.cpp
|
||||
Directory.cpp
|
||||
DirectoryEntry.cpp
|
||||
DirIterator.cpp
|
||||
|
|
|
@ -1,211 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <LibCore/DeprecatedFile.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibFileSystem/FileSystem.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <utime.h>
|
||||
|
||||
#ifdef AK_OS_SERENITY
|
||||
# include <serenity.h>
|
||||
#endif
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<NonnullRefPtr<DeprecatedFile>> DeprecatedFile::open(DeprecatedString filename, OpenMode mode, mode_t permissions)
|
||||
{
|
||||
auto file = DeprecatedFile::construct(move(filename));
|
||||
if (!file->open_impl(mode, permissions))
|
||||
return Error::from_errno(file->error());
|
||||
return file;
|
||||
}
|
||||
|
||||
DeprecatedFile::DeprecatedFile(DeprecatedString filename, Object* parent)
|
||||
: IODevice(parent)
|
||||
, m_filename(move(filename))
|
||||
{
|
||||
}
|
||||
|
||||
DeprecatedFile::~DeprecatedFile()
|
||||
{
|
||||
if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes && mode() != OpenMode::NotOpen)
|
||||
close();
|
||||
}
|
||||
|
||||
bool DeprecatedFile::open(int fd, OpenMode mode, ShouldCloseFileDescriptor should_close)
|
||||
{
|
||||
set_fd(fd);
|
||||
set_mode(mode);
|
||||
m_should_close_file_descriptor = should_close;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeprecatedFile::open(OpenMode mode)
|
||||
{
|
||||
return open_impl(mode, 0666);
|
||||
}
|
||||
|
||||
bool DeprecatedFile::open_impl(OpenMode mode, mode_t permissions)
|
||||
{
|
||||
VERIFY(!m_filename.is_null());
|
||||
int flags = 0;
|
||||
if (has_flag(mode, OpenMode::ReadOnly) && has_flag(mode, OpenMode::WriteOnly)) {
|
||||
flags |= O_RDWR | O_CREAT;
|
||||
} else if (has_flag(mode, OpenMode::ReadOnly)) {
|
||||
flags |= O_RDONLY;
|
||||
} else if (has_flag(mode, OpenMode::WriteOnly)) {
|
||||
flags |= O_WRONLY | O_CREAT;
|
||||
bool should_truncate = !(has_flag(mode, OpenMode::Append) || has_flag(mode, OpenMode::MustBeNew));
|
||||
if (should_truncate)
|
||||
flags |= O_TRUNC;
|
||||
}
|
||||
if (has_flag(mode, OpenMode::Append))
|
||||
flags |= O_APPEND;
|
||||
if (has_flag(mode, OpenMode::Truncate))
|
||||
flags |= O_TRUNC;
|
||||
if (has_flag(mode, OpenMode::MustBeNew))
|
||||
flags |= O_EXCL;
|
||||
if (!has_flag(mode, OpenMode::KeepOnExec))
|
||||
flags |= O_CLOEXEC;
|
||||
int fd = ::open(m_filename.characters(), flags, permissions);
|
||||
if (fd < 0) {
|
||||
set_error(errno);
|
||||
return false;
|
||||
}
|
||||
|
||||
set_fd(fd);
|
||||
set_mode(mode);
|
||||
return true;
|
||||
}
|
||||
|
||||
int DeprecatedFile::leak_fd()
|
||||
{
|
||||
m_should_close_file_descriptor = ShouldCloseFileDescriptor::No;
|
||||
return fd();
|
||||
}
|
||||
|
||||
bool DeprecatedFile::is_device() const
|
||||
{
|
||||
struct stat st;
|
||||
if (fstat(fd(), &st) < 0)
|
||||
return false;
|
||||
return S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode);
|
||||
}
|
||||
|
||||
bool DeprecatedFile::is_block_device() const
|
||||
{
|
||||
struct stat stat;
|
||||
if (fstat(fd(), &stat) < 0)
|
||||
return false;
|
||||
return S_ISBLK(stat.st_mode);
|
||||
}
|
||||
|
||||
bool DeprecatedFile::is_char_device() const
|
||||
{
|
||||
struct stat stat;
|
||||
if (fstat(fd(), &stat) < 0)
|
||||
return false;
|
||||
return S_ISCHR(stat.st_mode);
|
||||
}
|
||||
|
||||
bool DeprecatedFile::is_directory() const
|
||||
{
|
||||
struct stat st;
|
||||
if (fstat(fd(), &st) < 0)
|
||||
return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
bool DeprecatedFile::is_link() const
|
||||
{
|
||||
struct stat stat;
|
||||
if (fstat(fd(), &stat) < 0)
|
||||
return false;
|
||||
return S_ISLNK(stat.st_mode);
|
||||
}
|
||||
|
||||
DeprecatedString DeprecatedFile::real_path_for(DeprecatedString const& filename)
|
||||
{
|
||||
if (filename.is_null())
|
||||
return {};
|
||||
auto* path = realpath(filename.characters(), nullptr);
|
||||
DeprecatedString real_path(path);
|
||||
free(path);
|
||||
return real_path;
|
||||
}
|
||||
|
||||
DeprecatedString DeprecatedFile::current_working_directory()
|
||||
{
|
||||
char* cwd = getcwd(nullptr, 0);
|
||||
if (!cwd) {
|
||||
perror("getcwd");
|
||||
return {};
|
||||
}
|
||||
|
||||
auto cwd_as_string = DeprecatedString(cwd);
|
||||
free(cwd);
|
||||
|
||||
return cwd_as_string;
|
||||
}
|
||||
|
||||
DeprecatedString DeprecatedFile::absolute_path(DeprecatedString const& path)
|
||||
{
|
||||
if (!Core::System::stat(path).is_error())
|
||||
return DeprecatedFile::real_path_for(path);
|
||||
|
||||
if (path.starts_with("/"sv))
|
||||
return LexicalPath::canonicalized_path(path);
|
||||
|
||||
auto working_directory = DeprecatedFile::current_working_directory();
|
||||
auto full_path = LexicalPath::join(working_directory, path);
|
||||
|
||||
return LexicalPath::canonicalized_path(full_path.string());
|
||||
}
|
||||
|
||||
Optional<DeprecatedString> DeprecatedFile::resolve_executable_from_environment(StringView filename)
|
||||
{
|
||||
if (filename.is_empty())
|
||||
return {};
|
||||
|
||||
// Paths that aren't just a file name generally count as already resolved.
|
||||
if (filename.contains('/')) {
|
||||
if (access(DeprecatedString { filename }.characters(), X_OK) != 0)
|
||||
return {};
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
auto const* path_str = getenv("PATH");
|
||||
StringView path;
|
||||
if (path_str)
|
||||
path = { path_str, strlen(path_str) };
|
||||
if (path.is_empty())
|
||||
path = DEFAULT_PATH_SV;
|
||||
|
||||
auto directories = path.split_view(':');
|
||||
|
||||
for (auto directory : directories) {
|
||||
auto file = DeprecatedString::formatted("{}/{}", directory, filename);
|
||||
|
||||
if (access(file.characters(), X_OK) == 0)
|
||||
return file;
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <LibCore/IODevice.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
///
|
||||
/// Use of Core::File for reading/writing data is deprecated.
|
||||
/// Please use Core::File and Core::InputBufferedFile instead.
|
||||
///
|
||||
class DeprecatedFile final : public IODevice {
|
||||
C_OBJECT(DeprecatedFile)
|
||||
public:
|
||||
virtual ~DeprecatedFile() override;
|
||||
|
||||
static ErrorOr<NonnullRefPtr<DeprecatedFile>> open(DeprecatedString filename, OpenMode, mode_t = 0644);
|
||||
|
||||
DeprecatedString filename() const { return m_filename; }
|
||||
|
||||
bool is_directory() const;
|
||||
bool is_device() const;
|
||||
bool is_block_device() const;
|
||||
bool is_char_device() const;
|
||||
bool is_link() const;
|
||||
|
||||
static DeprecatedString current_working_directory();
|
||||
static DeprecatedString absolute_path(DeprecatedString const& path);
|
||||
|
||||
static DeprecatedString real_path_for(DeprecatedString const& filename);
|
||||
|
||||
virtual bool open(OpenMode) override;
|
||||
|
||||
enum class ShouldCloseFileDescriptor {
|
||||
No = 0,
|
||||
Yes
|
||||
};
|
||||
bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
|
||||
[[nodiscard]] int leak_fd();
|
||||
|
||||
static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
|
||||
|
||||
private:
|
||||
DeprecatedFile(Object* parent = nullptr)
|
||||
: IODevice(parent)
|
||||
{
|
||||
}
|
||||
explicit DeprecatedFile(DeprecatedString filename, Object* parent = nullptr);
|
||||
|
||||
bool open_impl(OpenMode, mode_t);
|
||||
|
||||
DeprecatedString m_filename;
|
||||
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
||||
};
|
||||
|
||||
}
|
|
@ -15,7 +15,6 @@ class ChildEvent;
|
|||
class ConfigFile;
|
||||
class CustomEvent;
|
||||
class DateTime;
|
||||
class DeprecatedFile;
|
||||
class DirIterator;
|
||||
class DeferredInvocationContext;
|
||||
class ElapsedTimer;
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/DeprecatedFile.h>
|
||||
#include <LibCore/SessionManagement.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <limits.h>
|
||||
|
|
Loading…
Add table
Reference in a new issue