* win32/{win32.c,dir.h} (rb_w32_uopendir): new API to pass UTF-8 path.

* win32/win32.c (opendir_internal, rb_w32_opendir): extract and merge
  common part of rb_w32_opendir() and rb_w32_uopendir().

* dir.c (do_opendir, glob_helper): encoding.

* dir.c (dir_initialize, do_opendir): convert path to UTF-8 and call
  rb_w32_uopendir() instead of rb_w32_opendir() on Windows.
  fixes #4491, reported by Joey Zhou.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2011-04-28 07:22:18 +00:00
parent e269a71eeb
commit 22d5cf9226
4 changed files with 70 additions and 32 deletions

View File

@ -1,3 +1,16 @@
Thu Apr 28 16:15:49 2011 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/{win32.c,dir.h} (rb_w32_uopendir): new API to pass UTF-8 path.
* win32/win32.c (opendir_internal, rb_w32_opendir): extract and merge
common part of rb_w32_opendir() and rb_w32_uopendir().
* dir.c (do_opendir, glob_helper): encoding.
* dir.c (dir_initialize, do_opendir): convert path to UTF-8 and call
rb_w32_uopendir() instead of rb_w32_opendir() on Windows.
fixes #4491, reported by Joey Zhou.
Thu Apr 28 15:32:53 2011 NAKAMURA Usaku <usa@ruby-lang.org> Thu Apr 28 15:32:53 2011 NAKAMURA Usaku <usa@ruby-lang.org>
* test/dl/test_base.rb (DL::LIBC_SO): its always msvc*.dll on * test/dl/test_base.rb (DL::LIBC_SO): its always msvc*.dll on

20
dir.c
View File

