OSDN Git Service

snap to klp-dev-plus-aosp 3f69e99 with history
[android-x86/bionic.git] / libc / bionic / statvfs.cpp
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sys/statvfs.h>
18
19 #include <sys/statfs.h>
20
21 // Paper over the fact that 32-bit kernels use fstatfs64/statfs64 with an extra argument,
22 // but 64-bit kernels don't have the "64" bit suffix or the extra size_t argument.
23 #if __LP64__
24 #  define __fstatfs64(fd,size,buf) fstatfs(fd,buf)
25 #  define __statfs64(path,size,buf) statfs(path,buf)
26 #else
27 extern "C" int __fstatfs64(int, size_t, struct statfs*);
28 extern "C" int __statfs64(const char*, size_t, struct statfs*);
29 #endif
30
31 #define ST_VALID 0x0020
32
33 static void __statfs_to_statvfs(const struct statfs& in, struct statvfs* out) {
34   out->f_bsize = in.f_bsize;
35   out->f_frsize = in.f_frsize;
36   out->f_blocks = in.f_blocks;
37   out->f_bfree = in.f_bfree;
38   out->f_bavail = in.f_bavail;
39   out->f_files = in.f_files;
40   out->f_ffree = in.f_ffree;
41   out->f_favail = in.f_ffree;
42   out->f_fsid = in.f_fsid.__val[0] | (static_cast<uint64_t>(in.f_fsid.__val[1]) << 32);
43   out->f_flag = in.f_flags & ~ST_VALID;
44   out->f_namemax = in.f_namelen;
45 }
46
47 int statvfs(const char* path, struct statvfs* result) {
48   struct statfs tmp;
49   int rc = __statfs64(path, sizeof(tmp), &tmp);
50   if (rc != 0) {
51     return rc;
52   }
53   __statfs_to_statvfs(tmp, result);
54   return 0;
55 }
56
57 int fstatvfs(int fd, struct statvfs* result) {
58   struct statfs tmp;
59   int rc = __fstatfs64(fd, sizeof(tmp), &tmp);
60   if (rc != 0) {
61     return rc;
62   }
63   __statfs_to_statvfs(tmp, result);
64   return 0;
65 }