Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 4 | #ifndef IMAGELOADER_IMAGELOADER_IMPL_H_ |
| 5 | #define IMAGELOADER_IMAGELOADER_IMPL_H_ |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 6 | |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 7 | #include <map> |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 8 | #include <string> |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 9 | #include <utility> |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include <base/files/file_path.h> |
| 13 | #include <base/gtest_prod_util.h> |
| 14 | #include <base/macros.h> |
| 15 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 16 | #include "imageloader/helper_process_proxy.h" |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 17 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 18 | namespace imageloader { |
| 19 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 20 | using Keys = std::vector<std::vector<uint8_t>>; |
| 21 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 22 | struct ImageLoaderConfig { |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 23 | ImageLoaderConfig(const Keys& keys, |
| 24 | const char* storage_path, |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 25 | const char* mount_path) |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 26 | : keys(keys), storage_dir(storage_path), mount_path(mount_path) {} |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 27 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 28 | Keys keys; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 29 | base::FilePath storage_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 30 | base::FilePath mount_path; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class ImageLoaderImpl { |
| 34 | public: |
| 35 | // Instantiate an object with a configuration object. |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 36 | explicit ImageLoaderImpl(ImageLoaderConfig config) |
| 37 | : config_(std::move(config)) {} |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 38 | ImageLoaderImpl(const ImageLoaderImpl&) = delete; |
| 39 | ImageLoaderImpl& operator=(const ImageLoaderImpl&) = delete; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 40 | |
| 41 | // Register a component. |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 42 | bool RegisterComponent(const std::string& name, |
| 43 | const std::string& version, |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 44 | const std::string& component_folder_abs_path); |
| 45 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 46 | // Remove a component. |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 47 | bool RemoveComponent(const std::string& name); |
| 48 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 49 | // Enumerates all mount point paths with prefix of |parent_dir| and returns |
| 50 | // them with |paths|. If |dry_run| is true, no mount points are unmounted. |
| 51 | // If |dry_run| is false, all mount points returned in |paths| are unmounted. |
| 52 | bool CleanupAll(bool dry_run, |
| 53 | const base::FilePath& parent_dir, |
| 54 | std::vector<std::string>* paths, |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 55 | HelperProcessProxy* proxy); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 56 | |
| 57 | // Cleanup a mount point at |path|. |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 58 | bool Cleanup(const base::FilePath& path, HelperProcessProxy* proxy); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 59 | |
Amin Hassani | 0badef1 | 2019-03-18 17:08:02 -0700 | [diff] [blame] | 60 | // Cleanup a DLC module image mount point given DLC |id| and |package|. |
| 61 | bool UnloadDlcImage(const std::string& id, |
| 62 | const std::string& package, |
| 63 | HelperProcessProxy* proxy); |
Xiaochu Liu | f6106e5 | 2018-08-10 13:09:00 -0700 | [diff] [blame] | 64 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 65 | // Get component version given component name. |
| 66 | std::string GetComponentVersion(const std::string& name); |
| 67 | |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 68 | // Get component metadata given component name. |
| 69 | bool GetComponentMetadata(const std::string& name, |
| 70 | std::map<std::string, std::string>* out_metadata); |
| 71 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 72 | // Load the specified component. This returns the mount point or an empty |
| 73 | // string on failure. |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 74 | std::string LoadComponent(const std::string& name, HelperProcessProxy* proxy); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 75 | |
Xiaochu Liu | 78dc7f5 | 2018-11-26 11:25:43 -0800 | [diff] [blame] | 76 | // Load the specified DLC module image. This returns the mount point or an |
| 77 | // empty string on failure. |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 78 | std::string LoadDlcImage(const std::string& id, |
Amin Hassani | 0badef1 | 2019-03-18 17:08:02 -0700 | [diff] [blame] | 79 | const std::string& package, |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 80 | const std::string& a_or_b, |
Xiaochu Liu | 83a51ac | 2018-06-22 13:48:37 -0700 | [diff] [blame] | 81 | HelperProcessProxy* proxy); |
| 82 | |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 83 | // Load the specified component at a set mount point. |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 84 | bool LoadComponent(const std::string& name, |
| 85 | const std::string& mount_point, |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 86 | HelperProcessProxy* proxy); |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 87 | |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 88 | // Load the specified component from the given path. |
| 89 | std::string LoadComponentAtPath(const std::string& name, |
| 90 | const base::FilePath& absolute_path, |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 91 | HelperProcessProxy* proxy); |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 92 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 93 | // The directory hierarchy for a component consists of the storage_root (i.e. |
| 94 | // `/var/lib/imageloader`), the component_root |
| 95 | // (`/var/lib/imageloader/ComponentName`), and the version folder (i.e. |
| 96 | // `/var/lib/imageloader/ComponentName/23.0.0.205`). That is: |
| 97 | // [storage_root]/ |
| 98 | // [storage_root]/[component_root] |
| 99 | // [storage_root]/[component_root]/[version] |
| 100 | // |
| 101 | // Inside the `component_root` there is a current version hint file: |
| 102 | // [storage_root]/[component_root]/latest-version |
| 103 | |
| 104 | // Return the path to latest-version file for |component_name|. |
| 105 | base::FilePath GetLatestVersionFilePath(const std::string& component_name); |
| 106 | |
| 107 | // Return the path to the [component_root] folder for |component_name|. |
| 108 | base::FilePath GetComponentRoot(const std::string& component_name); |
| 109 | |
| 110 | // Return the path to a given version of |component_name|. |
| 111 | base::FilePath GetVersionPath(const std::string& component_name, |
| 112 | const std::string& version); |
| 113 | |
| 114 | // Return the path to the current version of |component_name|. |
| 115 | bool GetPathToCurrentComponentVersion(const std::string& component_name, |
| 116 | base::FilePath* result); |
| 117 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 118 | private: |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 119 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable); |
| 120 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable); |
Greg Kerr | 285f58f | 2018-10-25 11:33:46 -0700 | [diff] [blame] | 121 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ValidIdTest); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 122 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 123 | // The configuration traits. |
| 124 | ImageLoaderConfig config_; |
| 125 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 126 | // Remove component if removable. |
| 127 | bool RemoveComponentAtPath(const std::string& name, |
| 128 | const base::FilePath& component_root, |
| 129 | const base::FilePath& component_path); |
| 130 | |
Greg Kerr | 285f58f | 2018-10-25 11:33:46 -0700 | [diff] [blame] | 131 | // Report if a component name is valid or not. |
| 132 | static bool IsIdValid(const std::string& id); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | } // namespace imageloader |
| 136 | |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 137 | #endif // IMAGELOADER_IMAGELOADER_IMPL_H_ |