Patrick Venture | aca9813 | 2019-03-18 11:14:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 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 "handler.hpp" |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <cstdio> |
| 21 | #include <filesystem> |
| 22 | #include <fstream> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace ethstats |
| 26 | { |
| 27 | |
| 28 | bool EthStats::validIfNameAndField(const std::string& path) const |
| 29 | { |
| 30 | namespace fs = std::filesystem; |
| 31 | |
| 32 | // TODO: Transition to using the netlink api. |
| 33 | std::error_code ec; |
| 34 | if (!fs::exists(path, ec)) |
| 35 | { |
| 36 | std::fprintf(stderr, "Path: '%s' doesn't exist. ec(%d, %s)\n", |
| 37 | path.c_str(), ec.value(), ec.message().c_str()); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | std::uint64_t EthStats::readStatistic(const std::string& path) const |
| 45 | { |
| 46 | // We know the file exists, so just read it. |
| 47 | std::uint64_t value = 0; |
| 48 | std::ifstream ifs; |
| 49 | |
| 50 | ifs.open(path); |
| 51 | ifs >> value; |
| 52 | |
| 53 | return value; |
| 54 | } |
| 55 | |
Patrick Venture | aca9813 | 2019-03-18 11:14:16 -0700 | [diff] [blame] | 56 | } // namespace ethstats |