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 | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 19 | #include <fcntl.h> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 22 | #include <string.h> |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 23 | #include <unistd.h> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 24 | |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 25 | #include "private/bionic_macros.h" |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 26 | #include "private/ScopedReaddir.h" |
| 27 | |
| 28 | // A smart pointer to the scandir dirent**. |
| 29 | class ScandirResult { |
| 30 | public: |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 31 | ScandirResult() : names_(nullptr), size_(0), capacity_(0) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | ~ScandirResult() { |
| 35 | while (size_ > 0) { |
| 36 | free(names_[--size_]); |
| 37 | } |
| 38 | free(names_); |
| 39 | } |
| 40 | |
| 41 | size_t size() { |
| 42 | return size_; |
| 43 | } |
| 44 | |
| 45 | dirent** release() { |
| 46 | dirent** result = names_; |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 47 | names_ = nullptr; |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 48 | size_ = capacity_ = 0; |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | bool Add(dirent* entry) { |
| 53 | if (size_ >= capacity_) { |
| 54 | size_t new_capacity = capacity_ + 32; |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 55 | dirent** new_names = |
| 56 | reinterpret_cast<dirent**>(realloc(names_, new_capacity * sizeof(dirent*))); |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 57 | if (new_names == nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | names_ = new_names; |
| 61 | capacity_ = new_capacity; |
| 62 | } |
| 63 | |
| 64 | dirent* copy = CopyDirent(entry); |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 65 | if (copy == nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | names_[size_++] = copy; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void Sort(int (*comparator)(const dirent**, const dirent**)) { |
| 73 | // If we have entries and a comparator, sort them. |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 74 | if (size_ > 0 && comparator != nullptr) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 75 | qsort(names_, size_, sizeof(dirent*), |
| 76 | reinterpret_cast<int (*)(const void*, const void*)>(comparator)); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | dirent** names_; |
| 82 | size_t size_; |
| 83 | size_t capacity_; |
| 84 | |
| 85 | static dirent* CopyDirent(dirent* original) { |
| 86 | // Allocate the minimum number of bytes necessary, rounded up to a 4-byte boundary. |
| 87 | size_t size = ((original->d_reclen + 3) & ~3); |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 88 | dirent* copy = reinterpret_cast<dirent*>(malloc(size)); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 89 | memcpy(copy, original, original->d_reclen); |
| 90 | return copy; |
| 91 | } |
| 92 | |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(ScandirResult); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 96 | int scandirat(int parent_fd, const char* dir_name, dirent*** name_list, |
| 97 | int (*filter)(const dirent*), |
| 98 | int (*comparator)(const dirent**, const dirent**)) { |
| 99 | DIR* dir = nullptr; |
| 100 | if (parent_fd == AT_FDCWD) { |
| 101 | dir = opendir(dir_name); |
| 102 | } else { |
| 103 | int dir_fd = openat(parent_fd, dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY); |
| 104 | if (dir_fd != -1) { |
| 105 | dir = fdopendir(dir_fd); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ScopedReaddir reader(dir); |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 110 | if (reader.IsBad()) { |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | ScandirResult names; |
| 115 | dirent* entry; |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 116 | while ((entry = reader.ReadEntry()) != nullptr) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 117 | // If we have a filter, skip names that don't match. |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 118 | if (filter != nullptr && !(*filter)(entry)) { |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | names.Add(entry); |
| 122 | } |
| 123 | |
| 124 | names.Sort(comparator); |
| 125 | |
| 126 | size_t size = names.size(); |
| 127 | *name_list = names.release(); |
| 128 | return size; |
| 129 | } |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame^] | 130 | __strong_alias(scandirat64, scandirat); |
| 131 | |
| 132 | int scandir(const char* dir_path, dirent*** name_list, |
| 133 | int (*filter)(const dirent*), |
| 134 | int (*comparator)(const dirent**, const dirent**)) { |
| 135 | return scandirat(AT_FDCWD, dir_path, name_list, filter, comparator); |
| 136 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 137 | __strong_alias(scandir64, scandir); |