2020-08-28 15:54:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-28 15:54:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCompress/Gzip.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-13 20:24:28 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2020-08-28 15:54:49 +00:00
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
static ErrorOr<void> decompress_file(NonnullOwnPtr<Core::File> input_stream, Stream& output_stream)
|
2020-08-28 15:54:49 +00:00
|
|
|
{
|
2022-11-29 14:43:31 +00:00
|
|
|
auto gzip_stream = Compress::GzipDecompressor { move(input_stream) };
|
2020-08-28 15:54:49 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
|
|
|
while (!gzip_stream.is_eof()) {
|
|
|
|
auto span = TRY(gzip_stream.read(buffer));
|
2023-01-19 23:19:34 +00:00
|
|
|
TRY(output_stream.write_entire_buffer(span));
|
2020-08-28 15:54:49 +00:00
|
|
|
}
|
2021-03-26 15:35:30 +00:00
|
|
|
|
2022-11-29 14:43:31 +00:00
|
|
|
return {};
|
2020-08-28 15:54:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 20:24:28 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2020-08-28 15:54:49 +00:00
|
|
|
{
|
2021-11-26 21:32:37 +00:00
|
|
|
Vector<StringView> filenames;
|
2020-08-28 15:54:49 +00:00
|
|
|
bool keep_input_files { false };
|
|
|
|
bool write_to_stdout { false };
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(keep_input_files, "Keep (don't delete) input files", "keep", 'k');
|
|
|
|
args_parser.add_option(write_to_stdout, "Write to stdout, keep original files unchanged", "stdout", 'c');
|
|
|
|
args_parser.add_positional_argument(filenames, "File to decompress", "FILE");
|
2022-01-13 20:24:28 +00:00
|
|
|
args_parser.parse(args);
|
2020-08-28 15:54:49 +00:00
|
|
|
|
|
|
|
if (write_to_stdout)
|
|
|
|
keep_input_files = true;
|
|
|
|
|
2021-04-29 09:28:01 +00:00
|
|
|
for (auto filename : filenames) {
|
2020-08-28 15:54:49 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString input_filename;
|
|
|
|
DeprecatedString output_filename;
|
2022-07-11 17:32:29 +00:00
|
|
|
if (filename.ends_with(".gz"sv)) {
|
2022-01-14 09:43:19 +00:00
|
|
|
input_filename = filename;
|
2022-01-17 21:30:44 +00:00
|
|
|
output_filename = filename.substring_view(0, filename.length() - 3);
|
|
|
|
} else {
|
2022-12-04 18:02:33 +00:00
|
|
|
input_filename = DeprecatedString::formatted("{}.gz", filename);
|
2022-01-17 21:30:44 +00:00
|
|
|
output_filename = filename;
|
|
|
|
}
|
2020-08-28 15:54:49 +00:00
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto input_stream_result = TRY(Core::File::open(input_filename, Core::File::OpenMode::Read));
|
|
|
|
auto output_stream = write_to_stdout ? TRY(Core::File::standard_output()) : TRY(Core::File::open(output_filename, Core::File::OpenMode::Write));
|
2021-03-26 15:35:30 +00:00
|
|
|
|
2023-01-19 23:19:34 +00:00
|
|
|
TRY(decompress_file(move(input_stream_result), *output_stream));
|
2020-08-28 15:54:49 +00:00
|
|
|
|
2022-01-13 20:24:28 +00:00
|
|
|
if (!keep_input_files)
|
|
|
|
TRY(Core::System::unlink(input_filename));
|
2020-08-28 15:54:49 +00:00
|
|
|
}
|
2021-03-26 15:35:30 +00:00
|
|
|
return 0;
|
2020-08-28 15:54:49 +00:00
|
|
|
}
|