Weird error occurance

Josh moonburntm at gmail.com
Wed Aug 7 21:20:58 PDT 2013


I'm having a rather weird error when things that should have no 
effect are changed. The purpose of the program is to recursively 
go through the directories, creating a DirEntry for each 
file/folder found. The parts in try-catch are there to skip 
anything that is unreachable. "C:\Users\All Users\Application 
Data" is the first real problem I've had. This is because on 
Windows 7, it is a locked shortcut, I presume for XP 
compatibility. First, the code:

import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.exception;
import std.file;
import std.process;
import std.stdio;
import std.string;

void main()
{
	writeln(scanDrive("C:\\Users", true).data.length);
}

alias Appender!(DirEntry[]) DirEntryAppender;

DirEntryAppender scanDrive(string drive, bool saving)
{
	bool b = (drive == "C:\\Users\\All Users\\Application Data");
	string[] names;
	DirEntryAppender entries;
	try
	{
		foreach (d; dirEntries(drive, SpanMode.shallow))//this is the 
line that causes the error
		{
		}
	}
	catch (FileException fe)
	{
		return DirEntryAppender([]);
	}
	foreach (d; dirEntries(drive, SpanMode.shallow))
	{
		names ~= d.name;
	}
	if (saving)
	{
		DirEntry d = dirEntry(drive);
		entries.put(d);
		foreach (n; names)
		{
			try
			{
				d = dirEntry(n);
				if (d.isDir && !canFind(d.name, "Windows") && 
!canFind(d.name, "Program Files") && !canFind(d.name, 
"ProgramData") && !canFind(d.name, "Josh"))
					entries.put(scanDrive(n, saving).data);
				else
					entries.put(d);
			}
			catch (FileException fe)
			{
			}
		}
	}
	return entries;
}

The results are:
g	si	sp	b	w
0	0	0	0	1
0	0	0	1	1
0	0	1	0	0
0	0	1	1	0
0	1	1	0	0
0	1	1	1	0
1	0	0	0	1
1	0	0	1	1
1	0	1	0	0
1	0	1	1	1
1	1	1	0	0
1	1	1	1	1

g = compiled with -g
si = the if (saving) line, 0 means commented out with the 
contents of the if left uncommented
sp = saving is a parameter, 0 means all occurrences commented 
out, including the if (saving) line
b = bool b = ... line, 0 means commented out
w = works without error

The error that I'm getting is an infinite loop of:
object.Error: Access Violation
----------------
0x0040B426 in void 
std.typecons.__T10RefCountedTS3std4file15DirIteratorImplVE3std8typecons24RefCountedAutoInitialize0Z.RefCounted.__dtor()
----------------
Bypasses std.file.FileException at std\file.d(2311)
=== Bypassed ===
std.file.FileException at std\file.d(2311): C:\Users\All 
Users\Application Data: Access is denied.
----------------
0x0040F842 in bool std.file.cenforce!(bool).cenforce(bool, lazy 
const(char)[], immutable(char)[], uint)
----------------

Does anybody know why simply creating an unused bool variable, or 
compiling with -g would make it work? I'm using Windows 7, with 
DMD v2.062.

Any help would be much appreciated.

Thanks,
Josh


More information about the Digitalmars-d-learn mailing list