cleaned up some code. Fixed a bug where a temporary directory would be deleted even when it was not created

This commit is contained in:
Andrei Medar 2020-09-22 18:30:07 +00:00
parent c2c3f6b44d
commit e0e0f1cd5f
7 changed files with 95 additions and 223 deletions

View File

@ -0,0 +1,3 @@
BasedOnStyle: Google
DerivePointerAlignment: false
PointerAlignment: Left

View File

@ -29,16 +29,6 @@ target_link_libraries(helpers PUBLIC
libarchive_sapi libarchive_sapi
) )
add_executable(sapi_example
sapi_example.cc
)
target_link_libraries(sapi_example PRIVATE
libarchive_sapi
sapi::sapi
glog::glog
)
add_executable(sapi_minitar add_executable(sapi_minitar
sapi_minitar.cc sapi_minitar.cc
) )
@ -50,29 +40,6 @@ target_link_libraries(sapi_minitar PRIVATE
#glog::glog #glog::glog
) )
#add_executable(untar
# untar.cc
#)
#
#target_link_libraries(untar PRIVATE
# archive
#)
#
#
#
#add_executable(tarfilter
# tarfilter.cc
#)
#
#target_link_libraries(tarfilter PRIVATE
# archive
#)
add_executable(minitar add_executable(minitar
orig/minitar.cc orig/minitar.cc
) )

View File

@ -1,32 +1,22 @@
#include "helpers.h" #include "helpers.h"
std::string MakeAbsolutePathAtCWD(const std::string &path) { std::string MakeAbsolutePathAtCWD(const std::string& path) {
std::string result = sandbox2::file_util::fileops::MakeAbsolute( std::string result = sandbox2::file_util::fileops::MakeAbsolute(
path, sandbox2::file_util::fileops::GetCWD()); path, sandbox2::file_util::fileops::GetCWD());
CHECK(result != "") << "Could not create absolute path for: " << path; CHECK(result != "") << "Could not create absolute path for: " << path;
return sandbox2::file::CleanPath(result); return sandbox2::file::CleanPath(result);
} }
std::vector<std::string> MakeAbsolutePathsVec(const char *argv[]) { std::vector<std::string> MakeAbsolutePathsVec(const char* argv[]) {
std::vector<std::string> arr; std::vector<std::string> arr;
sandbox2::util::CharPtrArrToVecString(const_cast<char *const *>(argv), &arr); sandbox2::util::CharPtrArrToVecString(const_cast<char* const*>(argv), &arr);
std::transform(arr.begin(), arr.end(), arr.begin(), MakeAbsolutePathAtCWD); std::transform(arr.begin(), arr.end(), arr.begin(), MakeAbsolutePathAtCWD);
return arr; return arr;
} }
// std::string GetErrorString(sapi::v::Ptr *archive, LibarchiveSandbox &sandbox, std::string CheckStatusAndGetString(const sapi::StatusOr<char*>& status,
// LibarchiveApi &api) { LibarchiveSandbox& sandbox) {
// sapi::StatusOr<char *> ret = api.archive_error_string(archive);
// CHECK(ret.ok() && ret) << "Could not get error message";
// sapi::StatusOr<std::string> ret2 =
// sandbox.GetCString(sapi::v::RemotePtr(ret.value())); CHECK(ret.ok()) <<
// "Could not transfer error message"; return ret2.value();
// }
std::string CheckStatusAndGetString(const sapi::StatusOr<char *> &status,
LibarchiveSandbox &sandbox) {
CHECK(status.ok() && status.value() != NULL) << "Could not get error message"; CHECK(status.ok() && status.value() != NULL) << "Could not get error message";
sapi::StatusOr<std::string> ret = sapi::StatusOr<std::string> ret =
@ -35,23 +25,12 @@ std::string CheckStatusAndGetString(const sapi::StatusOr<char *> &status,
return ret.value(); return ret.value();
} }
// std::string CallFunctionAndGetString(sapi::v::Ptr *archive, LibarchiveSandbox
// &sandbox, LibarchiveApi *api, sapi::StatusOr<char *>
// (LibarchiveApi::*func)(sapi::v::Ptr *)) {
// sapi::StatusOr<char *> ret = (api->*func)(archive);
// CHECK(ret.ok() && ret) << "Could not get error message";
// sapi::StatusOr<std::string> ret2 =
// sandbox.GetCString(sapi::v::RemotePtr(ret.value())); CHECK(ret.ok()) <<
// "Could not transfer error message"; return ret2.value();
// }
std::string CreateTempDirAtCWD() { std::string CreateTempDirAtCWD() {
std::string cwd = sandbox2::file_util::fileops::GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();
CHECK(!cwd.empty()) << "Could not get current working directory"; CHECK(!cwd.empty()) << "Could not get current working directory";
cwd.append("/"); cwd.append("/");
sapi::StatusOr<std::string> result = sandbox2::CreateTempDir(cwd); sapi::StatusOr<std::string> result = sandbox2::CreateTempDir(cwd);
CHECK(result.ok()) << "Could not create temporary directory"; CHECK(result.ok()) << "Could not create temporary directory at " << cwd;
return result.value(); return result.value();
} }

