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 GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_ 16 #define GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_ 17 18 #include <syscall.h> 19 20 #include "guetzli_sandbox.h" // NOLINT(build/include) 21 #include "absl/status/statusor.h" 22 #include "sandboxed_api/transaction.h" 23 #include "sandboxed_api/vars.h" 24 25 namespace guetzli::sandbox { 26 27 enum class ImageType { kJpeg, kPng }; 28 29 struct TransactionParams { 30 const char* in_file = nullptr; 31 const char* out_file = nullptr; 32 int verbose = 0; 33 int quality = 0; 34 int memlimit_mb = 0; 35 }; 36 37 // Instance of this transaction shouldn't be reused 38 // Create a new one for each processing operation 39 class GuetzliTransaction : public sapi::Transaction { 40 public: 41 explicit GuetzliTransaction(TransactionParams params, int retry_count = 0) Transaction(std::make_unique<GuetzliSapiSandbox> ())42 : sapi::Transaction(std::make_unique<GuetzliSapiSandbox>()), 43 params_(std::move(params)) { 44 set_retry_count(retry_count); 45 SetTimeLimit(absl::InfiniteDuration()); 46 } 47 48 private: 49 absl::Status Main() final; 50 51 absl::Status LinkOutFile(int out_fd) const; 52 absl::StatusOr<ImageType> GetImageTypeFromFd(int fd) const; 53 54 const TransactionParams params_; 55 ImageType image_type_ = ImageType::kJpeg; 56 }; 57 58 } // namespace guetzli::sandbox 59 60 #endif // GUETZLI_SANDBOXED_GUETZLI_TRANSACTION_H_ 61