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