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