1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:37:44 +00:00

file: Port to Core::Stream

Some nicer way of dealing with `stat` would be good. :thonk:
This commit is contained in:
Sam Atkins 2022-09-20 11:50:37 +01:00 committed by Linus Groh
parent 7684e514dd
commit 813fc10aae

View file

@ -10,6 +10,7 @@
#include <LibCore/FileStream.h> #include <LibCore/FileStream.h>
#include <LibCore/MappedFile.h> #include <LibCore/MappedFile.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibELF/Image.h> #include <LibELF/Image.h>
#include <LibELF/Validation.h> #include <LibELF/Validation.h>
@ -153,9 +154,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments); args_parser.parse(arguments);
bool all_ok = true; bool all_ok = true;
// Read accounts for longest possible offset + signature we currently match against.
auto buffer = TRY(ByteBuffer::create_uninitialized(0x9006));
for (auto path : paths) { for (auto const& path : paths) {
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly); auto file_or_error = Core::Stream::File::open(path, Core::Stream::OpenMode::Read);
if (file_or_error.is_error()) { if (file_or_error.is_error()) {
perror(path.characters()); perror(path.characters());
all_ok = false; all_ok = false;
@ -166,15 +169,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
struct stat file_stat = TRY(Core::System::lstat(path)); struct stat file_stat = TRY(Core::System::lstat(path));
auto file_size_in_bytes = file_stat.st_size; auto file_size_in_bytes = file_stat.st_size;
if (file->is_directory()) { if (S_ISDIR(file_stat.st_mode)) {
outln("{}: directory", path); outln("{}: directory", path);
} else if (!file_size_in_bytes) { } else if (!file_size_in_bytes) {
outln("{}: empty", path); outln("{}: empty", path);
} else { } else {
// Read accounts for longest possible offset + signature we currently match against. auto bytes = TRY(file->read(buffer));
auto bytes = file->read(0x9006);
auto file_name_guess = Core::guess_mime_type_based_on_filename(path); auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(bytes.bytes()).value_or(file_name_guess); auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(bytes).value_or(file_name_guess);
auto human_readable_description = get_description_from_mime_type(mime_type, String(path)).value_or(mime_type); auto human_readable_description = get_description_from_mime_type(mime_type, String(path)).value_or(mime_type);
outln("{}: {}", path, flag_mime_only ? mime_type : human_readable_description); outln("{}: {}", path, flag_mime_only ? mime_type : human_readable_description);
} }