• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
17 #include "host/commands/cvd/acloud/config.h"
18 
19 #include <fstream>
20 
21 #include <google/protobuf/text_format.h>
22 
23 #include "common/libs/utils/files.h"
24 #include "common/libs/utils/users.h"
25 
26 namespace cuttlefish {
27 
AcloudConfig(const acloud::UserConfig & usr_cfg)28 AcloudConfig::AcloudConfig(const acloud::UserConfig& usr_cfg)
29     : launch_args(usr_cfg.launch_args()) {
30   // TODO(weihsu): Add back fields/variables (except of cheeps and emulator
31   // fields) in config files. Remove cheeps (Android on ChromeOS) and emulator
32   // fields.
33 
34   // TODO(weihsu): Verify validity of configurations.
35 }
36 
37 template <typename ProtoType>
ParseTextProtoConfigHelper(const std::string & config_path)38 Result<ProtoType> ParseTextProtoConfigHelper(const std::string& config_path) {
39   std::ifstream t(config_path);
40   std::stringstream buffer;
41   buffer << t.rdbuf();
42 
43   ProtoType proto_result;
44   google::protobuf::TextFormat::Parser p;
45   CF_EXPECT(p.ParseFromString(buffer.str(), &proto_result),
46             "Failed to parse config: " << config_path);
47   return proto_result;
48 }
49 
50 /**
51  * Return path to default config file.
52  */
GetDefaultConfigFile(const uid_t uid)53 Result<const std::string> GetDefaultConfigFile(const uid_t uid) {
54   const std::string home = CF_EXPECT(SystemWideUserHome(uid));
55   return (std::string(home) + "/.config/acloud/acloud.config");
56 }
57 
LoadAcloudConfig(const std::string & user_config_path,const uid_t uid)58 Result<AcloudConfig> LoadAcloudConfig(const std::string& user_config_path,
59                                       const uid_t uid) {
60   acloud::UserConfig proto_result_user;
61   if (FileExists(user_config_path)) {
62     proto_result_user = CF_EXPECT(
63         ParseTextProtoConfigHelper<acloud::UserConfig>(user_config_path));
64   } else {
65     const std::string conf_path = CF_EXPECT(GetDefaultConfigFile(uid));
66     CF_EXPECT(user_config_path == conf_path,
67               "The specified config file does not exist.");
68 
69     // If the default config does not exist, acloud creates an empty object.
70     proto_result_user = acloud::UserConfig();
71   }
72   return AcloudConfig(proto_result_user);
73 }
74 
75 }  // namespace cuttlefish
76