1*14675a02SAndroid Build Coastguard Worker /* 2*14675a02SAndroid Build Coastguard Worker * Copyright 2022 Google LLC 3*14675a02SAndroid Build Coastguard Worker * 4*14675a02SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*14675a02SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*14675a02SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*14675a02SAndroid Build Coastguard Worker * 8*14675a02SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*14675a02SAndroid Build Coastguard Worker * 10*14675a02SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*14675a02SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*14675a02SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*14675a02SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*14675a02SAndroid Build Coastguard Worker * limitations under the License. 15*14675a02SAndroid Build Coastguard Worker */ 16*14675a02SAndroid Build Coastguard Worker 17*14675a02SAndroid Build Coastguard Worker #ifndef FCP_CLIENT_CACHE_TEMP_FILES_H_ 18*14675a02SAndroid Build Coastguard Worker #define FCP_CLIENT_CACHE_TEMP_FILES_H_ 19*14675a02SAndroid Build Coastguard Worker 20*14675a02SAndroid Build Coastguard Worker #include <filesystem> 21*14675a02SAndroid Build Coastguard Worker #include <memory> 22*14675a02SAndroid Build Coastguard Worker #include <string> 23*14675a02SAndroid Build Coastguard Worker 24*14675a02SAndroid Build Coastguard Worker #include "absl/status/status.h" 25*14675a02SAndroid Build Coastguard Worker #include "absl/status/statusor.h" 26*14675a02SAndroid Build Coastguard Worker #include "fcp/client/files.h" 27*14675a02SAndroid Build Coastguard Worker #include "fcp/client/log_manager.h" 28*14675a02SAndroid Build Coastguard Worker 29*14675a02SAndroid Build Coastguard Worker namespace fcp { 30*14675a02SAndroid Build Coastguard Worker namespace client { 31*14675a02SAndroid Build Coastguard Worker namespace cache { 32*14675a02SAndroid Build Coastguard Worker 33*14675a02SAndroid Build Coastguard Worker // Manages temporary files created by the federated compute runtime. Unlike 34*14675a02SAndroid Build Coastguard Worker // other Files implementations, TempFiles will clean up created temporary files 35*14675a02SAndroid Build Coastguard Worker // eagerly as part its construction and deletion. 36*14675a02SAndroid Build Coastguard Worker class TempFiles : public Files { 37*14675a02SAndroid Build Coastguard Worker public: 38*14675a02SAndroid Build Coastguard Worker static constexpr char kParentDir[] = "fcp"; 39*14675a02SAndroid Build Coastguard Worker // The subdirectory temporary files will be created in. Files in this 40*14675a02SAndroid Build Coastguard Worker // directory are deleted at the end of a federated computation. 41*14675a02SAndroid Build Coastguard Worker static constexpr char kTempFilesDir[] = "tmp"; 42*14675a02SAndroid Build Coastguard Worker 43*14675a02SAndroid Build Coastguard Worker // Factory method to create TempFiles. The provided cache dir is the 44*14675a02SAndroid Build Coastguard Worker // absolute path for storing cached files. TempFiles will attempt to 45*14675a02SAndroid Build Coastguard Worker // create subdirectories and files, so the directory must grant read/write 46*14675a02SAndroid Build Coastguard Worker // access. 47*14675a02SAndroid Build Coastguard Worker static absl::StatusOr<std::unique_ptr<TempFiles>> Create( 48*14675a02SAndroid Build Coastguard Worker const std::string& cache_dir, LogManager* log_manager); 49*14675a02SAndroid Build Coastguard Worker 50*14675a02SAndroid Build Coastguard Worker // Creates a temporary file. TempFiles will delete these files at the end 51*14675a02SAndroid Build Coastguard Worker // of a federated computation run, or upon the next creation of a TempFiles 52*14675a02SAndroid Build Coastguard Worker // instance. 53*14675a02SAndroid Build Coastguard Worker // On success, returns a file path. 54*14675a02SAndroid Build Coastguard Worker // On error, returns 55*14675a02SAndroid Build Coastguard Worker // - INTERNAL - unexpected error. 56*14675a02SAndroid Build Coastguard Worker // - INVALID_ARGUMENT - on "expected" errors such as I/O issues. 57*14675a02SAndroid Build Coastguard Worker absl::StatusOr<std::string> CreateTempFile( 58*14675a02SAndroid Build Coastguard Worker const std::string& prefix, const std::string& suffix) override; 59*14675a02SAndroid Build Coastguard Worker 60*14675a02SAndroid Build Coastguard Worker // Any temporary Files created with TempFiles will be deleted. 61*14675a02SAndroid Build Coastguard Worker ~TempFiles() override; 62*14675a02SAndroid Build Coastguard Worker 63*14675a02SAndroid Build Coastguard Worker // TempFiles is neither copyable nor movable. 64*14675a02SAndroid Build Coastguard Worker TempFiles(const TempFiles&) = delete; 65*14675a02SAndroid Build Coastguard Worker TempFiles& operator=(const TempFiles&) = delete; 66*14675a02SAndroid Build Coastguard Worker 67*14675a02SAndroid Build Coastguard Worker private: TempFiles(std::filesystem::path temp_files_dir,LogManager * log_manager)68*14675a02SAndroid Build Coastguard Worker TempFiles(std::filesystem::path temp_files_dir, LogManager* log_manager) 69*14675a02SAndroid Build Coastguard Worker : temp_files_dir_(temp_files_dir), log_manager_(*log_manager) {} 70*14675a02SAndroid Build Coastguard Worker 71*14675a02SAndroid Build Coastguard Worker const std::filesystem::path temp_files_dir_; 72*14675a02SAndroid Build Coastguard Worker LogManager& log_manager_; 73*14675a02SAndroid Build Coastguard Worker }; 74*14675a02SAndroid Build Coastguard Worker 75*14675a02SAndroid Build Coastguard Worker } // namespace cache 76*14675a02SAndroid Build Coastguard Worker } // namespace client 77*14675a02SAndroid Build Coastguard Worker } // namespace fcp 78*14675a02SAndroid Build Coastguard Worker 79*14675a02SAndroid Build Coastguard Worker #endif // FCP_CLIENT_CACHE_TEMP_FILES_H_ 80