View File

@ -9,26 +9,27 @@
#include "sandboxed_api/sandbox2/util/path.h" #include "sandboxed_api/sandbox2/util/path.h"
#include "sandboxed_api/sandbox2/util/temp_file.h" #include "sandboxed_api/sandbox2/util/temp_file.h"
inline constexpr size_t kBlockSize = 10240;
inline constexpr size_t kBuffSize = 16384;
// Used to convert the paths provided as arguments for the program // Used to convert the paths provided as arguments for the program
// (the paths used) to an array of absolute paths. This allows the user // (the paths used) to an array of absolute paths. This allows the user
// to use either relative or absolute paths // to use either relative or absolute paths
std::vector<std::string> MakeAbsolutePathsVec(const char *argv[]); std::vector<std::string> MakeAbsolutePathsVec(const char* argv[]);
// Converts only one string to an absolute path by prepending the current // Converts only one string to an absolute path by prepending the current
// working directory to the relative path // working directory to the relative path
std::string MakeAbsolutePathAtCWD(const std::string &path); std::string MakeAbsolutePathAtCWD(const std::string& path);
// Calls the archive_error_string and returns the mesage after it was // This function takes a status as argument and after checking the status
// transferred to the client process. std::string GetErrorString(sapi::v::Ptr // it transfers the string. This is used mostly with archive_error_string
// *archive, LibarchiveSandbox &sandbox, LibarchiveApi &api); // and other library functions that return a char *.
std::string CheckStatusAndGetString(const sapi::StatusOr<char*>& status,
std::string CheckStatusAndGetString(const sapi::StatusOr<char *> &status, LibarchiveSandbox& sandbox);
LibarchiveSandbox &sandbox);
// std::string CallFunctionAndGetString(sapi::v::Ptr *archive, LibarchiveSandbox
// &sandbox, LibarchiveApi *api, sapi::StatusOr<char *>
// (LibarchiveApi::*func)(sapi::v::Ptr *));
// Creates a temporary directory in the current working directory and
// returns the path. This is used in the extract function where the sandbox
// changes the current working directory to this temporary directory.
std::string CreateTempDirAtCWD(); std::string CreateTempDirAtCWD();
#endif // SAPI_LIBARCHIVE_HELPERS_H #endif // SAPI_LIBARCHIVE_HELPERS_H

View File

