aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-01-07 14:43:08 +0000
committerRon Yorston <rmy@pobox.com>2014-01-07 14:43:08 +0000
commit94150aba92d308e417aa552a1b3a957be264b423 (patch)
treed9b570e2641188c68917e85d7cb387fe0bbfbecc
parent63b71e4a6f8c65a4bef7617efb50a2b473e388b3 (diff)
downloadbusybox-w32-94150aba92d308e417aa552a1b3a957be264b423.tar.gz
busybox-w32-94150aba92d308e417aa552a1b3a957be264b423.tar.bz2
busybox-w32-94150aba92d308e417aa552a1b3a957be264b423.zip
Revise mingw_stat to minimise changes from upstream BusyBox
-rw-r--r--coreutils/cp.c4
-rw-r--r--coreutils/libcoreutils/cp_mv_stat.c4
-rw-r--r--coreutils/stat.c8
-rw-r--r--include/mingw.h45
-rw-r--r--libbb/copy_file.c4
-rw-r--r--libbb/recursive_action.c2
-rw-r--r--util-linux/swaponoff.c2
-rw-r--r--win32/mingw.c42
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)
149 /* If there are only two arguments and... */ 149 /* If there are only two arguments and... */
150 if (argc == 2) { 150 if (argc == 2) {
151 s_flags = cp_mv_stat2(*argv, &source_stat, 151 s_flags = cp_mv_stat2(*argv, &source_stat,
152#if !ENABLE_PLATFORM_MINGW32
153 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); 152 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
154#else
155 (flags & FILEUTILS_DEREFERENCE) ? mingw_stat : lstat);
156#endif
157 if (s_flags < 0) 153 if (s_flags < 0)
158 return EXIT_FAILURE; 154 return EXIT_FAILURE;
159 d_flags = cp_mv_stat(last, &dest_stat); 155 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)
46 46
47int FAST_FUNC cp_mv_stat(const char *fn, struct stat *fn_stat) 47int FAST_FUNC cp_mv_stat(const char *fn, struct stat *fn_stat)
48{ 48{
49#if !ENABLE_PLATFORM_MINGW32
50 return cp_mv_stat2(fn, fn_stat, stat); 49 return cp_mv_stat2(fn, fn_stat, stat);
51#else
52 return cp_mv_stat2(fn, fn_stat, mingw_stat);
53#endif
54} 50}
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,
338 printf(pformat, (unsigned long long) statbuf->st_blocks); 338 printf(pformat, (unsigned long long) statbuf->st_blocks);
339 } else if (m == 'o') { 339 } else if (m == 'o') {
340 strcat(pformat, "lu"); 340 strcat(pformat, "lu");
341#if !ENABLE_PLATFORM_MINGW32
342 printf(pformat, (unsigned long) statbuf->st_blksize); 341 printf(pformat, (unsigned long) statbuf->st_blksize);
343#else
344 printf(pformat, (unsigned long) 4096);
345#endif
346 } else if (m == 'x') { 342 } else if (m == 'x') {
347 printfs(pformat, human_time(statbuf->st_atime)); 343 printfs(pformat, human_time(statbuf->st_atime));
348 } else if (m == 'X') { 344 } else if (m == 'X') {
@@ -550,7 +546,6 @@ static bool do_statfs(const char *filename, const char *format)
550static bool do_stat(const char *filename, const char *format) 546static bool do_stat(const char *filename, const char *format)
551{ 547{
552 struct stat statbuf; 548 struct stat statbuf;
553 int status;
554#if ENABLE_SELINUX 549#if ENABLE_SELINUX
555 security_context_t scontext = NULL; 550 security_context_t scontext = NULL;
556 551
@@ -565,8 +560,7 @@ static bool do_stat(const char *filename, const char *format)
565 } 560 }
566 } 561 }
567#endif 562#endif
568 status = option_mask32 & OPT_DEREFERENCE ? stat(filename, &statbuf) : lstat(filename, &statbuf); 563 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
569 if (status != 0) {
570 bb_perror_msg("can't stat '%s'", filename); 564 bb_perror_msg("can't stat '%s'", filename);
571 return 0; 565 return 0;
572 } 566 }
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
204/* 204/*
205 * sys/stat.h 205 * sys/stat.h
206 */ 206 */
207typedef int blkcnt_t;
208typedef int nlink_t;
209
210#define S_ISUID 04000 207#define S_ISUID 04000
211#define S_ISGID 02000 208#define S_ISGID 02000
212#define S_ISVTX 01000 209#define S_ISVTX 01000
@@ -233,27 +230,37 @@ NOIMPL(fchown,int fd UNUSED_PARAM, uid_t uid UNUSED_PARAM, gid_t gid UNUSED_PARA
233int mingw_mkdir(const char *path, int mode); 230int mingw_mkdir(const char *path, int mode);
234#define mkdir mingw_mkdir 231#define mkdir mingw_mkdir
235 232
236/* Use mingw_lstat()/mingw_stat() instead of lstat()/stat() and
237 * mingw_fstat() instead of fstat() on Windows.
238 */
239#if ENABLE_LFS 233#if ENABLE_LFS
240# define off_t off64_t 234# define off_t off64_t
241#endif 235#endif
242#define lseek _lseeki64 236#define lseek _lseeki64
243#define stat _stati64
244int mingw_lstat(const char *file_name, struct stat *buf);
245int mingw_stat(const char *file_name, struct stat *buf);
246int mingw_fstat(int fd, struct stat *buf);
247#define fstat mingw_fstat
248#define lstat mingw_lstat
249#define _stati64(x,y) mingw_stat(x,y)
250 237
251/* The Windows stat structure doesn't have the st_blocks member. This 238typedef int nlink_t;
252 * macro calculates st_blocks from st_size. It would be better if we 239typedef int blksize_t;
253 * could put brackets around it but most references to st_blocks in BusyBox 240typedef off_t blkcnt_t;
254 * are pretty simple and work without brackets. 241
255 */ 242struct mingw_stat {
256#define st_blocks st_size+511>>9 243 dev_t st_dev;
244 ino_t st_ino;
245 mode_t st_mode;
246 nlink_t st_nlink;
247 uid_t st_uid;
248 gid_t st_gid;
249 dev_t st_rdev;
250 off_t st_size;
251 time_t st_atime;
252 time_t st_mtime;
253 time_t st_ctime;
254 blksize_t st_blksize;
255 blkcnt_t st_blocks;
256};
257
258int mingw_lstat(const char *file_name, struct mingw_stat *buf);
259int mingw_stat(const char *file_name, struct mingw_stat *buf);
260int mingw_fstat(int fd, struct mingw_stat *buf);
261#define lstat mingw_lstat
262#define stat mingw_stat
263#define fstat mingw_fstat
257 264
258/* 265/*
259 * sys/sysmacros.h 266 * 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)
81 smallint retval = 0; 81 smallint retval = 0;
82 smallint dest_exists = 0; 82 smallint dest_exists = 0;
83 smallint ovr; 83 smallint ovr;
84 int status;
85 84
86/* Inverse of cp -d ("cp without -d") */ 85/* Inverse of cp -d ("cp without -d") */
87#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0)) 86#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0))
88 87
89 status = FLAGS_DEREF ? stat(source, &source_stat) : lstat(source, &source_stat); 88 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
90 if (status < 0) {
91 /* This may be a dangling symlink. 89 /* This may be a dangling symlink.
92 * Making [sym]links to dangling symlinks works, so... */ 90 * Making [sym]links to dangling symlinks works, so... */
93 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) 91 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,
73 if (depth == 0) 73 if (depth == 0)
74 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0; 74 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
75 follow &= flags; 75 follow &= flags;
76 status = follow ? stat(fileName, &statbuf) : lstat(fileName, &statbuf); 76 status = (follow ? stat : lstat)(fileName, &statbuf);
77 if (status < 0) { 77 if (status < 0) {
78#ifdef DEBUG_RECURS_ACTION 78#ifdef DEBUG_RECURS_ACTION
79 bb_error_msg("status=%d flags=%x", status, flags); 79 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)
60#if ENABLE_DESKTOP 60#if ENABLE_DESKTOP
61 /* test for holes */ 61 /* test for holes */
62 if (S_ISREG(st.st_mode)) 62 if (S_ISREG(st.st_mode))
63 if ((st.st_blocks) * (off_t)512 < st.st_size) 63 if (st.st_blocks * (off_t)512 < st.st_size)
64 bb_error_msg("warning: swap file has holes"); 64 bb_error_msg("warning: swap file has holes");
65#endif 65#endif
66 66
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
212 * If follow is true then act like stat() and report on the link 212 * If follow is true then act like stat() and report on the link
213 * target. Otherwise report on the link itself. 213 * target. Otherwise report on the link itself.
214 */ 214 */
215static int do_lstat(int follow, const char *file_name, struct stat *buf) 215static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf)
216{ 216{
217 int err; 217 int err;
218 WIN32_FILE_ATTRIBUTE_DATA fdata; 218 WIN32_FILE_ATTRIBUTE_DATA fdata;
@@ -253,6 +253,13 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
253 FindClose(handle); 253 FindClose(handle);
254 } 254 }
255 } 255 }
256
257 /*
258 * Assume a block is 4096 bytes and calculate number of 512 byte
259 * sectors.
260 */
261 buf->st_blksize = 4096;
262 buf->st_blocks = ((buf->st_size+4095)>>12)<<3;
256 return 0; 263 return 0;
257 } 264 }
258 errno = err; 265 errno = err;
@@ -265,7 +272,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
265 * complete. Note that Git stat()s are redirected to mingw_lstat() 272 * complete. Note that Git stat()s are redirected to mingw_lstat()
266 * too, since Windows doesn't really handle symlinks that well. 273 * too, since Windows doesn't really handle symlinks that well.
267 */ 274 */
268static int do_stat_internal(int follow, const char *file_name, struct stat *buf) 275static int do_stat_internal(int follow, const char *file_name, struct mingw_stat *buf)
269{ 276{
270 int namelen; 277 int namelen;
271 static char alt_name[PATH_MAX]; 278 static char alt_name[PATH_MAX];
@@ -292,17 +299,16 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
292 return do_lstat(follow, alt_name, buf); 299 return do_lstat(follow, alt_name, buf);
293} 300}
294 301
295int mingw_lstat(const char *file_name, struct stat *buf) 302int mingw_lstat(const char *file_name, struct mingw_stat *buf)
296{ 303{
297 return do_stat_internal(0, file_name, buf); 304 return do_stat_internal(0, file_name, buf);
298} 305}
299int mingw_stat(const char *file_name, struct stat *buf) 306int mingw_stat(const char *file_name, struct mingw_stat *buf)
300{ 307{
301 return do_stat_internal(1, file_name, buf); 308 return do_stat_internal(1, file_name, buf);
302} 309}
303 310
304#undef fstat 311int mingw_fstat(int fd, struct mingw_stat *buf)
305int mingw_fstat(int fd, struct stat *buf)
306{ 312{
307 HANDLE fh = (HANDLE)_get_osfhandle(fd); 313 HANDLE fh = (HANDLE)_get_osfhandle(fd);
308 BY_HANDLE_FILE_INFORMATION fdata; 314 BY_HANDLE_FILE_INFORMATION fdata;
@@ -312,8 +318,26 @@ int mingw_fstat(int fd, struct stat *buf)
312 return -1; 318 return -1;
313 } 319 }
314 /* direct non-file handles to MS's fstat() */ 320 /* direct non-file handles to MS's fstat() */
315 if (GetFileType(fh) != FILE_TYPE_DISK) 321 if (GetFileType(fh) != FILE_TYPE_DISK) {
316 return _fstati64(fd, buf); 322 struct _stati64 buf64;
323
324 if ( _fstati64(fd, &buf64) != 0 ) {
325 return -1;
326 }
327 buf->st_dev = 0;
328 buf->st_ino = 0;
329 buf->st_mode = S_IREAD|S_IWRITE;
330 buf->st_nlink = 1;
331 buf->st_uid = 0;
332 buf->st_gid = 0;
333 buf->st_rdev = 0;
334 buf->st_size = buf64.st_size;
335 buf->st_atime = buf64.st_atime;
336 buf->st_mtime = buf64.st_mtime;
337 buf->st_ctime = buf64.st_ctime;
338 buf->st_blksize = 4096;
339 buf->st_blocks = ((buf64.st_size+4095)>>12)<<3;
340 }
317 341
318 if (GetFileInformationByHandle(fh, &fdata)) { 342 if (GetFileInformationByHandle(fh, &fdata)) {
319 buf->st_ino = 0; 343 buf->st_ino = 0;
@@ -327,6 +351,8 @@ int mingw_fstat(int fd, struct stat *buf)
327 buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime)); 351 buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
328 buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime)); 352 buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
329 buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime)); 353 buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
354 buf->st_blksize = 4096;
355 buf->st_blocks = ((buf->st_size+4095)>>12)<<3;
330 return 0; 356 return 0;
331 } 357 }
332 errno = EBADF; 358 errno = EBADF;