1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SAPI_LIBARCHIVE_EXAMPLES_MINITAR_H 16 #define SAPI_LIBARCHIVE_EXAMPLES_MINITAR_H 17 18 #include <archive.h> 19 #include <archive_entry.h> 20 #include <fcntl.h> 21 22 #include "libarchive_sapi.sapi.h" // NOLINT(build/include) 23 #include "sandbox.h" // NOLINT(build/include) 24 #include "sandboxed_api/sandbox2/util.h" 25 #include "sandboxed_api/util/path.h" 26 #include "sandboxed_api/util/temp_file.h" 27 28 // Creates an archive file at the given filename. 29 absl::Status CreateArchive(const char* filename, int compress, 30 const char** argv, bool verbose = true); 31 32 // Extracts an archive file. If do_extract is true, the files will 33 // be created relative to the current working directory. If do_extract 34 // is false then the function will just print the entries of the archive. 35 absl::Status ExtractArchive(const char* filename, int do_extract, int flags, 36 bool verbose = true); 37 38 // This function is only called from the "extract function". It is still 39 // isolated in order to not modify the code structure as much. 40 absl::StatusOr<int> CopyData(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw, 41 LibarchiveApi& api, 42 SapiLibarchiveSandboxExtract& sandbox); 43 44 inline constexpr size_t kBlockSize = 10240; 45 inline constexpr size_t kBuffSize = 16384; 46 47 // Converts one string to an absolute path by prepending the current 48 // working directory to the relative path. 49 // The path is also cleaned at the end. 50 std::string MakeAbsolutePathAtCWD(const std::string& path); 51 52 // This function takes a status as argument and after checking the status 53 // it transfers the string. This is used mostly with archive_error_string 54 // and other library functions that return a char*. 55 absl::StatusOr<std::string> CheckStatusAndGetString( 56 const absl::StatusOr<char*>& status, LibarchiveSandbox& sandbox); 57 58 // Creates a temporary directory in the current working directory and 59 // returns the path. This is used in the extract function where the sandboxed 60 // process changes the current working directory to this temporary directory. 61 absl::StatusOr<std::string> CreateTempDirAtCWD(); 62 63 #endif // SAPI_LIBARCHIVE_EXAMPLES_MINITAR_H 64