Directory size - Security ACLs causing Endless loop

P

Phil Rayner

Basically put I am trying to get the disk-space used of a directory from a
web application.

The problem arises when the application (more the user that the process runs
as) doesn't have access to one of the subdirectories (in this case the logs
directory).

I want the application to add the filespace used by files that the process
does have access to.

I have writen diskspace functions which I have included at the end of this
post.



Now the problem is quite strange. When I run this code on my development box
(windows XP) it runs flawlessly, and works as expected. When I run this code
on our staging server (2003 Server) it runs flawlessly, but when I run this
on our production server, the process seems to get into an endless loop and
I have to do an IISRESET to fix the problem (recycling the APP pool doesn't
fix it). On the production server each time I run the diskspace report (the
code that calls the above functions) the process will get in to an endless
loop and take 100% of CPU.





Is there some problem with how I am nesting the Try catches?



Can anyone tell me what I might be doing wrong, and if there is a reference
implementation for getting directory sizes that works when you don't have
the security ACLs to some of the files or directories within the directory?



Other code that might help is not using try catches, but first checking the
ACLs on a file. The only code I have found to do this uses the
FileIOPermission object, and requires statements such as
filePermission.Demand(), which in themselves must use a try catch, and seem
to have more problems than the code I have provided above (using
filePermission.Demand() the code still runs on my development box, but not
on our staging server or our production server).



Any help on this would be much appreciated.









PS. As a side note can anyone suggest how to test this on our production
server without requiring an IISRESET each time it crashes/endless loops?





// Diskspace functions:



public long DirSize(DirectoryInfo d)

{



if (d.Exists)

{

long Size = 0;

// Add file sizes.

FileInfo[] fis = d.GetFiles();

foreach (FileInfo fi in fis)

{

Size += fi.Length;

}





// Add subdirectory sizes.

DirectoryInfo[] dis = d.GetDirectories();

foreach (DirectoryInfo di in dis)

{

try

{

long dirSize = DirSize(di);

Size += dirSize;

}

catch {}

}





return (Size);

}



return 0;

}





public Decimal SitePathSizeNoError(String path)

{

Decimal size = 0;

//Get the parent directory as the path we have is the Htdocs, and we
want to include the logs too

DirectoryInfo dir = Directory.GetParent (path);

if (!dir.Exists)

return -1;



DirectoryInfo[] subdirs = dir.GetDirectories();

foreach (DirectoryInfo subdir in subdirs)

{

if (subdir.Name != "vhost")

//foreach child (except 'vhost' folder') get size;

size += DirSize (subdir);

}



//Convert size to MB

size = size/1048576;

return Math.Round (size, 2);

}





public String SitePathSizeString(String path)

{

Decimal size;



try

{

size = SitePathSizeNoError(path);

}

catch (Exception ex)

{

return String.Format("Error accessing path {0}.
{1}",path,ex.Message);

}



if (size >= 0)

{

_planDiskUsage += size;

return String.Format("{0} Mb",size);

}

else

return String.Format("Path Not found. {0}",path);

}
 
M

[MSFT]

Hello,

Was the problem related to the Directory you pass to function "DirSize"?
for example, if you create a new and empty folder on your disk and test it
with your code, will it also generate the error? I aslo suggest you create
a simple windows form application with function "DirSize" and test if it
also generate same problem with same input.

Luke
 
P

Phil Rayner

It works fine with an empty folder. Its when I add a sub folder to that
folder with no security access (ie I purposfully take away the security ACLs
on that folder for the ASP.NET worker process.

It only seems to be a problem trapping the error. I suspect it is a problem
in the try {} catch { } statements.
 
M

[MSFT]

Simple Try..catch statement won't cause a application hang. Since the CPU
usage is almost 100%, I suspect the problem occur in the loop, it cannot
jump out for some reasons. Have you try a windows form with fully trusted
permission?

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top