2020-10-01 08:15:33 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// This file contains the main function from the original minitar example:
|
|
|
|
// https://github.com/libarchive/libarchive/blob/master/examples/minitar/minitar.c
|
|
|
|
// Most of the logic is the same, it was only simplified a bit since this is
|
|
|
|
// only used for the command line tool.
|
|
|
|
// No sandboxing takes place in this function.
|
2020-09-28 23:00:33 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
#include "sapi_minitar.h" // NOLINT(build/include)
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
static void PrintUsage() {
|
|
|
|
/* Many program options depend on compile options. */
|
|
|
|
const char* m =
|
|
|
|
"Usage: minitar [-"
|
|
|
|
"c"
|
|
|
|
"j"
|
|
|
|
"tvx"
|
|
|
|
"y"
|
|
|
|
"Z"
|
|
|
|
"z"
|
|
|
|
"] [-f file] [file]\n";
|
|
|
|
|
|
|
|
std::cout << m << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
int main(int unused_argc, const char** argv) {
|
2020-09-28 23:00:33 +08:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
2020-10-07 18:07:13 +08:00
|
|
|
const char* filename = nullptr;
|
|
|
|
int compress;
|
|
|
|
int flags;
|
|
|
|
int mode;
|
|
|
|
int opt;
|
2020-09-28 23:00:33 +08:00
|
|
|
|
|
|
|
mode = 'x';
|
|
|
|
int verbose = 0;
|
|
|
|
compress = '\0';
|
|
|
|
flags = ARCHIVE_EXTRACT_TIME;
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
while (*++argv != nullptr && **argv == '-') {
|
2020-09-28 23:00:33 +08:00
|
|
|
const char* p = *argv + 1;
|
|
|
|
|
|
|
|
while ((opt = *p++) != '\0') {
|
|
|
|
switch (opt) {
|
|
|
|
case 'c':
|
|
|
|
mode = opt;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (*p != '\0')
|
|
|
|
filename = p;
|
|
|
|
else
|
|
|
|
filename = *++argv;
|
|
|
|
p += strlen(p);
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
compress = opt;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
flags |= ARCHIVE_EXTRACT_PERM;
|
|
|
|
flags |= ARCHIVE_EXTRACT_ACL;
|
|
|
|
flags |= ARCHIVE_EXTRACT_FFLAGS;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
mode = opt;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
mode = opt;
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
compress = opt;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
compress = opt;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
compress = opt;
|
|
|
|
break;
|
|
|
|
default:
|
2020-10-07 18:07:13 +08:00
|
|
|
PrintUsage();
|
2020-09-28 23:00:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::Status status;
|
2020-09-28 23:00:33 +08:00
|
|
|
switch (mode) {
|
|
|
|
case 'c':
|
2020-10-07 18:07:13 +08:00
|
|
|
status = CreateArchive(filename, compress, argv, verbose);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Archive creation failed with message: "
|
|
|
|
<< status.message();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
break;
|
|
|
|
case 't':
|
2020-10-07 18:07:13 +08:00
|
|
|
status = ExtractArchive(filename, 0, flags, verbose);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Archive extraction failed with message: "
|
|
|
|
<< status.message();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
break;
|
|
|
|
case 'x':
|
2020-10-07 18:07:13 +08:00
|
|
|
status = ExtractArchive(filename, 1, flags, verbose);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Archive extraction failed with message: "
|
|
|
|
<< status.message();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|