2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-08-07 17:14:58 +02:00
|
|
|
|
2021-12-03 14:02:07 +01:00
|
|
|
#include "lib/zlib/GunzipReader.hxx"
|
|
|
|
#include "io/FileReader.hxx"
|
|
|
|
#include "io/StdioOutputStream.hxx"
|
2020-04-02 16:40:18 +02:00
|
|
|
#include "fs/NarrowPath.hxx"
|
2018-07-17 21:56:43 +02:00
|
|
|
#include "util/PrintException.hxx"
|
2014-08-07 17:14:58 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
static void
|
|
|
|
Copy(OutputStream &dest, Reader &src)
|
2014-08-07 17:14:58 +02:00
|
|
|
{
|
|
|
|
while (true) {
|
2023-05-15 11:00:21 +02:00
|
|
|
std::byte buffer[4096];
|
2015-12-16 11:05:33 +01:00
|
|
|
size_t nbytes = src.Read(buffer, sizeof(buffer));
|
2014-08-07 17:14:58 +02:00
|
|
|
if (nbytes == 0)
|
2015-12-16 11:05:33 +01:00
|
|
|
break;
|
2014-08-07 17:14:58 +02:00
|
|
|
|
2023-05-15 11:00:21 +02:00
|
|
|
dest.Write(std::span{buffer}.first(nbytes));
|
2014-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
static void
|
|
|
|
CopyGunzip(OutputStream &dest, Reader &_src)
|
2014-08-07 17:14:58 +02:00
|
|
|
{
|
2015-12-16 11:05:33 +01:00
|
|
|
GunzipReader src(_src);
|
|
|
|
Copy(dest, src);
|
2014-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
static void
|
|
|
|
CopyGunzip(FILE *_dest, Path src_path)
|
2014-08-07 17:14:58 +02:00
|
|
|
{
|
|
|
|
StdioOutputStream dest(_dest);
|
2015-12-16 11:05:33 +01:00
|
|
|
FileReader src(src_path);
|
|
|
|
CopyGunzip(dest, src);
|
2014-08-07 17:14:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-03-12 20:56:11 +01:00
|
|
|
main(int argc, [[maybe_unused]] char **argv)
|
2018-07-17 21:56:43 +02:00
|
|
|
try {
|
2014-08-07 17:14:58 +02:00
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: run_gunzip PATH\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:40:18 +02:00
|
|
|
FromNarrowPath path = argv[1];
|
2014-08-07 17:14:58 +02:00
|
|
|
|
2018-07-17 21:56:43 +02:00
|
|
|
CopyGunzip(stdout, path);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} catch (...) {
|
|
|
|
PrintException(std::current_exception());
|
|
|
|
return EXIT_FAILURE;
|
2014-08-07 17:14:58 +02:00
|
|
|
}
|