blob: 5bb958cfcc628ba4a2d606a15fbee7becef8ee96 [file] [log] [blame]
Patrick Venturea6b4abd2019-07-19 10:58:55 -07001/*
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>
Patrick Venturea6b4abd2019-07-19 10:58:55 -070020#include <string>
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080021#include <system_error>
Patrick Venturea6b4abd2019-07-19 10:58:55 -070022#include <vector>
23
24namespace ipmi_flash
25{
26namespace fs = std::filesystem;
27
28std::vector<std::string> GetJsonList(const std::string& directory)
29{
30 std::vector<std::string> output;
31
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080032 try
Patrick Venturea6b4abd2019-07-19 10:58:55 -070033 {
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080034 for (const auto& p : fs::recursive_directory_iterator(directory))
Patrick Venturea6b4abd2019-07-19 10:58:55 -070035 {
William A. Kennington III7f165702021-01-29 00:19:37 -080036 auto path = p.path();
37 if (path.extension().string() == ".json")
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080038 {
William A. Kennington III7f165702021-01-29 00:19:37 -080039 output.push_back(path.string());
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080040 }
Patrick Venturea6b4abd2019-07-19 10:58:55 -070041 }
42 }
William A. Kennington IIIbfcf1322021-01-29 00:10:23 -080043 catch (const fs::filesystem_error& e)
44 {
45 // Ignore missing directories and just return an empty list
46 if (e.code() == std::error_code(ENOENT, std::generic_category()))
47 {
48 return output;
49 }
50 throw;
51 }
Patrick Venturea6b4abd2019-07-19 10:58:55 -070052
53 return output;
54}
55
56} // namespace ipmi_flash