DirectoryScanner.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include "DirectoryScanner.h"
  9. #include "llvm/Support/Path.h"
  10. namespace clang {
  11. using namespace llvm;
  12. Optional<sys::fs::file_status> getFileStatus(StringRef Path) {
  13. sys::fs::file_status Status;
  14. std::error_code EC = status(Path, Status);
  15. if (EC)
  16. return None;
  17. return Status;
  18. }
  19. std::vector<std::string> scanDirectory(StringRef Path) {
  20. using namespace llvm::sys;
  21. std::vector<std::string> Result;
  22. std::error_code EC;
  23. for (auto It = fs::directory_iterator(Path, EC),
  24. End = fs::directory_iterator();
  25. !EC && It != End; It.increment(EC)) {
  26. auto status = getFileStatus(It->path());
  27. if (!status.hasValue())
  28. continue;
  29. Result.emplace_back(sys::path::filename(It->path()));
  30. }
  31. return Result;
  32. }
  33. std::vector<DirectoryWatcher::Event>
  34. getAsFileEvents(const std::vector<std::string> &Scan) {
  35. std::vector<DirectoryWatcher::Event> Events;
  36. Events.reserve(Scan.size());
  37. for (const auto &File : Scan) {
  38. Events.emplace_back(DirectoryWatcher::Event{
  39. DirectoryWatcher::Event::EventKind::Modified, File});
  40. }
  41. return Events;
  42. }
  43. } // namespace clang