DirIterator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Vector.h>
  27. #include <LibCore/DirIterator.h>
  28. #include <errno.h>
  29. namespace Core {
  30. DirIterator::DirIterator(const StringView& path, Flags flags)
  31. : m_path(path)
  32. , m_flags(flags)
  33. {
  34. m_dir = opendir(path.to_string().characters());
  35. if (!m_dir) {
  36. m_error = errno;
  37. }
  38. }
  39. DirIterator::~DirIterator()
  40. {
  41. if (m_dir) {
  42. closedir(m_dir);
  43. m_dir = nullptr;
  44. }
  45. }
  46. bool DirIterator::advance_next()
  47. {
  48. if (!m_dir)
  49. return false;
  50. while (true) {
  51. errno = 0;
  52. auto* de = readdir(m_dir);
  53. if (!de) {
  54. m_error = errno;
  55. m_next = String();
  56. return false;
  57. }
  58. m_next = de->d_name;
  59. if (m_next.is_null())
  60. return false;
  61. if (m_flags & Flags::SkipDots && m_next.starts_with('.'))
  62. continue;
  63. if (m_flags & Flags::SkipParentAndBaseDir && (m_next == "." || m_next == ".."))
  64. continue;
  65. return !m_next.is_empty();
  66. }
  67. }
  68. bool DirIterator::has_next()
  69. {
  70. if (!m_next.is_null())
  71. return true;
  72. return advance_next();
  73. }
  74. String DirIterator::next_path()
  75. {
  76. if (m_next.is_null())
  77. advance_next();
  78. auto tmp = m_next;
  79. m_next = String();
  80. return tmp;
  81. }
  82. String DirIterator::next_full_path()
  83. {
  84. return String::formatted("{}/{}", m_path, next_path());
  85. }
  86. String find_executable_in_path(String filename)
  87. {
  88. if (filename.starts_with('/')) {
  89. if (access(filename.characters(), X_OK) == 0)
  90. return filename;
  91. return {};
  92. }
  93. for (auto directory : String { getenv("PATH") }.split(':')) {
  94. auto fullpath = String::formatted("{}/{}", directory, filename);
  95. if (access(fullpath.characters(), X_OK) == 0)
  96. return fullpath;
  97. }
  98. return {};
  99. }
  100. }