summaryrefslogtreecommitdiff
path: root/system/gd/storage/legacy_config_file.cc
blob: 4a46dd59617dcb756990ab8e417fc804ffc77ac2 (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
/*
 * Copyright 2020 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.
 */

#include "storage/legacy_config_file.h"

#include <cerrno>
#include <fstream>
#include <sstream>

#include "common/strings.h"
#include "os/files.h"
#include "os/log.h"
#include "storage/device.h"

namespace bluetooth {
namespace storage {

LegacyConfigFile::LegacyConfigFile(std::string path) : path_(std::move(path)) {
  ASSERT(!path_.empty());
};

std::optional<ConfigCache> LegacyConfigFile::Read(size_t temp_devices_capacity) {
  ASSERT(!path_.empty());
  std::ifstream config_file(path_);
  if (!config_file || !config_file.is_open()) {
    LOG_ERROR("unable to open file '%s', error: %s", path_.c_str(), strerror(errno));
    return std::nullopt;
  }
  int line_num = 0;
  ConfigCache cache(temp_devices_capacity, Device::kLinkKeyProperties);
  std::string line;
  std::string section(ConfigCache::kDefaultSectionName);
  while (std::getline(config_file, line)) {
    ++line_num;
    line = common::StringTrim(std::move(line));
    if (line.front() == '\0' || line.front() == '#') {
      continue;
    }
    if (line.front() == '[') {
      if (line.back() != ']') {
        LOG_WARN("unterminated section name on line %d", line_num);
        return std::nullopt;
      }
      // Read 'test' from '[text]', hence -2
      section = line.substr(1, line.size() - 2);
    } else {
      auto tokens = common::StringSplit(line, "=", 2);
      if (tokens.size() != 2) {
        LOG_WARN("no key/value separator found on line %d", line_num);
        return std::nullopt;
      }
      tokens[0] = common::StringTrim(std::move(tokens[0]));
      tokens[1] = common::StringTrim(std::move(tokens[1]));
      cache.SetProperty(section, tokens[0], std::move(tokens[1]));
    }
  }
  return cache;
}

bool LegacyConfigFile::Write(const ConfigCache& cache) {
  return os::WriteToFile(path_, cache.SerializeToLegacyFormat());
}

bool LegacyConfigFile::Delete() {
  if (!os::FileExists(path_)) {
    LOG_WARN("Config file at \"%s\" does not exist", path_.c_str());
    return false;
  }
  return os::RemoveFile(path_);
}

}  // namespace storage
}  // namespace bluetooth