aboutsummaryrefslogtreecommitdiff
path: root/host/libs/config/fetcher_config.h
blob: bad5b5f1fba8a3ec8f187f6b55b513e0b7bdb154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <map>
#include <memory>
#include <ostream>
#include <string>

#include "common/libs/utils/result.h"

namespace Json {
class Value;
}

namespace cuttlefish {

// Order in enum is not guaranteed to be stable, serialized as a string.
enum class FileSource {
  UNKNOWN_PURPOSE = 0,
  DEFAULT_BUILD,
  SYSTEM_BUILD,
  KERNEL_BUILD,
  LOCAL_FILE,
  GENERATED,
  BOOTLOADER_BUILD,
  ANDROID_EFI_LOADER_BUILD,
  BOOT_BUILD,
  HOST_PACKAGE_BUILD,
};

/*
 * Attempts to answer the general question "where did this file come from, and
 * what purpose is it serving?
 */
struct CvdFile {
  FileSource source;
  std::string build_id;
  std::string build_target;
  std::string file_path;

  CvdFile();
  CvdFile(const FileSource& source, const std::string& build_id,
          const std::string& build_target, const std::string& file_path);
};

std::ostream& operator<<(std::ostream&, const CvdFile&);

/**
 * A report of state to transfer from fetch_cvd to downstream consumers.
 *
 * This includes data intended for programmatic access by other tools such as
 * assemble_cvd. assemble_cvd can use signals like that multiple build IDs are
 * present to judge that it needs to do super image remixing or rebuilding the
 * boot image for a new kernel.
 *
 * The output json also includes data relevant for human debugging, like which
 * flags fetch_cvd was invoked with.
 */
class FetcherConfig {
  std::unique_ptr<Json::Value> dictionary_;

 public:
  FetcherConfig();
  FetcherConfig(FetcherConfig&&);
  ~FetcherConfig();

  bool SaveToFile(const std::string& file) const;
  bool LoadFromFile(const std::string& file);

  // For debugging only, not intended for programmatic access.
  void RecordFlags();

  bool add_cvd_file(const CvdFile& file, bool override_entry = false);
  std::map<std::string, CvdFile> get_cvd_files() const;

  std::string FindCvdFileWithSuffix(const std::string& suffix) const;

  Result<void> AddFilesToConfig(FileSource purpose, const std::string& build_id,
                                const std::string& build_target,
                                const std::vector<std::string>& paths,
                                const std::string& directory_prefix,
                                bool override_entry = false);
};

} // namespace cuttlefish