Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -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 "fs.hpp" |
| 18 | |
| 19 | #include <filesystem> |
| 20 | #include <regex> |
| 21 | #include <string> |
William A. Kennington III | bfcf132 | 2021-01-29 00:10:23 -0800 | [diff] [blame^] | 22 | #include <system_error> |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | namespace ipmi_flash |
| 26 | { |
| 27 | namespace fs = std::filesystem; |
| 28 | |
| 29 | std::vector<std::string> GetJsonList(const std::string& directory) |
| 30 | { |
| 31 | std::vector<std::string> output; |
| 32 | |
William A. Kennington III | bfcf132 | 2021-01-29 00:10:23 -0800 | [diff] [blame^] | 33 | try |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 34 | { |
William A. Kennington III | bfcf132 | 2021-01-29 00:10:23 -0800 | [diff] [blame^] | 35 | for (const auto& p : fs::recursive_directory_iterator(directory)) |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 36 | { |
William A. Kennington III | bfcf132 | 2021-01-29 00:10:23 -0800 | [diff] [blame^] | 37 | auto ps = p.path().string(); |
| 38 | |
| 39 | /** TODO: openbmc/phosphor-ipmi-blobs/blob/de8a16e2e8/fs.cpp#L27 is |
| 40 | * nicer, may be worth finding a way to make this into a util. |
| 41 | */ |
| 42 | if (std::regex_match(ps, std::regex(".+.json$"))) |
| 43 | { |
| 44 | output.push_back(ps); |
| 45 | } |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
William A. Kennington III | bfcf132 | 2021-01-29 00:10:23 -0800 | [diff] [blame^] | 48 | catch (const fs::filesystem_error& e) |
| 49 | { |
| 50 | // Ignore missing directories and just return an empty list |
| 51 | if (e.code() == std::error_code(ENOENT, std::generic_category())) |
| 52 | { |
| 53 | return output; |
| 54 | } |
| 55 | throw; |
| 56 | } |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 57 | |
| 58 | return output; |
| 59 | } |
| 60 | |
| 61 | } // namespace ipmi_flash |