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