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 #include <iostream>
17 
18 #include <android-base/strings.h>
19 #include <android-base/logging.h>
20 
21 #include "common/libs/fs/shared_buf.h"
22 #include "common/libs/fs/shared_fd.h"
23 #include "host/commands/assemble_cvd/assembler_defs.h"
24 #include "host/commands/assemble_cvd/flags.h"
25 #include "host/libs/config/fetcher_config.h"
26 
27 namespace {
28 
29 std::string kFetcherConfigFile = "fetcher_config.json";
30 
FindFetcherConfig(const std::vector<std::string> & files)31 cuttlefish::FetcherConfig FindFetcherConfig(const std::vector<std::string>& files) {
32   cuttlefish::FetcherConfig fetcher_config;
33   for (const auto& file : files) {
34     auto expected_pos = file.size() - kFetcherConfigFile.size();
35     if (file.rfind(kFetcherConfigFile) == expected_pos) {
36       if (fetcher_config.LoadFromFile(file)) {
37         return fetcher_config;
38       }
39       LOG(ERROR) << "Could not load fetcher config file.";
40     }
41   }
42   return fetcher_config;
43 }
44 
45 } // namespace
46 
main(int argc,char ** argv)47 int main(int argc, char** argv) {
48   setenv("ANDROID_LOG_TAGS", "*:v", /* overwrite */ 0);
49   ::android::base::InitLogging(argv, android::base::StderrLogger);
50 
51   if (isatty(0)) {
52     LOG(FATAL) << "stdin was a tty, expected to be passed the output of a previous stage. "
53                << "Did you mean to run launch_cvd?";
54     return cuttlefish::AssemblerExitCodes::kInvalidHostConfiguration;
55   } else {
56     int error_num = errno;
57     if (error_num == EBADF) {
58       LOG(FATAL) << "stdin was not a valid file descriptor, expected to be passed the output "
59                  << "of launch_cvd. Did you mean to run launch_cvd?";
60       return cuttlefish::AssemblerExitCodes::kInvalidHostConfiguration;
61     }
62   }
63 
64   std::string input_files_str;
65   {
66     auto input_fd = cuttlefish::SharedFD::Dup(0);
67     auto bytes_read = cuttlefish::ReadAll(input_fd, &input_files_str);
68     if (bytes_read < 0) {
69       LOG(FATAL) << "Failed to read input files. Error was \"" << input_fd->StrError() << "\"";
70     }
71   }
72   std::vector<std::string> input_files = android::base::Split(input_files_str, "\n");
73 
74   auto config = InitFilesystemAndCreateConfig(&argc, &argv, FindFetcherConfig(input_files));
75 
76   std::cout << GetConfigFilePath(*config) << "\n";
77   std::cout << std::flush;
78 
79   return cuttlefish::AssemblerExitCodes::kSuccess;
80 }
81