• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 ANDROID_INSTALLD_UNIQUE_FILE_H
18 #define ANDROID_INSTALLD_UNIQUE_FILE_H
19 
20 #include <functional>
21 #include <string>
22 
23 namespace android {
24 namespace installd {
25 
26 // A file management helper that serves two purposes:
27 //
28 // 1. Closes the file description on destruction, similar unique_fd.
29 // 2. Runs a cleanup function on after close, if not cancelled.
30 //
31 // The class does not assume the relationship between the given fd and file path.
32 //
33 // Example:
34 //
35 //   UniqueFile file(open(...),
36 //                           filepath,
37 //                           [](const std::string& path) {
38 //                               unlink(path.c_str());
39 //                           });
40 //   if (file.fd() == -1) {
41 //       // Error opening...
42 //   }
43 //
44 //   ...
45 //   if (error) {
46 //       // At this point, when the UniqueFile is destructed, the cleanup function will run
47 //       // (e.g. to delete the file) after the fd is closed.
48 //       return -1;
49 //   }
50 //
51 //   (Success case)
52 //   file.DisableCleanup();
53 //   // At this point, when the UniqueFile is destructed, the cleanup function will not run
54 //   // (e.g. leaving the file around) after the fd is closed.
55 //
56 class UniqueFile {
57  private:
58     using CleanUpFunction = std::function<void (const std::string&)>;
59 
60  public:
61     UniqueFile();
62     UniqueFile(int value, std::string path);
63     UniqueFile(int value, std::string path, CleanUpFunction cleanup);
64     UniqueFile(UniqueFile&& other);
65     ~UniqueFile();
66 
67     UniqueFile& operator=(UniqueFile&& other);
68 
fd()69     int fd() const {
70         return value_;
71     }
72 
path()73     const std::string& path() const {
74       return path_;
75     }
76 
DisableAutoClose()77     void DisableAutoClose() {
78         auto_close_ = false;
79     }
80 
DisableCleanup()81     void DisableCleanup() {
82         do_cleanup_ = false;
83     }
84 
85     void reset();
86     void reset(int new_value, std::string path, CleanUpFunction new_cleanup = nullptr);
87 
88  private:
89     void release();
90 
91     int value_;
92     std::string path_;
93     CleanUpFunction cleanup_;
94     bool do_cleanup_;
95     bool auto_close_;
96 };
97 
98 }  // namespace installd
99 }  // namespace android
100 
101 #endif  // ANDROID_INSTALLD_UNIQUE_FILE_H
102