blob: 8058cfb0803f58cd32c6744a2212ba1bd3a72266 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080028
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070029#pragma once
30
31/**
32 * @file dirent.h
33 * @brief Directory entry iteration.
34 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <sys/cdefs.h>
Elliott Hughes414dd2d2024-10-16 14:48:30 +000037
38#include <stdint.h>
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070039#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
41__BEGIN_DECLS
42
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070043/** d_type value when the type is not known. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080044#define DT_UNKNOWN 0
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070045/** d_type value for a FIFO. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080046#define DT_FIFO 1
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070047/** d_type value for a character device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080048#define DT_CHR 2
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070049/** d_type value for a directory. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080050#define DT_DIR 4
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070051/** d_type value for a block device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080052#define DT_BLK 6
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070053/** d_type value for a regular file. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080054#define DT_REG 8
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070055/** d_type value for a symbolic link. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080056#define DT_LNK 10
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070057/** d_type value for a socket. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080058#define DT_SOCK 12
59#define DT_WHT 14
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070061#if defined(__LP64__)
62#define __DIRENT64_INO_T ino_t
63#else
64#define __DIRENT64_INO_T uint64_t /* Historical accident. */
65#endif
66
Elliott Hughesdb1ea342014-01-17 18:42:49 -080067#define __DIRENT64_BODY \
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070068 __DIRENT64_INO_T d_ino; \
69 off64_t d_off; \
70 unsigned short d_reclen; \
71 unsigned char d_type; \
72 char d_name[256]; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080073
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070074/** The structure returned by readdir(). Identical to dirent64 on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080075struct dirent { __DIRENT64_BODY };
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070076/** The structure returned by readdir64(). Identical to dirent on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080077struct dirent64 { __DIRENT64_BODY };
78
Calin Juravlef963da22014-05-13 11:01:11 +010079#undef __DIRENT64_BODY
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070080#undef __DIRENT64_INO_T
Calin Juravlef963da22014-05-13 11:01:11 +010081
Elliott Hughes8c79b4e2014-11-10 14:56:49 -080082/* glibc compatibility. */
83#undef _DIRENT_HAVE_D_NAMLEN /* Linux doesn't have a d_namlen field. */
84#define _DIRENT_HAVE_D_RECLEN
85#define _DIRENT_HAVE_D_OFF
86#define _DIRENT_HAVE_D_TYPE
87
Elliott Hughesa8a31782014-01-09 12:37:12 -080088#define d_fileno d_ino
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070090/** The structure returned by opendir()/fopendir(). */
Elliott Hughes063cfb22012-10-25 20:55:23 -070091typedef struct DIR DIR;
92
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070093/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000094 * [opendir(3)](https://man7.org/linux/man-pages/man3/opendir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070095 * opens a directory stream for the directory at `__path`.
96 *
97 * Returns null and sets `errno` on failure.
98 */
zijunzhao7d2df8b2023-03-17 23:15:17 +000099DIR* _Nullable opendir(const char* _Nonnull __path);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700100
101/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000102 * [fopendir(3)](https://man7.org/linux/man-pages/man3/opendir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700103 * opens a directory stream for the directory at `__dir_fd`.
104 *
105 * Returns null and sets `errno` on failure.
106 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000107DIR* _Nullable fdopendir(int __dir_fd);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700108
109/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000110 * [readdir(3)](https://man7.org/linux/man-pages/man3/readdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700111 * returns the next directory entry in the given directory.
112 *
113 * Returns a pointer to a directory entry on success,
114 * or returns null and leaves `errno` unchanged at the end of the directory,
115 * or returns null and sets `errno` on failure.
116 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000117struct dirent* _Nullable readdir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700118
119/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000120 * [readdir64(3)](https://man7.org/linux/man-pages/man3/readdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700121 * returns the next directory entry in the given directory.
122 *
123 * Returns a pointer to a directory entry on success,
124 * or returns null and leaves `errno` unchanged at the end of the directory,
125 * or returns null and sets `errno` on failure.
126 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700127struct dirent64* _Nullable readdir64(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700128
zijunzhao7d2df8b2023-03-17 23:15:17 +0000129int readdir_r(DIR* _Nonnull __dir, struct dirent* _Nonnull __entry, struct dirent* _Nullable * _Nonnull __buffer) __attribute__((__deprecated__("readdir_r is deprecated; use readdir instead")));
Elliott Hughes655e4302023-06-16 12:39:33 -0700130int readdir64_r(DIR* _Nonnull __dir, struct dirent64* _Nonnull __entry, struct dirent64* _Nullable * _Nonnull __buffer) __attribute__((__deprecated__("readdir64_r is deprecated; use readdir64 instead")));
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700131
132/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000133 * [closedir(3)](https://man7.org/linux/man-pages/man3/closedir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700134 * closes a directory stream.
135 *
136 * Returns 0 on success and returns -1 and sets `errno` on failure.
137 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000138int closedir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700139
140/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000141 * [rewinddir(3)](https://man7.org/linux/man-pages/man3/rewinddir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700142 * rewinds a directory stream to the first entry.
143 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000144void rewinddir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700145
146/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000147 * [seekdir(3)](https://man7.org/linux/man-pages/man3/seekdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700148 * seeks a directory stream to the given entry, which must be a value returned
149 * by telldir().
150 *
151 * Available since API level 23.
152 */
Dan Albert02ce4012024-10-25 19:13:49 +0000153
154#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhao7d2df8b2023-03-17 23:15:17 +0000155void seekdir(DIR* _Nonnull __dir, long __location) __INTRODUCED_IN(23);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700156
157/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000158 * [telldir(3)](https://man7.org/linux/man-pages/man3/telldir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700159 * returns a value representing the current position in the directory
160 * for use with seekdir().
161 *
162 * Returns the current position on success and returns -1 and sets `errno` on failure.
163 *
164 * Available since API level 23.
165 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000166long telldir(DIR* _Nonnull __dir) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +0000167#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
168
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700169
170/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000171 * [dirfd(3)](https://man7.org/linux/man-pages/man3/dirfd.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700172 * returns the file descriptor backing the given directory stream.
173 *
174 * Returns a file descriptor on success and returns -1 and sets `errno` on failure.
175 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000176int dirfd(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700177
178/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000179 * [alphasort](https://man7.org/linux/man-pages/man3/alphasort.3.html) is a
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700180 * comparator for use with scandir() that uses strcoll().
181 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000182int alphasort(const struct dirent* _Nonnull * _Nonnull __lhs, const struct dirent* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700183
184/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000185 * [alphasort64](https://man7.org/linux/man-pages/man3/alphasort.3.html) is a
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700186 * comparator for use with scandir64() that uses strcmp().
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700187 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700188int alphasort64(const struct dirent64* _Nonnull * _Nonnull __lhs, const struct dirent64* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700189
190/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000191 * [scandir(3)](https://man7.org/linux/man-pages/man3/scandir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700192 * scans all the directory `__path`, filtering entries with `__filter` and
193 * sorting them with qsort() using the given `__comparator`, and storing them
194 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000195 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700196 *
197 * Returns the number of entries returned in the list on success,
198 * and returns -1 and sets `errno` on failure.
199 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000200int scandir(const char* _Nonnull __path, struct dirent* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent* _Nonnull), int (* _Nullable __comparator)(const struct dirent* _Nonnull * _Nonnull, const struct dirent* _Nonnull * _Nonnull));
Elliott Hughes6331e802015-10-27 11:10:36 -0700201
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700202/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000203 * [scandir64(3)](https://man7.org/linux/man-pages/man3/scandir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700204 * scans all the directory `__path`, filtering entries with `__filter` and
205 * sorting them with qsort() using the given `__comparator`, and storing them
206 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000207 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700208 *
209 * Returns the number of entries returned in the list on success,
210 * and returns -1 and sets `errno` on failure.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700211 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700212int scandir64(const char* _Nonnull __path, struct dirent64* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent64* _Nonnull), int (* _Nullable __comparator)(const struct dirent64* _Nonnull * _Nonnull, const struct dirent64* _Nonnull * _Nonnull));
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700213
Elliott Hughes6331e802015-10-27 11:10:36 -0700214#if defined(__USE_GNU)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700215
216/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000217 * [scandirat64(3)](https://man7.org/linux/man-pages/man3/scandirat.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700218 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
219 * filtering entries with `__filter` and sorting them with qsort() using the
220 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
221 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000222 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700223 *
224 * Returns the number of entries returned in the list on success,
225 * and returns -1 and sets `errno` on failure.
226 *
227 * Available since API level 24.
228 */
Dan Albert02ce4012024-10-25 19:13:49 +0000229
230#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhao7d2df8b2023-03-17 23:15:17 +0000231int scandirat64(int __dir_fd, const char* _Nonnull __path, struct dirent64* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent64* _Nonnull), int (* _Nullable __comparator)(const struct dirent64* _Nonnull * _Nonnull, const struct dirent64* _Nonnull * _Nonnull)) __INTRODUCED_IN(24);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700232
233/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000234 * [scandirat(3)](https://man7.org/linux/man-pages/man3/scandirat.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700235 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
236 * filtering entries with `__filter` and sorting them with qsort() using the
237 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
238 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000239 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700240 *
241 * Returns the number of entries returned in the list on success,
242 * and returns -1 and sets `errno` on failure.
243 *
244 * Available since API level 24.
245 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000246int scandirat(int __dir_fd, const char* _Nonnull __path, struct dirent* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent* _Nonnull), int (* _Nullable __comparator)(const struct dirent* _Nonnull * _Nonnull, const struct dirent* _Nonnull * _Nonnull)) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +0000247#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
248
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700249
Elliott Hughes6331e802015-10-27 11:10:36 -0700250#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800251
252__END_DECLS