blob: 0fe0db7035f82817030a33c0a94ba19a88e1e0e2 [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <tuple>
#include <utility>
#include <base/files/file_util.h>
#include <base/strings/string_util.h>
#include "runtime_probe/utils/file_utils.h"
using base::DictionaryValue;
using base::FilePath;
using base::PathExists;
using base::ReadFileToStringWithMaxSize;
using base::TrimWhitespaceASCII;
using base::TrimPositions::TRIM_ALL;
using std::pair;
using std::string;
using std::vector;
namespace {
constexpr int kReadFileMaxSize = 1024;
// Get string to be used as key name in |MapFilesToDict|.
string GetKeyName(const string& key) {
return key;
}
string GetKeyName(const pair<string, string>& key) {
return key.first;
}
// Get string to be used as file name in |MapFilesToDict|.
string GetFileName(const string& key) {
return key;
}
string GetFileName(const pair<string, string>& key) {
return key.second;
}
} // namespace
namespace runtime_probe {
template <typename KeyType>
DictionaryValue MapFilesToDict(const FilePath& dir_path,
const vector<KeyType>& keys,
const vector<KeyType>& optional_keys) {
DictionaryValue ret;
for (const auto& key : keys) {
string file_name = GetFileName(key);
string key_name = GetKeyName(key);
const auto file_path = dir_path.Append(file_name);
string content;
/* missing file */
if (!PathExists(file_path))
return {};
/* file exists, but somehow we can't read it */
if (!ReadFileToStringWithMaxSize(file_path, &content, kReadFileMaxSize)) {
LOG(ERROR) << file_path.value() << " exists, but we can't read it";
return {};
}
ret.SetString(key_name, TrimWhitespaceASCII(content, TRIM_ALL));
}
for (const auto& key : optional_keys) {
string file_name = GetFileName(key);
string key_name = GetKeyName(key);
const auto file_path = dir_path.Append(file_name);
string content;
if (!base::PathExists(file_path))
continue;
if (ReadFileToStringWithMaxSize(file_path, &content, kReadFileMaxSize))
ret.SetString(key_name, TrimWhitespaceASCII(content, TRIM_ALL));
}
return ret;
}
// Explicit template instantiation
template DictionaryValue MapFilesToDict<string>(
const FilePath& dir_path,
const vector<string>& keys,
const vector<string>& optional_keys);
// Explicit template instantiation
template DictionaryValue MapFilesToDict<pair<string, string>>(
const FilePath& dir_path,
const vector<pair<string, string>>& keys,
const vector<pair<string, string>>& optional_keys);
} // namespace runtime_probe