blob: ed52bcce0e6f7da0c0da074b1fce1b3032bb833c [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 Hughesb8104622014-08-19 09:18:03 -070028
Elliott Hughes733cedd2020-02-21 23:21:28 -080029#pragma once
30
31/**
32 * @file sys/stat.h
33 * @brief File status.
34 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Elliott Hughes414dd2d2024-10-16 14:48:30 +000036#include <sys/cdefs.h>
37
Elliott Hughes5704c422016-01-25 18:06:24 -080038#include <bits/timespec.h>
Yabin Cuidb499032014-12-09 20:15:48 -080039#include <linux/stat.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
42__BEGIN_DECLS
43
Elliott Hughes5cc8a462022-10-06 00:26:18 +000044#if defined(__aarch64__) || defined(__riscv)
Elliott Hughesdb1ea342014-01-17 18:42:49 -080045#define __STAT64_BODY \
Elliott Hughes9257c472014-12-18 15:17:42 -080046 dev_t st_dev; \
47 ino_t st_ino; \
48 mode_t st_mode; \
49 nlink_t st_nlink; \
Elliott Hughes6b555932014-02-18 16:43:31 -080050 uid_t st_uid; \
51 gid_t st_gid; \
Elliott Hughes9257c472014-12-18 15:17:42 -080052 dev_t st_rdev; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080053 unsigned long __pad1; \
Elliott Hughes9257c472014-12-18 15:17:42 -080054 off_t st_size; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080055 int st_blksize; \
56 int __pad2; \
57 long st_blocks; \
Elliott Hughes0ac0df82014-11-07 19:15:10 -080058 struct timespec st_atim; \
59 struct timespec st_mtim; \
60 struct timespec st_ctim; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080061 unsigned int __unused4; \
62 unsigned int __unused5; \
63
Elliott Hughesb8fa5b12013-12-19 16:50:22 -080064#elif defined(__x86_64__)
Elliott Hughesdb1ea342014-01-17 18:42:49 -080065#define __STAT64_BODY \
Elliott Hughes1c52e6c2014-12-18 12:38:44 -080066 dev_t st_dev; \
67 ino_t st_ino; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080068 unsigned long st_nlink; \
Elliott Hughes1c52e6c2014-12-18 12:38:44 -080069 mode_t st_mode; \
Elliott Hughes6b555932014-02-18 16:43:31 -080070 uid_t st_uid; \
71 gid_t st_gid; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080072 unsigned int __pad0; \
Elliott Hughes1c52e6c2014-12-18 12:38:44 -080073 dev_t st_rdev; \
74 off_t st_size; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080075 long st_blksize; \
76 long st_blocks; \
Elliott Hughes0ac0df82014-11-07 19:15:10 -080077 struct timespec st_atim; \
78 struct timespec st_mtim; \
79 struct timespec st_ctim; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080080 long __pad3[3]; \
81
Elliott Hughes9257c472014-12-18 15:17:42 -080082#else /* __arm__ || __i386__ */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080083#define __STAT64_BODY \
84 unsigned long long st_dev; \
85 unsigned char __pad0[4]; \
86 unsigned long __st_ino; \
87 unsigned int st_mode; \
Elliott Hughes9257c472014-12-18 15:17:42 -080088 nlink_t st_nlink; \
Elliott Hughes6b555932014-02-18 16:43:31 -080089 uid_t st_uid; \
90 gid_t st_gid; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080091 unsigned long long st_rdev; \
92 unsigned char __pad3[4]; \
93 long long st_size; \
94 unsigned long st_blksize; \
95 unsigned long long st_blocks; \
Elliott Hughes0ac0df82014-11-07 19:15:10 -080096 struct timespec st_atim; \
97 struct timespec st_mtim; \
98 struct timespec st_ctim; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080099 unsigned long long st_ino; \
100
Raghu Gandham6437eac2012-08-02 16:50:10 -0700101#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
Elliott Hughes219e6022024-08-27 19:12:08 +0000103/** The file information returned by fstat()/fstatat()/lstat()/stat(). */
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800104struct stat { __STAT64_BODY };
Elliott Hughes219e6022024-08-27 19:12:08 +0000105
106/**
107 * A synonym for `struct stat` on Android,
108 * provided for source compatibility with other systems.
109 */
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800110struct stat64 { __STAT64_BODY };
111
Calin Juravlef963da22014-05-13 11:01:11 +0100112#undef __STAT64_BODY
113
Elliott Hughes0ac0df82014-11-07 19:15:10 -0800114/* Compatibility with older versions of POSIX. */
115#define st_atime st_atim.tv_sec
116#define st_mtime st_mtim.tv_sec
117#define st_ctime st_ctim.tv_sec
118/* Compatibility with glibc. */
119#define st_atimensec st_atim.tv_nsec
120#define st_mtimensec st_mtim.tv_nsec
121#define st_ctimensec st_ctim.tv_nsec
Dan Alberta4e774c2017-06-12 14:59:38 -0700122/* Compatibility with Linux headers and old NDKs. */
123#define st_atime_nsec st_atim.tv_nsec
124#define st_mtime_nsec st_mtim.tv_nsec
125#define st_ctime_nsec st_ctim.tv_nsec
David 'Digit' Turner09baf4e2009-06-22 12:16:06 +0200126
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700127#if defined(__USE_BSD)
Hakan Kvistf27b7fb2012-10-10 08:32:52 +0200128/* Permission macros provided by glibc for compatibility with BSDs. */
129#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO) /* 0777 */
130#define ALLPERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) /* 07777 */
131#define DEFFILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) /* 0666 */
132#endif
133
Elliott Hughes1f1a51a2016-03-31 17:05:30 -0700134#if defined(__USE_BSD) || defined(__USE_GNU)
135#define S_IREAD S_IRUSR
136#define S_IWRITE S_IWUSR
137#define S_IEXEC S_IXUSR
138#endif
139
Elliott Hughes92887612016-08-16 13:26:35 -0700140/* POSIX mandates these, but Linux doesn't implement them as distinct file types. */
141#define S_TYPEISMQ(__sb) 0
142#define S_TYPEISSEM(__sb) 0
143#define S_TYPEISSHM(__sb) 0
144#define S_TYPEISTMO(__sb) 0
145
Elliott Hughes219e6022024-08-27 19:12:08 +0000146/**
147 * [chmod(2)](https://man7.org/linux/man-pages/man2/chmod.2.html)
148 * changes the mode of a file given a path.
149 *
150 * Returns 0 on success and returns -1 and sets `errno` on failure.
151 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000152int chmod(const char* _Nonnull __path, mode_t __mode);
Elliott Hughes219e6022024-08-27 19:12:08 +0000153
154/**
155 * [fchmod(2)](https://man7.org/linux/man-pages/man2/fchmod.2.html)
156 * changes the mode of a file given a file descriptor.
157 *
158 * Returns 0 on success and returns -1 and sets `errno` on failure.
159 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000160int fchmod(int __fd, mode_t __mode);
Elliott Hughes219e6022024-08-27 19:12:08 +0000161
162/**
163 * [fchmodat(2)](https://man7.org/linux/man-pages/man2/fchmodat.2.html)
164 * changes the mode of a file.
165 *
166 * Returns 0 on success and returns -1 and sets `errno` on failure.
167 */
168int fchmodat(int __dir_fd, const char* _Nonnull __path, mode_t __mode, int __flags);
169
170/**
171 * [chmod(2)](https://man7.org/linux/man-pages/man2/chmod.2.html)
172 * changes the mode of a file given a path, without following symlinks.
173 *
174 * Equivalent to `fchmodat(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW)`.
175 *
176 * Available since API 36.
177 *
178 * Returns 0 on success and returns -1 and sets `errno` on failure.
179 */
180int lchmod(const char* _Nonnull __path, mode_t __mode) __INTRODUCED_IN(36);
181
182/**
183 * [mkdir(2)](https://man7.org/linux/man-pages/man2/mkdir.2.html)
184 * creates a directory.
185 *
186 * Returns 0 on success and returns -1 and sets `errno` on failure.
187 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000188int mkdir(const char* _Nonnull __path, mode_t __mode);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughes219e6022024-08-27 19:12:08 +0000190/**
191 * [mkdirat(2)](https://man7.org/linux/man-pages/man2/mkdirat.2.html)
192 * creates a directory.
193 *
194 * Returns 0 on success and returns -1 and sets `errno` on failure.
195 */
196int mkdirat(int __dir_fd, const char* _Nonnull __path, mode_t __mode);
197
198/**
199 * [fstat(2)](https://man7.org/linux/man-pages/man2/fstat.2.html)
200 * gets file status given a file descriptor.
201 *
202 * Returns 0 on success and returns -1 and sets `errno` on failure.
203 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000204int fstat(int __fd, struct stat* _Nonnull __buf);
Elliott Hughes219e6022024-08-27 19:12:08 +0000205
206/** An alias for fstat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700207int fstat64(int __fd, struct stat64* _Nonnull __buf);
Elliott Hughes219e6022024-08-27 19:12:08 +0000208
209/**
210 * [fstatat(2)](https://man7.org/linux/man-pages/man2/fstatat.2.html)
211 * gets file status.
212 *
213 * Returns 0 on success and returns -1 and sets `errno` on failure.
214 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000215int fstatat(int __dir_fd, const char* _Nonnull __path, struct stat* _Nonnull __buf, int __flags);
Elliott Hughes219e6022024-08-27 19:12:08 +0000216
217/** An alias for fstatat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700218int fstatat64(int __dir_fd, const char* _Nonnull __path, struct stat64* _Nonnull __buf, int __flags);
Elliott Hughes219e6022024-08-27 19:12:08 +0000219
220/**
221 * [lstat(2)](https://man7.org/linux/man-pages/man2/lstat.2.html)
222 * gets file status given a path, without following symlinks.
223 *
224 * Returns 0 on success and returns -1 and sets `errno` on failure.
225 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000226int lstat(const char* _Nonnull __path, struct stat* _Nonnull __buf);
Elliott Hughes219e6022024-08-27 19:12:08 +0000227
228/** An alias for lstat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700229int lstat64(const char* _Nonnull __path, struct stat64* _Nonnull __buf);
Elliott Hughes219e6022024-08-27 19:12:08 +0000230
231/**
232 * [stat(2)](https://man7.org/linux/man-pages/man2/stat.2.html)
233 * gets file status given a path.
234 *
235 * Returns 0 on success and returns -1 and sets `errno` on failure.
236 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000237int stat(const char* _Nonnull __path, struct stat* _Nonnull __buf);
Elliott Hughes219e6022024-08-27 19:12:08 +0000238
239/** An alias for stat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700240int stat64(const char* _Nonnull __path, struct stat64* _Nonnull __buf);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800241
Elliott Hughes219e6022024-08-27 19:12:08 +0000242/**
243 * [mknod(2)](https://man7.org/linux/man-pages/man2/mknod.2.html)
244 * creates a directory, special, or regular file.
245 *
246 * Returns 0 on success and returns -1 and sets `errno` on failure.
247 */
zijunzhao1cda74d2023-02-03 02:28:40 +0000248int mknod(const char* _Nonnull __path, mode_t __mode, dev_t __dev);
Elliott Hughes219e6022024-08-27 19:12:08 +0000249
250/**
251 * [mknodat(2)](https://man7.org/linux/man-pages/man2/mknodat.2.html)
252 * creates a directory, special, or regular file.
253 *
254 * Returns 0 on success and returns -1 and sets `errno` on failure.
255 */
256int mknodat(int __dir_fd, const char* _Nonnull __path, mode_t __mode, dev_t __dev);
257
258/**
259 * [umask(2)](https://man7.org/linux/man-pages/man2/umask.2.html)
260 * gets and sets the process-wide file mode creation mask.
261 *
262 * Returns the previous file mode creation mask.
263 */
George Burgess IV90242352018-02-06 12:51:31 -0800264mode_t umask(mode_t __mask);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265
George Burgess IVb97049c2017-07-24 15:05:05 -0700266#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
267#include <bits/fortify/stat.h>
268#endif
Nick Kralevichcd587702012-09-26 10:02:30 -0700269
Elliott Hughes219e6022024-08-27 19:12:08 +0000270/**
271 * [mkfifo(2)](https://man7.org/linux/man-pages/man2/mkfifo.2.html)
272 * creates a FIFO.
273 *
274 * Returns 0 on success and returns -1 and sets `errno` on failure.
275 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700276int mkfifo(const char* _Nonnull __path, mode_t __mode);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277
Elliott Hughes219e6022024-08-27 19:12:08 +0000278/**
279 * [mkfifoat(2)](https://man7.org/linux/man-pages/man2/mkfifoat.2.html)
280 * creates a FIFO.
281 *
282 * Returns 0 on success and returns -1 and sets `errno` on failure.
283 */
284int mkfifoat(int __dir_fd, const char* _Nonnull __path, mode_t __mode) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
zijunzhao1cda74d2023-02-03 02:28:40 +0000286/**
287 * Used in the tv_nsec field of an argument to utimensat()/futimens()
288 * to set that time to the current time.
289 */
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800290#define UTIME_NOW ((1L << 30) - 1L)
zijunzhao1cda74d2023-02-03 02:28:40 +0000291
292/**
293 * Used in the tv_nsec field of an argument to utimensat()/futimens()
294 * to _not_ set that time.
295 */
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800296#define UTIME_OMIT ((1L << 30) - 2L)
zijunzhao1cda74d2023-02-03 02:28:40 +0000297
298/**
299 * [utimensat(2)](https://man7.org/linux/man-pages/man2/utimensat.2.html) sets
300 * file timestamps.
301 *
302 * Note: Linux supports `__path` being NULL (in which case `__dir_fd` need not
303 * be a directory), allowing futimens() to be implemented with utimensat().
304 * For normal use of utimensat(), though, `__path` should be non-null.
305 *
306 * `__times[0]` is the access time (atime), and `__times[1]` the last modification time (mtime).
307 * If `__times` is NULL, both times are set to the current time.
308 * See also UTIME_NOW and UTIME_OMIT.
309 *
310 * Returns 0 on success and returns -1 and sets `errno` on failure.
311 */
312int utimensat(int __dir_fd, const char* __BIONIC_COMPLICATED_NULLNESS __path, const struct timespec __times[_Nullable 2], int __flags);
313
314/**
315 * [futimens(2)](https://man7.org/linux/man-pages/man2/utimensat.2.html) sets
316 * the given file descriptor's timestamp.
317 *
318 * `__times[0]` is the access time (atime), and `__times[1]` the last modification time (mtime).
319 * If `__times` is NULL, both times are set to the current time.
320 * See also UTIME_NOW and UTIME_OMIT.
321 *
322 * Returns 0 on success and returns -1 and sets `errno` on failure.
zijunzhao1cda74d2023-02-03 02:28:40 +0000323 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700324int futimens(int __fd, const struct timespec __times[_Nullable 2]);
Ken Sumrallae2d5ba2011-03-18 11:55:12 -0700325
Elliott Hughes733cedd2020-02-21 23:21:28 -0800326#if defined(__USE_GNU)
327/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000328 * [statx(2)](https://man7.org/linux/man-pages/man2/statx.2.html) returns
Elliott Hughes733cedd2020-02-21 23:21:28 -0800329 * extended file status information.
330 *
331 * Returns 0 on success and returns -1 and sets `errno` on failure.
zijunzhao1cda74d2023-02-03 02:28:40 +0000332 *
333 * Available since API level 30.
Elliott Hughes733cedd2020-02-21 23:21:28 -0800334 */
Elliott Hughes68168932024-09-12 12:07:01 +0000335int statx(int __dir_fd, const char* _Nullable __path, int __flags, unsigned __mask, struct statx* _Nonnull __buf) __INTRODUCED_IN(30);
Elliott Hughes733cedd2020-02-21 23:21:28 -0800336#endif
337
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338__END_DECLS