| 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**)) { | 
| Elliott Hughes | f6c25d6 | 2023-03-28 22:34:05 +0000 | [diff] [blame] | 72 | qsort(names_, size_, sizeof(dirent*), | 
|  | 73 | reinterpret_cast<int (*)(const void*, const void*)>(comparator)); | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
|  | 76 | private: | 
|  | 77 | dirent** names_; | 
|  | 78 | size_t size_; | 
|  | 79 | size_t capacity_; | 
|  | 80 |  | 
|  | 81 | static dirent* CopyDirent(dirent* original) { | 
|  | 82 | // Allocate the minimum number of bytes necessary, rounded up to a 4-byte boundary. | 
|  | 83 | size_t size = ((original->d_reclen + 3) & ~3); | 
| Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 84 | dirent* copy = reinterpret_cast<dirent*>(malloc(size)); | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 85 | memcpy(copy, original, original->d_reclen); | 
|  | 86 | return copy; | 
|  | 87 | } | 
|  | 88 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 89 | BIONIC_DISALLOW_COPY_AND_ASSIGN(ScandirResult); | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 90 | }; | 
|  | 91 |  | 
| Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 92 | int scandirat(int parent_fd, const char* dir_name, dirent*** name_list, | 
|  | 93 | int (*filter)(const dirent*), | 
|  | 94 | int (*comparator)(const dirent**, const dirent**)) { | 
|  | 95 | DIR* dir = nullptr; | 
|  | 96 | if (parent_fd == AT_FDCWD) { | 
|  | 97 | dir = opendir(dir_name); | 
|  | 98 | } else { | 
|  | 99 | int dir_fd = openat(parent_fd, dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY); | 
|  | 100 | if (dir_fd != -1) { | 
|  | 101 | dir = fdopendir(dir_fd); | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | ScopedReaddir reader(dir); | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 106 | if (reader.IsBad()) { | 
|  | 107 | return -1; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | ScandirResult names; | 
|  | 111 | dirent* entry; | 
| Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 112 | while ((entry = reader.ReadEntry()) != nullptr) { | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 113 | // If we have a filter, skip names that don't match. | 
| Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 114 | if (filter != nullptr && !(*filter)(entry)) { | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 115 | continue; | 
|  | 116 | } | 
|  | 117 | names.Add(entry); | 
|  | 118 | } | 
|  | 119 |  | 
| Elliott Hughes | f6c25d6 | 2023-03-28 22:34:05 +0000 | [diff] [blame] | 120 | if (comparator != nullptr) { | 
|  | 121 | names.Sort(comparator); | 
|  | 122 | } | 
| Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | size_t size = names.size(); | 
|  | 125 | *name_list = names.release(); | 
|  | 126 | return size; | 
|  | 127 | } | 
| Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 128 | __strong_alias(scandirat64, scandirat); | 
|  | 129 |  | 
|  | 130 | int scandir(const char* dir_path, dirent*** name_list, | 
|  | 131 | int (*filter)(const dirent*), | 
|  | 132 | int (*comparator)(const dirent**, const dirent**)) { | 
|  | 133 | return scandirat(AT_FDCWD, dir_path, name_list, filter, comparator); | 
|  | 134 | } | 
| Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 135 | __strong_alias(scandir64, scandir); |