OSDN Git Service

Add <sys/statvfs.h>.
authorElliott Hughes <enh@google.com>
Tue, 9 Jul 2013 20:25:03 +0000 (13:25 -0700)
committerElliott Hughes <enh@google.com>
Tue, 9 Jul 2013 20:25:03 +0000 (13:25 -0700)
Bug: 2512019
Change-Id: I6e7fd3fa281977cc4bc270481a95416b5b2dc351

libc/Android.mk
libc/bionic/statvfs.cpp [new file with mode: 0644]
libc/include/sys/cdefs.h
libc/include/sys/signalfd.h
libc/include/sys/statvfs.h [new file with mode: 0644]
libc/include/sys/swap.h
libc/include/sys/vfs.h
tests/Android.mk
tests/statvfs_test.cpp [new file with mode: 0644]

index eba0a00..16b24de 100644 (file)
@@ -225,6 +225,7 @@ libc_bionic_src_files := \
     bionic/setlocale.cpp \
     bionic/signalfd.cpp \
     bionic/sigwait.cpp \
+    bionic/statvfs.cpp \
     bionic/__strcat_chk.cpp \
     bionic/__strchr_chk.cpp \
     bionic/__strcpy_chk.cpp \
diff --git a/libc/bionic/statvfs.cpp b/libc/bionic/statvfs.cpp
new file mode 100644 (file)
index 0000000..5d58281
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/statvfs.h>
+
+#include <sys/statfs.h>
+
+extern "C" int __statfs64(const char*, size_t, struct statfs*);
+extern "C" int __fstatfs64(int, size_t, struct statfs*);
+
+#define ST_VALID 0x0020
+
+static void __statfs_to_statvfs(const struct statfs& in, struct statvfs* out) {
+  out->f_bsize = in.f_bsize;
+  out->f_frsize = in.f_frsize;
+  out->f_blocks = in.f_blocks;
+  out->f_bfree = in.f_bfree;
+  out->f_bavail = in.f_bavail;
+  out->f_files = in.f_files;
+  out->f_ffree = in.f_ffree;
+  out->f_favail = in.f_ffree;
+  out->f_fsid = in.f_fsid.__val[0] | (static_cast<uint64_t>(in.f_fsid.__val[1]) << 32);
+  out->f_flag = in.f_flags & ~ST_VALID;
+  out->f_namemax = in.f_namelen;
+}
+
+int statvfs(const char* path, struct statvfs* result) {
+  struct statfs tmp;
+  int rc = __statfs64(path, sizeof(tmp), &tmp);
+  if (rc != 0) {
+    return rc;
+  }
+  __statfs_to_statvfs(tmp, result);
+  return 0;
+}
+
+int fstatvfs(int fd, struct statvfs* result) {
+  struct statfs tmp;
+  int rc = __fstatfs64(fd, sizeof(tmp), &tmp);
+  if (rc != 0) {
+    return rc;
+  }
+  __statfs_to_statvfs(tmp, result);
+  return 0;
+}
index c7fb9de..a4c1aff 100644 (file)
 #define __statement(x) (x)
 #endif
 
-#define __printflike(x, y) __attribute__((__format__(printf, x, y))) __attribute__((__nonnull__(x)))
-#define __scanflike(x, y) __attribute__((__format__(scanf, x, y))) __attribute__((__nonnull__(x)))
+#define __nonnull(args) __attribute__((__nonnull__ args))
+
+#define __printflike(x, y) __attribute__((__format__(printf, x, y))) __nonnull((x))
+#define __scanflike(x, y) __attribute__((__format__(scanf, x, y))) __nonnull((x))
 
 /*
  * C99 defines the restrict type qualifier keyword, which was made available
index a249d65..2537ab9 100644 (file)
@@ -35,7 +35,7 @@
 
 __BEGIN_DECLS
 
-extern int signalfd(int fd, const sigset_t* mask, int flags) __attribute__((__nonnull__(2)));
+extern int signalfd(int fd, const sigset_t* mask, int flags) __nonnull((2));
 
 __END_DECLS
 
diff --git a/libc/include/sys/statvfs.h b/libc/include/sys/statvfs.h
new file mode 100644 (file)
index 0000000..e910c03
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _SYS_STATVFS_H_
+#define _SYS_STATVFS_H_
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+struct statvfs {
+  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;
+};
+
+#define ST_RDONLY      0x0001
+#define ST_NOSUID      0x0002
+#define ST_NODEV       0x0004
+#define ST_NOEXEC      0x0008
+#define ST_SYNCHRONOUS 0x0010
+#define ST_MANDLOCK    0x0040
+#define ST_NOATIME     0x0400
+#define ST_NODIRATIME  0x0800
+#define ST_RELATIME    0x1000
+
+extern int statvfs(const char* __restrict, struct statvfs* __restrict) __nonnull((1, 2));
+extern int fstatvfs(int, struct statvfs*) __nonnull((2));
+
+__END_DECLS
+
+#endif /* _SYS_STATVFS_H_ */
index 85627f9..97a6f36 100644 (file)
@@ -33,8 +33,8 @@
 
 __BEGIN_DECLS
 
