blob: 7692b87037069b57661bb4083f6a2a7a39b5f96f [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>
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 Kerr9944e242017-01-26 15:09:31 -080015#include "helper_process.h"
Greg Kerr89be05f2016-07-27 10:40:32 -070016
Greg Kerra6c0c522016-07-25 11:15:31 -070017namespace imageloader {
18
Eric Caruso0b79bc82017-03-21 13:44:34 -070019using Keys = std::vector<std::vector<uint8_t>>;
20
Greg Kerra6c0c522016-07-25 11:15:31 -070021struct ImageLoaderConfig {
Eric Caruso0b79bc82017-03-21 13:44:34 -070022 ImageLoaderConfig(const Keys& keys, const char* storage_path,
Greg Kerr9944e242017-01-26 15:09:31 -080023 const char* mount_path)
Eric Caruso0b79bc82017-03-21 13:44:34 -070024 : keys(keys),
Greg Kerr89be05f2016-07-27 10:40:32 -070025 storage_dir(storage_path),
Greg Kerr9944e242017-01-26 15:09:31 -080026 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.
40 bool RegisterComponent(const std::string& name, const std::string& version,
41 const std::string& component_folder_abs_path);
42
Xiaochu Liu5e708b82017-11-13 13:59:12 -080043 // Remove a component.
Xiaochu Liu7a224d92017-10-06 17:33:41 -070044 bool RemoveComponent(const std::string& name);
45
Xiaochu Liu5e708b82017-11-13 13:59:12 -080046 // 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 Kerra6c0c522016-07-25 11:15:31 -070057 // Get component version given component name.
58 std::string GetComponentVersion(const std::string& name);
59
Eric Caruso26a91442017-10-25 16:05:40 -070060 // Get component metadata given component name.
61 bool GetComponentMetadata(const std::string& name,
62 std::map<std::string, std::string>* out_metadata);
63
Greg Kerr019d59c2016-11-17 14:28:49 -080064 // Load the specified component. This returns the mount point or an empty
65 // string on failure.
Greg Kerr9944e242017-01-26 15:09:31 -080066 std::string LoadComponent(const std::string& name, HelperProcess* process);
Greg Kerra6c0c522016-07-25 11:15:31 -070067
Greg Kerrc5b91692016-09-14 12:09:22 -070068 // Load the specified component at a set mount point.
Greg Kerr9944e242017-01-26 15:09:31 -080069 bool LoadComponent(const std::string& name, const std::string& mount_point,
70 HelperProcess* process);
Greg Kerrc5b91692016-09-14 12:09:22 -070071
Greg Kerr772abab2017-06-16 14:51:01 -070072 // 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 Kerr019d59c2016-11-17 14:28:49 -080077 // 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 Kerra6c0c522016-07-25 11:15:31 -0700102 private:
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700103 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable);
104 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable);
105
Greg Kerra6c0c522016-07-25 11:15:31 -0700106 // The configuration traits.
107 ImageLoaderConfig config_;
108
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700109 // 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 Kerra6c0c522016-07-25 11:15:31 -0700114 DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl);
115};
116
117} // namespace imageloader
118
Greg Kerr2f76fde2016-08-29 16:39:45 -0700119#endif // IMAGELOADER_IMAGELOADER_IMPL_H_