@ -75,6 +75,8 @@ char *strchr(char*,char);
#define mkdir(p, m) rb_w32_umkdir((p), (m)) #define mkdir(p, m) rb_w32_umkdir((p), (m))
#undef rmdir #undef rmdir
#define rmdir(p) rb_w32_urmdir(p) #define rmdir(p) rb_w32_urmdir(p)
#undef opendir
#define opendir(p) rb_w32_uopendir(p)
#endif #endif
#define FNM_NOESCAPE 0x01 #define FNM_NOESCAPE 0x01
@ -402,6 +404,7 @@ dir_initialize(int argc, VALUE *argv, VALUE dir)
} }
GlobPathValue(dirname, FALSE); GlobPathValue(dirname, FALSE);
dirname = rb_str_encode_ospath(dirname);
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dp); TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dp);
if (dp->dir) closedir(dp->dir); if (dp->dir) closedir(dp->dir);
@ -1033,9 +1036,20 @@ do_lstat(const char *path, struct stat *pst, int flags)
} }
static DIR * static DIR *
do_opendir(const char *path, int flags) do_opendir(const char *path, int flags, rb_encoding *enc)
{ {
DIR *dirp = opendir(path); DIR *dirp;
#ifdef _WIN32
volatile VALUE tmp;
if (enc != rb_usascii_encoding() &&
enc != rb_ascii8bit_encoding() &&
enc != rb_utf8_encoding()) {
tmp = rb_enc_str_new(path, strlen(path), enc);
tmp = rb_str_encode_ospath(tmp);
path = RSTRING_PTR(tmp);
}
#endif
dirp = opendir(path);
if (dirp == NULL && !to_be_ignored(errno)) if (dirp == NULL && !to_be_ignored(errno))
sys_warning(path); sys_warning(path);
@ -1354,7 +1368,7 @@ glob_helper(
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
IF_HAVE_READDIR_R(DEFINE_STRUCT_DIRENT entry); IF_HAVE_READDIR_R(DEFINE_STRUCT_DIRENT entry);
dirp = do_opendir(*path ? path : ".", flags); dirp = do_opendir(*path ? path : ".", flags, enc);
if (dirp == NULL) return 0; if (dirp == NULL) return 0;
while (READDIR(dirp, enc, &STRUCT_DIRENT(entry), dp)) { while (READDIR(dirp, enc, &STRUCT_DIRENT(entry), dp)) {

View File

@ -28,6 +28,7 @@ typedef struct {
DIR* rb_w32_opendir(const char*); DIR* rb_w32_opendir(const char*);
DIR* rb_w32_uopendir(const char*);
struct direct* rb_w32_readdir(DIR *); struct direct* rb_w32_readdir(DIR *);
struct direct* rb_w32_readdir_with_enc(DIR *, rb_encoding *); struct direct* rb_w32_readdir_with_enc(DIR *, rb_encoding *);
long rb_w32_telldir(DIR *); long rb_w32_telldir(DIR *);

View File

@ -1690,14 +1690,30 @@ open_dir_handle(const WCHAR *filename, WIN32_FIND_DATAW *fd)
} }
static DIR * static DIR *
opendir_internal(HANDLE fh, WIN32_FIND_DATAW *fd) opendir_internal(WCHAR *wpath, const char *filename)
{ {
struct stati64 sbuf;
WIN32_FIND_DATAW fd;
HANDLE fh;
DIR *p; DIR *p;
long len; long len;
long idx; long idx;
WCHAR *tmpW; WCHAR *tmpW;
char *tmp; char *tmp;
//
// check to see if we've got a directory
//
if (wstati64(wpath, &sbuf) < 0) {
return NULL;
}
if (!(sbuf.st_mode & S_IFDIR) &&
(!ISALPHA(filename[0]) || filename[1] != ':' || filename[2] != '\0' ||
((1 << ((filename[0] & 0x5f) - 'A')) & GetLogicalDrives()) == 0)) {
errno = ENOTDIR;
return NULL;
}
fh = open_dir_handle(wpath, &fd);
if (fh == INVALID_HANDLE_VALUE) { if (fh == INVALID_HANDLE_VALUE) {
return NULL; return NULL;
} }
@ -1718,7 +1734,7 @@ opendir_internal(HANDLE fh, WIN32_FIND_DATAW *fd)
// of the previous string found. // of the previous string found.
// //
do { do {
len = lstrlenW(fd->cFileName) + 1; len = lstrlenW(fd.cFileName) + 1;
// //
// bump the string table size by enough for the // bump the string table size by enough for the
@ -1734,7 +1750,7 @@ opendir_internal(HANDLE fh, WIN32_FIND_DATAW *fd)
} }
p->start = tmpW; p->start = tmpW;
memcpy(&p->start[idx], fd->cFileName, len * sizeof(WCHAR)); memcpy(&p->start[idx], fd.cFileName, len * sizeof(WCHAR));
if (p->nfiles % DIRENT_PER_CHAR == 0) { if (p->nfiles % DIRENT_PER_CHAR == 0) {
tmp = realloc(p->bits, p->nfiles / DIRENT_PER_CHAR + 1); tmp = realloc(p->bits, p->nfiles / DIRENT_PER_CHAR + 1);
@ -1743,14 +1759,14 @@ opendir_internal(HANDLE fh, WIN32_FIND_DATAW *fd)
p->bits = tmp; p->bits = tmp;
p->bits[p->nfiles / DIRENT_PER_CHAR] = 0; p->bits[p->nfiles / DIRENT_PER_CHAR] = 0;
} }
if (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
SetBit(p->bits, BitOfIsDir(p->nfiles)); SetBit(p->bits, BitOfIsDir(p->nfiles));
if (fd->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
SetBit(p->bits, BitOfIsRep(p->nfiles)); SetBit(p->bits, BitOfIsRep(p->nfiles));
p->nfiles++; p->nfiles++;
idx += len; idx += len;
} while (FindNextFileW(fh, fd)); } while (FindNextFileW(fh, &fd));
FindClose(fh); FindClose(fh);
p->size = idx; p->size = idx;
p->curr = p->start; p->curr = p->start;
@ -1789,31 +1805,25 @@ mbstr_to_wstr(UINT cp, const char *str, int clen, long *plen)
DIR * DIR *
rb_w32_opendir(const char *filename) rb_w32_opendir(const char *filename)
{ {
struct stati64 sbuf; DIR *ret;
WIN32_FIND_DATAW fd; WCHAR *wpath = filecp_to_wstr(filename, NULL);
HANDLE fh; if (!wpath)
WCHAR *wpath;
if (!(wpath = filecp_to_wstr(filename, NULL)))
return NULL; return NULL;
ret = opendir_internal(wpath, filename);
//
// check to see if we've got a directory
//
if (wstati64(wpath, &sbuf) < 0) {
free(wpath); free(wpath);
return NULL; return ret;
} }
if (!(sbuf.st_mode & S_IFDIR) &&
(!ISALPHA(filename[0]) || filename[1] != ':' || filename[2] != '\0' || DIR *
((1 << ((filename[0] & 0x5f) - 'A')) & GetLogicalDrives()) == 0)) { rb_w32_uopendir(const char *filename)
free(wpath); {
errno = ENOTDIR; DIR *ret;
WCHAR *wpath = utf8_to_wstr(filename, NULL);
if (!wpath)
return NULL; return NULL;
} ret = opendir_internal(wpath, filename);
fh = open_dir_handle(wpath, &fd);
free(wpath); free(wpath);
return opendir_internal(fh, &fd); return ret;
} }
// //