1 /*
2  * Copyright 2020 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 "storage/legacy_config_file.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <cerrno>
22 #include <fstream>
23 #include <sstream>
24 
25 #include "common/strings.h"
26 #include "os/files.h"
27 #include "storage/device.h"
28 
29 namespace bluetooth {
30 namespace storage {
31 
LegacyConfigFile(std::string path)32 LegacyConfigFile::LegacyConfigFile(std::string path) : path_(std::move(path)) {
33   log::assert_that(!path_.empty(), "assert failed: !path_.empty()");
34 }
35 
Read(size_t temp_devices_capacity)36 std::optional<ConfigCache> LegacyConfigFile::Read(size_t temp_devices_capacity) {
37   log::assert_that(!path_.empty(), "assert failed: !path_.empty()");
38   std::ifstream config_file(path_);
39   if (!config_file || !config_file.is_open()) {
40     log::error("unable to open file '{}', error: {}", path_, strerror(errno));
41     return std::nullopt;
42   }
43   [[maybe_unused]] int line_num = 0;
44   ConfigCache cache(temp_devices_capacity, Device::kLinkKeyProperties);
45   std::string line;
46   std::string section(ConfigCache::kDefaultSectionName);
47   while (std::getline(config_file, line)) {
48     ++line_num;
49     line = common::StringTrim(std::move(line));
50     if (line.empty()) {
51       continue;
52     }
53 
54     if (line.front() == '\0' || line.front() == '#') {
55       continue;
56     }
57     if (line.front() == '[') {
58       if (line.back() != ']') {
59         log::warn("unterminated section name on line {}", line_num);
60         return std::nullopt;
61       }
62       // Read 'test' from '[text]', hence -2
63       section = line.substr(1, line.size() - 2);
64     } else {
65       auto tokens = common::StringSplit(line, "=", 2);
66       if (tokens.size() != 2) {
67         log::warn("no key/value separator found on line {}", line_num);
68         return std::nullopt;
69       }
70       tokens[0] = common::StringTrim(std::move(tokens[0]));
71       tokens[1] = common::StringTrim(std::move(tokens[1]));
72       cache.SetProperty(section, tokens[0], std::move(tokens[1]));
73     }
74   }
75   return cache;
76 }
77 
Write(const ConfigCache & cache)78 bool LegacyConfigFile::Write(const ConfigCache& cache) {
79   return os::WriteToFile(path_, cache.SerializeToLegacyFormat());
80 }
81 
Delete()82 bool LegacyConfigFile::Delete() {
83   if (!os::FileExists(path_)) {
84     log::warn("Config file at \"{}\" does not exist", path_);
85     return false;
86   }
87   return os::RemoveFile(path_);
88 }
89 
90 }  // namespace storage
91 }  // namespace bluetooth
92