From 94150aba92d308e417aa552a1b3a957be264b423 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 7 Jan 2014 14:43:08 +0000 Subject: Revise mingw_stat to minimise changes from upstream BusyBox --- coreutils/cp.c | 4 ---- coreutils/libcoreutils/cp_mv_stat.c | 4 ---- coreutils/stat.c | 8 +------ include/mingw.h | 45 +++++++++++++++++++++---------------- libbb/copy_file.c | 4 +--- libbb/recursive_action.c | 2 +- util-linux/swaponoff.c | 2 +- win32/mingw.c | 42 +++++++++++++++++++++++++++------- 8 files changed, 64 insertions(+), 47 deletions(-) diff --git a/coreutils/cp.c b/coreutils/cp.c index fee82c26b..de2e512be 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -149,11 +149,7 @@ int cp_main(int argc, char **argv) /* If there are only two arguments and... */ if (argc == 2) { s_flags = cp_mv_stat2(*argv, &source_stat, -#if !ENABLE_PLATFORM_MINGW32 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); -#else - (flags & FILEUTILS_DEREFERENCE) ? mingw_stat : lstat); -#endif if (s_flags < 0) return EXIT_FAILURE; d_flags = cp_mv_stat(last, &dest_stat); diff --git a/coreutils/libcoreutils/cp_mv_stat.c b/coreutils/libcoreutils/cp_mv_stat.c index 6e9fd09f0..5ba07ecc3 100644 --- a/coreutils/libcoreutils/cp_mv_stat.c +++ b/coreutils/libcoreutils/cp_mv_stat.c @@ -46,9 +46,5 @@ int FAST_FUNC cp_mv_stat2(const char *fn, struct stat *fn_stat, stat_func sf) int FAST_FUNC cp_mv_stat(const char *fn, struct stat *fn_stat) { -#if !ENABLE_PLATFORM_MINGW32 return cp_mv_stat2(fn, fn_stat, stat); -#else - return cp_mv_stat2(fn, fn_stat, mingw_stat); -#endif } diff --git a/coreutils/stat.c b/coreutils/stat.c index e501fb41d..dc9d81c35 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -338,11 +338,7 @@ static void FAST_FUNC print_stat(char *pformat, const char m, printf(pformat, (unsigned long long) statbuf->st_blocks); } else if (m == 'o') { strcat(pformat, "lu"); -#if !ENABLE_PLATFORM_MINGW32 printf(pformat, (unsigned long) statbuf->st_blksize); -#else - printf(pformat, (unsigned long) 4096); -#endif } else if (m == 'x') { printfs(pformat, human_time(statbuf->st_atime)); } else if (m == 'X') { @@ -550,7 +546,6 @@ static bool do_statfs(const char *filename, const char *format) static bool do_stat(const char *filename, const char *format) { struct stat statbuf; - int status; #if ENABLE_SELINUX security_context_t scontext = NULL; @@ -565,8 +560,7 @@ static bool do_stat(const char *filename, const char *format) } } #endif - status = option_mask32 & OPT_DEREFERENCE ? stat(filename, &statbuf) : lstat(filename, &statbuf); - if (status != 0) { + if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) { bb_perror_msg("can't stat '%s'", filename); return 0; } diff --git a/include/mingw.h b/include/mingw.h index e8c55f646..b3fc603c0 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -204,9 +204,6 @@ NOIMPL(mingw_bind,SOCKET s UNUSED_PARAM,const struct sockaddr* sa UNUSED_PARAM,i /* * sys/stat.h */ -typedef int blkcnt_t; -typedef int nlink_t; - #define S_ISUID 04000 #define S_ISGID 02000 #define S_ISVTX 01000 @@ -233,27 +230,37 @@ NOIMPL(fchown,int fd UNUSED_PARAM, uid_t uid UNUSED_PARAM, gid_t gid UNUSED_PARA int mingw_mkdir(const char *path, int mode); #define mkdir mingw_mkdir -/* Use mingw_lstat()/mingw_stat() instead of lstat()/stat() and - * mingw_fstat() instead of fstat() on Windows. - */ #if ENABLE_LFS # define off_t off64_t #endif #define lseek _lseeki64 -#define stat _stati64 -int mingw_lstat(const char *file_name, struct stat *buf); -int mingw_stat(const char *file_name, struct stat *buf); -int mingw_fstat(int fd, struct stat *buf); -#define fstat mingw_fstat -#define lstat mingw_lstat -#define _stati64(x,y) mingw_stat(x,y) -/* The Windows stat structure doesn't have the st_blocks member. This - * macro calculates st_blocks from st_size. It would be better if we - * could put brackets around it but most references to st_blocks in BusyBox - * are pretty simple and work without brackets. - */ -#define st_blocks st_size+511>>9 +typedef int nlink_t; +typedef int blksize_t; +typedef off_t blkcnt_t; + +struct mingw_stat { + dev_t st_dev; + ino_t st_ino; + mode_t st_mode; + nlink_t st_nlink; + uid_t st_uid; + gid_t st_gid; + dev_t st_rdev; + off_t st_size; + time_t st_atime; + time_t st_mtime; + time_t st_ctime; + blksize_t st_blksize; + blkcnt_t st_blocks; +}; + +int mingw_lstat(const char *file_name, struct mingw_stat *buf); +int mingw_stat(const char *file_name, struct mingw_stat *buf); +int mingw_fstat(int fd, struct mingw_stat *buf); +#define lstat mingw_lstat +#define stat mingw_stat +#define fstat mingw_fstat /* * sys/sysmacros.h diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 7da31f69d..be65c4b47 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -81,13 +81,11 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags) smallint retval = 0; smallint dest_exists = 0; smallint ovr; - int status; /* Inverse of cp -d ("cp without -d") */ #define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0)) - status = FLAGS_DEREF ? stat(source, &source_stat) : lstat(source, &source_stat); - if (status < 0) { + if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) { /* This may be a dangling symlink. * Making [sym]links to dangling symlinks works, so... */ if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c index 7ec3a1443..b5cf7c0ab 100644 --- a/libbb/recursive_action.c +++ b/libbb/recursive_action.c @@ -73,7 +73,7 @@ int FAST_FUNC recursive_action(const char *fileName, if (depth == 0) follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0; follow &= flags; - status = follow ? stat(fileName, &statbuf) : lstat(fileName, &statbuf); + status = (follow ? stat : lstat)(fileName, &statbuf); if (status < 0) { #ifdef DEBUG_RECURS_ACTION bb_error_msg("status=%d flags=%x", status, flags); diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c index 94565f855..3f223343e 100644 --- a/util-linux/swaponoff.c +++ b/util-linux/swaponoff.c @@ -60,7 +60,7 @@ static int swap_enable_disable(char *device) #if ENABLE_DESKTOP /* test for holes */ if (S_ISREG(st.st_mode)) - if ((st.st_blocks) * (off_t)512 < st.st_size) + if (st.st_blocks * (off_t)512 < st.st_size) bb_error_msg("warning: swap file has holes"); #endif diff --git a/win32/mingw.c b/win32/mingw.c index 7dda6cd1b..b9f720e1a 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -212,7 +212,7 @@ static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fd * If follow is true then act like stat() and report on the link * target. Otherwise report on the link itself. */ -static int do_lstat(int follow, const char *file_name, struct stat *buf) +static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) { int err; WIN32_FILE_ATTRIBUTE_DATA fdata; @@ -253,6 +253,13 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) FindClose(handle); } } + + /* + * Assume a block is 4096 bytes and calculate number of 512 byte + * sectors. + */ + buf->st_blksize = 4096; + buf->st_blocks = ((buf->st_size+4095)>>12)<<3; return 0; } errno = err; @@ -265,7 +272,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) * complete. Note that Git stat()s are redirected to mingw_lstat() * too, since Windows doesn't really handle symlinks that well. */ -static int do_stat_internal(int follow, const char *file_name, struct stat *buf) +static int do_stat_internal(int follow, const char *file_name, struct mingw_stat *buf) { int namelen; static char alt_name[PATH_MAX]; @@ -292,17 +299,16 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf) return do_lstat(follow, alt_name, buf); } -int mingw_lstat(const char *file_name, struct stat *buf) +int mingw_lstat(const char *file_name, struct mingw_stat *buf) { return do_stat_internal(0, file_name, buf); } -int mingw_stat(const char *file_name, struct stat *buf) +int mingw_stat(const char *file_name, struct mingw_stat *buf) { return do_stat_internal(1, file_name, buf); } -#undef fstat -int mingw_fstat(int fd, struct stat *buf) +int mingw_fstat(int fd, struct mingw_stat *buf) { HANDLE fh = (HANDLE)_get_osfhandle(fd); BY_HANDLE_FILE_INFORMATION fdata; @@ -312,8 +318,26 @@ int mingw_fstat(int fd, struct stat *buf) return -1; } /* direct non-file handles to MS's fstat() */ - if (GetFileType(fh) != FILE_TYPE_DISK) - return _fstati64(fd, buf); + if (GetFileType(fh) != FILE_TYPE_DISK) { + struct _stati64 buf64; + + if ( _fstati64(fd, &buf64) != 0 ) { + return -1; + } + buf->st_dev = 0; + buf->st_ino = 0; + buf->st_mode = S_IREAD|S_IWRITE; + buf->st_nlink = 1; + buf->st_uid = 0; + buf->st_gid = 0; + buf->st_rdev = 0; + buf->st_size = buf64.st_size; + buf->st_atime = buf64.st_atime; + buf->st_mtime = buf64.st_mtime; + buf->st_ctime = buf64.st_ctime; + buf->st_blksize = 4096; + buf->st_blocks = ((buf64.st_size+4095)>>12)<<3; + } if (GetFileInformationByHandle(fh, &fdata)) { buf->st_ino = 0; @@ -327,6 +351,8 @@ int mingw_fstat(int fd, struct stat *buf) buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); + buf->st_blksize = 4096; + buf->st_blocks = ((buf->st_size+4095)>>12)<<3; return 0; } errno = EBADF; -- cgit v1.2.3-55-g6feb