2 * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
17 #include <package/hpkg/HPKGDefs.h>
18 #include <package/hpkg/PackageReader.h>
19 #include <package/hpkg/PackageWriter.h>
21 #include <DataPositionIOWrapper.h>
25 #include "PackageWriterListener.h"
28 using BPackageKit::BHPKG::BPackageReader
;
29 using BPackageKit::BHPKG::BPackageWriter
;
30 using BPackageKit::BHPKG::BPackageWriterListener
;
31 using BPackageKit::BHPKG::BPackageWriterParameters
;
35 create_stdio(bool isInput
)
37 BFdIO
* dataIO
= new BFdIO(isInput
? 0 : 1, false);
38 return new BDataPositionIOWrapper(dataIO
);
43 command_recompress(int argc
, const char* const* argv
)
47 int32 compressionLevel
= BPackageKit::BHPKG::B_HPKG_COMPRESSION_LEVEL_BEST
;
50 static struct option sLongOptions
[] = {
51 { "help", no_argument
, 0, 'h' },
52 { "quiet", no_argument
, 0, 'q' },
53 { "verbose", no_argument
, 0, 'v' },
57 opterr
= 0; // don't print errors
58 int c
= getopt_long(argc
, (char**)argv
, "+0123456789:hqv",
74 compressionLevel
= c
- '0';
78 print_usage_and_exit(false);
90 print_usage_and_exit(true);
95 // The remaining arguments are the input package file and the output
96 // package file, i.e. two more arguments.
97 if (argc
- optind
!= 2)
98 print_usage_and_exit(true);
100 const char* inputPackageFileName
= argv
[optind
++];
101 const char* outputPackageFileName
= argv
[optind
++];
103 // open the input package
104 status_t error
= B_OK
;
105 BPositionIO
* inputFile
;
106 if (strcmp(inputPackageFileName
, "-") == 0) {
107 inputFile
= create_stdio(true);
109 BFile
* inputFileFile
= new BFile
;
110 error
= inputFileFile
->SetTo(inputPackageFileName
, O_RDONLY
);
112 fprintf(stderr
, "Error: Failed to open input file \"%s\": %s\n",
113 inputPackageFileName
, strerror(error
));
116 inputFile
= inputFileFile
;
119 // write the output package
120 BPackageWriterParameters writerParameters
;
121 writerParameters
.SetCompressionLevel(compressionLevel
);
122 if (compressionLevel
== 0) {
123 writerParameters
.SetCompression(
124 BPackageKit::BHPKG::B_HPKG_COMPRESSION_NONE
);
127 PackageWriterListener
listener(verbose
, quiet
);
128 BPackageWriter
packageWriter(&listener
);
129 if (strcmp(outputPackageFileName
, "-") == 0) {
130 if (compressionLevel
!= 0) {
131 fprintf(stderr
, "Error: Writing to stdout is supported only with "
132 "compression level 0.\n");
136 error
= packageWriter
.Init(create_stdio(false), true,
139 error
= packageWriter
.Init(outputPackageFileName
, &writerParameters
);
143 error
= packageWriter
.Recompress(inputFile
);
148 printf("\nsuccessfully wrote package '%s'\n", outputPackageFileName
);