summaryrefslogtreecommitdiff
path: root/artd/file_utils.h
blob: 77c7ffb19c2f1fd4c22edf70170e76ede6c5b3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_ARTD_FILE_UTILS_H_
#define ART_ARTD_FILE_UTILS_H_

#include <sys/types.h>

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "aidl/com/android/server/art/FsPermission.h"
#include "android-base/result.h"
#include "base/os.h"

namespace art {
namespace artd {

// A class that creates a new file that will eventually be committed to the given path. The new file
// is created at a temporary location. It will not overwrite the file at the given path until
// `CommitOrAbandon` has been called and will be automatically cleaned up on object destruction
// unless `CommitOrAbandon` has been called.
// The new file is opened without O_CLOEXEC so that it can be passed to subprocesses.
class NewFile {
 public:
  // Creates a new file at the given path with the given permission.
  static android::base::Result<std::unique_ptr<NewFile>> Create(
      const std::string& path, const aidl::com::android::server::art::FsPermission& fs_permission);

  NewFile(const NewFile&) = delete;
  NewFile& operator=(const NewFile&) = delete;
  NewFile(NewFile&& other) noexcept
      : fd_(std::exchange(other.fd_, -1)),
        final_path_(std::move(other.final_path_)),
        temp_path_(std::move(other.temp_path_)),
        temp_id_(std::move(other.temp_id_)),
        fs_permission_(other.fs_permission_) {}

  // Deletes the file if it is not committed.
  virtual ~NewFile();

  int Fd() const { return fd_; }

  // The path that the file will eventually be committed to.
  const std::string& FinalPath() const { return final_path_; }

  // The path to the new file.
  const std::string& TempPath() const { return temp_path_; }

  // The unique ID of the new file. Can be used by `BuildTempPath` for reconstructing the path to
  // the file.
  const std::string& TempId() const { return temp_id_; }

  // Closes the new file, keeps it, moves the file to the final path, and overwrites any existing
  // file at that path, or abandons the file on failure. The fd will be invalid after this function
  // is called.
  android::base::Result<void> CommitOrAbandon();

  // Closes the new file and keeps it at the temporary location. The file will not be automatically
  // cleaned up on object destruction. The file can be found at `TempPath()` (i.e.,
  // `BuildTempPath(FinalPath(), TempId())`). The fd will be invalid after this function is called.
  virtual android::base::Result<void> Keep();

  // Unlinks and closes the new file if it is not committed. The fd will be invalid after this
  // function is called.
  void Cleanup();

  // Commits all new files, replacing old files, and removes given files in addition. Or abandons
  // new files and restores old files at best effort if any error occurs. The fds will be invalid
  // after this function is called.
  //
  // Note: This function is NOT thread-safe. It is intended to be used in single-threaded code or in
  // cases where some race condition is acceptable.
  //
  // Usage:
  //
  // Commit `file_1` and `file_2`, and remove the file at "path_3":
  //   CommitAllOrAbandon({file_1, file_2}, {"path_3"});
  static android::base::Result<void> CommitAllOrAbandon(
      const std::vector<NewFile*>& files_to_commit,
      const std::vector<std::string_view>& files_to_remove = {});

  // Returns the path to a temporary file. See `Keep`.
  static std::string BuildTempPath(std::string_view final_path, const std::string& id);

 private:
  NewFile(const std::string& path,
          const aidl::com::android::server::art::FsPermission& fs_permission)
      : final_path_(path), fs_permission_(fs_permission) {}

  android::base::Result<void> Init();

  // Unlinks the new file. The fd will still be valid after this function is called.
  void Unlink();

  int fd_ = -1;
  std::string final_path_;
  std::string temp_path_;
  std::string temp_id_;
  aidl::com::android::server::art::FsPermission fs_permission_;
};

// Opens a file for reading.
android::base::Result<std::unique_ptr<File>> OpenFileForReading(const std::string& path);

// Converts FsPermission to Linux access mode for a file.
mode_t FileFsPermissionToMode(const aidl::com::android::server::art::FsPermission& fs_permission);

// Converts FsPermission to Linux access mode for a directory.
mode_t DirFsPermissionToMode(const aidl::com::android::server::art::FsPermission& fs_permission);

// Changes the owner based on FsPermission.
android::base::Result<void> Chown(
    const std::string& path, const aidl::com::android::server::art::FsPermission& fs_permission);

// Moves every file in `files_to_move` from a given location to another, replacing the existing file
// at the destination if it exists, and removes files in `files_to_remove` in addition. Or abandons
// all files in `files_to_move` and restores old files at best effort if any error occurs.
//
// This function does not accept duplicate paths. Passing duplicate paths to this function leads to
// undefined behavior.
//
// Note: This function is NOT thread-safe. It is intended to be used in single-threaded code or in
// cases where some race condition is acceptable.
//
// Usage:
//
// Move file at `path_1` to `path_2`, move file at `path_3` to `file_4`, and remove the file at
// "path_5":
//   MoveAllOrAbandon({{"path_1", "path_2"}, {"path_3", "path_4}}, {"path_5"});
android::base::Result<void> MoveAllOrAbandon(
    const std::vector<std::pair<std::string_view, std::string_view>>& files_to_move,
    const std::vector<std::string_view>& files_to_remove = {});

// Same as above, but takes `std::string`s.
android::base::Result<void> MoveAllOrAbandon(
    const std::vector<std::pair<std::string, std::string>>& files_to_move,
    const std::vector<std::string>& files_to_remove = {});

}  // namespace artd
}  // namespace art

#endif  // ART_ARTD_FILE_UTILS_H_