@ -10,13 +10,13 @@
class SapiLibarchiveSandboxCreate : public LibarchiveSandbox { class SapiLibarchiveSandboxCreate : public LibarchiveSandbox {
public: public:
// TODO // TODO
explicit SapiLibarchiveSandboxCreate(const std::vector<std::string> &files, explicit SapiLibarchiveSandboxCreate(const std::vector<std::string>& files,
absl::string_view archive_path) absl::string_view archive_path)
: files_(files), archive_path_(archive_path) {} : files_(files), archive_path_(archive_path) {}
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder *) override { sandbox2::PolicyBuilder*) override {
sandbox2::PolicyBuilder policy = sandbox2::PolicyBuilder policy =
sandbox2::PolicyBuilder() sandbox2::PolicyBuilder()
.AddDirectoryAt(archive_path_, "/output", false) .AddDirectoryAt(archive_path_, "/output", false)
@ -46,7 +46,7 @@ class SapiLibarchiveSandboxCreate : public LibarchiveSandbox {
__NR_getdents64, __NR_getdents64,
}); });
for (const auto &i : files_) { for (const auto& i : files_) {
std::cout << "ADD FILE -------" << i << std::endl; std::cout << "ADD FILE -------" << i << std::endl;
struct stat s; struct stat s;
stat(i.c_str(), &s); stat(i.c_str(), &s);
@ -75,14 +75,14 @@ class SapiLibarchiveSandboxExtract : public LibarchiveSandbox {
tmp_dir_(tmp_dir) {} tmp_dir_(tmp_dir) {}
private: private:
virtual void ModifyExecutor(sandbox2::Executor *executor) override { virtual void ModifyExecutor(sandbox2::Executor* executor) override {
if (do_extract_) { if (do_extract_) {
executor = &executor->set_cwd(std::string(tmp_dir_)); executor = &executor->set_cwd(std::string(tmp_dir_));
} }
} }
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder *) override { sandbox2::PolicyBuilder*) override {
sandbox2::PolicyBuilder policy = sandbox2::PolicyBuilder() sandbox2::PolicyBuilder policy = sandbox2::PolicyBuilder()
.AllowRead() .AllowRead()
.AllowWrite() .AllowWrite()

View File

@ -1,17 +0,0 @@
#include <iostream>
#include "libarchive_sapi.sapi.h"
int main() {
std::cout << "WORKS2" << std::endl;
LibarchiveSandbox sandbox;
sandbox.Init().IgnoreError();
LibarchiveApi api(&sandbox);
if (api.archive_write_disk_new().ok()) {
std::cout << "OK" << std::endl;
}
return 0;
}

View File

@ -49,7 +49,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <memory>
#include "helpers.h" #include "helpers.h"
#include "sandbox.h" #include "sandbox.h"
@ -124,21 +126,18 @@
#endif #endif
#ifndef NO_CREATE #ifndef NO_CREATE
static void create(const char *filename, int compress, const char **argv); static void create(const char* filename, int compress, const char** argv);
#endif #endif
static void errmsg(const char *); static void extract(const char* filename, int do_extract, int flags);
static void extract(const char *filename, int do_extract, int flags); static int copy_data(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw,
static int copy_data(sapi::v::RemotePtr *ar, sapi::v::RemotePtr *aw, LibarchiveApi& api, SapiLibarchiveSandboxExtract& sandbox);
LibarchiveApi &api, SapiLibarchiveSandboxExtract &sandbox);
static void msg(const char *);
static void usage(void); static void usage(void);
static int verbose = 0; static int verbose = 0;
int main(int argc, const char **argv) { int main(int argc, const char** argv) {
google::InitGoogleLogging(argv[0]); google::InitGoogleLogging(argv[0]);
std::cout << "BEGIN\n"; const char* filename = NULL;
const char *filename = NULL;
int compress, flags, mode, opt; int compress, flags, mode, opt;
(void)argc; (void)argc;
@ -149,7 +148,7 @@ int main(int argc, const char **argv) {
/* Among other sins, getopt(3) pulls in printf(3). */ /* Among other sins, getopt(3) pulls in printf(3). */
while (*++argv != NULL && **argv == '-') { while (*++argv != NULL && **argv == '-') {
const char *p = *argv + 1; const char* p = *argv + 1;
while ((opt = *p++) != '\0') { while ((opt = *p++) != '\0') {
switch (opt) { switch (opt) {
@ -219,50 +218,33 @@ int main(int argc, const char **argv) {
break; break;
} }
return (0); return EXIT_SUCCESS;
} }
#ifndef NO_CREATE #ifndef NO_CREATE
// static char buff[16384];
static void create(const char *initial_filename, int compress, static void create(const char* initial_filename, int compress,
const char **argv) { const char** argv) {
// We split the filename path into dirname and filename. To the filename we // We split the filename path into dirname and filename. To the filename we
// prepend /output/ so that it will work with the security policy. // prepend /output/ so that it will work with the security policy.
std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename)); std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename));
std::cout << "initial_filename = " << initial_filename << std::endl;
auto [archive_path, filename_tmp] = sandbox2::file::SplitPath(abs_path); auto [archive_path, filename_tmp] = sandbox2::file::SplitPath(abs_path);
std::cout << "filename_tmp = " << filename_tmp << std::endl;
std::cout << "archive_path_first = " << archive_path << std::endl;
std::string filename("/output/"); std::string filename("/output/");
filename.append(filename_tmp); filename.append(filename_tmp);
std::cout << "filename = " << filename << std::endl;
std::cout << "archive_path = " << archive_path << std::endl;
std::cout << "absolute_paths: " << std::endl;
std::vector<std::string> absolute_paths = MakeAbsolutePathsVec(argv); std::vector<std::string> absolute_paths = MakeAbsolutePathsVec(argv);
for (const auto &i : absolute_paths) {
std::cout << i << std::endl;
}
std::cout << "=======\n";
std::vector<std::string> relative_paths; std::vector<std::string> relative_paths;
sandbox2::util::CharPtrArrToVecString(const_cast<char *const *>(argv), sandbox2::util::CharPtrArrToVecString(const_cast<char* const*>(argv),
&relative_paths); &relative_paths);
SapiLibarchiveSandboxCreate sandbox(absolute_paths, archive_path); SapiLibarchiveSandboxCreate sandbox(absolute_paths, archive_path);
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization"; CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
LibarchiveApi api(&sandbox); LibarchiveApi api(&sandbox);
ssize_t len; sapi::StatusOr<archive*> ret = api.archive_write_new();
int fd;
sapi::StatusOr<archive *> ret = api.archive_write_new();
CHECK(ret.ok()) << "write_new call failed"; CHECK(ret.ok()) << "write_new call failed";
CHECK(ret.value() != NULL) << "Failed to create write archive"; CHECK(ret.value() != NULL) << "Failed to create write archive";
@ -309,10 +291,11 @@ static void create(const char *initial_filename, int compress,
CHECK(ret2.value() != ARCHIVE_FATAL) CHECK(ret2.value() != ARCHIVE_FATAL)
<< "Unexpected result from write_set_format_ustar call"; << "Unexpected result from write_set_format_ustar call";
const char *filename_ptr = filename.data(); const char* filename_ptr = filename.data();
if (filename_ptr != NULL && strcmp(filename_ptr, "-") == 0) { if (filename_ptr != NULL && strcmp(filename_ptr, "-") == 0) {
filename_ptr = NULL; filename_ptr = NULL;
} }
ret2 = api.archive_write_open_filename( ret2 = api.archive_write_open_filename(
&a, sapi::v::ConstCStr(filename_ptr).PtrBefore()); &a, sapi::v::ConstCStr(filename_ptr).PtrBefore());
CHECK(ret2.ok()) << "write_open_filename call failed"; CHECK(ret2.ok()) << "write_open_filename call failed";
@ -321,9 +304,7 @@ static void create(const char *initial_filename, int compress,
int file_idx = 0; int file_idx = 0;
// while (*argv != NULL) {
for (int file_idx = 0; file_idx < absolute_paths.size(); ++file_idx) { for (int file_idx = 0; file_idx < absolute_paths.size(); ++file_idx) {
std::cout << "\n\nhandling file: " << relative_paths[file_idx] << std::endl;
ret = api.archive_read_disk_new(); ret = api.archive_read_disk_new();
CHECK(ret.ok()) << "read_disk_new call failed"; CHECK(ret.ok()) << "read_disk_new call failed";
CHECK(ret.value() != NULL) << "Failed to create read_disk archive"; CHECK(ret.value() != NULL) << "Failed to create read_disk archive";
@ -337,13 +318,9 @@ static void create(const char *initial_filename, int compress,
<< "Unexpected result from read_disk_set_standard_lookup call"; << "Unexpected result from read_disk_set_standard_lookup call";
#endif #endif
// ret2 = api.archive_read_disk_open(&disk,
// sapi::v::ConstCStr(*argv).PtrBefore());
ret2 = api.archive_read_disk_open( ret2 = api.archive_read_disk_open(
&disk, &disk,
sapi::v::ConstCStr(absolute_paths[file_idx].c_str()).PtrBefore()); sapi::v::ConstCStr(absolute_paths[file_idx].c_str()).PtrBefore());
CHECK(ret2.ok()) << "read_disk_open call failed"; CHECK(ret2.ok()) << "read_disk_open call failed";
CHECK(ret2.value() == ARCHIVE_OK) CHECK(ret2.value() == ARCHIVE_OK)
<< CheckStatusAndGetString(api.archive_error_string(&disk), sandbox); << CheckStatusAndGetString(api.archive_error_string(&disk), sandbox);
@ -351,7 +328,7 @@ static void create(const char *initial_filename, int compress,
for (;;) { for (;;) {
int needcr = 0; int needcr = 0;
sapi::StatusOr<archive_entry *> ret3; sapi::StatusOr<archive_entry*> ret3;
ret3 = api.archive_entry_new(); ret3 = api.archive_entry_new();
CHECK(ret3.ok()) << "entry_new call failed"; CHECK(ret3.ok()) << "entry_new call failed";
@ -381,18 +358,13 @@ static void create(const char *initial_filename, int compress,
// of it will become /absolute/path/test_files/file1 and we change it to // of it will become /absolute/path/test_files/file1 and we change it to
// test_files/file1 so that it is relative. // test_files/file1 so that it is relative.
// std::cout << "relative = " << relative_paths[file_idx] << std::endl;
// std::cout << "absolute = " << absolute_paths[file_idx] << std::endl;
std::string path_name = std::string path_name =
CheckStatusAndGetString(api.archive_entry_pathname(&entry), sandbox); CheckStatusAndGetString(api.archive_entry_pathname(&entry), sandbox);
std::cout << "path_name initial = " << path_name << std::endl;
path_name.replace(path_name.begin(), path_name.replace(path_name.begin(),
path_name.begin() + absolute_paths[file_idx].length(), path_name.begin() + absolute_paths[file_idx].length(),
relative_paths[file_idx]); relative_paths[file_idx]);
// std::cout << "path_name after = " << path_name << std::endl;
// On top of those changes, we need to remove leading '/' characters // On top of those changes, we need to remove leading '/' characters
// and also remove everything up to the last occurrence of '../'. // and also remove everything up to the last occurrence of '../'.
@ -401,23 +373,17 @@ static void create(const char *initial_filename, int compress,
path_name.erase(path_name.begin(), path_name.begin() + found); path_name.erase(path_name.begin(), path_name.begin() + found);
} }
std::cout << "path_name 2 = " << path_name << std::endl;
found = path_name.rfind("../"); found = path_name.rfind("../");
std::cout << "found = " << found << std::endl;
if (found != std::string::npos) { if (found != std::string::npos) {
path_name = path_name.substr(found + 3); path_name = path_name.substr(found + 3);
} }
std::cout << "path_name 3 = " << path_name << std::endl;
CHECK(api.archive_entry_set_pathname( CHECK(api.archive_entry_set_pathname(
&entry, sapi::v::ConstCStr(path_name.c_str()).PtrBefore()) &entry, sapi::v::ConstCStr(path_name.c_str()).PtrBefore())
.ok()) .ok())
<< "Could not set pathname"; << "Could not set pathname";
if (verbose) { if (verbose) {
std::cout << "pathname = ";
std::cout << CheckStatusAndGetString(api.archive_entry_pathname(&entry), std::cout << CheckStatusAndGetString(api.archive_entry_pathname(&entry),
sandbox); sandbox);
needcr = 1; needcr = 1;
@ -435,45 +401,44 @@ static void create(const char *initial_filename, int compress,
<< "Unexpected result from write_header call"; << "Unexpected result from write_header call";
if (ret2.value() > ARCHIVE_FAILED) { if (ret2.value() > ARCHIVE_FAILED) {
// TODO copy part here // int fd = open(CheckStatusAndGetString(
int fd = open(CheckStatusAndGetString( // api.archive_entry_sourcepath(&entry), sandbox)
api.archive_entry_sourcepath(&entry), sandbox) // .c_str(),
.c_str(), // O_RDONLY);
O_RDONLY); int fd = open(path_name.c_str(), O_RDONLY);
CHECK(fd >= 0) << "Could not open file"; CHECK(fd >= 0) << "Could not open file";
sapi::v::Fd sapi_fd(fd); sapi::v::Fd sapi_fd(fd);
sapi::v::Int read_ret; sapi::v::Int read_ret;
sapi::v::Array<char> buff(16384); sapi::v::Array<char> buff(kBuffSize);
sapi::v::UInt ssize(16384); sapi::v::UInt ssize(kBuffSize);
CHECK(sandbox.Allocate(&buff, true).ok())
<< "Could not allocate remote buffer";
CHECK(sandbox.Allocate(&buff, true).ok()) << "Could not allocate remote buffer";
// CHECK(sandbox.Allocate(&buff, true).ok()) << "Could not allocate
// buffer";
CHECK(sandbox.TransferToSandboxee(&sapi_fd).ok()) CHECK(sandbox.TransferToSandboxee(&sapi_fd).ok())
<< "Could not transfer file descriptor"; << "Could not transfer file descriptor";
// sandbox.Call()
CHECK(sandbox.Call("read", &read_ret, &sapi_fd, buff.PtrNone(), &ssize) CHECK(sandbox.Call("read", &read_ret, &sapi_fd, buff.PtrNone(), &ssize)
.ok()) .ok())
<< "Read call failed"; << "Read call failed";
while (read_ret.GetValue() > 0) { while (read_ret.GetValue() > 0) {
CHECK(api.archive_write_data(&a, buff.PtrNone(), read_ret.GetValue()) CHECK(api.archive_write_data(&a, buff.PtrNone(), read_ret.GetValue())
.ok()) .ok())
<< "write_data call failed"; << "write_data call failed";
// CHECK(ret.ok());
CHECK( CHECK(
sandbox.Call("read", &read_ret, &sapi_fd, buff.PtrNone(), &ssize) sandbox.Call("read", &read_ret, &sapi_fd, buff.PtrNone(), &ssize)
.ok()) .ok())
<< "Read call failed"; << "Read call failed";
} }
// TODO close fds // sapi_fd variable goes out of scope here so both the local and the
// CHECK(sapi_fd.CloseRemoteFd(sandbox.GetRpcChannel()).ok()) // remote file descriptors are closed.
// << "Could not close remote fd";
// sapi_fd.CloseLocalFd();
} }
CHECK(api.archive_entry_free(&entry).ok()) << "entry_free call failed"; CHECK(api.archive_entry_free(&entry).ok()) << "entry_free call failed";
if (needcr) { if (needcr) {
std::cout << std::endl; std::cout << std::endl;
} }
@ -486,9 +451,6 @@ static void create(const char *initial_filename, int compress,
ret2 = api.archive_read_free(&disk); ret2 = api.archive_read_free(&disk);
CHECK(ret2.ok()) << "read_free call failed"; CHECK(ret2.ok()) << "read_free call failed";
CHECK(!ret2.value()) << "Unexpected result from read_free call"; CHECK(!ret2.value()) << "Unexpected result from read_free call";
// ++argv;
// ++file_idx;
} }
ret2 = api.archive_write_close(&a); ret2 = api.archive_write_close(&a);
@ -501,45 +463,45 @@ static void create(const char *initial_filename, int compress,
} }
#endif #endif
static void extract(const char *filename, int do_extract, int flags) { static void extract(const char* filename, int do_extract, int flags) {
std::string tmp_dir; std::string tmp_dir;
if (do_extract) { if (do_extract) {
tmp_dir = CreateTempDirAtCWD(); tmp_dir = CreateTempDirAtCWD();
} }
// We can use a struct like this in order to delete the temporary
// directory that was created earlier whenever the function ends.
struct ExtractTempDirectoryCleanup { struct ExtractTempDirectoryCleanup {
~ExtractTempDirectoryCleanup() { ~ExtractTempDirectoryCleanup() {
sandbox2::file_util::fileops::DeleteRecursively(capture); sandbox2::file_util::fileops::DeleteRecursively(dir);
} }
const std::string capture; std::string dir;
} cleanup{tmp_dir}; };
// We should only delete it if the do_extract flag is true which
// means that this struct is instantiated only in that case.
std::shared_ptr<ExtractTempDirectoryCleanup> cleanup_ptr;
if (do_extract) {
cleanup_ptr = std::make_unique<ExtractTempDirectoryCleanup>();
cleanup_ptr->dir = tmp_dir;
}
std::cout << "extract" << std::endl;
std::string filename_absolute = MakeAbsolutePathAtCWD(filename); std::string filename_absolute = MakeAbsolutePathAtCWD(filename);
std::cout << "filename = " << filename_absolute << std::endl;
SapiLibarchiveSandboxExtract sandbox(filename_absolute, do_extract, tmp_dir); SapiLibarchiveSandboxExtract sandbox(filename_absolute, do_extract, tmp_dir);
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization"; CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
LibarchiveApi api(&sandbox); LibarchiveApi api(&sandbox);
// struct archive *a; sapi::StatusOr<archive*> ret = api.archive_read_new();
// struct archive *ext;
// struct archive_entry *entry;
// int r;
sapi::StatusOr<archive *> ret = api.archive_read_new();
CHECK(ret.ok()) << "archive_read_new call failed"; CHECK(ret.ok()) << "archive_read_new call failed";
// std::cout << "RET VALUE = " << ret.value() << std::endl;
CHECK(ret.value() != NULL) << "Failed to create read archive"; CHECK(ret.value() != NULL) << "Failed to create read archive";
// a = ret.value();
sapi::v::RemotePtr a(ret.value()); sapi::v::RemotePtr a(ret.value());
ret = api.archive_write_disk_new(); ret = api.archive_write_disk_new();
CHECK(ret.ok()) << "write_disk_new call failed"; CHECK(ret.ok()) << "write_disk_new call failed";
CHECK(ret.value() != NULL) << "Failed to create write disk archive"; CHECK(ret.value() != NULL) << "Failed to create write disk archive";
// ext = ret.value();
sapi::v::RemotePtr ext(ret.value()); sapi::v::RemotePtr ext(ret.value());
@ -586,34 +548,23 @@ static void extract(const char *filename, int do_extract, int flags) {
<< "Unexpected result from write_disk_set_standard_lookup call"; << "Unexpected result from write_disk_set_standard_lookup call";
#endif #endif
const char *filename_ptr = filename_absolute.c_str(); const char* filename_ptr = filename_absolute.c_str();
if (filename_ptr != NULL && strcmp(filename_ptr, "-") == 0) { if (filename_ptr != NULL && strcmp(filename_ptr, "-") == 0) {
filename_ptr = NULL; filename_ptr = NULL;
} }
// sapi::v::ConstCStr sapi_filename(filename_absolute.c_str());
std::cout << "opening filename" << std::endl;
ret2 = api.archive_read_open_filename( ret2 = api.archive_read_open_filename(
&a, sapi::v::ConstCStr(filename_ptr).PtrBefore(), 10240); &a, sapi::v::ConstCStr(filename_ptr).PtrBefore(), kBlockSize);
CHECK(ret2.ok()) << "read_open_filename call failed"; CHECK(ret2.ok()) << "read_open_filename call failed";
// CHECK(!ret2.value()) << GetErrorString(&a_ptr, sandbox, api);
CHECK(!ret2.value()) << CheckStatusAndGetString(api.archive_error_string(&a), CHECK(!ret2.value()) << CheckStatusAndGetString(api.archive_error_string(&a),
sandbox); sandbox);
// CHECK(!ret2.value()) << CallFunctionAndGetString(&a_ptr, sandbox, &api,
// &api.archive_error_string);
for (;;) { for (;;) {
int needcr = 0; int needcr = 0;
std::cout << "================reading headers==============" << std::endl; sapi::v::IntBase<struct archive_entry*> entry_ptr_tmp(0);
sapi::v::IntBase<struct archive_entry *> entry_ptr_tmp(0);
ret2 = api.archive_read_next_header(&a, entry_ptr_tmp.PtrBoth()); ret2 = api.archive_read_next_header(&a, entry_ptr_tmp.PtrAfter());
// std::cout << "val = " << ret2.value() << std::endl;
CHECK(ret2.ok()) << "read_next_header call failed"; CHECK(ret2.ok()) << "read_next_header call failed";
// CHECK(ret2.value() != ARCHIVE_OK) << GetErrorString(&a_ptr, sandbox,
// api);
if (ret2.value() == ARCHIVE_EOF) { if (ret2.value() == ARCHIVE_EOF) {
break; break;
@ -635,15 +586,10 @@ static void extract(const char *filename, int do_extract, int flags) {
needcr = 1; needcr = 1;
} }
std::cout << "qqqqq" << std::endl;
if (do_extract) { if (do_extract) {
std::cout << "EXTRACT HERE" << std::endl;
ret2 = api.archive_write_header(&ext, &entry); ret2 = api.archive_write_header(&ext, &entry);
CHECK(ret2.ok()) << "write_header call failed"; CHECK(ret2.ok()) << "write_header call failed";
std::cout << "val = " << ret2.value() << std::endl;
if (ret2.value() != ARCHIVE_OK) { if (ret2.value() != ARCHIVE_OK) {
std::cout << CheckStatusAndGetString(api.archive_error_string(&a), std::cout << CheckStatusAndGetString(api.archive_error_string(&a),
sandbox); sandbox);
@ -658,8 +604,6 @@ static void extract(const char *filename, int do_extract, int flags) {
} }
} }
std::cout << "out of loop" << std::endl;
ret2 = api.archive_read_close(&a); ret2 = api.archive_read_close(&a);
CHECK(ret2.ok()) << "read_close call failed"; CHECK(ret2.ok()) << "read_close call failed";
CHECK(!ret2.value()) << "Unexpected value from read_close call"; CHECK(!ret2.value()) << "Unexpected value from read_close call";
@ -675,25 +619,20 @@ static void extract(const char *filename, int do_extract, int flags) {
ret2 = api.archive_write_free(&ext); ret2 = api.archive_write_free(&ext);
CHECK(ret2.ok()) << "write_free call failed"; CHECK(ret2.ok()) << "write_free call failed";
CHECK(!ret2.value()) << "Unexpected result from write_free call"; CHECK(!ret2.value()) << "Unexpected result from write_free call";
// if (do_extract) {
// sandbox2::file_util::fileops::DeleteRecursively(tmp_dir);
// }
} }
static int copy_data(sapi::v::RemotePtr *ar, sapi::v::RemotePtr *aw, static int copy_data(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw,
LibarchiveApi &api, LibarchiveApi& api,
SapiLibarchiveSandboxExtract &sandbox) { SapiLibarchiveSandboxExtract& sandbox) {
std::cout << "CALL COPY_DATA XXXXXXXXXXXX\n";
sapi::StatusOr<int> ret; sapi::StatusOr<int> ret;
sapi::v::IntBase<struct archive_entry *> buff_ptr_tmp(0); sapi::v::IntBase<struct archive_entry*> buff_ptr_tmp(0);
sapi::v::ULLong size; sapi::v::ULLong size;
sapi::v::SLLong offset; sapi::v::SLLong offset;
for (;;) { for (;;) {
ret = api.archive_read_data_block(ar, buff_ptr_tmp.PtrBoth(), ret = api.archive_read_data_block(ar, buff_ptr_tmp.PtrAfter(),
size.PtrBoth(), offset.PtrBoth()); size.PtrAfter(), offset.PtrAfter());
CHECK(ret.ok()) << "read_data_block call failed"; CHECK(ret.ok()) << "read_data_block call failed";
if (ret.value() == ARCHIVE_EOF) { if (ret.value() == ARCHIVE_EOF) {
@ -720,18 +659,18 @@ static int copy_data(sapi::v::RemotePtr *ar, sapi::v::RemotePtr *aw,
} }
} }
static void msg(const char *m) { write(1, m, strlen(m)); } // static void msg(const char* m) { write(1, m, strlen(m)); }
static void errmsg(const char *m) { // static void errmsg(const char* m) {
if (m == NULL) { // if (m == NULL) {
m = "Error: No error description provided.\n"; // m = "Error: No error description provided.\n";
} // }
write(2, m, strlen(m)); // write(2, m, strlen(m));
} // }
static void usage(void) { static void usage(void) {
/* Many program options depend on compile options. */ /* Many program options depend on compile options. */
const char *m = const char* m =
"Usage: minitar [-" "Usage: minitar [-"
#ifndef NO_CREATE #ifndef NO_CREATE
"c" "c"
@ -751,6 +690,6 @@ static void usage(void) {
#endif #endif
"] [-f file] [file]\n"; "] [-f file] [file]\n";
errmsg(m); std::cout << m << std::endl;
exit(1); exit(EXIT_FAILURE);
} }