blob: 2d93bfc82adf58281c5ac41b7de02b611e92141a [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $Id: bsd-statvfs.c,v 1.2 2014/01/17 07:10:59 dtucker Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002
3/*
Adam Langleyd0592972015-03-30 14:49:51 -07004 * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
Greg Hartmanbd77cf72015-02-25 13:21:06 -08005 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include "includes.h"
20
Adam Langleyd0592972015-03-30 14:49:51 -070021#if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080022
Adam Langleyd0592972015-03-30 14:49:51 -070023#include <sys/param.h>
24#ifdef HAVE_SYS_MOUNT_H
25# include <sys/mount.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080026#endif
27
Adam Langleyd0592972015-03-30 14:49:51 -070028#if defined(ANDROID)
29#include <sys/vfs.h>
30#include <string.h>
31#define MNAMELEN PATH_MAX
32#endif
33
34#include <errno.h>
35
36static void
37copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080038{
Adam Langleyd0592972015-03-30 14:49:51 -070039 to->f_bsize = from->f_bsize;
40 to->f_frsize = from->f_bsize; /* no exact equivalent */
41 to->f_blocks = from->f_blocks;
42 to->f_bfree = from->f_bfree;
43 to->f_bavail = from->f_bavail;
44 to->f_files = from->f_files;
45 to->f_ffree = from->f_ffree;
46 to->f_favail = from->f_ffree; /* no exact equivalent */
47 to->f_fsid = 0; /* XXX fix me */
48#if GCE_PLATFORM_SDK_VERSION >= 19
49 to->f_flag = from->f_flags;
50#else
51 to->f_flag = from->f_spare[0];
52#endif
53 to->f_namemax = MNAMELEN;
54}
55
56# ifndef HAVE_STATVFS
57int statvfs(const char *path, struct statvfs *buf)
58{
59# ifdef HAVE_STATFS
60 struct statfs fs;
61
62 memset(&fs, 0, sizeof(fs));
63 if (statfs(path, &fs) == -1)
64 return -1;
65 copy_statfs_to_statvfs(buf, &fs);
66 return 0;
67# else
Greg Hartmanbd77cf72015-02-25 13:21:06 -080068 errno = ENOSYS;
69 return -1;
Adam Langleyd0592972015-03-30 14:49:51 -070070# endif
Greg Hartmanbd77cf72015-02-25 13:21:06 -080071}
Adam Langleyd0592972015-03-30 14:49:51 -070072# endif
73
74# ifndef HAVE_FSTATVFS
75int fstatvfs(int fd, struct statvfs *buf)
76{
77# ifdef HAVE_FSTATFS
78 struct statfs fs;
79
80 memset(&fs, 0, sizeof(fs));
81 if (fstatfs(fd, &fs) == -1)
82 return -1;
83 copy_statfs_to_statvfs(buf, &fs);
84 return 0;
85# else
86 errno = ENOSYS;
87 return -1;
88# endif
89}
90# endif
91
Greg Hartmanbd77cf72015-02-25 13:21:06 -080092#endif