// 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);
// 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);
// Opens a file for reading.
android::base::Result<std::unique_ptr<File>> OpenFileForReading(const std::string& path);
// Opens a directory as a dirfd.
android::base::Result<android::base::unique_fd> OpenDirectory(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_
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.