Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <dirent.h> |
| 18 | |
Elliott Hughes | 13a7610 | 2021-03-16 16:19:16 -0700 | [diff] [blame^] | 19 | #include <assert.h> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Elliott Hughes | 13a7610 | 2021-03-16 16:19:16 -0700 | [diff] [blame^] | 21 | #include <fcntl.h> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 23 | #include <string.h> |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 25 | |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 26 | #include "platform/bionic/macros.h" |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 27 | #include "private/ScopedReaddir.h" |
| 28 | |
| 29 | // A smart pointer to the scandir dirent**. |
| 30 | class ScandirResult { |
| 31 | public: |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 32 | ScandirResult() : names_(nullptr), size_(0), capacity_(0) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ~ScandirResult() { |
Elliott Hughes | 13a7610 | 2021-03-16 16:19:16 -0700 | [diff] [blame^] | 36 | // We always call release(), so this can't happen. |
| 37 | if (names_ != nullptr) __assert(__FILE__, __LINE__, "missing call to release()"); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | size_t size() { |
| 41 | return size_; |
| 42 | } |
| 43 | |
| 44 | dirent** release() { |
| 45 | dirent** result = names_; |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 46 | names_ = nullptr; |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 47 | size_ = capacity_ = 0; |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | bool Add(dirent* entry) { |
| 52 | if (size_ >= capacity_) { |
| 53 | size_t new_capacity = capacity_ + 32; |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 54 | dirent** new_names = |
| 55 | reinterpret_cast<dirent**>(realloc(names_, new_capacity * sizeof(dirent*))); |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 56 | if (new_names == nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | names_ = new_names; |
| 60 | capacity_ = new_capacity; |
| 61 | } |
| 62 | |
| 63 | dirent* copy = CopyDirent(entry); |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 64 | if (copy == nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | names_[size_++] = copy; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | void Sort(int (*comparator)(const dirent**, const dirent**)) { |
| 72 | // If we have entries and a comparator, sort them. |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 73 | if (size_ > 0 && comparator != nullptr) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 74 | qsort(names_, size_, sizeof(dirent*), |
| 75 | reinterpret_cast<int (*)(const void*, const void*)>(comparator)); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | dirent** names_; |
| 81 | size_t size_; |
| 82 | size_t capacity_; |
| 83 | |
| 84 | static dirent* CopyDirent(dirent* original) { |
| 85 | // Allocate the minimum number of bytes necessary, rounded up to a 4-byte boundary. |
| 86 | size_t size = ((original->d_reclen + 3) & ~3); |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 87 | dirent* copy = reinterpret_cast<dirent*>(malloc(size)); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 88 | memcpy(copy, original, original->d_reclen); |
| 89 | return copy; |
| 90 | } |
| 91 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 92 | BIONIC_DISALLOW_COPY_AND_ASSIGN(ScandirResult); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 95 | int scandirat(int parent_fd, const char* dir_name, dirent*** name_list, |
| 96 | int (*filter)(const dirent*), |
| 97 | int (*comparator)(const dirent**, const dirent**)) { |
| 98 | DIR* dir = nullptr; |
| 99 | if (parent_fd == AT_FDCWD) { |
| 100 | dir = opendir(dir_name); |
| 101 | } else { |
| 102 | int dir_fd = openat(parent_fd, dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY); |
| 103 | if (dir_fd != -1) { |
| 104 | dir = fdopendir(dir_fd); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ScopedReaddir reader(dir); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 109 | if (reader.IsBad()) { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | ScandirResult names; |
| 114 | dirent* entry; |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 115 | while ((entry = reader.ReadEntry()) != nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 116 | // If we have a filter, skip names that don't match. |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 117 | if (filter != nullptr && !(*filter)(entry)) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 118 | continue; |
| 119 | } |
| 120 | names.Add(entry); |
| 121 | } |
| 122 | |
| 123 | names.Sort(comparator); |
| 124 | |
| 125 | size_t size = names.size(); |
| 126 | *name_list = names.release(); |
| 127 | return size; |
| 128 | } |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 129 | __strong_alias(scandirat64, scandirat); |
| 130 | |
| 131 | int scandir(const char* dir_path, dirent*** name_list, |
| 132 | int (*filter)(const dirent*), |
| 133 | int (*comparator)(const dirent**, const dirent**)) { |
| 134 | return scandirat(AT_FDCWD, dir_path, name_list, filter, comparator); |
| 135 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 136 | __strong_alias(scandir64, scandir); |