Support statvfs on API levels before 19.
Split statfs and statvfs. The former has been available forever, and the
latter is implemented in terms of the former. The implementation has
been moved into headers so that it can be used at low API levels.
There's no reason for any Android or Linux code to use statvfs rather
than statfs, but code that needs to build on Darwin too will want to use
statvfs because Darwin's statfs is very spartan.
Bug: https://github.com/android-ndk/ndk/issues/609
Test: treehugger
Change-Id: Icf3d5723a260099fddb2d9f902e3047b0f041647
diff --git a/libc/include/sys/statvfs.h b/libc/include/sys/statvfs.h
index e60cadc..93fa3d7 100644
--- a/libc/include/sys/statvfs.h
+++ b/libc/include/sys/statvfs.h
@@ -14,8 +14,12 @@
* limitations under the License.
*/
-#ifndef _SYS_STATVFS_H_
-#define _SYS_STATVFS_H_
+#pragma once
+
+/**
+ * @file sys/statvfs.h
+ * @brief Filesystem statistics.
+ */
#include <stdint.h>
#include <sys/cdefs.h>
@@ -23,47 +27,123 @@
__BEGIN_DECLS
-#ifdef __LP64__
-#define __STATVFS64_RESERVED uint32_t __f_reserved[6];
-#else
-#define __STATVFS64_RESERVED
+struct statvfs {
+ /** Block size. */
+ unsigned long f_bsize;
+ /** Fragment size. */
+ unsigned long f_frsize;
+ /** Total size of filesystem in `f_frsize` blocks. */
+ fsblkcnt_t f_blocks;
+ /** Number of free blocks. */
+ fsblkcnt_t f_bfree;
+ /** Number of free blocks for non-root. */
+ fsblkcnt_t f_bavail;
+ /** Number of inodes. */
+ fsfilcnt_t f_files;
+ /** Number of free inodes. */
+ fsfilcnt_t f_ffree;
+ /** Number of free inodes for non-root. */
+ fsfilcnt_t f_favail;
+ /** Filesystem id. */
+ unsigned long f_fsid;
+ /** Mount flags. (See `ST_` constants.) */
+ unsigned long f_flag;
+ /** Maximum filename length. */
+ unsigned long f_namemax;
+
+#if defined(__LP64__)
+ uint32_t __f_reserved[6];
#endif
+};
-#define __STATVFS64_BODY \
- unsigned long f_bsize; \
- unsigned long f_frsize; \
- fsblkcnt_t f_blocks; \
- fsblkcnt_t f_bfree; \
- fsblkcnt_t f_bavail; \
- fsfilcnt_t f_files; \
- fsfilcnt_t f_ffree; \
- fsfilcnt_t f_favail; \
- unsigned long f_fsid; \
- unsigned long f_flag; \
- unsigned long f_namemax; \
- __STATVFS64_RESERVED
+struct statvfs64 {
+ /** Block size. */
+ unsigned long f_bsize;
+ /** Fragment size. */
+ unsigned long f_frsize;
+ /** Total size of filesystem in `f_frsize` blocks. */
+ fsblkcnt_t f_blocks;
+ /** Number of free blocks. */
+ fsblkcnt_t f_bfree;
+ /** Number of free blocks for non-root. */
+ fsblkcnt_t f_bavail;
+ /** Number of inodes. */
+ fsfilcnt_t f_files;
+ /** Number of free inodes. */
+ fsfilcnt_t f_ffree;
+ /** Number of free inodes for non-root. */
+ fsfilcnt_t f_favail;
+ /** Filesystem id. */
+ unsigned long f_fsid;
+ /** Mount flags. (See `ST_` constants.) */
+ unsigned long f_flag;
+ /** Maximum filename length. */
+ unsigned long f_namemax;
-struct statvfs { __STATVFS64_BODY };
-struct statvfs64 { __STATVFS64_BODY };
+#if defined(__LP64__)
+ uint32_t __f_reserved[6];
+#endif
+};
-#undef __STATVFS64_BODY
-#undef __STATVFS64_RESERVED
-
+/** Flag for `f_flag` in `struct statvfs`: mounted read-only. */
#define ST_RDONLY 0x0001
+
+/** Flag for `f_flag` in `struct statvfs`: setuid/setgid ignored. */
#define ST_NOSUID 0x0002
+
+/** Flag for `f_flag` in `struct statvfs`: access to device files disallowed. */
#define ST_NODEV 0x0004
+
+/** Flag for `f_flag` in `struct statvfs`: execution disallowed. */
#define ST_NOEXEC 0x0008
+
+/** Flag for `f_flag` in `struct statvfs`: writes synced immediately. */
#define ST_SYNCHRONOUS 0x0010
+
+/** Flag for `f_flag` in `struct statvfs`: mandatory locking permitted. */
#define ST_MANDLOCK 0x0040
+
+/** Flag for `f_flag` in `struct statvfs`: access times not updated. */
#define ST_NOATIME 0x0400
+
+/** Flag for `f_flag` in `struct statvfs`: directory access times not updated. */
#define ST_NODIRATIME 0x0800
+
+/** Flag for `f_flag` in `struct statvfs`: see `MS_RELATIME`. */
#define ST_RELATIME 0x1000
+#if __ANDROID_API__ >= 19
+// These functions are implemented as static inlines before API level 19.
+
+/**
+ * [statvfs(3)](http://man7.org/linux/man-pages/man3/statvfs.3.html)
+ * queries filesystem statistics for the given path.
+ *
+ * Returns 0 on success, and returns -1 and sets `errno` on failure.
+ */
int statvfs(const char* __path, struct statvfs* __buf) __INTRODUCED_IN(19);
-int statvfs64(const char* __path, struct statvfs64* __buf) __INTRODUCED_IN(21);
+
+/**
+ * [fstatvfs(3)](http://man7.org/linux/man-pages/man3/fstatvfs.3.html)
+ * queries filesystem statistics for the given file descriptor.
+ *
+ * Returns 0 on success, and returns -1 and sets `errno` on failure.
+ */
int fstatvfs(int __fd, struct statvfs* __buf) __INTRODUCED_IN(19);
+
+#endif
+
+#if __ANDROID_API__ >= 21
+// These functions are implemented as static inlines before API level 21.
+
+/** Equivalent to statvfs(). */
+int statvfs64(const char* __path, struct statvfs64* __buf) __INTRODUCED_IN(21);
+
+/** Equivalent to fstatvfs(). */
int fstatvfs64(int __fd, struct statvfs64* __buf) __INTRODUCED_IN(21);
+#endif
+
__END_DECLS
-#endif
+#include <android/legacy_sys_statvfs_inlines.h>