xref: /freebsd/contrib/llvm-project/clang/lib/DirectoryWatcher/DirectoryScanner.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 //===- DirectoryScanner.cpp - Utility functions for DirectoryWatcher ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DirectoryScanner.h"
10 
11 #include "llvm/Support/Path.h"
12 #include <optional>
13 
14 namespace clang {
15 
16 using namespace llvm;
17 
18 std::optional<sys::fs::file_status> getFileStatus(StringRef Path) {
19   sys::fs::file_status Status;
20   std::error_code EC = status(Path, Status);
21   if (EC)
22     return std::nullopt;
23   return Status;
24 }
25 
26 std::vector<std::string> scanDirectory(StringRef Path) {
27   using namespace llvm::sys;
28   std::vector<std::string> Result;
29 
30   std::error_code EC;
31   for (auto It = fs::directory_iterator(Path, EC),
32             End = fs::directory_iterator();
33        !EC && It != End; It.increment(EC)) {
34     auto status = getFileStatus(It->path());
35     if (!status)
36       continue;
37     Result.emplace_back(sys::path::filename(It->path()));
38   }
39 
40   return Result;
41 }
42 
43 std::vector<DirectoryWatcher::Event>
44 getAsFileEvents(const std::vector<std::string> &Scan) {
45   std::vector<DirectoryWatcher::Event> Events;
46   Events.reserve(Scan.size());
47 
48   for (const auto &File : Scan) {
49     Events.emplace_back(DirectoryWatcher::Event{
50         DirectoryWatcher::Event::EventKind::Modified, File});
51   }
52   return Events;
53 }
54 
55 } // namespace clang
56