blob: 5dce87207c00ffa685a1b4891dbdf0b4269ceed8 [file] [log] [blame]
Marc Olberding1e17db52025-08-27 12:25:28 -07001// SPDX-License-Identifier: Apache-2.0
Patrick Williams4a943522025-10-09 20:34:38 -04002// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Marc Olberding1e17db52025-08-27 12:25:28 -07003
4#include "utilities.hpp"
5
6#include <chrono>
7#include <filesystem>
8#include <format>
9#include <iostream>
10#include <thread>
11
12using namespace std::chrono_literals;
13
14void sleep_milliseconds(std::chrono::milliseconds milliseconds)
15{
16 std::cerr << std::format("Sleeping for {} milliseconds\n",
17 milliseconds.count());
18 std::this_thread::sleep_for(milliseconds);
19}
20
21void wait_for_path_to_exist(std::string_view path,
22 std::chrono::milliseconds timeout)
23{
Marc Olberding14ca31b2025-09-08 16:51:01 -070024 auto now = std::chrono::steady_clock::now();
25 auto end = now + timeout;
26 std::cerr << std::format("waiting for {} to exist\n", path);
Marc Olberding1e17db52025-08-27 12:25:28 -070027 while (true)
28 {
29 std::error_code ec;
30 bool exists = std::filesystem::exists(path, ec);
31 if (exists)
32 {
33 return;
34 }
Marc Olberding14ca31b2025-09-08 16:51:01 -070035 std::this_thread::sleep_for(1ms);
36
37 now = std::chrono::steady_clock::now();
38 if (now > end)
39 {
40 break;
41 }
Marc Olberding1e17db52025-08-27 12:25:28 -070042 }
Marc Olberding14ca31b2025-09-08 16:51:01 -070043 std::cerr << std::format("Failed to wait for {} to exist\n", path);
Marc Olberding1e17db52025-08-27 12:25:28 -070044}