1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <map>
19 #include <memory>
20 #include <ostream>
21 #include <string>
22 
23 #include "common/libs/utils/result.h"
24 
25 namespace Json {
26 class Value;
27 }
28 
29 namespace cuttlefish {
30 
31 // Order in enum is not guaranteed to be stable, serialized as a string.
32 enum class FileSource {
33   UNKNOWN_PURPOSE = 0,
34   DEFAULT_BUILD,
35   SYSTEM_BUILD,
36   KERNEL_BUILD,
37   LOCAL_FILE,
38   GENERATED,
39   BOOTLOADER_BUILD,
40   ANDROID_EFI_LOADER_BUILD,
41   BOOT_BUILD,
42   HOST_PACKAGE_BUILD,
43 };
44 
45 /*
46  * Attempts to answer the general question "where did this file come from, and
47  * what purpose is it serving?
48  */
49 struct CvdFile {
50   FileSource source;
51   std::string build_id;
52   std::string build_target;
53   std::string file_path;
54 
55   CvdFile();
56   CvdFile(const FileSource& source, const std::string& build_id,
57           const std::string& build_target, const std::string& file_path);
58 };
59 
60 std::ostream& operator<<(std::ostream&, const CvdFile&);
61 
62 /**
63  * A report of state to transfer from fetch_cvd to downstream consumers.
64  *
65  * This includes data intended for programmatic access by other tools such as
66  * assemble_cvd. assemble_cvd can use signals like that multiple build IDs are
67  * present to judge that it needs to do super image remixing or rebuilding the
68  * boot image for a new kernel.
69  *
70  * The output json also includes data relevant for human debugging, like which
71  * flags fetch_cvd was invoked with.
72  */
73 class FetcherConfig {
74   std::unique_ptr<Json::Value> dictionary_;
75 
76  public:
77   FetcherConfig();
78   FetcherConfig(FetcherConfig&&);
79   ~FetcherConfig();
80 
81   bool SaveToFile(const std::string& file) const;
82   bool LoadFromFile(const std::string& file);
83 
84   // For debugging only, not intended for programmatic access.
85   void RecordFlags();
86 
87   bool add_cvd_file(const CvdFile& file, bool override_entry = false);
88   std::map<std::string, CvdFile> get_cvd_files() const;
89 
90   std::string FindCvdFileWithSuffix(const std::string& suffix) const;
91 
92   Result<void> AddFilesToConfig(FileSource purpose, const std::string& build_id,
93                                 const std::string& build_target,
94                                 const std::vector<std::string>& paths,
95                                 const std::string& directory_prefix,
96                                 bool override_entry = false);
97 };
98 
99 } // namespace cuttlefish
100