[Issue 6138] New: Using dirEntries and chdir() can have unwanted results
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 9 16:18:29 PDT 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6138
Summary: Using dirEntries and chdir() can have unwanted results
Product: D
Version: D2
Platform: Other
OS/Version: Windows
Status: NEW
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2011-06-09 16:13:56 PDT ---
I can't officially classify this as a bug. Jonathan said it might be a good
idea to put it here just so it doesn't get lost, and then we can later decide
if it's a bug or if we should just be careful what we do.
Here's some buggy code which might throw an exception (if you're lucky!):
foreach (string entry; dirEntries(curdir, SpanMode.shallow))
{
if (entry.isdir)
{
foreach (string subentry; dirEntries(entry, SpanMode.shallow))
{
if (subentry.isdir)
{
chdir(rel2abs(subentry));
}
}
}
}
This might throw something like this:
std.file.FileException at std\file.d(1124): .\foo\bar.d: The system cannot find
the path specified.
The problem is chdir will modify curdir, and dirEntries doesn't store 'curdir'
internally as an absolute address, so calling chdir() screws up the behavior of
dirEntries.
A workaround is to call rel2abs as soon as possible when using 'curdir' (the
first line changed here):
foreach (string entry; dirEntries(rel2abs(curdir), SpanMode.shallow))
{
if (entry.isdir)
{
foreach (string subentry; dirEntries(entry, SpanMode.shallow))
{
if (subentry.isdir)
{
chdir(rel2abs(subentry));
}
}
}
}
I've had a use for code like this where I was invoking a script file which was
always relative to some subdirectory.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list