From dbd187697d1265d345e3e3e8b302a88b04592cc2 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 17 Jan 2023 14:42:42 +0000 Subject: win32: minor improvements to stat(2) Commit b11352dcb (win32: prevent stat(2) from updating access times) requested GENERIC_ALL access when opening files. It appears that GENERIC_READ is sufficient and also faster. The code to find the actual size of compressed or sparse files only needs to be invoked for regular files. Avoiding unnecessary calls to GetCompressedFileSize() makes stat(2) slightly faster and gives a more accurate number of blocks for symbolic links. --- win32/mingw.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/win32/mingw.c b/win32/mingw.c index bfc1531fb..847e7f6a5 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -444,7 +444,7 @@ static int has_exec_format(const char *name) return 0; /* Open file and try to avoid updating access time */ - fh = CreateFileA(name, GENERIC_ALL, 0, NULL, OPEN_EXISTING, 0, NULL); + fh = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (fh != INVALID_HANDLE_VALUE) { FILETIME last_access = { 0xffffffff, 0xffffffff }; @@ -725,14 +725,14 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) } #endif - /* Get actual size of compressed/sparse files */ - low = GetCompressedFileSize(file_name, &high); - if ((low == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) || - (buf->st_attr & FILE_ATTRIBUTE_DIRECTORY)) { - size = buf->st_size; - } - else { - size = low | (((off64_t)high)<<32); + /* Get actual size of compressed/sparse files. Only regular + * files need to be considered. */ + size = buf->st_size; + if (S_ISREG(buf->st_mode)) { + low = GetCompressedFileSize(file_name, &high); + if (low != INVALID_FILE_SIZE || GetLastError() == NO_ERROR) { + size = low | (((off64_t)high)<<32); + } } /* -- cgit v1.2.3-55-g6feb