xref: /llvm-project/clang/lib/DirectoryWatcher/DirectoryScanner.cpp (revision 9cf4419e2451febf09acdf28c7d52ebf436d3a7e)
177dd8a79SJan Korous //===- DirectoryScanner.cpp - Utility functions for DirectoryWatcher ------===//
277dd8a79SJan Korous //
377dd8a79SJan Korous // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477dd8a79SJan Korous // See https://llvm.org/LICENSE.txt for license information.
577dd8a79SJan Korous // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677dd8a79SJan Korous //
777dd8a79SJan Korous //===----------------------------------------------------------------------===//
877dd8a79SJan Korous 
977dd8a79SJan Korous #include "DirectoryScanner.h"
1077dd8a79SJan Korous 
1177dd8a79SJan Korous #include "llvm/Support/Path.h"
12*9cf4419eSKazu Hirata #include <optional>
1377dd8a79SJan Korous 
1477dd8a79SJan Korous namespace clang {
1577dd8a79SJan Korous 
1677dd8a79SJan Korous using namespace llvm;
1777dd8a79SJan Korous 
getFileStatus(StringRef Path)18*9cf4419eSKazu Hirata std::optional<sys::fs::file_status> getFileStatus(StringRef Path) {
1977dd8a79SJan Korous   sys::fs::file_status Status;
2077dd8a79SJan Korous   std::error_code EC = status(Path, Status);
2177dd8a79SJan Korous   if (EC)
225891420eSKazu Hirata     return std::nullopt;
2377dd8a79SJan Korous   return Status;
2477dd8a79SJan Korous }
2577dd8a79SJan Korous 
scanDirectory(StringRef Path)2677dd8a79SJan Korous std::vector<std::string> scanDirectory(StringRef Path) {
2777dd8a79SJan Korous   using namespace llvm::sys;
2877dd8a79SJan Korous   std::vector<std::string> Result;
2977dd8a79SJan Korous 
3077dd8a79SJan Korous   std::error_code EC;
3177dd8a79SJan Korous   for (auto It = fs::directory_iterator(Path, EC),
3277dd8a79SJan Korous             End = fs::directory_iterator();
3377dd8a79SJan Korous        !EC && It != End; It.increment(EC)) {
3477dd8a79SJan Korous     auto status = getFileStatus(It->path());
35452db157SKazu Hirata     if (!status)
3677dd8a79SJan Korous       continue;
3777dd8a79SJan Korous     Result.emplace_back(sys::path::filename(It->path()));
3877dd8a79SJan Korous   }
3977dd8a79SJan Korous 
4077dd8a79SJan Korous   return Result;
4177dd8a79SJan Korous }
4277dd8a79SJan Korous 
4377dd8a79SJan Korous std::vector<DirectoryWatcher::Event>
getAsFileEvents(const std::vector<std::string> & Scan)4477dd8a79SJan Korous getAsFileEvents(const std::vector<std::string> &Scan) {
4577dd8a79SJan Korous   std::vector<DirectoryWatcher::Event> Events;
4677dd8a79SJan Korous   Events.reserve(Scan.size());
4777dd8a79SJan Korous 
4877dd8a79SJan Korous   for (const auto &File : Scan) {
4977dd8a79SJan Korous     Events.emplace_back(DirectoryWatcher::Event{
5077dd8a79SJan Korous         DirectoryWatcher::Event::EventKind::Modified, File});
5177dd8a79SJan Korous   }
5277dd8a79SJan Korous   return Events;
5377dd8a79SJan Korous }
5477dd8a79SJan Korous 
5577dd8a79SJan Korous } // namespace clang
56