DirIterator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <unistd.h>
  30. namespace Core {
  31. DirIterator::DirIterator(String path, Flags flags)
  32. : m_path(move(path))
  33. , m_flags(flags)
  34. {
  35. m_dir = opendir(m_path.characters());
  36. if (!m_dir) {
  37. m_error = errno;
  38. }
  39. }
  40. DirIterator::~DirIterator()
  41. {
  42. if (m_dir) {
  43. closedir(m_dir);
  44. m_dir = nullptr;
  45. }
  46. }
  47. bool DirIterator::advance_next()
  48. {
  49. if (!m_dir)
  50. return false;
  51. while (true) {
  52. errno = 0;
  53. auto* de = readdir(m_dir);
  54. if (!de) {
  55. m_error = errno;
  56. m_next = String();
  57. return false;
  58. }
  59. m_next = de->d_name;
  60. if (m_next.is_null())
  61. return false;
  62. if (m_flags & Flags::SkipDots && m_next.starts_with('.'))
  63. continue;
  64. if (m_flags & Flags::SkipParentAndBaseDir && (m_next == "." || m_next == ".."))
  65. continue;
  66. return !m_next.is_empty();
  67. }
  68. }
  69. bool DirIterator::has_next()
  70. {
  71. if (!m_next.is_null())
  72. return true;
  73. return advance_next();
  74. }
  75. String DirIterator::next_path()
  76. {
  77. if (m_next.is_null())
  78. advance_next();
  79. auto tmp = m_next;
  80. m_next = String();
  81. return tmp;
  82. }
  83. String DirIterator::next_full_path()
  84. {
  85. return String::formatted("{}/{}", m_path, next_path());
  86. }
  87. String find_executable_in_path(String filename)
  88. {
  89. if (filename.starts_with('/')) {
  90. if (access(filename.characters(), X_OK) == 0)
  91. return filename;
  92. return {};
  93. }
  94. for (auto directory : String { getenv("PATH") }.split(':')) {
  95. auto fullpath = String::formatted("{}/{}", directory, filename);
  96. if (access(fullpath.characters(), X_OK) == 0)
  97. return fullpath;
  98. }
  99. return {};
  100. }
  101. }