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> |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 37 | #include "private/ErrnoRestorer.h" |
| 38 | #include "private/ScopedPthreadMutexLocker.h" |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | d1ead2a | 2014-06-06 15:24:20 -0700 | [diff] [blame] | 40 | extern "C" int __getdents64(unsigned int, dirent*, unsigned int); |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 42 | struct DIR { |
| 43 | int fd_; |
| 44 | size_t available_bytes_; |
| 45 | dirent* next_; |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 46 | long current_pos_; |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 47 | pthread_mutex_t mutex_; |
| 48 | dirent buff_[15]; |
| 49 | }; |
| 50 | |
| 51 | static DIR* __allocate_DIR(int fd) { |
| 52 | DIR* d = reinterpret_cast<DIR*>(malloc(sizeof(DIR))); |
| 53 | if (d == NULL) { |
| 54 | return NULL; |
| 55 | } |
| 56 | d->fd_ = fd; |
| 57 | d->available_bytes_ = 0; |
| 58 | d->next_ = NULL; |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 59 | d->current_pos_ = 0L; |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 60 | pthread_mutex_init(&d->mutex_, NULL); |
| 61 | return d; |
| 62 | } |
| 63 | |
| 64 | int dirfd(DIR* dirp) { |
| 65 | return dirp->fd_; |
| 66 | } |
| 67 | |
| 68 | DIR* fdopendir(int fd) { |
| 69 | // Is 'fd' actually a directory? |
| 70 | struct stat sb; |
| 71 | if (fstat(fd, &sb) == -1) { |
| 72 | return NULL; |
| 73 | } |
| 74 | if (!S_ISDIR(sb.st_mode)) { |
| 75 | errno = ENOTDIR; |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | return __allocate_DIR(fd); |
| 80 | } |
| 81 | |
| 82 | DIR* opendir(const char* path) { |
Elliott Hughes | f73183f | 2014-08-26 16:20:59 -0700 | [diff] [blame] | 83 | int fd = open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 84 | return (fd != -1) ? __allocate_DIR(fd) : NULL; |
| 85 | } |
| 86 | |
| 87 | static bool __fill_DIR(DIR* d) { |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 88 | int rc = TEMP_FAILURE_RETRY(__getdents64(d->fd_, d->buff_, sizeof(d->buff_))); |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 89 | if (rc <= 0) { |
| 90 | return false; |
| 91 | } |
| 92 | d->available_bytes_ = rc; |
| 93 | d->next_ = d->buff_; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static dirent* __readdir_locked(DIR* d) { |
| 98 | if (d->available_bytes_ == 0 && !__fill_DIR(d)) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | dirent* entry = d->next_; |
| 103 | d->next_ = reinterpret_cast<dirent*>(reinterpret_cast<char*>(entry) + entry->d_reclen); |
| 104 | d->available_bytes_ -= entry->d_reclen; |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 105 | // The directory entry offset uses 0, 1, 2 instead of real file offset, |
| 106 | // so the value range of long type is enough. |
| 107 | d->current_pos_ = static_cast<long>(entry->d_off); |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 108 | return entry; |
| 109 | } |
| 110 | |
| 111 | dirent* readdir(DIR* d) { |
| 112 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 113 | return __readdir_locked(d); |
| 114 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 115 | __strong_alias(readdir64, readdir); |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 116 | |
| 117 | int readdir_r(DIR* d, dirent* entry, dirent** result) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 118 | ErrnoRestorer errno_restorer; |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 119 | |
| 120 | *result = NULL; |
| 121 | errno = 0; |
| 122 | |
| 123 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 124 | |
| 125 | dirent* next = __readdir_locked(d); |
| 126 | if (errno != 0 && next == NULL) { |
| 127 | return errno; |
| 128 | } |
| 129 | |
| 130 | if (next != NULL) { |
| 131 | memcpy(entry, next, next->d_reclen); |
| 132 | *result = entry; |
| 133 | } |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 134 | return 0; |
| 135 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 136 | __strong_alias(readdir64_r, readdir_r); |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 137 | |
| 138 | int closedir(DIR* d) { |
| 139 | if (d == NULL) { |
| 140 | errno = EINVAL; |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | int fd = d->fd_; |
| 145 | pthread_mutex_destroy(&d->mutex_); |
| 146 | free(d); |
| 147 | return close(fd); |
| 148 | } |
| 149 | |
| 150 | void rewinddir(DIR* d) { |
| 151 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 152 | lseek(d->fd_, 0, SEEK_SET); |
| 153 | d->available_bytes_ = 0; |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 154 | d->current_pos_ = 0L; |
| 155 | } |
| 156 | |
| 157 | void seekdir(DIR* d, long offset) { |
| 158 | ScopedPthreadMutexLocker locker(&d->mutex_); |
| 159 | off_t ret = lseek(d->fd_, offset, SEEK_SET); |
| 160 | if (ret != -1L) { |
| 161 | d->available_bytes_ = 0; |
| 162 | d->current_pos_ = ret; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | long telldir(DIR* d) { |
| 167 | return d->current_pos_; |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | 701bec2 | 2013-02-25 13:14:31 -0800 | [diff] [blame] | 170 | int alphasort(const dirent** a, const dirent** b) { |
David 'Digit' Turner | c30396f | 2012-10-29 15:32:54 +0100 | [diff] [blame] | 171 | return strcoll((*a)->d_name, (*b)->d_name); |
| 172 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 173 | __strong_alias(alphasort64, alphasort); |