-extern int swapon(const char *, int) __attribute__((__nonnull__(1)));
-extern int swapoff(const char *) __attribute__((__nonnull__(1)));
+extern int swapon(const char*, int) __nonnull((1));
+extern int swapoff(const char*) __nonnull((1));
 
 __END_DECLS
 
index d14944d..6a55c99 100644 (file)
@@ -34,7 +34,7 @@
 
 __BEGIN_DECLS
 
-/* note: this corresponds to the kernel's statfs64 type */
+/* These correspond to the kernel's statfs64 type. */
 #ifdef __mips__
 struct statfs {
     uint32_t        f_type;
@@ -48,7 +48,8 @@ struct statfs {
     uint64_t        f_bavail;
     __kernel_fsid_t f_fsid;
     uint32_t        f_namelen;
-    uint32_t        f_spare[6];
+    uint32_t        f_flags;
+    uint32_t        f_spare[5];
 };
 #else
 struct statfs {
@@ -62,7 +63,8 @@ struct statfs {
     __kernel_fsid_t f_fsid;
     uint32_t        f_namelen;
     uint32_t        f_frsize;
-    uint32_t        f_spare[5];
+    uint32_t        f_flags;
+    uint32_t        f_spare[4];
 };
 #endif
 
index 34aaec9..efee17b 100644 (file)
@@ -74,6 +74,7 @@ test_src_files = \
     signal_test.cpp \
     stack_protector_test.cpp \
     stack_unwinding_test.cpp \
+    statvfs_test.cpp \
     stdio_test.cpp \
     stdlib_test.cpp \
     string_test.cpp \
diff --git a/tests/statvfs_test.cpp b/tests/statvfs_test.cpp
new file mode 100644 (file)
index 0000000..8afc6fd
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/statvfs.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+TEST(statvfs, statvfs) {
+  struct statvfs sb;
+  memset(&sb, 0, sizeof(sb));
+
+  ASSERT_EQ(0, statvfs("/", &sb));
+#if __BIONIC__
+  ASSERT_EQ(0U, sb.f_bfree);
+  ASSERT_EQ(0U, sb.f_ffree);
+  ASSERT_EQ(0U, sb.f_fsid);
+  ASSERT_TRUE((sb.f_flag & ST_RDONLY) != 0);
+#endif
+
+#if __BIONIC__
+  ASSERT_EQ(0, statvfs("/data/data", &sb));
+  ASSERT_NE(0U, sb.f_bfree);
+  ASSERT_NE(0U, sb.f_ffree);
+  ASSERT_NE(0U, sb.f_fsid);
+  ASSERT_FALSE((sb.f_flag & ST_RDONLY) != 0);
+  ASSERT_TRUE((sb.f_flag & ST_NOSUID) != 0);
+#endif
+}
+
+TEST(statvfs, fstatvfs) {
+  struct statvfs sb;
+  memset(&sb, 0, sizeof(sb));
+
+  int fd = open("/", O_RDONLY);
+  ASSERT_EQ(0, fstatvfs(fd, &sb));
+  close(fd);
+#if __BIONIC__
+  ASSERT_EQ(0U, sb.f_bfree);
+  ASSERT_EQ(0U, sb.f_ffree);
+  ASSERT_EQ(0U, sb.f_fsid);
+  ASSERT_TRUE((sb.f_flag & ST_RDONLY) != 0);
+#endif
+
+#if __BIONIC__
+  fd = open("/data/data", O_RDONLY);
+  ASSERT_EQ(0, fstatvfs(fd, &sb));
+  close(fd);
+  ASSERT_NE(0U, sb.f_bfree);
+  ASSERT_NE(0U, sb.f_ffree);
+  ASSERT_NE(0U, sb.f_fsid);
+  ASSERT_FALSE((sb.f_flag & ST_RDONLY) != 0);
+  ASSERT_TRUE((sb.f_flag & ST_NOSUID) != 0);
+#endif
+}