DirIterator.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Vector.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <errno.h>
  10. namespace Core {
  11. DirIterator::DirIterator(DeprecatedString path, Flags flags)
  12. : m_path(move(path))
  13. , m_flags(flags)
  14. {
  15. m_dir = opendir(m_path.characters());
  16. if (!m_dir) {
  17. m_error = Error::from_errno(errno);
  18. }
  19. }
  20. DirIterator::~DirIterator()
  21. {
  22. if (m_dir) {
  23. closedir(m_dir);
  24. m_dir = nullptr;
  25. }
  26. }
  27. DirIterator::DirIterator(DirIterator&& other)
  28. : m_dir(other.m_dir)
  29. , m_error(move(other.m_error))
  30. , m_next(move(other.m_next))
  31. , m_path(move(other.m_path))
  32. , m_flags(other.m_flags)
  33. {
  34. other.m_dir = nullptr;
  35. }
  36. bool DirIterator::advance_next()
  37. {
  38. if (!m_dir)
  39. return false;
  40. while (true) {
  41. errno = 0;
  42. auto* de = readdir(m_dir);
  43. if (!de) {
  44. if (errno != 0) {
  45. m_error = Error::from_errno(errno);
  46. dbgln("DirIteration error: {}", m_error.value());
  47. }
  48. m_next.clear();
  49. return false;
  50. }
  51. #ifdef AK_OS_SOLARIS
  52. m_next = DirectoryEntry::from_stat(m_dir, *de);
  53. #else
  54. m_next = DirectoryEntry::from_dirent(*de);
  55. #endif
  56. if (m_next->name.is_empty())
  57. return false;
  58. if (m_flags & Flags::SkipDots && m_next->name.starts_with('.'))
  59. continue;
  60. if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == ".."))
  61. continue;
  62. return !m_next->name.is_empty();
  63. }
  64. }
  65. bool DirIterator::has_next()
  66. {
  67. if (m_next.has_value())
  68. return true;
  69. return advance_next();
  70. }
  71. Optional<DirectoryEntry> DirIterator::next()
  72. {
  73. if (!m_next.has_value())
  74. advance_next();
  75. auto result = m_next;
  76. m_next.clear();
  77. return result;
  78. }
  79. DeprecatedString DirIterator::next_path()
  80. {
  81. auto entry = next();
  82. if (entry.has_value())
  83. return entry->name;
  84. return "";
  85. }
  86. DeprecatedString DirIterator::next_full_path()
  87. {
  88. StringBuilder builder;
  89. builder.append(m_path);
  90. if (!m_path.ends_with('/'))
  91. builder.append('/');
  92. builder.append(next_path());
  93. return builder.to_deprecated_string();
  94. }
  95. int DirIterator::fd() const
  96. {
  97. if (!m_dir)
  98. return -1;
  99. return dirfd(m_dir);
  100. }
  101. }