mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 23:27:44 +00:00
LibCore: Move Stream-based file into the Core
namespace
This commit is contained in:
parent
a96339b72b
commit
606a3982f3
218 changed files with 748 additions and 643 deletions
|
@ -10,6 +10,7 @@ set(SOURCES
|
|||
ElapsedTimer.cpp
|
||||
Event.cpp
|
||||
EventLoop.cpp
|
||||
File.cpp
|
||||
IODevice.cpp
|
||||
LockFile.cpp
|
||||
MappedFile.cpp
|
||||
|
@ -25,7 +26,6 @@ set(SOURCES
|
|||
Socket.cpp
|
||||
SOCKSProxyClient.cpp
|
||||
StandardPaths.cpp
|
||||
Stream.cpp
|
||||
System.cpp
|
||||
SystemServerTakeover.cpp
|
||||
TCPServer.cpp
|
||||
|
|
|
@ -40,8 +40,8 @@ ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_system(DeprecatedString
|
|||
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, AllowWriting allow_altering)
|
||||
{
|
||||
auto maybe_file = Stream::File::open(filename, allow_altering == AllowWriting::Yes ? Stream::OpenMode::ReadWrite : Stream::OpenMode::Read);
|
||||
OwnPtr<Stream::BufferedFile> buffered_file;
|
||||
auto maybe_file = File::open(filename, allow_altering == AllowWriting::Yes ? File::OpenMode::ReadWrite : File::OpenMode::Read);
|
||||
OwnPtr<BufferedFile> buffered_file;
|
||||
if (maybe_file.is_error()) {
|
||||
// If we attempted to open a read-only file that does not exist, we ignore the error, making it appear
|
||||
// the same as if we had opened an empty file. This behavior is a little weird, but is required by
|
||||
|
@ -49,7 +49,7 @@ ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& file
|
|||
if (!(allow_altering == AllowWriting::No && maybe_file.error().code() == ENOENT))
|
||||
return maybe_file.release_error();
|
||||
} else {
|
||||
buffered_file = TRY(Stream::BufferedFile::create(maybe_file.release_value()));
|
||||
buffered_file = TRY(BufferedFile::create(maybe_file.release_value()));
|
||||
}
|
||||
|
||||
auto config_file = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ConfigFile(filename, move(buffered_file))));
|
||||
|
@ -59,20 +59,20 @@ ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& file
|
|||
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, int fd)
|
||||
{
|
||||
auto file = TRY(Stream::File::adopt_fd(fd, Stream::OpenMode::ReadWrite));
|
||||
auto file = TRY(File::adopt_fd(fd, File::OpenMode::ReadWrite));
|
||||
return open(filename, move(file));
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, NonnullOwnPtr<Core::Stream::File> file)
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open(DeprecatedString const& filename, NonnullOwnPtr<Core::File> file)
|
||||
{
|
||||
auto buffered_file = TRY(Stream::BufferedFile::create(move(file)));
|
||||
auto buffered_file = TRY(BufferedFile::create(move(file)));
|
||||
|
||||
auto config_file = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ConfigFile(filename, move(buffered_file))));
|
||||
TRY(config_file->reparse());
|
||||
return config_file;
|
||||
}
|
||||
|
||||
ConfigFile::ConfigFile(DeprecatedString const& filename, OwnPtr<Stream::BufferedFile> open_file)
|
||||
ConfigFile::ConfigFile(DeprecatedString const& filename, OwnPtr<BufferedFile> open_file)
|
||||
: m_filename(filename)
|
||||
, m_file(move(open_file))
|
||||
{
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Core {
|
||||
|
@ -30,7 +31,7 @@ public:
|
|||
static ErrorOr<NonnullRefPtr<ConfigFile>> open_for_system(DeprecatedString const& app_name, AllowWriting = AllowWriting::No);
|
||||
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, AllowWriting = AllowWriting::No);
|
||||
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, int fd);
|
||||
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::Stream::File>);
|
||||
static ErrorOr<NonnullRefPtr<ConfigFile>> open(DeprecatedString const& filename, NonnullOwnPtr<Core::File>);
|
||||
~ConfigFile();
|
||||
|
||||
bool has_group(DeprecatedString const&) const;
|
||||
|
@ -78,12 +79,12 @@ public:
|
|||
DeprecatedString const& filename() const { return m_filename; }
|
||||
|
||||
private:
|
||||
ConfigFile(DeprecatedString const& filename, OwnPtr<Stream::BufferedFile> open_file);
|
||||
ConfigFile(DeprecatedString const& filename, OwnPtr<BufferedFile> open_file);
|
||||
|
||||
ErrorOr<void> reparse();
|
||||
|
||||
DeprecatedString m_filename;
|
||||
OwnPtr<Stream::BufferedFile> m_file;
|
||||
OwnPtr<BufferedFile> m_file;
|
||||
HashMap<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> m_groups;
|
||||
bool m_dirty { false };
|
||||
};
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Core {
|
|||
|
||||
///
|
||||
/// Use of Core::File for reading/writing data is deprecated.
|
||||
/// Please use Core::Stream::File and Core::Stream::BufferedFile instead.
|
||||
/// Please use Core::File and Core::BufferedFile instead.
|
||||
///
|
||||
class DeprecatedFile final : public IODevice {
|
||||
C_OBJECT(DeprecatedFile)
|
||||
|
|
|
@ -89,10 +89,10 @@ ErrorOr<LexicalPath> Directory::path() const
|
|||
return m_path.value();
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::File>> Directory::open(StringView filename, Stream::OpenMode mode) const
|
||||
ErrorOr<NonnullOwnPtr<File>> Directory::open(StringView filename, File::OpenMode mode) const
|
||||
{
|
||||
auto fd = TRY(System::openat(m_directory_fd, filename, Stream::File::open_mode_to_options(mode)));
|
||||
return Stream::File::adopt_fd(fd, mode);
|
||||
auto fd = TRY(System::openat(m_directory_fd, filename, File::open_mode_to_options(mode)));
|
||||
return File::adopt_fd(fd, mode);
|
||||
}
|
||||
|
||||
ErrorOr<struct stat> Directory::stat() const
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <AK/LexicalPath.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -37,7 +38,7 @@ public:
|
|||
static ErrorOr<Directory> create(DeprecatedString path, CreateDirectories, mode_t creation_mode = 0755);
|
||||
static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::File>> open(StringView filename, Stream::OpenMode mode) const;
|
||||
ErrorOr<NonnullOwnPtr<File>> open(StringView filename, File::OpenMode mode) const;
|
||||
ErrorOr<struct stat> stat() const;
|
||||
ErrorOr<DirIterator> create_iterator() const;
|
||||
|
||||
|
|
|
@ -5,22 +5,12 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Stream.h"
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#ifdef AK_OS_SERENITY
|
||||
# include <serenity.h>
|
||||
#endif
|
||||
#ifdef AK_OS_FREEBSD
|
||||
# include <sys/ucred.h>
|
||||
#endif
|
||||
|
||||
namespace Core::Stream {
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<NonnullOwnPtr<File>> File::open(StringView filename, OpenMode mode, mode_t permissions)
|
||||
{
|
||||
|
@ -36,7 +26,7 @@ ErrorOr<NonnullOwnPtr<File>> File::adopt_fd(int fd, OpenMode mode, ShouldCloseFi
|
|||
}
|
||||
|
||||
if (!has_any_flag(mode, OpenMode::ReadWrite)) {
|
||||
dbgln("Core::DeprecatedFile::adopt_fd: Attempting to adopt a file with neither Read nor Write specified in mode");
|
||||
dbgln("Core::File::adopt_fd: Attempting to adopt a file with neither Read nor Write specified in mode");
|
||||
return Error::from_errno(EINVAL);
|
||||
}
|
||||
|
108
Userland/Libraries/LibCore/File.h
Normal file
108
Userland/Libraries/LibCore/File.h
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/BufferedStream.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Stream.h>
|
||||
#include <LibIPC/Forward.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class File final : public SeekableStream {
|
||||
AK_MAKE_NONCOPYABLE(File);
|
||||
|
||||
public:
|
||||
enum class OpenMode : unsigned {
|
||||
NotOpen = 0,
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
ReadWrite = 3,
|
||||
Append = 4,
|
||||
Truncate = 8,
|
||||
MustBeNew = 16,
|
||||
KeepOnExec = 32,
|
||||
Nonblocking = 64,
|
||||
};
|
||||
|
||||
enum class ShouldCloseFileDescriptor {
|
||||
Yes,
|
||||
No,
|
||||
};
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
|
||||
static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_input();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_output();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_error();
|
||||
static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
|
||||
|
||||
File(File&& other) { operator=(move(other)); }
|
||||
|
||||
File& operator=(File&& other)
|
||||
{
|
||||
if (&other == this)
|
||||
return *this;
|
||||
|
||||
m_mode = exchange(other.m_mode, OpenMode::NotOpen);
|
||||
m_fd = exchange(other.m_fd, -1);
|
||||
m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096) override;
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
|
||||
virtual bool is_eof() const override;
|
||||
virtual bool is_open() const override;
|
||||
virtual void close() override;
|
||||
virtual ErrorOr<size_t> seek(i64 offset, SeekMode) override;
|
||||
virtual ErrorOr<void> truncate(size_t length) override;
|
||||
|
||||
int leak_fd(Badge<::IPC::File>)
|
||||
{
|
||||
m_should_close_file_descriptor = ShouldCloseFileDescriptor::No;
|
||||
return m_fd;
|
||||
}
|
||||
|
||||
int fd() const
|
||||
{
|
||||
return m_fd;
|
||||
}
|
||||
|
||||
virtual ~File() override
|
||||
{
|
||||
if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes)
|
||||
close();
|
||||
}
|
||||
|
||||
static int open_mode_to_options(OpenMode mode);
|
||||
|
||||
private:
|
||||
File(OpenMode mode, ShouldCloseFileDescriptor should_close = ShouldCloseFileDescriptor::Yes)
|
||||
: m_mode(mode)
|
||||
, m_should_close_file_descriptor(should_close)
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr<void> open_path(StringView filename, mode_t);
|
||||
|
||||
OpenMode m_mode { OpenMode::NotOpen };
|
||||
int m_fd { -1 };
|
||||
bool m_last_read_was_eof { false };
|
||||
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
||||
};
|
||||
|
||||
AK_ENUM_BITWISE_OPERATORS(File::OpenMode)
|
||||
|
||||
using BufferedFile = BufferedSeekable<File>;
|
||||
|
||||
}
|
|
@ -21,6 +21,7 @@ class DeferredInvocationContext;
|
|||
class ElapsedTimer;
|
||||
class Event;
|
||||
class EventLoop;
|
||||
class File;
|
||||
class IODevice;
|
||||
class LocalServer;
|
||||
class LocalSocket;
|
||||
|
@ -42,8 +43,4 @@ class UDPSocket;
|
|||
|
||||
enum class TimerShouldFireWhenNotVisible;
|
||||
|
||||
namespace Stream {
|
||||
class File;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/ProcessStatisticsReader.h>
|
||||
#include <pwd.h>
|
||||
|
||||
|
@ -90,7 +91,7 @@ ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(SeekableStream&
|
|||
|
||||
ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames)
|
||||
{
|
||||
auto proc_all_file = TRY(Core::Stream::File::open("/sys/kernel/processes"sv, Core::Stream::OpenMode::Read));
|
||||
auto proc_all_file = TRY(Core::File::open("/sys/kernel/processes"sv, Core::File::OpenMode::Read));
|
||||
return get_all(*proc_all_file, include_usernames);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,98 +25,3 @@
|
|||
#include <LibIPC/Forward.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
|
||||
namespace Core::Stream {
|
||||
|
||||
// Concrete classes.
|
||||
|
||||
enum class OpenMode : unsigned {
|
||||
NotOpen = 0,
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
ReadWrite = 3,
|
||||
Append = 4,
|
||||
Truncate = 8,
|
||||
MustBeNew = 16,
|
||||
KeepOnExec = 32,
|
||||
Nonblocking = 64,
|
||||
};
|
||||
|
||||
enum class ShouldCloseFileDescriptor {
|
||||
Yes,
|
||||
No,
|
||||
};
|
||||
|
||||
AK_ENUM_BITWISE_OPERATORS(OpenMode)
|
||||
|
||||
class File final : public SeekableStream {
|
||||
AK_MAKE_NONCOPYABLE(File);
|
||||
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
|
||||
static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_input();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_output();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_error();
|
||||
static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
|
||||
|
||||
File(File&& other) { operator=(move(other)); }
|
||||
|
||||
File& operator=(File&& other)
|
||||
{
|
||||
if (&other == this)
|
||||
return *this;
|
||||
|
||||
m_mode = exchange(other.m_mode, OpenMode::NotOpen);
|
||||
m_fd = exchange(other.m_fd, -1);
|
||||
m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096) override;
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
|
||||
virtual bool is_eof() const override;
|
||||
virtual bool is_open() const override;
|
||||
virtual void close() override;
|
||||
virtual ErrorOr<size_t> seek(i64 offset, SeekMode) override;
|
||||
virtual ErrorOr<void> truncate(size_t length) override;
|
||||
|
||||
int leak_fd(Badge<::IPC::File>)
|
||||
{
|
||||
m_should_close_file_descriptor = ShouldCloseFileDescriptor::No;
|
||||
return m_fd;
|
||||
}
|
||||
|
||||
int fd() const
|
||||
{
|
||||
return m_fd;
|
||||
}
|
||||
|
||||
virtual ~File() override
|
||||
{
|
||||
if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes)
|
||||
close();
|
||||
}
|
||||
|
||||
static int open_mode_to_options(OpenMode mode);
|
||||
|
||||
private:
|
||||
File(OpenMode mode, ShouldCloseFileDescriptor should_close = ShouldCloseFileDescriptor::Yes)
|
||||
: m_mode(mode)
|
||||
, m_should_close_file_descriptor(should_close)
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr<void> open_path(StringView filename, mode_t);
|
||||
|
||||
OpenMode m_mode { OpenMode::NotOpen };
|
||||
int m_fd { -1 };
|
||||
bool m_last_read_was_eof { false };
|
||||
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
||||
};
|
||||
|
||||
using BufferedFile = BufferedSeekable<File>;
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue