Serenity Operating System
1/*
2 * Copyright (c) 2022, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <LibCore/DirIterator.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <sys/time.h>
13
14namespace Test {
15
16inline double get_time_in_ms()
17{
18 struct timeval tv1;
19 auto return_code = gettimeofday(&tv1, nullptr);
20 VERIFY(return_code >= 0);
21 return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
22}
23
24template<typename Callback>
25inline void iterate_directory_recursively(DeprecatedString const& directory_path, Callback callback)
26{
27 Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
28
29 while (directory_iterator.has_next()) {
30 auto name = directory_iterator.next_path();
31 struct stat st = {};
32 if (fstatat(directory_iterator.fd(), name.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0)
33 continue;
34 bool is_directory = S_ISDIR(st.st_mode);
35 auto full_path = DeprecatedString::formatted("{}/{}", directory_path, name);
36 if (is_directory && name != "/Fixtures"sv) {
37 iterate_directory_recursively(full_path, callback);
38 } else if (!is_directory) {
39 callback(full_path);
40 }
41 }
42}
43
44}