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