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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-10-01 08:15:33 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
#include "sapi_minitar.h" // NOLINT(build/include)
|
2020-10-02 23:52:29 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
#include "absl/status/status.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/path.h"
|
2020-10-07 18:07:13 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::Status CreateArchive(const char* initial_filename, int compress,
|
|
|
|
const char** argv, bool verbose) {
|
2020-09-21 21:30:43 +08:00
|
|
|
// We split the filename path into dirname and filename. To the filename we
|
2020-09-25 23:46:06 +08:00
|
|
|
// prepend "/output/"" so that it will work with the security policy.
|
2020-09-21 21:30:43 +08:00
|
|
|
std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename));
|
2020-09-30 06:50:43 +08:00
|
|
|
auto [archive_path, filename_tmp] =
|
|
|
|
std::move(sandbox2::file::SplitPath(abs_path));
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-02 18:56:14 +08:00
|
|
|
std::string filename = sandbox2::file::JoinPath("/output/", filename_tmp);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-28 23:00:33 +08:00
|
|
|
std::vector<std::string> absolute_paths;
|
2020-09-23 02:30:07 +08:00
|
|
|
sandbox2::util::CharPtrArrToVecString(const_cast<char* const*>(argv),
|
2020-09-28 23:00:33 +08:00
|
|
|
&absolute_paths);
|
|
|
|
|
|
|
|
std::vector<std::string> relative_paths = absolute_paths;
|
|
|
|
|
|
|
|
std::transform(absolute_paths.begin(), absolute_paths.end(),
|
|
|
|
absolute_paths.begin(), MakeAbsolutePathAtCWD);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-25 02:47:19 +08:00
|
|
|
std::transform(relative_paths.begin(), relative_paths.end(),
|
|
|
|
relative_paths.begin(), sandbox2::file::CleanPath);
|
2020-10-01 08:15:33 +08:00
|
|
|
// At this point, we have the relative and absolute paths (cleaned) saved
|
2020-09-25 23:46:06 +08:00
|
|
|
// in vectors.
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// Initialize sandbox and api objects.
|
2020-09-21 21:30:43 +08:00
|
|
|
SapiLibarchiveSandboxCreate sandbox(absolute_paths, archive_path);
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(sandbox.Init());
|
2020-09-21 21:30:43 +08:00
|
|
|
LibarchiveApi api(&sandbox);
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(archive * ret_archive, api.archive_write_new());
|
|
|
|
if (ret_archive == nullptr) {
|
|
|
|
return absl::FailedPreconditionError("Failed to create write archive");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-25 23:46:06 +08:00
|
|
|
// Treat the pointer as remote. There is no need to copy the data
|
|
|
|
// to the client process.
|
2020-10-07 18:07:13 +08:00
|
|
|
sapi::v::RemotePtr a(ret_archive);
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
// switch (compress) {
|
|
|
|
// case 'j':
|
|
|
|
// case 'y':
|
|
|
|
if (compress == 'j' || compress == 'y') {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_add_filter_bzip2(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_add_filter_bzip2 call");
|
|
|
|
}
|
|
|
|
// break;
|
|
|
|
} else if (compress == 'Z') {
|
|
|
|
// case 'Z':
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_add_filter_compress(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_add_filter_compress call");
|
|
|
|
}
|
|
|
|
// break;
|
|
|
|
} else if (compress == 'z') {
|
|
|
|
// case 'z':
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_add_filter_gzip(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_add_filter_gzip call");
|
|
|
|
}
|
|
|
|
// break;
|
|
|
|
} else {
|
|
|
|
// default:
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_add_filter_none(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_add_filter_none call");
|
|
|
|
}
|
|
|
|
// break;
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_set_format_ustar(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_set_format_ustar call");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-23 02:30:07 +08:00
|
|
|
const char* filename_ptr = filename.data();
|
2020-10-07 18:07:13 +08:00
|
|
|
if (filename_ptr != nullptr && strcmp(filename_ptr, "-") == 0) {
|
|
|
|
filename_ptr = nullptr;
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
2020-09-23 02:30:07 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc,
|
|
|
|
api.archive_write_open_filename(
|
|
|
|
&a, sapi::v::ConstCStr(filename_ptr).PtrBefore()));
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_open_filename call");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-21 23:11:10 +08:00
|
|
|
int file_idx = 0;
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-25 23:46:06 +08:00
|
|
|
// We can directly use the vectors defined before.
|
2020-09-21 23:11:10 +08:00
|
|
|
for (int file_idx = 0; file_idx < absolute_paths.size(); ++file_idx) {
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(ret_archive, api.archive_read_disk_new());
|
|
|
|
if (ret_archive == nullptr) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Failed to create read_disk archive");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
sapi::v::RemotePtr disk(ret_archive);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_disk_set_standard_lookup(&disk));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_disk_set_standard_lookup call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-09-25 23:46:06 +08:00
|
|
|
// We use the absolute path first.
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc,
|
2020-10-02 23:52:29 +08:00
|
|
|
api.archive_read_disk_open(
|
2020-10-07 18:07:13 +08:00
|
|
|
&disk,
|
|
|
|
sapi::v::ConstCStr(absolute_paths[file_idx].c_str()).PtrBefore()));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(msg, CheckStatusAndGetString(
|
2021-01-22 22:01:05 +08:00
|
|
|
api.archive_error_string(&disk), sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
return absl::FailedPreconditionError(msg);
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
while (true) {
|
|
|
|
archive_entry* ret_archive_entry;
|
|
|
|
SAPI_ASSIGN_OR_RETURN(ret_archive_entry, api.archive_entry_new());
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (ret_archive_entry == nullptr) {
|
|
|
|
return absl::FailedPreconditionError("Failed to create archive_entry");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
sapi::v::RemotePtr entry(ret_archive_entry);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_next_header2(&disk, &entry));
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc == ARCHIVE_EOF) {
|
2020-09-21 21:30:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg,
|
|
|
|
CheckStatusAndGetString(api.archive_error_string(&disk), sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
return absl::FailedPreconditionError(msg);
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_disk_descend(&disk));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError("read_disk_descend call failed");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-21 23:11:10 +08:00
|
|
|
// After using the absolute path before, we now need to add the pathname
|
|
|
|
// to the archive entry. This would help store the files by their relative
|
2020-10-01 08:15:33 +08:00
|
|
|
// paths(similar to the usual tar command).
|
|
|
|
// However, in the case where a directory is added to the archive,
|
|
|
|
// all of the files inside of it are added as well so we replace the
|
|
|
|
// absolute path prefix with the relative one.
|
|
|
|
// Example:
|
|
|
|
// we add the folder "test_files" which becomes
|
|
|
|
// "/absolute/path/test_files" and the files inside of it will become
|
|
|
|
// similar to "/absolute/path/test_files/file1"
|
|
|
|
// which we then change to "test_files/file1" so that it is relative.
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
std::string path_name,
|
|
|
|
CheckStatusAndGetString(api.archive_entry_pathname(&entry), sandbox));
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-21 23:11:10 +08:00
|
|
|
path_name.replace(path_name.begin(),
|
|
|
|
path_name.begin() + absolute_paths[file_idx].length(),
|
|
|
|
relative_paths[file_idx]);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-09-21 23:11:10 +08:00
|
|
|
// On top of those changes, we need to remove leading '/' characters
|
|
|
|
// and also remove everything up to the last occurrence of '../'.
|
|
|
|
size_t found = path_name.find_first_not_of("/");
|
|
|
|
if (found != std::string::npos) {
|
2020-09-21 21:30:43 +08:00
|
|
|
path_name.erase(path_name.begin(), path_name.begin() + found);
|
2020-09-21 23:11:10 +08:00
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
// Search either for the last '/../' or check if
|
|
|
|
// the path has '../' in the beginning.
|
|
|
|
found = path_name.rfind("/../");
|
2020-09-21 23:11:10 +08:00
|
|
|
if (found != std::string::npos) {
|
2020-10-07 18:07:13 +08:00
|
|
|
path_name = path_name.substr(found + 4);
|
|
|
|
} else if (path_name.substr(0, 3) == "../") {
|
|
|
|
path_name = path_name.substr(3);
|
2020-09-21 23:11:10 +08:00
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(api.archive_entry_set_pathname(
|
|
|
|
&entry, sapi::v::ConstCStr(path_name.c_str()).PtrBefore()));
|
2020-09-21 21:30:43 +08:00
|
|
|
|
|
|
|
if (verbose) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_entry_pathname(&entry),
|
|
|
|
sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
std::cout << msg << std::endl;
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_header(&a, &entry));
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc < ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(msg, CheckStatusAndGetString(
|
2021-01-22 22:01:05 +08:00
|
|
|
api.archive_error_string(&a), sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
std::cout << msg << std::endl;
|
|
|
|
}
|
|
|
|
if (rc == ARCHIVE_FATAL) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_header call");
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:00:33 +08:00
|
|
|
// In the following section, the calls (read, archive_write_data) are done
|
|
|
|
// on the sandboxed process since we do not need to transfer the data in
|
|
|
|
// the client process.
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc > ARCHIVE_FAILED) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_entry_sourcepath(&entry),
|
|
|
|
sandbox));
|
|
|
|
int fd = open(msg.c_str(), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
return absl::FailedPreconditionError("Could not open file");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
|
|
|
sapi::v::Fd sapi_fd(fd);
|
|
|
|
sapi::v::Int read_ret;
|
2020-09-23 02:30:07 +08:00
|
|
|
sapi::v::Array<char> buff(kBuffSize);
|
|
|
|
sapi::v::UInt ssize(kBuffSize);
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// We allocate the buffer remotely and then we can simply use the
|
|
|
|
// remote pointer(with PtrNone).
|
2020-10-07 18:07:13 +08:00
|
|
|
// This allows us to keep the data in the remote process without always
|
|
|
|
// transferring the memory.
|
|
|
|
SAPI_RETURN_IF_ERROR(sandbox.Allocate(&buff, true));
|
2020-09-21 23:11:10 +08:00
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// We can use sapi methods that help us with file descriptors.
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(sandbox.TransferToSandboxee(&sapi_fd));
|
2020-09-23 02:30:07 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(
|
|
|
|
sandbox.Call("read", &read_ret, &sapi_fd, buff.PtrNone(), &ssize));
|
2020-09-23 02:30:07 +08:00
|
|
|
|
2020-09-21 21:30:43 +08:00
|
|
|
while (read_ret.GetValue() > 0) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc,
|
|
|
|
api.archive_write_data(&a, buff.PtrNone(), read_ret.GetValue()));
|
2020-10-07 18:07:13 +08:00
|
|
|
|
|
|
|
SAPI_RETURN_IF_ERROR(sandbox.Call("read", &read_ret, &sapi_fd,
|
2021-01-22 22:01:05 +08:00
|
|
|
buff.PtrNone(), &ssize));
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
2020-09-23 02:30:07 +08:00
|
|
|
// sapi_fd variable goes out of scope here so both the local and the
|
|
|
|
// remote file descriptors are closed.
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(api.archive_entry_free(&entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_close(&disk));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_close call");
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_free(&disk));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_free call");
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_close(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_close call");
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_free(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_free call");
|
|
|
|
}
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
return absl::OkStatus();
|
2020-09-14 22:39:16 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::Status ExtractArchive(const char* filename, int do_extract, int flags,
|
|
|
|
bool verbose) {
|
2020-09-21 21:30:43 +08:00
|
|
|
std::string tmp_dir;
|
|
|
|
if (do_extract) {
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(tmp_dir, CreateTempDirAtCWD());
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
2020-09-21 23:11:10 +08:00
|
|
|
|
2020-09-23 02:30:07 +08:00
|
|
|
// We can use a struct like this in order to delete the temporary
|
|
|
|
// directory that was created earlier whenever the function ends.
|
2020-09-21 23:11:10 +08:00
|
|
|
struct ExtractTempDirectoryCleanup {
|
2020-10-02 23:52:29 +08:00
|
|
|
ExtractTempDirectoryCleanup(const std::string& dir) : dir_(dir) {}
|
2020-09-21 23:11:10 +08:00
|
|
|
~ExtractTempDirectoryCleanup() {
|
2020-10-02 18:56:14 +08:00
|
|
|
sandbox2::file_util::fileops::DeleteRecursively(dir_);
|
2020-09-21 23:11:10 +08:00
|
|
|
}
|
2020-10-02 23:52:29 +08:00
|
|
|
|
2020-10-02 18:56:14 +08:00
|
|
|
private:
|
|
|
|
std::string dir_;
|
2020-09-23 02:30:07 +08:00
|
|
|
};
|
2020-09-21 23:11:10 +08:00
|
|
|
|
2020-09-23 02:30:07 +08:00
|
|
|
// We should only delete it if the do_extract flag is true which
|
|
|
|
// means that this struct is instantiated only in that case.
|
2020-10-07 18:07:13 +08:00
|
|
|
auto cleanup_ptr =
|
|
|
|
do_extract ? absl::make_unique<ExtractTempDirectoryCleanup>(tmp_dir)
|
|
|
|
: nullptr;
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2020-09-23 02:30:07 +08:00
|
|
|
std::string filename_absolute = MakeAbsolutePathAtCWD(filename);
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// Initialize sandbox and api objects.
|
2020-09-21 21:30:43 +08:00
|
|
|
SapiLibarchiveSandboxExtract sandbox(filename_absolute, do_extract, tmp_dir);
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(sandbox.Init());
|
2020-09-16 23:57:31 +08:00
|
|
|
LibarchiveApi api(&sandbox);
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(archive * ret_archive, api.archive_read_new());
|
|
|
|
if (ret_archive == nullptr) {
|
|
|
|
return absl::FailedPreconditionError("Failed to create read archive");
|
|
|
|
}
|
|
|
|
|
|
|
|
sapi::v::RemotePtr a(ret_archive);
|
2020-09-21 21:30:43 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(ret_archive, api.archive_write_disk_new());
|
|
|
|
if (ret_archive == nullptr) {
|
|
|
|
return absl::FailedPreconditionError("Failed to create write disk archive");
|
|
|
|
}
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
sapi::v::RemotePtr ext(ret_archive);
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
int rc;
|
|
|
|
std::string msg;
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_disk_set_options(&ext, flags));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_disk_set_options call");
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_support_filter_bzip2(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_support_filter_bzip2 call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_support_filter_gzip(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_suppport_filter_gzip call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_support_filter_compress(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_support_filter_compress call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_support_format_tar(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result fromread_support_format_tar call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_support_format_cpio(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_support_format_tar call");
|
|
|
|
}
|
2020-09-28 23:00:33 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_disk_set_standard_lookup(&ext));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_disk_set_standard_lookup call");
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-23 02:30:07 +08:00
|
|
|
const char* filename_ptr = filename_absolute.c_str();
|
2020-10-07 18:07:13 +08:00
|
|
|
if (filename_ptr != nullptr && strcmp(filename_ptr, "-") == 0) {
|
|
|
|
filename_ptr = nullptr;
|
2020-09-21 21:30:43 +08:00
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-25 23:46:06 +08:00
|
|
|
// The entries are saved with a relative path so they are all created
|
|
|
|
// relative to the current working directory.
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc, api.archive_read_open_filename(
|
|
|
|
&a, sapi::v::ConstCStr(filename_ptr).PtrBefore(), kBlockSize));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_error_string(&a), sandbox));
|
|
|
|
return absl::FailedPreconditionError(msg);
|
|
|
|
}
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
while (true) {
|
2020-10-02 23:52:29 +08:00
|
|
|
sapi::v::IntBase<archive_entry*> entry_ptr_tmp(0);
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc, api.archive_read_next_header(&a, entry_ptr_tmp.PtrAfter()));
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc == ARCHIVE_EOF) {
|
2020-09-16 23:57:31 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_error_string(&a), sandbox));
|
|
|
|
return absl::FailedPreconditionError(msg);
|
|
|
|
}
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-09-21 21:30:43 +08:00
|
|
|
sapi::v::RemotePtr entry(entry_ptr_tmp.GetValue());
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-09-16 23:57:31 +08:00
|
|
|
if (verbose && do_extract) {
|
|
|
|
std::cout << "x ";
|
|
|
|
}
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-09-16 23:57:31 +08:00
|
|
|
if (verbose || !do_extract) {
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg,
|
|
|
|
CheckStatusAndGetString(api.archive_entry_pathname(&entry), sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
std::cout << msg << std::endl;
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-09-16 23:57:31 +08:00
|
|
|
if (do_extract) {
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_header(&ext, &entry));
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(msg, CheckStatusAndGetString(
|
2021-01-22 22:01:05 +08:00
|
|
|
api.archive_error_string(&a), sandbox));
|
2020-10-07 18:07:13 +08:00
|
|
|
std::cout << msg << std::endl;
|
2020-10-01 08:15:33 +08:00
|
|
|
} else {
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, CopyData(&a, &ext, api, sandbox));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Failed to copy data between archive structs.");
|
|
|
|
}
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
2020-09-16 00:16:09 +08:00
|
|
|
}
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_close(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected value from read_close call");
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_read_free(&a));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from read_free call");
|
|
|
|
}
|
2020-09-16 00:16:09 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_close(&ext));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_close call");
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(rc, api.archive_write_free(&ext));
|
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unexpected result from write_free call");
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
return absl::OkStatus();
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::StatusOr<int> CopyData(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw,
|
|
|
|
LibarchiveApi& api,
|
|
|
|
SapiLibarchiveSandboxExtract& sandbox) {
|
|
|
|
int rc;
|
|
|
|
std::string msg;
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-02 23:52:29 +08:00
|
|
|
sapi::v::IntBase<archive_entry*> buff_ptr_tmp(0);
|
2020-09-16 23:57:31 +08:00
|
|
|
sapi::v::ULLong size;
|
|
|
|
sapi::v::SLLong offset;
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
while (true) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc, api.archive_read_data_block(ar, buff_ptr_tmp.PtrAfter(),
|
|
|
|
size.PtrAfter(), offset.PtrAfter()));
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc == ARCHIVE_EOF) {
|
2020-09-16 23:57:31 +08:00
|
|
|
return ARCHIVE_OK;
|
|
|
|
}
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_error_string(ar), sandbox));
|
|
|
|
std::cout << msg << std::endl;
|
|
|
|
return rc;
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:30:43 +08:00
|
|
|
sapi::v::RemotePtr buff(buff_ptr_tmp.GetValue());
|
2020-09-16 23:57:31 +08:00
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
rc, api.archive_write_data_block(aw, &buff, size.GetValue(),
|
|
|
|
offset.GetValue()));
|
2020-09-14 22:39:16 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
if (rc != ARCHIVE_OK) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
msg, CheckStatusAndGetString(api.archive_error_string(ar), sandbox));
|
|
|
|
std::cout << msg << std::endl;
|
|
|
|
return rc;
|
2020-09-16 23:57:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 22:39:16 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:00:33 +08:00
|
|
|
std::string MakeAbsolutePathAtCWD(const std::string& path) {
|
|
|
|
std::string result = sandbox2::file_util::fileops::MakeAbsolute(
|
|
|
|
path, sandbox2::file_util::fileops::GetCWD());
|
|
|
|
CHECK(result != "") << "Could not create absolute path for: " << path;
|
|
|
|
return sandbox2::file::CleanPath(result);
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::StatusOr<std::string> CheckStatusAndGetString(
|
|
|
|
const absl::StatusOr<char*>& status, LibarchiveSandbox& sandbox) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(char* str, status);
|
|
|
|
if (str == nullptr) {
|
|
|
|
return absl::FailedPreconditionError("Could not get string from archive");
|
|
|
|
}
|
|
|
|
return sandbox.GetCString(sapi::v::RemotePtr(str));
|
2020-09-28 23:00:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::StatusOr<std::string> CreateTempDirAtCWD() {
|
2020-09-28 23:00:33 +08:00
|
|
|
std::string cwd = sandbox2::file_util::fileops::GetCWD();
|
|
|
|
CHECK(!cwd.empty()) << "Could not get current working directory";
|
|
|
|
cwd.append("/");
|
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string result, sandbox2::CreateTempDir(cwd));
|
|
|
|
return result;
|
2020-09-14 22:39:16 +08:00
|
|
|
}
|