xref: /aosp_15_r20/external/sandboxed-api/contrib/libzip/utils/utils_zip.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2022 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 CONTRIB_LIBZIP_UTILS_UTILS_ZIP_H_
16 #define CONTRIB_LIBZIP_UTILS_UTILS_ZIP_H_
17 
18 #include <fcntl.h>
19 
20 #include "absl/log/die_if_null.h"
21 #include "contrib/libzip/sandboxed.h"
22 #include "sandboxed_api/util/status_macros.h"
23 
24 class LibZip {
25  public:
LibZip(ZipSandbox * sandbox,std::string filename,int flags)26   explicit LibZip(ZipSandbox* sandbox, std::string filename, int flags)
27       : sandbox_(ABSL_DIE_IF_NULL(sandbox)),
28         api_(sandbox_),
29         filename_(std::move(filename)),
30         flags_(flags),
31         rfd_(open(filename.c_str(), O_RDWR | O_CREAT)) {
32     OpenRemote().IgnoreError();
33   }
34 
35   ~LibZip();
36 
37   bool IsOpen();
38 
39   absl::StatusOr<std::string> GetName(uint64_t index);
40   absl::StatusOr<uint64_t> GetNumberEntries();
41   absl::StatusOr<std::vector<uint8_t>> ReadFile(uint64_t index);
42   absl::StatusOr<std::vector<uint8_t>> ReadFile(const std::string& filename);
43   absl::StatusOr<uint64_t> AddFile(const std::string& filename,
44                                    std::vector<uint8_t>& buf);
45   absl::StatusOr<uint64_t> AddFile(const std::string& filename, int fd);
46   absl::Status ReplaceFile(uint64_t index, std::vector<uint8_t>& buf);
47   absl::Status ReplaceFile(uint64_t index, int fd);
48   absl::Status DeleteFile(uint64_t index);
49 
50   absl::StatusOr<std::string> GetError();
51 
52   absl::Status Finish();
53   // Save a copy of file to another fd.
54   absl::Status Save(int fd);
55   // Save a copy to the same fd.
56   absl::Status Save();
57 
58  protected:
59   bool IsOpenLocal();
60   absl::Status OpenRemote();
61   absl::StatusOr<std::vector<uint8_t>> ReadFile(sapi::v::RemotePtr& zipfile,
62                                                 uint32_t size);
63   absl::StatusOr<uint64_t> AddFile(const std::string& filename,
64                                    sapi::v::RemotePtr& rzipsource);
65   absl::Status ReplaceFile(uint64_t index, sapi::v::RemotePtr& rzipsource);
66 
67   absl::StatusOr<void*> GetSource(std::vector<uint8_t>& buf);
68   absl::StatusOr<void*> GetSource(int fd, const std::string& mode);
69   absl::StatusOr<void*> CreateSourceFromFd(sapi::v::Fd& rfd);
70 
71   absl::Status Save(sapi::v::Fd& fd);
72 
73   absl::Status CheckOpen();
74   absl::Status CheckFinished();
75 
76  private:
77   ZipSandbox* sandbox_;
78   ZipApi api_;
79   int flags_;
80   std::unique_ptr<sapi::v::RemotePtr> zip_;
81   std::unique_ptr<sapi::v::RemotePtr> zipsource_;
82   sapi::v::Fd rfd_;
83   std::string filename_;
84 };
85 
86 #endif  // CONTRIB_LIBZIP_UTILS_UTILS_ZIP_H_
87