Commit 290f240e by Russell Belfer

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.
parent 1ec1de6d
......@@ -491,7 +491,7 @@ int git_path_direach(
{
ssize_t wd_len;
DIR *dir;
struct dirent *de;
struct dirent de_buf, *de;
if (git_path_to_dir(path) < GIT_SUCCESS)
return git_buf_lasterror(path);
......@@ -501,7 +501,7 @@ int git_path_direach(
if (!dir)
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;
if (is_dot_or_dotdot(de->d_name))
......@@ -547,7 +547,7 @@ int git_path_dirload(
path_len -= prefix_len;
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;
size_t entry_len;
......
......@@ -21,5 +21,6 @@
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
#define p_mkstemp(p) mkstemp(p)
#define p_setenv(n,v,o) setenv(n,v,o)
#define p_readdir_r(d,e,r) readdir_r(d,e,r)
#endif
......@@ -58,25 +58,37 @@ git__DIR *git__opendir(const char *dir)
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)
return NULL;
if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
return -1;
if (d->first)
d->first = 0;
else {
if (!FindNextFileW(d->h, &d->f))
return NULL;
else if (!FindNextFileW(d->h, &d->f)) {
*result = NULL;
return 0;
}
if (wcslen(d->f.cFileName) >= sizeof(d->entry.d_name))
return NULL;
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);
d->entry.d_ino = 0;
WideCharToMultiByte(gitwin_get_codepage(), 0, d->f.cFileName, -1, d->entry.d_name, GIT_PATH_MAX, NULL, NULL);
*result = entry;
return 0;
}
return &d->entry;
struct git__dirent *git__readdir(git__DIR *d)
{
struct git__dirent *result;
if (git__readdir_r(d, &d->entry, &result) < 0)
return NULL;
return result;
}
void git__rewinddir(git__DIR *d)
......
......@@ -24,6 +24,7 @@ typedef struct {
extern git__DIR *git__opendir(const char *);
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 int git__closedir(git__DIR *);
......@@ -36,4 +37,6 @@ extern int git__closedir(git__DIR *);
# define closedir git__closedir
# endif
#define p_readdir_r(d,e,r) git__readdir_r(d,e,r)
#endif /* INCLUDE_dir_h__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment