Fix readdir usage across platforms

This fixes the missing readdir_r from win32 and fixes other
platforms to always use the reentrant readdir_r form for reading
directory contents.
This commit is contained in:
Russell Belfer 2012-02-23 11:16:47 -08:00
parent 1ec1de6d43
commit 290f240ee0
4 changed files with 31 additions and 15 deletions

View File

@ -491,7 +491,7 @@ int git_path_direach(
{ {
ssize_t wd_len; ssize_t wd_len;
DIR *dir; DIR *dir;
struct dirent *de; struct dirent de_buf, *de;
if (git_path_to_dir(path) < GIT_SUCCESS) if (git_path_to_dir(path) < GIT_SUCCESS)
return git_buf_lasterror(path); return git_buf_lasterror(path);
@ -501,7 +501,7 @@ int git_path_direach(
if (!dir) if (!dir)
return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr); return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr);
while ((de = readdir(dir)) != NULL) { while (p_readdir_r(dir, &de_buf, &de) == 0 && de != NULL) {
int result; int result;
if (is_dot_or_dotdot(de->d_name)) if (is_dot_or_dotdot(de->d_name))
@ -547,7 +547,7 @@ int git_path_dirload(
path_len -= prefix_len; path_len -= prefix_len;
need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0; need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
while ((error = readdir_r(dir, &de_buf, &de)) == 0 && de != NULL) { while ((error = p_readdir_r(dir, &de_buf, &de)) == 0 && de != NULL) {
char *entry_path; char *entry_path;
size_t entry_len; size_t entry_len;

View File

@ -21,5 +21,6 @@
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__) #define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
#define p_mkstemp(p) mkstemp(p) #define p_mkstemp(p) mkstemp(p)
#define p_setenv(n,v,o) setenv(n,v,o) #define p_setenv(n,v,o) setenv(n,v,o)
#define p_readdir_r(d,e,r) readdir_r(d,e,r)
#endif #endif

View File

@ -58,25 +58,37 @@ git__DIR *git__opendir(const char *dir)
return new; return new;
} }
struct git__dirent *git__readdir(git__DIR *d) int git__readdir_r(
git__DIR *d, struct git__dirent *entry, struct git__dirent **result)
{ {
if (!d || d->h == INVALID_HANDLE_VALUE) if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
return NULL; return -1;
if (d->first) if (d->first)
d->first = 0; d->first = 0;
else { else if (!FindNextFileW(d->h, &d->f)) {
if (!FindNextFileW(d->h, &d->f)) *result = NULL;
return NULL; return 0;
} }
if (wcslen(d->f.cFileName) >= sizeof(d->entry.d_name)) if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
return -1;
entry->d_ino = 0;
WideCharToMultiByte(
gitwin_get_codepage(), 0, d->f.cFileName, -1,
entry->d_name, GIT_PATH_MAX, NULL, NULL);
*result = entry;
return 0;
}
struct git__dirent *git__readdir(git__DIR *d)
{
struct git__dirent *result;
if (git__readdir_r(d, &d->entry, &result) < 0)
return NULL; return NULL;
return result;
d->entry.d_ino = 0;
WideCharToMultiByte(gitwin_get_codepage(), 0, d->f.cFileName, -1, d->entry.d_name, GIT_PATH_MAX, NULL, NULL);
return &d->entry;
} }
void git__rewinddir(git__DIR *d) void git__rewinddir(git__DIR *d)

View File

@ -24,6 +24,7 @@ typedef struct {
extern git__DIR *git__opendir(const char *); extern git__DIR *git__opendir(const char *);
extern struct git__dirent *git__readdir(git__DIR *); extern struct git__dirent *git__readdir(git__DIR *);
extern int git__readdir_r(git__DIR*, struct git__dirent*, struct git__dirent**);
extern void git__rewinddir(git__DIR *); extern void git__rewinddir(git__DIR *);
extern int git__closedir(git__DIR *); extern int git__closedir(git__DIR *);
@ -36,4 +37,6 @@ extern int git__closedir(git__DIR *);
# define closedir git__closedir # define closedir git__closedir
# endif # endif
#define p_readdir_r(d,e,r) git__readdir_r(d,e,r)
#endif /* INCLUDE_dir_h__ */ #endif /* INCLUDE_dir_h__ */