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