Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 1 | /* |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 2 | * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au> |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 3 | * |
| 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 Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 19 | #if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS) |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 20 | |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 21 | #include <sys/param.h> |
| 22 | #ifdef HAVE_SYS_MOUNT_H |
| 23 | # include <sys/mount.h> |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 24 | #endif |
| 25 | |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | |
Darren Tucker | 1105756 | 2018-02-25 11:22:57 +1100 | [diff] [blame] | 28 | #ifndef MNAMELEN |
| 29 | # define MNAMELEN 32 |
| 30 | #endif |
| 31 | |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 32 | static void |
| 33 | copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from) |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 34 | { |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 35 | to->f_bsize = from->f_bsize; |
| 36 | to->f_frsize = from->f_bsize; /* no exact equivalent */ |
| 37 | to->f_blocks = from->f_blocks; |
| 38 | to->f_bfree = from->f_bfree; |
| 39 | to->f_bavail = from->f_bavail; |
| 40 | to->f_files = from->f_files; |
| 41 | to->f_ffree = from->f_ffree; |
| 42 | to->f_favail = from->f_ffree; /* no exact equivalent */ |
| 43 | to->f_fsid = 0; /* XXX fix me */ |
Darren Tucker | 1105756 | 2018-02-25 11:22:57 +1100 | [diff] [blame] | 44 | #ifdef HAVE_STRUCT_STATFS_F_FLAGS |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 45 | to->f_flag = from->f_flags; |
Darren Tucker | 1105756 | 2018-02-25 11:22:57 +1100 | [diff] [blame] | 46 | #else |
| 47 | to->f_flag = 0; |
| 48 | #endif |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 49 | to->f_namemax = MNAMELEN; |
| 50 | } |
| 51 | |
| 52 | # ifndef HAVE_STATVFS |
| 53 | int statvfs(const char *path, struct statvfs *buf) |
| 54 | { |
| 55 | # ifdef HAVE_STATFS |
| 56 | struct statfs fs; |
| 57 | |
| 58 | memset(&fs, 0, sizeof(fs)); |
| 59 | if (statfs(path, &fs) == -1) |
| 60 | return -1; |
| 61 | copy_statfs_to_statvfs(buf, &fs); |
| 62 | return 0; |
| 63 | # else |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 64 | errno = ENOSYS; |
| 65 | return -1; |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 66 | # endif |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 67 | } |
Darren Tucker | a5cf1e2 | 2014-01-17 18:10:58 +1100 | [diff] [blame] | 68 | # endif |
| 69 | |
| 70 | # ifndef HAVE_FSTATVFS |
| 71 | int fstatvfs(int fd, struct statvfs *buf) |
| 72 | { |
| 73 | # ifdef HAVE_FSTATFS |
| 74 | struct statfs fs; |
| 75 | |
| 76 | memset(&fs, 0, sizeof(fs)); |
| 77 | if (fstatfs(fd, &fs) == -1) |
| 78 | return -1; |
| 79 | copy_statfs_to_statvfs(buf, &fs); |
| 80 | return 0; |
| 81 | # else |
| 82 | errno = ENOSYS; |
| 83 | return -1; |
| 84 | # endif |
| 85 | } |
| 86 | # endif |
| 87 | |
Darren Tucker | 598eaa6 | 2008-06-09 03:32:29 +1000 | [diff] [blame] | 88 | #endif |