Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <dirent.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <pthread.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
| 38 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame^] | 39 | #include "private/ErrnoRestorer.h" |
| 40 | #include "private/ScopedPthreadMutexLocker.h" |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 41 | |
| 42 | struct DIR { |
| 43 | int fd_; |
| 44 | size_t available_bytes_; |
| 45 | dirent* next_; |
| 46 | pthread_mutex_t mutex_; |
| 47 | dirent buff_[15]; |
| 48 | }; |
| 49 | |
| 50 | static DIR* __allocate_DIR(int fd) { |
| 51 | DIR* d = reinterpret_cast<DIR*>(malloc(sizeof(DIR))); |
| 52 | if (d == NULL) { |
| 53 | return NULL; |
| 54 | } |
| 55 | d->fd_ = fd; |
| 56 | d->available_bytes_ = 0; |
| 57 | d->next_ = NULL; |
| 58 | pthread_mutex_init(&d->mutex_, NULL); |
| 59 | return d; |
| 60 | } |
| 61 | |
| 62 | int dirfd(DIR* dirp) { |
| 63 | return dirp->fd_; |
| 64 | } |
| 65 | |
| 66 | DIR* fdopendir(int fd) { |
| 67 | // Is 'fd' actually a directory? |
| 68 | struct stat sb; |
| 69 | if (fstat(fd, &sb) == -1) { |
| 70 | return NULL; |
| 71 | } |
| 72 | if (!S_ISDIR(sb.st_mode)) { |
| 73 | errno = ENOTDIR; |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | return __allocate_DIR(fd); |
| 78 | } |
| 79 | |
| 80 | DIR* opendir(const char* path) { |
| 81 | int fd = open(path, O_RDONLY | O_DIRECTORY); |
| 82 | return (fd != -1) ? __allocate_DIR(fd) : NULL; |
| 83 | } |
| 84 | |
| 85 | static bool __fill_DIR(DIR* d) { |
| 86 | int rc = TEMP_FAILURE_RETRY(getdents(d->fd_, d->buff_, sizeof(d->buff_))); |
| 87 | if (rc <= 0) { |
| 88 | return false; |
| 89 | } |
| 90 | d->available_bytes_ = rc; |
| 91 | d->next_ = d->buff_; |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | static dirent* __readdir_locked(DIR* d) { |
| 96 | if (d->available_bytes_ == 0 && !__fill_DIR(d)) { |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | dirent* entry = d->next_; |
| 101 | d->next_ = reinterpret_cast<dirent*>(reinterpret_cast<char*>(entry) + entry->d_reclen); |
| 102 | d->available_bytes_ -= entry->d_reclen; |
| 103 | return entry; |
| 104 | } |
| 105 | |
| 106 | dirent* readdir(DIR* d) { |
| 107 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 108 | return __readdir_locked(d); |
| 109 | } |
| 110 | |
| 111 | int readdir_r(DIR* d, dirent* entry, dirent** result) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame^] | 112 | ErrnoRestorer errno_restorer; |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 113 | |
| 114 | *result = NULL; |
| 115 | errno = 0; |
| 116 | |
| 117 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 118 | |
| 119 | dirent* next = __readdir_locked(d); |
| 120 | if (errno != 0 && next == NULL) { |
| 121 | return errno; |
| 122 | } |
| 123 | |
| 124 | if (next != NULL) { |
| 125 | memcpy(entry, next, next->d_reclen); |
| 126 | *result = entry; |
| 127 | } |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int closedir(DIR* d) { |
| 132 | if (d == NULL) { |
| 133 | errno = EINVAL; |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | int fd = d->fd_; |
| 138 | pthread_mutex_destroy(&d->mutex_); |
| 139 | free(d); |
| 140 | return close(fd); |
| 141 | } |
| 142 | |
| 143 | void rewinddir(DIR* d) { |
| 144 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 145 | lseek(d->fd_, 0, SEEK_SET); |
| 146 | d->available_bytes_ = 0; |
| 147 | } |
| 148 | |
| 149 | int scandir(const char* path, dirent*** namelist, |
| 150 | int(*filter)(const dirent*), |
| 151 | int(*compar)(const dirent**, const dirent**)) |
| 152 | { |
| 153 | int n_elem = 0; |
| 154 | dirent* this_de, *de; |
| 155 | dirent** de_list = NULL; |
| 156 | int de_list_size = 0; |
| 157 | |
| 158 | DIR* d = opendir(path); |
| 159 | if (d == NULL) { |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | while ((this_de = readdir(d)) != NULL) { |
| 164 | if (filter != NULL && (*filter)(this_de) == 0) { |
| 165 | continue; |
| 166 | } |
| 167 | if (n_elem == 0) { |
| 168 | de_list_size = 4; |
| 169 | de_list = (dirent**) malloc(sizeof(dirent*) * de_list_size); |
| 170 | if (de_list == NULL) { |
| 171 | return -1; |
| 172 | } |
| 173 | } else if (n_elem == de_list_size) { |
| 174 | de_list_size += 10; |
| 175 | dirent** de_list_new = (dirent**) realloc(de_list, sizeof(dirent*) * de_list_size); |
| 176 | if (de_list_new == NULL) { |
| 177 | free(de_list); |
| 178 | return -1; |
| 179 | } |
| 180 | de_list = de_list_new; |
| 181 | } |
| 182 | de = (dirent*) malloc(sizeof(dirent)); |
| 183 | *de = *this_de; |
| 184 | de_list[n_elem++] = de; |
| 185 | } |
| 186 | closedir(d); |
| 187 | if (n_elem && compar) { |
| 188 | qsort(de_list, n_elem, sizeof(dirent*), (int (*)(const void*, const void*)) compar); |
| 189 | } |
| 190 | *namelist = de_list; |
| 191 | return n_elem; |
| 192 | } |
David 'Digit' Turner | c30396f | 2012-10-29 15:32:54 +0100 | [diff] [blame] | 193 | |
| 194 | int alphasort(const struct dirent** a, const struct dirent** b) { |
| 195 | return strcoll((*a)->d_name, (*b)->d_name); |
| 196 | } |