From 8782950636f76faa8db0ed8c5d698ab0bd1fbf45 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 3 Apr 2013 12:09:21 +0100 Subject: df: limited implementation for WIN32 --- win32/Kbuild | 2 ++ win32/mntent.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ win32/mntent.h | 19 +++++++++++++++++++ win32/statfs.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ win32/sys/vfs.h | 17 +++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 win32/mntent.c create mode 100644 win32/mntent.h create mode 100644 win32/statfs.c create mode 100644 win32/sys/vfs.h (limited to 'win32') diff --git a/win32/Kbuild b/win32/Kbuild index 109b70e7b..564498c78 100644 --- a/win32/Kbuild +++ b/win32/Kbuild @@ -13,6 +13,8 @@ lib-$(CONFIG_PLATFORM_MINGW32) += regex.o lib-$(CONFIG_WIN32_NET) += net.o lib-$(CONFIG_PLATFORM_MINGW32) += poll.o lib-$(CONFIG_PLATFORM_MINGW32) += popen.o +lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o +lib-$(CONFIG_PLATFORM_MINGW32) += mntent.o lib-$(CONFIG_PLATFORM_MINGW32) += system.o lib-$(CONFIG_PLATFORM_MINGW32) += termios.o lib-$(CONFIG_PLATFORM_MINGW32) += uname.o diff --git a/win32/mntent.c b/win32/mntent.c new file mode 100644 index 000000000..22efbf1b7 --- /dev/null +++ b/win32/mntent.c @@ -0,0 +1,59 @@ +/* + * A simple WIN32 implementation of mntent routines. It only handles + * fixed logical drives. + */ +#include "libbb.h" + +struct mntdata { + DWORD flags; + int index; +}; + +FILE *setmntent(const char *file, const char *mode) +{ + struct mntdata *data; + + if ( (data=malloc(sizeof(struct mntdata))) == NULL ) { + return NULL; + } + + data->flags = GetLogicalDrives(); + data->index = -1; + + return (FILE *)data; +} + +struct mntent *getmntent(FILE *stream) +{ + struct mntdata *data = (struct mntdata *)stream; + static char mnt_fsname[4]; + static char mnt_dir[4]; + static struct mntent my_mount_entry = { mnt_fsname, mnt_dir, "", 0, 0 }; + static struct mntent *entry; + + entry = NULL; + while ( ++data->index < 26 ) { + if ( (data->flags & 1<index) != 0 ) { + mnt_fsname[0] = 'A' + data->index; + mnt_fsname[1] = ':'; + mnt_fsname[2] = '\0'; + mnt_dir[0] = 'A' + data->index; + mnt_dir[1] = ':'; + mnt_dir[2] = '\\'; + mnt_dir[3] = '\0'; + + if ( GetDriveType(mnt_dir) == DRIVE_FIXED ) { + entry = &my_mount_entry; + break; + } + } + } + + return entry; +} + +int endmntent(FILE *stream) +{ + free(stream); + return 0; +} diff --git a/win32/mntent.h b/win32/mntent.h new file mode 100644 index 000000000..b035bfa9c --- /dev/null +++ b/win32/mntent.h @@ -0,0 +1,19 @@ +#ifndef MNTENT_H +#define MNTENT_H + +#include + +struct mntent { + char *mnt_fsname; /* Device or server for filesystem. */ + char *mnt_dir; /* Directory mounted on. */ + char *mnt_type; /* Type of filesystem: ufs, nfs, etc. */ + char *mnt_opts; /* Comma-separated options for fs. */ + int mnt_freq; /* Dump frequency (in days). */ + int mnt_passno; /* Pass number for `fsck'. */ +}; + +extern FILE *setmntent(const char *file, const char *mode); +extern struct mntent *getmntent(FILE *stream); +extern int endmntent(FILE *stream); + +#endif diff --git a/win32/statfs.c b/win32/statfs.c new file mode 100644 index 000000000..8424d58fa --- /dev/null +++ b/win32/statfs.c @@ -0,0 +1,50 @@ +#include +#include "libbb.h" + +/* + * Code from libguestfs + */ +int statfs(const char *file, struct statfs *buf) +{ + ULONGLONG free_bytes_available; /* for user - similar to bavail */ + ULONGLONG total_number_of_bytes; + ULONGLONG total_number_of_free_bytes; /* for everyone - bfree */ + + if ( !GetDiskFreeSpaceEx(file, (PULARGE_INTEGER) &free_bytes_available, + (PULARGE_INTEGER) &total_number_of_bytes, + (PULARGE_INTEGER) &total_number_of_free_bytes) ) { + return -1; + } + + /* XXX I couldn't determine how to get block size. MSDN has a + * unhelpful hard-coded list here: + * http://support.microsoft.com/kb/140365 + * but this depends on the filesystem type, the size of the disk and + * the version of Windows. So this code assumes the disk is NTFS + * and the version of Windows is >= Win2K. + */ + if (total_number_of_bytes < UINT64_C(16) * 1024 * 1024 * 1024 * 1024) + buf->f_bsize = 4096; + else if (total_number_of_bytes < UINT64_C(32) * 1024 * 1024 * 1024 * 1024) + buf->f_bsize = 8192; + else if (total_number_of_bytes < UINT64_C(64) * 1024 * 1024 * 1024 * 1024) + buf->f_bsize = 16384; + else if (total_number_of_bytes < UINT64_C(128) * 1024 * 1024 * 1024 * 1024) + buf->f_bsize = 32768; + else + buf->f_bsize = 65536; + + /* As with stat, -1 indicates a field is not known. */ + buf->f_frsize = buf->f_bsize; + buf->f_blocks = total_number_of_bytes / buf->f_bsize; + buf->f_bfree = total_number_of_free_bytes / buf->f_bsize; + buf->f_bavail = free_bytes_available / buf->f_bsize; + buf->f_files = -1; + buf->f_ffree = -1; + buf->f_favail = -1; + buf->f_fsid = -1; + buf->f_flag = -1; + buf->f_namemax = FILENAME_MAX; + + return 0; +} diff --git a/win32/sys/vfs.h b/win32/sys/vfs.h new file mode 100644 index 000000000..cb7bda9af --- /dev/null +++ b/win32/sys/vfs.h @@ -0,0 +1,17 @@ +#include + +struct statfs { + uint64_t f_bsize; + uint64_t f_frsize; + uint64_t f_blocks; + uint64_t f_bfree; + uint64_t f_bavail; + uint64_t f_files; + uint64_t f_ffree; + uint64_t f_favail; + uint64_t f_fsid; + uint64_t f_flag; + uint64_t f_namemax; +}; + +extern int statfs(const char *file, struct statfs *buf); -- cgit v1.2.3-55-g6feb