blob: 1ed5b775e838beee8304b835b41419744c309db2 [file] [log] [blame]
Greg Kerra6c0c522016-07-25 11:15:31 -07001// 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 Kerr2f76fde2016-08-29 16:39:45 -07004#ifndef IMAGELOADER_IMAGELOADER_IMPL_H_
5#define IMAGELOADER_IMAGELOADER_IMPL_H_
Greg Kerra6c0c522016-07-25 11:15:31 -07006
Eric Caruso26a91442017-10-25 16:05:40 -07007#include <map>
Greg Kerra6c0c522016-07-25 11:15:31 -07008#include <string>
Ben Chan045849f2017-12-18 17:27:07 -08009#include <utility>
Greg Kerra6c0c522016-07-25 11:15:31 -070010#include <vector>
11
12#include <base/files/file_path.h>
13#include <base/gtest_prod_util.h>
14#include <base/macros.h>
15
Greg Kerr09f06de2018-02-16 15:32:07 -080016#include "imageloader/helper_process_proxy.h"
Greg Kerr89be05f2016-07-27 10:40:32 -070017
Greg Kerra6c0c522016-07-25 11:15:31 -070018namespace imageloader {
19
Eric Caruso0b79bc82017-03-21 13:44:34 -070020using Keys = std::vector<std::vector<uint8_t>>;
21
Greg Kerra6c0c522016-07-25 11:15:31 -070022struct ImageLoaderConfig {
Colin Howesad6271a2018-11-21 15:36:05 -080023 ImageLoaderConfig(const Keys& keys,
24 const char* storage_path,
Greg Kerr9944e242017-01-26 15:09:31 -080025 const char* mount_path)
Colin Howesad6271a2018-11-21 15:36:05 -080026 : keys(keys), storage_dir(storage_path), mount_path(mount_path) {}
Greg Kerr89be05f2016-07-27 10:40:32 -070027
Eric Caruso0b79bc82017-03-21 13:44:34 -070028 Keys keys;
Greg Kerra6c0c522016-07-25 11:15:31 -070029 base::FilePath storage_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -070030 base::FilePath mount_path;
Greg Kerra6c0c522016-07-25 11:15:31 -070031};
32
33class ImageLoaderImpl {
34 public:
35 // Instantiate an object with a configuration object.
Greg Kerr89be05f2016-07-27 10:40:32 -070036 explicit ImageLoaderImpl(ImageLoaderConfig config)
37 : config_(std::move(config)) {}
Greg Kerra6c0c522016-07-25 11:15:31 -070038
39 // Register a component.
Colin Howesad6271a2018-11-21 15:36:05 -080040 bool RegisterComponent(const std::string& name,
41 const std::string& version,
Greg Kerra6c0c522016-07-25 11:15:31 -070042 const std::string& component_folder_abs_path);
43
Xiaochu Liu5e708b82017-11-13 13:59:12 -080044 // Remove a component.
Xiaochu Liu7a224d92017-10-06 17:33:41 -070045 bool RemoveComponent(const std::string& name);
46
Xiaochu Liu5e708b82017-11-13 13:59:12 -080047 // Enumerates all mount point paths with prefix of |parent_dir| and returns
48 // them with |paths|. If |dry_run| is true, no mount points are unmounted.
49 // If |dry_run| is false, all mount points returned in |paths| are unmounted.
50 bool CleanupAll(bool dry_run,
51 const base::FilePath& parent_dir,
52 std::vector<std::string>* paths,
Greg Kerr09f06de2018-02-16 15:32:07 -080053 HelperProcessProxy* proxy);
Xiaochu Liu5e708b82017-11-13 13:59:12 -080054
55 // Cleanup a mount point at |path|.
Greg Kerr09f06de2018-02-16 15:32:07 -080056 bool Cleanup(const base::FilePath& path, HelperProcessProxy* proxy);
Xiaochu Liu5e708b82017-11-13 13:59:12 -080057
Amin Hassani0badef12019-03-18 17:08:02 -070058 // Cleanup a DLC module image mount point given DLC |id| and |package|.
59 bool UnloadDlcImage(const std::string& id,
60 const std::string& package,
61 HelperProcessProxy* proxy);
Xiaochu Liuf6106e52018-08-10 13:09:00 -070062
Greg Kerra6c0c522016-07-25 11:15:31 -070063 // Get component version given component name.
64 std::string GetComponentVersion(const std::string& name);
65
Eric Caruso26a91442017-10-25 16:05:40 -070066 // Get component metadata given component name.
67 bool GetComponentMetadata(const std::string& name,
68 std::map<std::string, std::string>* out_metadata);
69
Greg Kerr019d59c2016-11-17 14:28:49 -080070 // Load the specified component. This returns the mount point or an empty
71 // string on failure.
Greg Kerr09f06de2018-02-16 15:32:07 -080072 std::string LoadComponent(const std::string& name, HelperProcessProxy* proxy);
Greg Kerra6c0c522016-07-25 11:15:31 -070073
Xiaochu Liu78dc7f52018-11-26 11:25:43 -080074 // Load the specified DLC module image. This returns the mount point or an
75 // empty string on failure.
Colin Howesad6271a2018-11-21 15:36:05 -080076 std::string LoadDlcImage(const std::string& id,
Amin Hassani0badef12019-03-18 17:08:02 -070077 const std::string& package,
Colin Howesad6271a2018-11-21 15:36:05 -080078 const std::string& a_or_b,
Xiaochu Liu83a51ac2018-06-22 13:48:37 -070079 HelperProcessProxy* proxy);
80
Greg Kerrc5b91692016-09-14 12:09:22 -070081 // Load the specified component at a set mount point.
Colin Howesad6271a2018-11-21 15:36:05 -080082 bool LoadComponent(const std::string& name,
83 const std::string& mount_point,
Greg Kerr09f06de2018-02-16 15:32:07 -080084 HelperProcessProxy* proxy);
Greg Kerrc5b91692016-09-14 12:09:22 -070085
Greg Kerr772abab2017-06-16 14:51:01 -070086 // Load the specified component from the given path.
87 std::string LoadComponentAtPath(const std::string& name,
88 const base::FilePath& absolute_path,
Greg Kerr09f06de2018-02-16 15:32:07 -080089 HelperProcessProxy* proxy);
Greg Kerr772abab2017-06-16 14:51:01 -070090
Greg Kerr019d59c2016-11-17 14:28:49 -080091 // The directory hierarchy for a component consists of the storage_root (i.e.
92 // `/var/lib/imageloader`), the component_root
93 // (`/var/lib/imageloader/ComponentName`), and the version folder (i.e.
94 // `/var/lib/imageloader/ComponentName/23.0.0.205`). That is:
95 // [storage_root]/
96 // [storage_root]/[component_root]
97 // [storage_root]/[component_root]/[version]
98 //
99 // Inside the `component_root` there is a current version hint file:
100 // [storage_root]/[component_root]/latest-version
101
102 // Return the path to latest-version file for |component_name|.
103 base::FilePath GetLatestVersionFilePath(const std::string& component_name);
104
105 // Return the path to the [component_root] folder for |component_name|.
106 base::FilePath GetComponentRoot(const std::string& component_name);
107
108 // Return the path to a given version of |component_name|.
109 base::FilePath GetVersionPath(const std::string& component_name,
110 const std::string& version);
111
112 // Return the path to the current version of |component_name|.
113 bool GetPathToCurrentComponentVersion(const std::string& component_name,
114 base::FilePath* result);
115
Greg Kerra6c0c522016-07-25 11:15:31 -0700116 private:
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700117 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable);
118 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable);
Greg Kerr285f58f2018-10-25 11:33:46 -0700119 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ValidIdTest);
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700120
Greg Kerra6c0c522016-07-25 11:15:31 -0700121 // The configuration traits.
122 ImageLoaderConfig config_;
123
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700124 // Remove component if removable.
125 bool RemoveComponentAtPath(const std::string& name,
126 const base::FilePath& component_root,
127 const base::FilePath& component_path);
128
Greg Kerr285f58f2018-10-25 11:33:46 -0700129 // Report if a component name is valid or not.
130 static bool IsIdValid(const std::string& id);
131
Greg Kerra6c0c522016-07-25 11:15:31 -0700132 DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl);
133};
134
135} // namespace imageloader
136
Greg Kerr2f76fde2016-08-29 16:39:45 -0700137#endif // IMAGELOADER_IMAGELOADER_IMPL_H_