1 // Copyright 2015 The Android Open Source Project
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 //      http://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 WEBSERVER_WEBSERVD_TEMP_FILE_MANAGER_H_
16 #define WEBSERVER_WEBSERVD_TEMP_FILE_MANAGER_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include <base/files/file_path.h>
23 #include <base/macros.h>
24 
25 namespace webservd {
26 
27 // TempFileManager maintains life-times of temporary files associated with HTTP
28 // requests. Web server might require temporary storage to back certain large
29 // requests and this class is used to track those files and make sure that all
30 // the temporary files are deleted when the request is complete.
31 class TempFileManager {
32  public:
33   // FileSystemInterface allows to abstract the file system in tests.
34   class FileDeleterInterface {
35    public:
36     virtual bool DeleteFile(const base::FilePath& path) = 0;
37 
38    protected:
39     virtual ~FileDeleterInterface() = default;
40   };
41 
42   TempFileManager(const base::FilePath& temp_dir_path,
43                   FileDeleterInterface* file_deleter);
44   ~TempFileManager();
45 
46   // Generate a new temporary file name for a request with unique ID
47   // |request_id|. No actual file is created on the file system at this time.
48   // The file name is registered with the request ID so it can be later deleted
49   // when request is completed.
50   base::FilePath CreateTempFileName(const std::string& request_id);
51 
52   // Deletes all the files belonging to the given request.
53   void DeleteRequestTempFiles(const std::string& request_id);
54 
55  private:
56   // Deletes all the files in the list.
57   void DeleteFiles(const std::vector<base::FilePath>& files);
58 
59   // Root temp directory to store temporary files into.
60   base::FilePath temp_dir_path_;
61   // File system interface to abstract underlying file system for testing.
62   FileDeleterInterface* file_deleter_;
63 
64   // List of files belonging to a particular request.
65   std::map<std::string, std::vector<base::FilePath>> request_files_;
66 
67   DISALLOW_COPY_AND_ASSIGN(TempFileManager);
68 };
69 
70 // Actual implementation of FileDeleterInterface to delete temporary files
71 // on the real file system.
72 class FileDeleter : public TempFileManager::FileDeleterInterface {
73  public:
74   bool DeleteFile(const base::FilePath& path) override;
75 };
76 
77 }  // namespace webservd
78 
79 #endif  // WEBSERVER_WEBSERVD_TEMP_FILE_MANAGER_H_
80