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> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/files/file_path.h> |
| 12 | #include <base/gtest_prod_util.h> |
| 13 | #include <base/macros.h> |
| 14 | |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 15 | #include "helper_process.h" |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 16 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 17 | namespace imageloader { |
| 18 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 19 | using Keys = std::vector<std::vector<uint8_t>>; |
| 20 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 21 | struct ImageLoaderConfig { |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 22 | ImageLoaderConfig(const Keys& keys, const char* storage_path, |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 23 | const char* mount_path) |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 24 | : keys(keys), |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 25 | storage_dir(storage_path), |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 26 | 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)) {} |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 38 | |
| 39 | // Register a component. |
| 40 | bool RegisterComponent(const std::string& name, const std::string& version, |
| 41 | const std::string& component_folder_abs_path); |
| 42 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame^] | 43 | // Remove a component. |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 44 | bool RemoveComponent(const std::string& name); |
| 45 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame^] | 46 | // Enumerates all mount point paths with prefix of |parent_dir| and returns |
| 47 | // them with |paths|. If |dry_run| is true, no mount points are unmounted. |
| 48 | // If |dry_run| is false, all mount points returned in |paths| are unmounted. |
| 49 | bool CleanupAll(bool dry_run, |
| 50 | const base::FilePath& parent_dir, |
| 51 | std::vector<std::string>* paths, |
| 52 | HelperProcess* process); |
| 53 | |
| 54 | // Cleanup a mount point at |path|. |
| 55 | bool Cleanup(const base::FilePath& path, HelperProcess* process); |
| 56 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 57 | // Get component version given component name. |
| 58 | std::string GetComponentVersion(const std::string& name); |
| 59 | |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 60 | // Get component metadata given component name. |
| 61 | bool GetComponentMetadata(const std::string& name, |
| 62 | std::map<std::string, std::string>* out_metadata); |
| 63 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 64 | // Load the specified component. This returns the mount point or an empty |
| 65 | // string on failure. |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 66 | std::string LoadComponent(const std::string& name, HelperProcess* process); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 67 | |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 68 | // Load the specified component at a set mount point. |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 69 | bool LoadComponent(const std::string& name, const std::string& mount_point, |
| 70 | HelperProcess* process); |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 71 | |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 72 | // Load the specified component from the given path. |
| 73 | std::string LoadComponentAtPath(const std::string& name, |
| 74 | const base::FilePath& absolute_path, |
| 75 | HelperProcess* process); |
| 76 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 77 | // The directory hierarchy for a component consists of the storage_root (i.e. |
| 78 | // `/var/lib/imageloader`), the component_root |
| 79 | // (`/var/lib/imageloader/ComponentName`), and the version folder (i.e. |
| 80 | // `/var/lib/imageloader/ComponentName/23.0.0.205`). That is: |
| 81 | // [storage_root]/ |
| 82 | // [storage_root]/[component_root] |
| 83 | // [storage_root]/[component_root]/[version] |
| 84 | // |
| 85 | // Inside the `component_root` there is a current version hint file: |
| 86 | // [storage_root]/[component_root]/latest-version |
| 87 | |
| 88 | // Return the path to latest-version file for |component_name|. |
| 89 | base::FilePath GetLatestVersionFilePath(const std::string& component_name); |
| 90 | |
| 91 | // Return the path to the [component_root] folder for |component_name|. |
| 92 | base::FilePath GetComponentRoot(const std::string& component_name); |
| 93 | |
| 94 | // Return the path to a given version of |component_name|. |
| 95 | base::FilePath GetVersionPath(const std::string& component_name, |
| 96 | const std::string& version); |
| 97 | |
| 98 | // Return the path to the current version of |component_name|. |
| 99 | bool GetPathToCurrentComponentVersion(const std::string& component_name, |
| 100 | base::FilePath* result); |
| 101 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 102 | private: |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 103 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable); |
| 104 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable); |
| 105 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 106 | // The configuration traits. |
| 107 | ImageLoaderConfig config_; |
| 108 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 109 | // Remove component if removable. |
| 110 | bool RemoveComponentAtPath(const std::string& name, |
| 111 | const base::FilePath& component_root, |
| 112 | const base::FilePath& component_path); |
| 113 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 114 | DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl); |
| 115 | }; |
| 116 | |
| 117 | } // namespace imageloader |
| 118 | |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 119 | #endif // IMAGELOADER_IMAGELOADER_IMPL_H_ |