www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6138] New: Using dirEntries and chdir() can have unwanted results

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 puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



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 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: -------
Jun 09 2011