blob: 10d8764392c247b28977e02c982f717c73641208 [file] [log] [blame]
Darren Tucker598eaa62008-06-09 03:32:29 +10001/*
Darren Tuckera5cf1e22014-01-17 18:10:58 +11002 * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
Darren Tucker598eaa62008-06-09 03:32:29 +10003 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "includes.h"
18
Darren Tuckera5cf1e22014-01-17 18:10:58 +110019#if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
Darren Tucker598eaa62008-06-09 03:32:29 +100020
Darren Tuckera5cf1e22014-01-17 18:10:58 +110021#include <sys/param.h>
22#ifdef HAVE_SYS_MOUNT_H
23# include <sys/mount.h>
Darren Tucker598eaa62008-06-09 03:32:29 +100024#endif
25
Darren Tuckera5cf1e22014-01-17 18:10:58 +110026#include <errno.h>
27
Darren Tucker11057562018-02-25 11:22:57 +110028#ifndef MNAMELEN
29# define MNAMELEN 32
30#endif
31
Darren Tuckerd561b0b2019-10-28 16:09:04 +110032#ifdef HAVE_STRUCT_STATFS_F_FILES
33# define HAVE_STRUCT_STATFS
34#endif
35
36#ifdef HAVE_STRUCT_STATFS
Darren Tuckera5cf1e22014-01-17 18:10:58 +110037static void
38copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
Darren Tucker598eaa62008-06-09 03:32:29 +100039{
Darren Tuckera5cf1e22014-01-17 18:10:58 +110040 to->f_bsize = from->f_bsize;
41 to->f_frsize = from->f_bsize; /* no exact equivalent */
42 to->f_blocks = from->f_blocks;
43 to->f_bfree = from->f_bfree;
44 to->f_bavail = from->f_bavail;
45 to->f_files = from->f_files;
46 to->f_ffree = from->f_ffree;
47 to->f_favail = from->f_ffree; /* no exact equivalent */
48 to->f_fsid = 0; /* XXX fix me */
Darren Tucker11057562018-02-25 11:22:57 +110049#ifdef HAVE_STRUCT_STATFS_F_FLAGS
Darren Tuckera5cf1e22014-01-17 18:10:58 +110050 to->f_flag = from->f_flags;
Darren Tucker11057562018-02-25 11:22:57 +110051#else
52 to->f_flag = 0;
53#endif
Darren Tuckera5cf1e22014-01-17 18:10:58 +110054 to->f_namemax = MNAMELEN;
55}
Darren Tuckerd561b0b2019-10-28 16:09:04 +110056#endif
Darren Tuckera5cf1e22014-01-17 18:10:58 +110057
58# ifndef HAVE_STATVFS
59int statvfs(const char *path, struct statvfs *buf)
60{
Darren Tuckerd561b0b2019-10-28 16:09:04 +110061# if defined(HAVE_STATFS) && defined(HAVE_STRUCT_STATFS)
Darren Tuckera5cf1e22014-01-17 18:10:58 +110062 struct statfs fs;
63
64 memset(&fs, 0, sizeof(fs));
65 if (statfs(path, &fs) == -1)
66 return -1;
67 copy_statfs_to_statvfs(buf, &fs);
68 return 0;
69# else
Darren Tucker598eaa62008-06-09 03:32:29 +100070 errno = ENOSYS;
71 return -1;
Darren Tuckera5cf1e22014-01-17 18:10:58 +110072# endif
Darren Tucker598eaa62008-06-09 03:32:29 +100073}
Darren Tuckera5cf1e22014-01-17 18:10:58 +110074# endif
75
76# ifndef HAVE_FSTATVFS
77int fstatvfs(int fd, struct statvfs *buf)
78{
Darren Tuckerd561b0b2019-10-28 16:09:04 +110079# if defined(HAVE_FSTATFS) && defined(HAVE_STRUCT_STATFS)
Darren Tuckera5cf1e22014-01-17 18:10:58 +110080 struct statfs fs;
81
82 memset(&fs, 0, sizeof(fs));
83 if (fstatfs(fd, &fs) == -1)
84 return -1;
85 copy_statfs_to_statvfs(buf, &fs);
86 return 0;
87# else
88 errno = ENOSYS;
89 return -1;
90# endif
91}
92# endif
93
Darren Tucker598eaa62008-06-09 03:32:29 +100094#endif