1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:47:44 +00:00

gzip: Port to LibMain

This commit is contained in:
Rummskartoffel 2022-01-08 13:03:22 +01:00 committed by Andreas Kling
parent 69445f3dcb
commit 748218d5d0
2 changed files with 7 additions and 14 deletions

View file

@ -102,7 +102,7 @@ target_link_libraries(grep LibRegex)
target_link_libraries(gron LibMain) target_link_libraries(gron LibMain)
target_link_libraries(groups LibMain) target_link_libraries(groups LibMain)
target_link_libraries(gunzip LibCompress) target_link_libraries(gunzip LibCompress)
target_link_libraries(gzip LibCompress) target_link_libraries(gzip LibCompress LibMain)
target_link_libraries(id LibMain) target_link_libraries(id LibMain)
target_link_libraries(ini LibMain) target_link_libraries(ini LibMain)
target_link_libraries(jp LibMain) target_link_libraries(jp LibMain)

View file

@ -8,9 +8,11 @@
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/FileStream.h> #include <LibCore/FileStream.h>
#include <LibCore/MappedFile.h> #include <LibCore/MappedFile.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
Vector<StringView> filenames; Vector<StringView> filenames;
bool keep_input_files { false }; bool keep_input_files { false };
@ -22,7 +24,7 @@ int main(int argc, char** argv)
args_parser.add_option(write_to_stdout, "Write to stdout, keep original files unchanged", "stdout", 'c'); args_parser.add_option(write_to_stdout, "Write to stdout, keep original files unchanged", "stdout", 'c');
args_parser.add_option(decompress, "Decompress", "decompress", 'd'); args_parser.add_option(decompress, "Decompress", "decompress", 'd');
args_parser.add_positional_argument(filenames, "Files", "FILES"); args_parser.add_positional_argument(filenames, "Files", "FILES");
args_parser.parse(argc, argv); args_parser.parse(arguments);
if (write_to_stdout) if (write_to_stdout)
keep_input_files = true; keep_input_files = true;
@ -41,12 +43,7 @@ int main(int argc, char** argv)
// We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression) // We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression)
// TODO: automatically fallback to buffered streaming for very large files // TODO: automatically fallback to buffered streaming for very large files
auto file_or_error = Core::MappedFile::map(input_filename); auto file = TRY(Core::MappedFile::map(input_filename));
if (file_or_error.is_error()) {
warnln("Failed opening input file for reading: {}", file_or_error.error());
return 1;
}
auto file = file_or_error.value();
AK::Optional<ByteBuffer> output_bytes; AK::Optional<ByteBuffer> output_bytes;
if (decompress) { if (decompress) {
@ -78,11 +75,7 @@ int main(int argc, char** argv)
} }
if (!keep_input_files) { if (!keep_input_files) {
const auto retval = unlink(String(input_filename).characters()); TRY(Core::System::unlink(input_filename));
if (retval != 0) {
warnln("Failed removing input file");
return 1;
}
} }
} }
return 0; return 0;