1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

LibCore: Move Core::Stream::File::exists() to Core::File

`Core::Stream::File` shouldn't hold any utility methods that are
unrelated to constructing a `Core::Stream`, so let's just replace the
existing `Core::File::exists` with the nicer looking implementation.
This commit is contained in:
Tim Schumacher 2022-12-07 21:34:00 +01:00 committed by Sam Atkins
parent bd272e638c
commit 2fc2025f49
10 changed files with 14 additions and 18 deletions

View file

@ -12,6 +12,7 @@
#include <AK/Types.h> #include <AK/Types.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/Directory.h> #include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibCore/Stream.h> #include <LibCore/Stream.h>
#include <LibUnicode/Emoji.h> #include <LibUnicode/Emoji.h>
@ -45,7 +46,7 @@ static void set_image_path_for_emoji(StringView emoji_resource_path, Emoji& emoj
} }
auto path = DeprecatedString::formatted("{}/{}.png", emoji_resource_path, builder.build()); auto path = DeprecatedString::formatted("{}/{}.png", emoji_resource_path, builder.build());
if (Core::Stream::File::exists(path)) if (Core::File::exists(path))
emoji.image_path = move(path); emoji.image_path = move(path);
} }
@ -339,7 +340,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments); args_parser.parse(arguments);
auto emoji_test_file = TRY(open_file(emoji_test_path, Core::Stream::OpenMode::Read)); auto emoji_test_file = TRY(open_file(emoji_test_path, Core::Stream::OpenMode::Read));
VERIFY(!emoji_resource_path.is_empty() && Core::Stream::File::exists(emoji_resource_path)); VERIFY(!emoji_resource_path.is_empty() && Core::File::exists(emoji_resource_path));
EmojiData emoji_data {}; EmojiData emoji_data {};
TRY(parse_emoji_test_data(*emoji_test_file, emoji_data)); TRY(parse_emoji_test_data(*emoji_test_file, emoji_data));

View file

