1/* $Id: bsd-statvfs.c,v 1.2 2014/01/17 07:10:59 dtucker Exp $ */
2
3/*
4 * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
5 *
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
21#if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
22
23#include <sys/param.h>
24#ifdef HAVE_SYS_MOUNT_H
25# include <sys/mount.h>
26#endif
27
28#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)
38{
39	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
68	errno = ENOSYS;
69	return -1;
70#  endif
71}
72# 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
92#endif
93