blob: 3a7b5b414955c9e6d8e4b4e636518cf1d7f90882 [file] [log] [blame]
Elliott Hughes063cfb22012-10-25 20:55:23 -07001/*
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
39#include <private/ScopedPthreadMutexLocker.h>
40
41struct DIR {
42 int fd_;
43 size_t available_bytes_;
44 dirent* next_;
45 pthread_mutex_t mutex_;
46 dirent buff_[15];
47};
48
49static DIR* __allocate_DIR(int fd) {
50 DIR* d = reinterpret_cast<DIR*>(malloc(sizeof(DIR)));
51 if (d == NULL) {
52 return NULL;
53 }
54 d->fd_ = fd;
55 d->available_bytes_ = 0;
56 d->next_ = NULL;
57 pthread_mutex_init(&d->mutex_, NULL);
58 return d;
59}
60
61int dirfd(DIR* dirp) {
62 return dirp->fd_;
63}
64
65DIR* fdopendir(int fd) {
66 // Is 'fd' actually a directory?
67 struct stat sb;
68 if (fstat(fd, &sb) == -1) {
69 return NULL;
70 }
71 if (!S_ISDIR(sb.st_mode)) {
72 errno = ENOTDIR;
73 return NULL;
74 }
75
76 return __allocate_DIR(fd);
77}
78
79DIR* opendir(const char* path) {
80 int fd = open(path, O_RDONLY | O_DIRECTORY);
81 return (fd != -1) ? __allocate_DIR(fd) : NULL;
82}
83
84static bool __fill_DIR(DIR* d) {
85 int rc = TEMP_FAILURE_RETRY(getdents(d->fd_, d->buff_, sizeof(d->buff_)));
86 if (rc <= 0) {
87 return false;
88 }
89 d->available_bytes_ = rc;
90 d->next_ = d->buff_;
91 return true;
92}
93
94static dirent* __readdir_locked(DIR* d) {
95 if (d->available_bytes_ == 0 && !__fill_DIR(d)) {
96 return NULL;
97 }
98
99 dirent* entry = d->next_;
100 d->next_ = reinterpret_cast<dirent*>(reinterpret_cast<char*>(entry) + entry->d_reclen);
101 d->available_bytes_ -= entry->d_reclen;
102 return entry;
103}
104
105dirent* readdir(DIR* d) {
106 ScopedPthreadMutexLocker locker(&d->mutex_);
107 return __readdir_locked(d);
108}
109
110int readdir_r(DIR* d, dirent* entry, dirent** result) {
111 int saved_errno = errno;
112
113 *result = NULL;
114 errno = 0;
115
116 ScopedPthreadMutexLocker locker(&d->mutex_);
117
118 dirent* next = __readdir_locked(d);
119 if (errno != 0 && next == NULL) {
120 return errno;
121 }
122
123 if (next != NULL) {
124 memcpy(entry, next, next->d_reclen);
125 *result = entry;
126 }
127 errno = saved_errno;
128 return 0;
129}
130
131int 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
143void rewinddir(DIR* d) {
144 ScopedPthreadMutexLocker locker(&d->mutex_);
145 lseek(d->fd_, 0, SEEK_SET);
146 d->available_bytes_ = 0;
147}
148
149int 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' Turnerc30396f2012-10-29 15:32:54 +0100193
194int alphasort(const struct dirent** a, const struct dirent** b) {
195 return strcoll((*a)->d_name, (*b)->d_name);
196}