diff options
author | Ron Yorston <rmy@pobox.com> | 2022-05-22 10:06:30 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2022-05-22 10:06:30 +0100 |
commit | b68bda6482aed55d13b5bb622f60bc63ea930472 (patch) | |
tree | 08f53fae2891215705ef47974be7b06bc305f9b9 | |
parent | bd584ee9d76ca4353fcd5af87a4f413316d3a552 (diff) | |
download | busybox-w32-b68bda6482aed55d13b5bb622f60bc63ea930472.tar.gz busybox-w32-b68bda6482aed55d13b5bb622f60bc63ea930472.tar.bz2 busybox-w32-b68bda6482aed55d13b5bb622f60bc63ea930472.zip |
win32: code shrink directory tests
Add a function to check if a file is a directory and use it in
various places.
Replace some uses of S_ISDIR() with a test of the Windows file
attributes.
Saves 32-48 bytes.
-rw-r--r-- | win32/mingw.c | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index bea800435..bf9b75a7f 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -220,6 +220,7 @@ static int get_dev_fd(const char *filename) | |||
220 | return -1; | 220 | return -1; |
221 | } | 221 | } |
222 | 222 | ||
223 | static int mingw_is_directory(const char *path); | ||
223 | #undef open | 224 | #undef open |
224 | int mingw_open (const char *filename, int oflags, ...) | 225 | int mingw_open (const char *filename, int oflags, ...) |
225 | { | 226 | { |
@@ -247,8 +248,7 @@ int mingw_open (const char *filename, int oflags, ...) | |||
247 | update_special_fd(dev, fd); | 248 | update_special_fd(dev, fd); |
248 | } | 249 | } |
249 | else if ((oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) { | 250 | else if ((oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) { |
250 | DWORD attrs = GetFileAttributes(filename); | 251 | if (mingw_is_directory(filename)) |
251 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) | ||
252 | errno = EISDIR; | 252 | errno = EISDIR; |
253 | } | 253 | } |
254 | return fd; | 254 | return fd; |
@@ -355,7 +355,7 @@ static inline mode_t file_attr_to_st_mode(DWORD attr) | |||
355 | return fMode; | 355 | return fMode; |
356 | } | 356 | } |
357 | 357 | ||
358 | static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) | 358 | static int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fdata) |
359 | { | 359 | { |
360 | size_t len; | 360 | size_t len; |
361 | 361 | ||
@@ -592,6 +592,14 @@ static DWORD is_symlink(const char *pathname) | |||
592 | return 0; | 592 | return 0; |
593 | } | 593 | } |
594 | 594 | ||
595 | static int mingw_is_directory(const char *path) | ||
596 | { | ||
597 | WIN32_FILE_ATTRIBUTE_DATA fdata; | ||
598 | |||
599 | return get_file_attr(path, &fdata) == 0 && | ||
600 | (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); | ||
601 | } | ||
602 | |||
595 | #if ENABLE_FEATURE_EXTRA_FILE_DATA | 603 | #if ENABLE_FEATURE_EXTRA_FILE_DATA |
596 | static int count_subdirs(const char *pathname) | 604 | static int count_subdirs(const char *pathname) |
597 | { | 605 | { |
@@ -669,7 +677,7 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) | |||
669 | buf->st_mtim = filetime_to_timespec(&(fdata.ftLastWriteTime)); | 677 | buf->st_mtim = filetime_to_timespec(&(fdata.ftLastWriteTime)); |
670 | buf->st_ctim = filetime_to_timespec(&(fdata.ftCreationTime)); | 678 | buf->st_ctim = filetime_to_timespec(&(fdata.ftCreationTime)); |
671 | } | 679 | } |
672 | buf->st_nlink = S_ISDIR(buf->st_mode) ? 2 : 1; | 680 | buf->st_nlink = (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY) ? 2 : 1; |
673 | 681 | ||
674 | #if ENABLE_FEATURE_EXTRA_FILE_DATA | 682 | #if ENABLE_FEATURE_EXTRA_FILE_DATA |
675 | flags = FILE_FLAG_BACKUP_SEMANTICS; | 683 | flags = FILE_FLAG_BACKUP_SEMANTICS; |
@@ -682,7 +690,7 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) | |||
682 | buf->st_dev = hdata.dwVolumeSerialNumber; | 690 | buf->st_dev = hdata.dwVolumeSerialNumber; |
683 | buf->st_ino = hdata.nFileIndexLow | | 691 | buf->st_ino = hdata.nFileIndexLow | |
684 | (((ino_t)hdata.nFileIndexHigh)<<32); | 692 | (((ino_t)hdata.nFileIndexHigh)<<32); |
685 | buf->st_nlink = S_ISDIR(buf->st_mode) ? | 693 | buf->st_nlink = (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY) ? |
686 | count_subdirs(file_name) : | 694 | count_subdirs(file_name) : |
687 | hdata.nNumberOfLinks; | 695 | hdata.nNumberOfLinks; |
688 | } | 696 | } |
@@ -700,7 +708,7 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) | |||
700 | /* Get actual size of compressed/sparse files */ | 708 | /* Get actual size of compressed/sparse files */ |
701 | low = GetCompressedFileSize(file_name, &high); | 709 | low = GetCompressedFileSize(file_name, &high); |
702 | if ((low == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) || | 710 | if ((low == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) || |
703 | S_ISDIR(buf->st_mode)) { | 711 | (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY)) { |
704 | size = buf->st_size; | 712 | size = buf->st_size; |
705 | } | 713 | } |
706 | else { | 714 | else { |
@@ -782,13 +790,14 @@ int mingw_fstat(int fd, struct mingw_stat *buf) | |||
782 | buf->st_dev = fdata.dwVolumeSerialNumber; | 790 | buf->st_dev = fdata.dwVolumeSerialNumber; |
783 | buf->st_ino = fdata.nFileIndexLow | | 791 | buf->st_ino = fdata.nFileIndexLow | |
784 | (((uint64_t)fdata.nFileIndexHigh)<<32); | 792 | (((uint64_t)fdata.nFileIndexHigh)<<32); |
785 | buf->st_nlink = S_ISDIR(buf->st_mode) ? 2 : fdata.nNumberOfLinks; | 793 | buf->st_nlink = (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY) ? |
794 | 2 : fdata.nNumberOfLinks; | ||
786 | #endif | 795 | #endif |
787 | success: | 796 | success: |
788 | #if !ENABLE_FEATURE_EXTRA_FILE_DATA | 797 | #if !ENABLE_FEATURE_EXTRA_FILE_DATA |
789 | buf->st_dev = 0; | 798 | buf->st_dev = 0; |
790 | buf->st_ino = 0; | 799 | buf->st_ino = 0; |
791 | buf->st_nlink = S_ISDIR(buf->st_mode) ? 2 : 1; | 800 | buf->st_nlink = (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY) ? 2 : 1; |
792 | #endif | 801 | #endif |
793 | buf->st_rdev = 0; | 802 | buf->st_rdev = 0; |
794 | buf->st_uid = DEFAULT_UID; | 803 | buf->st_uid = DEFAULT_UID; |
@@ -1256,7 +1265,6 @@ int link(const char *oldpath, const char *newpath) | |||
1256 | int symlink(const char *target, const char *linkpath) | 1265 | int symlink(const char *target, const char *linkpath) |
1257 | { | 1266 | { |
1258 | DWORD flag = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; | 1267 | DWORD flag = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
1259 | struct stat st; | ||
1260 | DECLARE_PROC_ADDR(BOOLEAN, CreateSymbolicLinkA, LPCSTR, LPCSTR, DWORD); | 1268 | DECLARE_PROC_ADDR(BOOLEAN, CreateSymbolicLinkA, LPCSTR, LPCSTR, DWORD); |
1261 | char *targ, *relative = NULL; | 1269 | char *targ, *relative = NULL; |
1262 | 1270 | ||
@@ -1271,7 +1279,7 @@ int symlink(const char *target, const char *linkpath) | |||
1271 | (int)(name - linkpath), linkpath, target); | 1279 | (int)(name - linkpath), linkpath, target); |
1272 | } | 1280 | } |
1273 | 1281 | ||
1274 | if (stat(relative ?: target, &st) != -1 && S_ISDIR(st.st_mode)) | 1282 | if (mingw_is_directory(relative ?: target)) |
1275 | flag |= SYMBOLIC_LINK_FLAG_DIRECTORY; | 1283 | flag |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
1276 | free(relative); | 1284 | free(relative); |
1277 | 1285 | ||
@@ -1638,12 +1646,8 @@ int mingw_chdir(const char *dirname) | |||
1638 | #undef chmod | 1646 | #undef chmod |
1639 | int mingw_chmod(const char *path, int mode) | 1647 | int mingw_chmod(const char *path, int mode) |
1640 | { | 1648 | { |
1641 | WIN32_FILE_ATTRIBUTE_DATA fdata; | 1649 | if (mingw_is_directory(path)) |
1642 | |||
1643 | if ( get_file_attr(path, &fdata) == 0 && | ||
1644 | fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { | ||
1645 | mode |= 0222; | 1650 | mode |= 0222; |
1646 | } | ||
1647 | 1651 | ||
1648 | return chmod(path, mode); | 1652 | return chmod(path, mode); |
1649 | } | 1653 | } |