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 --- configs/mingw32_defconfig | 6 ++--- include/platform.h | 1 - libbb/find_mount_point.c | 37 +++++++++++++++++++++++++++++ win32/Kbuild | 2 ++ win32/mntent.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ win32/mntent.h | 19 +++++++++++++++ win32/statfs.c | 50 +++++++++++++++++++++++++++++++++++++++ win32/sys/vfs.h | 17 ++++++++++++++ 8 files changed, 187 insertions(+), 4 deletions(-) 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 diff --git a/configs/mingw32_defconfig b/configs/mingw32_defconfig index 52b6a50cb..1447a00f1 100644 --- a/configs/mingw32_defconfig +++ b/configs/mingw32_defconfig @@ -1,7 +1,7 @@ # # Automatically generated make config: don't edit -# Busybox version: 1.22.0.git -# Thu Mar 14 10:41:31 2013 +# Busybox version: 1.22.0-TIG-1095-ge4f28be +# Wed Apr 3 11:24:01 2013 # CONFIG_HAVE_DOT_CONFIG=y # CONFIG_PLATFORM_POSIX is not set @@ -213,7 +213,7 @@ CONFIG_DD=y # CONFIG_FEATURE_DD_SIGNAL_HANDLING is not set # CONFIG_FEATURE_DD_THIRD_STATUS_LINE is not set CONFIG_FEATURE_DD_IBS_OBS=y -# CONFIG_DF is not set +CONFIG_DF=y # CONFIG_FEATURE_DF_FANCY is not set CONFIG_DIRNAME=y CONFIG_DOS2UNIX=y diff --git a/include/platform.h b/include/platform.h index fa4267b4b..432cb9cae 100644 --- a/include/platform.h +++ b/include/platform.h @@ -420,7 +420,6 @@ typedef unsigned smalluint; # undef HAVE_VASPRINTF # undef HAVE_UNLOCKED_STDIO # undef HAVE_UNLOCKED_LINE_OPS -# undef HAVE_MNTENT_H # undef HAVE_SYS_STATFS_H #endif diff --git a/libbb/find_mount_point.c b/libbb/find_mount_point.c index 9676b5f52..b9bc1dd76 100644 --- a/libbb/find_mount_point.c +++ b/libbb/find_mount_point.c @@ -24,10 +24,18 @@ struct mntent* FAST_FUNC find_mount_point(const char *name, int subdir_too) struct mntent *mountEntry; dev_t devno_of_name; bool block_dev; +#if ENABLE_PLATFORM_MINGW32 + static char mnt_fsname[4]; + static char mnt_dir[4]; + struct mntent my_mount_entry = { mnt_fsname, mnt_dir, "", "", 0, 0 }; + char *current, *path; + DWORD len; +#endif if (stat(name, &s) != 0) return NULL; +#if !ENABLE_PLATFORM_MINGW32 devno_of_name = s.st_dev; block_dev = 0; /* Why S_ISCHR? - UBI volumes use char devices, not block */ @@ -64,6 +72,35 @@ struct mntent* FAST_FUNC find_mount_point(const char *name, int subdir_too) break; } endmntent(mtab_fp); +#else + mountEntry = NULL; + path = NULL; + current = NULL; + + if ( isalpha(name[0]) && name[1] == ':' ) { + path = name; + } + else { + if ( (len=GetCurrentDirectory(0, NULL)) > 0 && + (current=malloc(len+1)) != NULL && + GetCurrentDirectory(len, current) ) { + path = current; + } + } + + if ( path && isalpha(path[0]) && path[1] == ':' ) { + mnt_fsname[0] = path[0]; + mnt_fsname[1] = path[1]; + mnt_fsname[2] = '\0'; + mnt_dir[0] = path[0]; + mnt_dir[1] = path[1]; + mnt_dir[2] = '\\'; + mnt_dir[3] = '\0'; + + mountEntry = &my_mount_entry; + } + free(current); +#endif return mountEntry; } 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