opendir() is reimplemented using GetFistFile/GetNextFile those are faster then _findfirst/_findnext

This commit is contained in:
Dmitry Stogov 2006-11-10 11:20:48 +00:00
parent a5f951f27c
commit 41c81f20e1
5 changed files with 39 additions and 32 deletions

2
NEWS
View file

@ -3,6 +3,8 @@ PHP NEWS
?? ??? 2007, PHP 5.2.1 ?? ??? 2007, PHP 5.2.1
- Windows related optimizations (Dmitry, Stas) - Windows related optimizations (Dmitry, Stas)
. removed unnecessary checks for ISREG file and corresponding stat() calls . removed unnecessary checks for ISREG file and corresponding stat() calls
. opendir() is reimplemented using GetFistFile/GetNextFile those are faster
then _findfirst/_findnext
- Zend Memory Manager Improvements (Dmitry) - Zend Memory Manager Improvements (Dmitry)
. use HeapAlloc() instead of VirtualAlloc() . use HeapAlloc() instead of VirtualAlloc()
. use "win32" storage manager (instead of "malloc") on Windows by default . use "win32" storage manager (instead of "malloc") on Windows by default

View file

@ -7,11 +7,15 @@
* on Windows 95/NT. * on Windows 95/NT.
*/ */
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <io.h> #include <io.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <direct.h>
/* struct dirent - same as Unix */ /* struct dirent - same as Unix */
@ -25,10 +29,10 @@ struct dirent {
/* typedef DIR - not the same as Unix */ /* typedef DIR - not the same as Unix */
typedef struct { typedef struct {
long handle; /* _findfirst/_findnext handle */ HANDLE handle; /* _findfirst/_findnext handle */
short offset; /* offset into directory */ short offset; /* offset into directory */
short finished; /* 1 if there are not more files */ short finished; /* 1 if there are not more files */
struct _finddata_t fileinfo; /* from _findfirst/_findnext */ WIN32_FIND_DATA fileinfo; /* from _findfirst/_findnext */
char *dir; /* the dir we are reading */ char *dir; /* the dir we are reading */
struct dirent dent; /* the dirent to return */ struct dirent dent; /* the dirent to return */
} DIR; } DIR;
@ -38,7 +42,6 @@ DIR *opendir(const char *);
struct dirent *readdir(DIR *); struct dirent *readdir(DIR *);
int readdir_r(DIR *, struct dirent *, struct dirent **); int readdir_r(DIR *, struct dirent *, struct dirent **);
int closedir(DIR *); int closedir(DIR *);
void rewinddir(DIR *); int rewinddir(DIR *);
#endif /* READDIR_H */ #endif /* READDIR_H */

View file

@ -25,10 +25,6 @@
#include <dirent.h> #include <dirent.h>
#endif #endif
#ifdef PHP_WIN32
#include "win32/readdir.h"
#endif
#include "php_reentrancy.h" #include "php_reentrancy.h"
#include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */ #include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */

View file

@ -2,8 +2,8 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "readdir.h"
#include "php.h" #include "php.h"
#include "readdir.h"
/********************************************************************** /**********************************************************************
* Implement dirent-style opendir/readdir/rewinddir/closedir on Win32 * Implement dirent-style opendir/readdir/rewinddir/closedir on Win32
@ -23,14 +23,14 @@ DIR *opendir(const char *dir)
{ {
DIR *dp; DIR *dp;
char *filespec; char *filespec;
long handle; HANDLE handle;
int index; int index;
filespec = malloc(strlen(dir) + 2 + 1); filespec = (char *)malloc(strlen(dir) + 2 + 1);
strcpy(filespec, dir); strcpy(filespec, dir);
index = strlen(filespec) - 1; index = strlen(filespec) - 1;
if (index >= 0 && (filespec[index] == '/' || if (index >= 0 && (filespec[index] == '/' ||
(filespec[index] == '\\' && !IsDBCSLeadByte(filespec[index-1])))) (filespec[index] == '\\' && (index == 0 || !IsDBCSLeadByte(filespec[index-1])))))
filespec[index] = '\0'; filespec[index] = '\0';
strcat(filespec, "/*"); strcat(filespec, "/*");
@ -38,8 +38,9 @@ DIR *opendir(const char *dir)
dp->offset = 0; dp->offset = 0;
dp->finished = 0; dp->finished = 0;
if ((handle = _findfirst(filespec, &(dp->fileinfo))) < 0) { if ((handle = FindFirstFile(filespec, &(dp->fileinfo))) == INVALID_HANDLE_VALUE) {
if (errno == ENOENT) { DWORD err = GetLastError();
if (err == ERROR_NO_MORE_FILES) {
dp->finished = 1; dp->finished = 1;
} else { } else {
free(dp); free(dp);
@ -60,14 +61,14 @@ struct dirent *readdir(DIR *dp)
return NULL; return NULL;
if (dp->offset != 0) { if (dp->offset != 0) {
if (_findnext(dp->handle, &(dp->fileinfo)) < 0) { if (FindNextFile(dp->handle, &(dp->fileinfo)) == 0) {
dp->finished = 1; dp->finished = 1;
return NULL; return NULL;
} }
} }
dp->offset++; dp->offset++;
strlcpy(dp->dent.d_name, dp->fileinfo.name, _MAX_FNAME+1); strlcpy(dp->dent.d_name, dp->fileinfo.cFileName, _MAX_FNAME+1);
dp->dent.d_ino = 1; dp->dent.d_ino = 1;
dp->dent.d_reclen = strlen(dp->dent.d_name); dp->dent.d_reclen = strlen(dp->dent.d_name);
dp->dent.d_off = dp->offset; dp->dent.d_off = dp->offset;
@ -83,7 +84,7 @@ int readdir_r(DIR *dp, struct dirent *entry, struct dirent **result)
} }
if (dp->offset != 0) { if (dp->offset != 0) {
if (_findnext(dp->handle, &(dp->fileinfo)) < 0) { if (FindNextFile(dp->handle, &(dp->fileinfo)) == 0) {
dp->finished = 1; dp->finished = 1;
*result = NULL; *result = NULL;
return 0; return 0;
@ -91,7 +92,7 @@ int readdir_r(DIR *dp, struct dirent *entry, struct dirent **result)
} }
dp->offset++; dp->offset++;
strlcpy(dp->dent.d_name, dp->fileinfo.name, _MAX_FNAME+1); strlcpy(dp->dent.d_name, dp->fileinfo.cFileName, _MAX_FNAME+1);
dp->dent.d_ino = 1; dp->dent.d_ino = 1;
dp->dent.d_reclen = strlen(dp->dent.d_name); dp->dent.d_reclen = strlen(dp->dent.d_name);
dp->dent.d_off = dp->offset; dp->dent.d_off = dp->offset;
@ -107,7 +108,7 @@ int closedir(DIR *dp)
{ {
if (!dp) if (!dp)
return 0; return 0;
_findclose(dp->handle); FindClose(dp->handle);
if (dp->dir) if (dp->dir)
free(dp->dir); free(dp->dir);
if (dp) if (dp)
@ -120,27 +121,28 @@ int rewinddir(DIR *dp)
{ {
/* Re-set to the beginning */ /* Re-set to the beginning */
char *filespec; char *filespec;
long handle; HANDLE handle;
int index; int index;
_findclose(dp->handle); FindClose(dp->handle);
dp->offset = 0; dp->offset = 0;
dp->finished = 0; dp->finished = 0;
filespec = malloc(strlen(dp->dir) + 2 + 1); filespec = (char *)malloc(strlen(dp->dir) + 2 + 1);
strcpy(filespec, dp->dir); strcpy(filespec, dp->dir);
index = strlen(filespec) - 1; index = strlen(filespec) - 1;
if (index >= 0 && (filespec[index] == '/' || filespec[index] == '\\')) if (index >= 0 && (filespec[index] == '/' ||
(filespec[index] == '\\' && (index == 0 || !IsDBCSLeadByte(filespec[index-1])))))
filespec[index] = '\0'; filespec[index] = '\0';
strcat(filespec, "/*"); strcat(filespec, "/*");
if ((handle = _findfirst(filespec, &(dp->fileinfo))) < 0) { if ((handle = FindFirstFile(filespec, &(dp->fileinfo))) == INVALID_HANDLE_VALUE) {
if (errno == ENOENT) dp->finished = 1;
dp->finished = 1; }
}
dp->handle = handle; dp->handle = handle;
free(filespec); free(filespec);
return 0; return 0;
} }

View file

@ -7,11 +7,15 @@
* on Windows 95/NT. * on Windows 95/NT.
*/ */
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <io.h> #include <io.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <direct.h>
/* struct dirent - same as Unix */ /* struct dirent - same as Unix */
@ -25,10 +29,10 @@ struct dirent {
/* typedef DIR - not the same as Unix */ /* typedef DIR - not the same as Unix */
typedef struct { typedef struct {
long handle; /* _findfirst/_findnext handle */ HANDLE handle; /* _findfirst/_findnext handle */
short offset; /* offset into directory */ short offset; /* offset into directory */
short finished; /* 1 if there are not more files */ short finished; /* 1 if there are not more files */
struct _finddata_t fileinfo; /* from _findfirst/_findnext */ WIN32_FIND_DATA fileinfo; /* from _findfirst/_findnext */
char *dir; /* the dir we are reading */ char *dir; /* the dir we are reading */
struct dirent dent; /* the dirent to return */ struct dirent dent; /* the dirent to return */
} DIR; } DIR;