dir.c: glob short names

* dir.c (glob_helper): match patterns against legacy short names
  too, not only ordinary names.  [ruby-core:67954] [Bug #10819]
* win32/dir.h (struct direct): add short name members.
* win32/win32.c (opendir_internal, readdir_internal): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49892 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-03-08 07:57:38 +00:00
parent db24cb705d
commit 5b06e83345
5 changed files with 55 additions and 8 deletions

View File

@ -1,3 +1,12 @@
Sun Mar 8 16:57:35 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (glob_helper): match patterns against legacy short names
too, not only ordinary names. [ruby-core:67954] [Bug #10819]
* win32/dir.h (struct direct): add short name members.
* win32/win32.c (opendir_internal, readdir_internal): ditto.
Sat Mar 7 09:36:05 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> Sat Mar 7 09:36:05 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
* array.c: document that first element is kept when using * array.c: document that first element is kept when using

14
dir.c
View File

@ -1554,6 +1554,18 @@ glob_func_caller(VALUE val)
return Qnil; return Qnil;
} }
static inline int
dirent_match(const char *pat, rb_encoding *enc, const char *name, const struct dirent *dp, int flags)
{
if (fnmatch(pat, enc, name, flags) == 0) return 1;
#ifdef _WIN32
if (dp->d_altname) {
if (fnmatch(pat, enc, dp->d_altname, flags) == 0) return 1;
}
#endif
return 0;
}
static int static int
glob_helper( glob_helper(
const char *path, const char *path,
@ -1753,7 +1765,7 @@ glob_helper(
# endif # endif
case PLAIN: case PLAIN:
case MAGICAL: case MAGICAL:
if (fnmatch(p->str, enc, name, flags) == 0) if (dirent_match(p->str, enc, name, dp, flags))
*new_end++ = p->next; *new_end++ = p->next;
default: default:
break; break;

View File

@ -256,7 +256,11 @@ class TestDir < Test::Unit::TestCase
skip unless File.directory?(short) skip unless File.directory?(short)
entries = Dir.glob("#{short}/Common*") entries = Dir.glob("#{short}/Common*")
assert_not_empty(entries, bug10819) assert_not_empty(entries, bug10819)
assert_equal(Dir.glob("#{File.expand_path(short)}/Common*"), entries, bug10819) long = File.expand_path(short)
assert_equal(Dir.glob("#{long}/Common*"), entries, bug10819)
wild = short.sub(/1\z/, '*')
assert_include(Dir.glob(wild), long, bug10819)
assert_empty(entries - Dir.glob("#{wild}/Common*"), bug10819)
end end
end end

View File

@ -13,6 +13,8 @@ struct direct
long d_namlen; long d_namlen;
ino_t d_ino; ino_t d_ino;
char *d_name; char *d_name;
char *d_altname; /* short name */
short d_altlen;
char d_isdir; /* directory */ char d_isdir; /* directory */
char d_isrep; /* reparse point */ char d_isrep; /* reparse point */
}; };

View File

@ -1870,6 +1870,7 @@ opendir_internal(WCHAR *wpath, const char *filename)
HANDLE fh; HANDLE fh;
DIR *p; DIR *p;
long len; long len;
long altlen;
long idx; long idx;
WCHAR *tmpW; WCHAR *tmpW;
char *tmp; char *tmp;
@ -1908,12 +1909,13 @@ opendir_internal(WCHAR *wpath, const char *filename)
// //
do { do {
len = lstrlenW(fd.cFileName) + 1; len = lstrlenW(fd.cFileName) + 1;
altlen = lstrlenW(fd.cAlternateFileName) + 1;
// //
// bump the string table size by enough for the // bump the string table size by enough for the
// new name and it's null terminator // new name and it's null terminator
// //
tmpW = realloc(p->start, (idx + len) * sizeof(WCHAR)); tmpW = realloc(p->start, (idx + len + altlen) * sizeof(WCHAR));
if (!tmpW) { if (!tmpW) {
error: error:
rb_w32_closedir(p); rb_w32_closedir(p);
@ -1924,6 +1926,7 @@ opendir_internal(WCHAR *wpath, const char *filename)
p->start = tmpW; p->start = tmpW;
memcpy(&p->start[idx], fd.cFileName, len * sizeof(WCHAR)); memcpy(&p->start[idx], fd.cFileName, len * sizeof(WCHAR));
memcpy(&p->start[idx + len], fd.cAlternateFileName, altlen * 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);
@ -1938,7 +1941,7 @@ opendir_internal(WCHAR *wpath, const char *filename)
SetBit(p->bits, BitOfIsRep(p->nfiles)); SetBit(p->bits, BitOfIsRep(p->nfiles));
p->nfiles++; p->nfiles++;
idx += len; idx += len + altlen;
} while (FindNextFileW(fh, &fd)); } while (FindNextFileW(fh, &fd));
FindClose(fh); FindClose(fh);
p->size = idx; p->size = idx;
@ -2023,6 +2026,7 @@ move_to_next_entry(DIR *dirp)
if (dirp->curr) { if (dirp->curr) {
dirp->loc++; dirp->loc++;
dirp->curr += lstrlenW(dirp->curr) + 1; dirp->curr += lstrlenW(dirp->curr) + 1;
dirp->curr += lstrlenW(dirp->curr) + 1;
if (dirp->curr >= (dirp->start + dirp->size)) { if (dirp->curr >= (dirp->start + dirp->size)) {
dirp->curr = NULL; dirp->curr = NULL;
} }
@ -2035,11 +2039,16 @@ move_to_next_entry(DIR *dirp)
// //
/* License: Ruby's */ /* License: Ruby's */
static BOOL static BOOL
win32_direct_conv(const WCHAR *file, struct direct *entry, const void *enc) win32_direct_conv(const WCHAR *file, const WCHAR *alt, struct direct *entry, const void *enc)
{ {
UINT cp = *((UINT *)enc); UINT cp = *((UINT *)enc);
if (!(entry->d_name = wstr_to_mbstr(cp, file, -1, &entry->d_namlen))) if (!(entry->d_name = wstr_to_mbstr(cp, file, -1, &entry->d_namlen)))
return FALSE; return FALSE;
if (alt && *alt) {
long altlen = 0;
entry->d_altname = wstr_to_mbstr(cp, alt, -1, &altlen);
entry->d_altlen = altlen;
}
return TRUE; return TRUE;
} }
@ -2091,16 +2100,21 @@ rb_w32_conv_from_wstr(const WCHAR *wstr, long *lenp, rb_encoding *enc)
/* License: Ruby's */ /* License: Ruby's */
static BOOL static BOOL
ruby_direct_conv(const WCHAR *file, struct direct *entry, const void *enc) ruby_direct_conv(const WCHAR *file, const WCHAR *alt, struct direct *entry, const void *enc)
{ {
if (!(entry->d_name = rb_w32_conv_from_wstr(file, &entry->d_namlen, enc))) if (!(entry->d_name = rb_w32_conv_from_wstr(file, &entry->d_namlen, enc)))
return FALSE; return FALSE;
if (alt && *alt) {
long altlen = 0;
entry->d_altname = rb_w32_conv_from_wstr(alt, &altlen, enc);
entry->d_altlen = altlen;
}
return TRUE; return TRUE;
} }
/* License: Artistic or GPL */ /* License: Artistic or GPL */
static struct direct * static struct direct *
readdir_internal(DIR *dirp, BOOL (*conv)(const WCHAR *, struct direct *, const void *), const void *enc) readdir_internal(DIR *dirp, BOOL (*conv)(const WCHAR *, const WCHAR *, struct direct *, const void *), const void *enc)
{ {
static int dummy = 0; static int dummy = 0;
@ -2111,7 +2125,11 @@ readdir_internal(DIR *dirp, BOOL (*conv)(const WCHAR *, struct direct *, const v
// //
if (dirp->dirstr.d_name) if (dirp->dirstr.d_name)
free(dirp->dirstr.d_name); free(dirp->dirstr.d_name);
conv(dirp->curr, &dirp->dirstr, enc); if (dirp->dirstr.d_altname)
free(dirp->dirstr.d_altname);
dirp->dirstr.d_altname = 0;
dirp->dirstr.d_altlen = 0;
conv(dirp->curr, dirp->curr + lstrlenW(dirp->curr) + 1, &dirp->dirstr, enc);
// //
// Fake inode // Fake inode
@ -2202,6 +2220,8 @@ rb_w32_closedir(DIR *dirp)
if (dirp) { if (dirp) {
if (dirp->dirstr.d_name) if (dirp->dirstr.d_name)
free(dirp->dirstr.d_name); free(dirp->dirstr.d_name);
if (dirp->dirstr.d_altname)
free(dirp->dirstr.d_altname);
if (dirp->start) if (dirp->start)
free(dirp->start); free(dirp->start);
if (dirp->bits) if (dirp->bits)