2021-05-03 18:49:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Valtteri Koskivuori <vkoskiv@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-07-03 14:45:59 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2023-02-01 19:39:31 +00:00
|
|
|
#include <AK/String.h>
|
2021-05-03 18:49:11 +00:00
|
|
|
#include <AK/Vector.h>
|
2023-07-03 14:45:59 +00:00
|
|
|
#include <LibArchive/Zip.h>
|
2023-03-07 15:59:02 +00:00
|
|
|
#include <LibAudio/Loader.h>
|
2021-05-07 20:49:26 +00:00
|
|
|
#include <LibCompress/Gzip.h>
|
2021-05-03 18:49:11 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 10:32:25 +00:00
|
|
|
#include <LibCore/MappedFile.h>
|
2021-05-03 18:49:11 +00:00
|
|
|
#include <LibCore/MimeData.h>
|
2021-12-08 18:47:04 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-10-17 23:21:44 +00:00
|
|
|
#include <LibELF/Image.h>
|
|
|
|
#include <LibELF/Validation.h>
|
2023-03-21 18:58:06 +00:00
|
|
|
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
2021-12-08 18:47:04 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-05-03 18:49:11 +00:00
|
|
|
#include <stdio.h>
|
2021-06-15 17:29:04 +00:00
|
|
|
#include <sys/stat.h>
|
2021-05-03 18:49:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> description_only(StringView description, StringView)
|
2021-05-03 18:49:11 +00:00
|
|
|
{
|
2023-02-01 19:39:31 +00:00
|
|
|
return String::from_utf8(description);
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Ideally Gfx::ImageDecoder could tell us the image type directly.
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> image_details(StringView description, StringView path)
|
2021-05-03 18:49:11 +00:00
|
|
|
{
|
2023-02-02 15:10:29 +00:00
|
|
|
auto mapped_file = TRY(Core::MappedFile::map(path));
|
2023-01-20 01:27:10 +00:00
|
|
|
auto mime_type = Core::guess_mime_type_based_on_filename(path);
|
2023-02-02 15:10:29 +00:00
|
|
|
auto image_decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(mapped_file->bytes(), mime_type);
|
2021-07-26 23:12:53 +00:00
|
|
|
if (!image_decoder)
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2021-05-03 18:49:11 +00:00
|
|
|
|
2023-02-26 18:57:48 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.appendff("{}, {} x {}", description, image_decoder->width(), image_decoder->height());
|
|
|
|
if (image_decoder->is_animated()) {
|
|
|
|
builder.appendff(", animated with {} frames that loop", image_decoder->frame_count());
|
|
|
|
int loop_count = image_decoder->loop_count();
|
|
|
|
if (loop_count == 0)
|
|
|
|
builder.appendff(" indefinitely");
|
|
|
|
else
|
|
|
|
builder.appendff(" {} {}", loop_count, loop_count == 1 ? "time" : "times");
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
return builder.to_string();
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> audio_details(StringView description, StringView path)
|
2023-03-07 15:59:02 +00:00
|
|
|
{
|
|
|
|
auto loader_or_error = Audio::Loader::create(path);
|
|
|
|
if (loader_or_error.is_error())
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2023-03-07 15:59:02 +00:00
|
|
|
|
|
|
|
auto loader = loader_or_error.release_value();
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.appendff("{}, {} Hz, {}-bit {}, {} samples ({} s)",
|
|
|
|
description,
|
|
|
|
loader->sample_rate(),
|
|
|
|
loader->bits_per_sample(),
|
|
|
|
loader->num_channels() == 1 ? "Mono" : "Stereo",
|
|
|
|
loader->total_samples(),
|
|
|
|
loader->total_samples() / loader->sample_rate());
|
|
|
|
auto metadata = loader->metadata();
|
|
|
|
Vector<String> metadata_parts;
|
|
|
|
if (metadata.title.has_value()) {
|
|
|
|
// FIXME: Use “pretty quotation” once our terminal fonts support these characters.
|
|
|
|
if (auto title_text = String::formatted("\"{}\"", metadata.title.value_or({})); !title_text.is_error()) {
|
|
|
|
// We intentionally discard the error, because not printing part of the metadata due to OOM is not a problem.
|
|
|
|
(void)metadata_parts.try_append(title_text.release_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metadata.album.has_value()) {
|
|
|
|
if (auto album_text = String::formatted("(Album: {})", metadata.album.value_or({})); !album_text.is_error())
|
|
|
|
(void)metadata_parts.try_append(album_text.release_value());
|
|
|
|
}
|
|
|
|
if (auto all_artists = metadata.all_artists(); !all_artists.is_error() && all_artists.value().has_value()) {
|
|
|
|
if (auto artist_text = String::formatted("by {}", all_artists.release_value().release_value()); !artist_text.is_error())
|
|
|
|
(void)metadata_parts.try_append(artist_text.release_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!metadata_parts.is_empty()) {
|
|
|
|
// New line for the metadata.
|
|
|
|
builder.append_code_point('\n');
|
|
|
|
builder.join(" "sv, metadata_parts);
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
return builder.to_string();
|
2023-03-07 15:59:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> gzip_details(StringView description, StringView path)
|
2021-05-07 20:49:26 +00:00
|
|
|
{
|
2023-02-02 15:10:29 +00:00
|
|
|
auto mapped_file = TRY(Core::MappedFile::map(path));
|
|
|
|
if (!Compress::GzipDecompressor::is_likely_compressed(mapped_file->bytes()))
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2021-05-07 20:49:26 +00:00
|
|
|
|
2023-06-16 03:35:33 +00:00
|
|
|
auto gzip_details = TRY(Compress::GzipDecompressor::describe_header(mapped_file->bytes()));
|
2021-05-07 20:49:26 +00:00
|
|
|
if (!gzip_details.has_value())
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2021-05-07 20:49:26 +00:00
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
return TRY(String::formatted("{}, {}", description, gzip_details.value()));
|
2021-05-07 20:49:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 14:45:59 +00:00
|
|
|
static ErrorOr<Optional<String>> zip_details(StringView description, StringView path)
|
|
|
|
{
|
|
|
|
auto mapped_file = TRY(Core::MappedFile::map(path));
|
|
|
|
auto zip_file = Archive::Zip::try_create(mapped_file->bytes());
|
2023-07-27 13:21:12 +00:00
|
|
|
auto statistics = TRY(zip_file->calculate_statistics());
|
2023-07-03 14:45:59 +00:00
|
|
|
return TRY(String::formatted("{}, {} {}, {} {} totaling {} uncompressed",
|
|
|
|
description,
|
2023-07-27 13:21:12 +00:00
|
|
|
statistics.directory_count(),
|
|
|
|
statistics.directory_count() == 1 ? "directory" : "directories",
|
|
|
|
statistics.file_count(),
|
|
|
|
statistics.file_count() == 1 ? "file" : "files",
|
|
|
|
AK::human_readable_size(statistics.total_uncompressed_bytes())));
|
2023-07-03 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> elf_details(StringView description, StringView path)
|
2021-10-17 23:21:44 +00:00
|
|
|
{
|
2023-02-02 15:10:29 +00:00
|
|
|
auto mapped_file = TRY(Core::MappedFile::map(path));
|
|
|
|
auto elf_data = mapped_file->bytes();
|
2021-10-17 23:21:44 +00:00
|
|
|
ELF::Image elf_image(elf_data);
|
|
|
|
if (!elf_image.is_valid())
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2021-10-17 23:21:44 +00:00
|
|
|
|
2022-01-13 16:50:17 +00:00
|
|
|
StringBuilder interpreter_path_builder;
|
2022-01-13 18:54:53 +00:00
|
|
|
auto result_or_error = ELF::validate_program_headers(*(const ElfW(Ehdr)*)elf_data.data(), elf_data.size(), elf_data, &interpreter_path_builder);
|
2022-01-13 16:50:17 +00:00
|
|
|
if (result_or_error.is_error() || !result_or_error.value())
|
2023-02-01 19:39:31 +00:00
|
|
|
return OptionalNone {};
|
2022-01-13 16:50:17 +00:00
|
|
|
auto interpreter_path = interpreter_path_builder.string_view();
|
2021-10-17 23:21:44 +00:00
|
|
|
|
|
|
|
auto& header = *reinterpret_cast<const ElfW(Ehdr)*>(elf_data.data());
|
|
|
|
|
|
|
|
auto bitness = header.e_ident[EI_CLASS] == ELFCLASS64 ? "64" : "32";
|
|
|
|
auto byteorder = header.e_ident[EI_DATA] == ELFDATA2LSB ? "LSB" : "MSB";
|
|
|
|
|
|
|
|
bool is_dynamically_linked = !interpreter_path.is_empty();
|
2023-02-01 19:39:31 +00:00
|
|
|
auto dynamic_section = TRY(String::formatted(", dynamically linked, interpreter {}", interpreter_path));
|
2021-10-17 23:21:44 +00:00
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
return TRY(String::formatted("{} {}-bit {} {}, {}, version {} ({}){}",
|
2021-10-17 23:21:44 +00:00
|
|
|
description,
|
|
|
|
bitness,
|
|
|
|
byteorder,
|
2022-07-11 17:32:29 +00:00
|
|
|
ELF::Image::object_file_type_to_string(header.e_type).value_or("(?)"sv),
|
|
|
|
ELF::Image::object_machine_type_to_string(header.e_machine).value_or("(?)"sv),
|
2021-10-17 23:21:44 +00:00
|
|
|
header.e_ident[EI_ABIVERSION],
|
2022-07-11 17:32:29 +00:00
|
|
|
ELF::Image::object_abi_type_to_string(header.e_ident[EI_OSABI]).value_or("(?)"sv),
|
2023-02-01 19:39:31 +00:00
|
|
|
is_dynamically_linked ? dynamic_section : String {}));
|
2021-10-17 23:21:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 23:03:23 +00:00
|
|
|
struct PatternAndFunction {
|
|
|
|
StringView matching_pattern;
|
|
|
|
ErrorOr<Optional<String>> (*details)(StringView, StringView);
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr Array s_pattern_with_specialized_functions {
|
|
|
|
PatternAndFunction { "application/gzip"sv, gzip_details },
|
|
|
|
PatternAndFunction { "application/zip"sv, zip_details },
|
|
|
|
PatternAndFunction { "extra/elf"sv, elf_details },
|
|
|
|
PatternAndFunction { "audio/*"sv, audio_details },
|
|
|
|
PatternAndFunction { "image/*"sv, image_details },
|
|
|
|
};
|
2023-02-26 23:40:02 +00:00
|
|
|
|
2023-02-01 19:39:31 +00:00
|
|
|
static ErrorOr<Optional<String>> get_description_from_mime_type(StringView mime, StringView path)
|
2021-05-03 18:49:11 +00:00
|
|
|
{
|
2023-07-11 23:03:23 +00:00
|
|
|
auto const description = Core::get_description_from_mime_type(mime);
|
|
|
|
|
|
|
|
if (!description.has_value())
|
|
|
|
return OptionalNone {};
|
|
|
|
|
|
|
|
for (auto const& pattern : s_pattern_with_specialized_functions) {
|
|
|
|
if (mime.matches(pattern.matching_pattern))
|
|
|
|
return pattern.details(*description, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return description_only(*description, path);
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 18:47:04 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-05-03 18:49:11 +00:00
|
|
|
{
|
2021-12-08 18:47:04 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-05-03 18:49:11 +00:00
|
|
|
|
2023-02-27 00:04:47 +00:00
|
|
|
Vector<StringView> paths;
|
2021-05-03 18:49:11 +00:00
|
|
|
bool flag_mime_only = false;
|
2023-07-21 20:47:27 +00:00
|
|
|
bool flag_brief_mode = false;
|
2021-05-03 18:49:11 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Determine type of files");
|
|
|
|
args_parser.add_option(flag_mime_only, "Only print mime type", "mime-type", 'I');
|
2023-07-21 20:47:27 +00:00
|
|
|
args_parser.add_option(flag_brief_mode, "Do not prepend file names to output lines", "brief", 'b');
|
2021-05-03 18:49:11 +00:00
|
|
|
args_parser.add_positional_argument(paths, "Files to identify", "files", Core::ArgsParser::Required::Yes);
|
2021-12-08 18:47:04 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-05-03 18:49:11 +00:00
|
|
|
|
2021-05-07 13:35:23 +00:00
|
|
|
bool all_ok = true;
|
|
|
|
|
2022-09-20 10:50:37 +00:00
|
|
|
for (auto const& path : paths) {
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file_or_error = Core::File::open(path, Core::File::OpenMode::Read);
|
2021-12-08 18:47:04 +00:00
|
|
|
if (file_or_error.is_error()) {
|
2023-02-27 00:04:47 +00:00
|
|
|
warnln("{}: {}", path, file_or_error.release_error());
|
2021-05-07 13:35:23 +00:00
|
|
|
all_ok = false;
|
|
|
|
continue;
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|
2021-12-08 18:47:04 +00:00
|
|
|
auto file = file_or_error.release_value();
|
2021-05-27 11:19:13 +00:00
|
|
|
|
2021-12-08 18:47:04 +00:00
|
|
|
struct stat file_stat = TRY(Core::System::lstat(path));
|
2021-06-15 17:29:04 +00:00
|
|
|
|
2023-07-21 20:47:27 +00:00
|
|
|
if (!flag_brief_mode)
|
|
|
|
out("{}: ", path);
|
|
|
|
|
2021-06-15 17:29:04 +00:00
|
|
|
auto file_size_in_bytes = file_stat.st_size;
|
2022-09-20 10:50:37 +00:00
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
2023-07-21 20:47:27 +00:00
|
|
|
outln("directory");
|
2021-06-15 17:29:04 +00:00
|
|
|
} else if (!file_size_in_bytes) {
|
2023-07-21 20:47:27 +00:00
|
|
|
outln("empty");
|
2021-05-27 11:19:13 +00:00
|
|
|
} else {
|
|
|
|
auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
|
2023-04-26 23:21:46 +00:00
|
|
|
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(*file).value_or(file_name_guess);
|
2023-02-01 19:39:31 +00:00
|
|
|
auto human_readable_description = TRY(get_description_from_mime_type(mime_type, path));
|
|
|
|
if (!human_readable_description.has_value())
|
|
|
|
human_readable_description = TRY(String::from_utf8(mime_type));
|
2023-07-21 20:47:27 +00:00
|
|
|
outln("{}", flag_mime_only ? mime_type : *human_readable_description);
|
2021-05-27 11:19:13 +00:00
|
|
|
}
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 13:35:23 +00:00
|
|
|
return all_ok ? 0 : 1;
|
2021-05-03 18:49:11 +00:00
|
|
|
}
|