@ -234,7 +234,7 @@ int main()
return 1; return 1;
} }
if (!Core::File::exists("components.ini")) { if (!Core::File::exists("components.ini"sv)) {
warnln("\e[31mError:\e[0m There is no 'components.ini' in the current working directory."); warnln("\e[31mError:\e[0m There is no 'components.ini' in the current working directory.");
warnln(" It can be generated by running CMake with 'cmake ../.. -G Ninja'"); warnln(" It can be generated by running CMake with 'cmake ../.. -G Ninja'");
return 1; return 1;

View file

@ -12,7 +12,7 @@
#include <LibTLS/TLSv12.h> #include <LibTLS/TLSv12.h>
#include <LibTest/TestCase.h> #include <LibTest/TestCase.h>
static char const* ca_certs_file = "./ca_certs.ini"; static StringView ca_certs_file = "./ca_certs.ini"sv;
static int port = 443; static int port = 443;
constexpr auto DEFAULT_SERVER = "www.google.com"sv; constexpr auto DEFAULT_SERVER = "www.google.com"sv;

View file

@ -38,7 +38,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments); args_parser.parse(arguments);
if (filename) { if (filename) {
if (!Core::File::exists(filename) || Core::File::is_directory(filename)) { if (!Core::File::exists({ filename, strlen(filename) }) || Core::File::is_directory(filename)) {
warnln("File does not exist or is a directory: {}", filename); warnln("File does not exist or is a directory: {}", filename);
return 1; return 1;
} }

View file

@ -9,6 +9,7 @@
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
@ -191,10 +192,9 @@ bool File::looks_like_shared_library(DeprecatedString const& filename)
return filename.ends_with(".so"sv) || filename.contains(".so."sv); return filename.ends_with(".so"sv) || filename.contains(".so."sv);
} }
bool File::exists(DeprecatedString const& filename) bool File::exists(StringView filename)
{ {
struct stat st; return !Core::System::stat(filename).is_error();
return stat(filename.characters(), &st) == 0;
} }
ErrorOr<size_t> File::size(DeprecatedString const& filename) ErrorOr<size_t> File::size(DeprecatedString const& filename)

View file

@ -47,7 +47,7 @@ public:
bool looks_like_shared_library() const; bool looks_like_shared_library() const;
static bool looks_like_shared_library(DeprecatedString const& filename); static bool looks_like_shared_library(DeprecatedString const& filename);
static bool exists(DeprecatedString const& filename); static bool exists(StringView filename);
static ErrorOr<size_t> size(DeprecatedString const& filename); static ErrorOr<size_t> size(DeprecatedString const& filename);
static DeprecatedString current_working_directory(); static DeprecatedString current_working_directory();
static DeprecatedString absolute_path(DeprecatedString const& path); static DeprecatedString absolute_path(DeprecatedString const& path);

View file

@ -165,11 +165,6 @@ ErrorOr<NonnullOwnPtr<File>> File::adopt_fd(int fd, OpenMode mode, ShouldCloseFi
return file; return file;
} }
bool File::exists(StringView filename)
{
return !Core::System::stat(filename).is_error();
}
ErrorOr<NonnullOwnPtr<File>> File::standard_input() ErrorOr<NonnullOwnPtr<File>> File::standard_input()
{ {
return File::adopt_fd(STDIN_FILENO, OpenMode::Read, ShouldCloseFileDescriptor::No); return File::adopt_fd(STDIN_FILENO, OpenMode::Read, ShouldCloseFileDescriptor::No);

View file

@ -220,7 +220,6 @@ class File final : public SeekableStream {
public: public:
static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644); 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>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
static bool exists(StringView filename);
static ErrorOr<NonnullOwnPtr<File>> standard_input(); static ErrorOr<NonnullOwnPtr<File>> standard_input();
static ErrorOr<NonnullOwnPtr<File>> standard_output(); static ErrorOr<NonnullOwnPtr<File>> standard_output();

View file

@ -6,6 +6,7 @@
#include "ImageCodecPluginSerenity.h" #include "ImageCodecPluginSerenity.h"
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/LocalServer.h> #include <LibCore/LocalServer.h>
#include <LibCore/Stream.h> #include <LibCore/Stream.h>
#include <LibCore/System.h> #include <LibCore/System.h>
@ -26,7 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::pledge("stdio recvfd sendfd accept unix rpath")); TRY(Core::System::pledge("stdio recvfd sendfd accept unix rpath"));
// This must be first; we can't check if /tmp/webdriver exists once we've unveiled other paths. // This must be first; we can't check if /tmp/webdriver exists once we've unveiled other paths.
if (Core::Stream::File::exists("/tmp/webdriver"sv)) if (Core::File::exists("/tmp/webdriver"sv))
TRY(Core::System::unveil("/tmp/webdriver", "rw")); TRY(Core::System::unveil("/tmp/webdriver", "rw"));
TRY(Core::System::unveil("/sys/kernel/processes", "r")); TRY(Core::System::unveil("/sys/kernel/processes", "r"));

View file

@ -9,7 +9,7 @@
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/Stream.h> #include <LibCore/File.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <LibTimeZone/TimeZone.h> #include <LibTimeZone/TimeZone.h>
@ -239,7 +239,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
atime.tv_nsec = UTIME_OMIT; atime.tv_nsec = UTIME_OMIT;
for (auto path : paths) { for (auto path : paths) {
if (Core::Stream::File::exists(path)) { if (Core::File::exists(path)) {
if (utimensat(AT_FDCWD, path.characters(), times, 0) == -1) if (utimensat(AT_FDCWD, path.characters(), times, 0) == -1)
err("failed to touch '{}': {}", path, strerror(errno)); err("failed to touch '{}': {}", path, strerror(errno));
} else if (!no_create_file) { } else if (!no_create_file) {