blob: 5d42aaa4d558d7e4e7852a360d97eb4a22ff1cb1 [file] [log] [blame]
Elliott Hughes06040fd2013-07-09 13:25:03 -07001/*
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
21extern "C" int __statfs64(const char*, size_t, struct statfs*);
22extern "C" int __fstatfs64(int, size_t, struct statfs*);
23
24#define ST_VALID 0x0020
25
Elliott Hughes6184c1f2013-07-09 14:18:36 -070026#if defined(__mips__)
27#define __val val
28#endif
29
Elliott Hughes06040fd2013-07-09 13:25:03 -070030static void __statfs_to_statvfs(const struct statfs& in, struct statvfs* out) {
31 out->f_bsize = in.f_bsize;
32 out->f_frsize = in.f_frsize;
33 out->f_blocks = in.f_blocks;
34 out->f_bfree = in.f_bfree;
35 out->f_bavail = in.f_bavail;
36 out->f_files = in.f_files;
37 out->f_ffree = in.f_ffree;
38 out->f_favail = in.f_ffree;
39 out->f_fsid = in.f_fsid.__val[0] | (static_cast<uint64_t>(in.f_fsid.__val[1]) << 32);
40 out->f_flag = in.f_flags & ~ST_VALID;
41 out->f_namemax = in.f_namelen;
42}
43
44int statvfs(const char* path, struct statvfs* result) {
45 struct statfs tmp;
46 int rc = __statfs64(path, sizeof(tmp), &tmp);
47 if (rc != 0) {
48 return rc;
49 }
50 __statfs_to_statvfs(tmp, result);
51 return 0;
52}
53
54int fstatvfs(int fd, struct statvfs* result) {
55 struct statfs tmp;
56 int rc = __fstatfs64(fd, sizeof(tmp), &tmp);
57 if (rc != 0) {
58 return rc;
59 }
60 __statfs_to_statvfs(tmp, result);
61 return 0;
62}