2021-03-27 16:11:13 +03:00
|
|
|
/*
|
2021-04-22 23:40:43 +03:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2021-03-27 16:11:13 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-27 16:11:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCompress/Gzip.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 13:32:25 +03:00
|
|
|
#include <LibCore/MappedFile.h>
|
2022-01-08 15:03:22 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-03-27 16:11:13 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-01-08 15:03:22 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-03-27 16:11:13 +03:00
|
|
|
{
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> filenames;
|
2021-03-27 16:11:13 +03:00
|
|
|
bool keep_input_files { false };
|
|
|
|
bool write_to_stdout { false };
|
2021-08-22 16:30:13 +03:00
|
|
|
bool decompress { false };
|
2021-03-27 16:11:13 +03:00
|
|
|
|
|
|
|
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');
|
2021-08-22 16:30:13 +03:00
|
|
|
args_parser.add_option(decompress, "Decompress", "decompress", 'd');
|
|
|
|
args_parser.add_positional_argument(filenames, "Files", "FILES");
|
2022-01-08 15:03:22 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-03-27 16:11:13 +03:00
|
|
|
|
|
|
|
if (write_to_stdout)
|
|
|
|
keep_input_files = true;
|
|
|
|
|
2021-04-29 12:28:01 +03:00
|
|
|
for (auto const& input_filename : filenames) {
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString output_filename;
|
2021-08-22 16:30:13 +03:00
|
|
|
if (decompress) {
|
|
|
|
if (!input_filename.ends_with(".gz"sv)) {
|
|
|
|
warnln("unknown suffix for: {}, skipping", input_filename);
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-27 00:32:37 +03:00
|
|
|
output_filename = input_filename.substring_view(0, input_filename.length() - ".gz"sv.length());
|
2021-08-22 16:30:13 +03:00
|
|
|
} else {
|
2023-12-16 17:19:34 +03:00
|
|
|
output_filename = ByteString::formatted("{}.gz", input_filename);
|
2021-08-22 16:30:13 +03:00
|
|
|
}
|
2021-03-27 16:11:13 +03:00
|
|
|
|
2023-04-01 02:12:04 +03:00
|
|
|
auto output_stream = write_to_stdout ? TRY(Core::File::standard_output()) : TRY(Core::File::open(output_filename, Core::File::OpenMode::Write));
|
2021-03-27 16:11:13 +03:00
|
|
|
|
2022-01-08 15:06:49 +03:00
|
|
|
if (decompress)
|
2023-04-01 02:12:04 +03:00
|
|
|
TRY(Compress::GzipDecompressor::decompress_file(input_filename, move(output_stream)));
|
2022-01-08 15:06:49 +03:00
|
|
|
else
|
2023-04-01 02:12:04 +03:00
|
|
|
TRY(Compress::GzipCompressor::compress_file(input_filename, move(output_stream)));
|
2021-03-27 16:11:13 +03:00
|
|
|
|
|
|
|
if (!keep_input_files) {
|
2022-01-08 15:03:22 +03:00
|
|
|
TRY(Core::System::unlink(input_filename));
|
2021-03-27 16:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 02:12:04 +03:00
|
|
|
|
2021-03-27 16:11:13 +03:00
|
|
|
return 0;
|
|
|
|
}
|