Directory size - Security ACLs causing Endless loop

Discussion in 'ASP .Net Security' started by Phil Rayner, Sep 6, 2005.

  1. Phil Rayner

    Phil Rayner Guest

    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);

    }
    Phil Rayner, Sep 6, 2005
    #1
    1. Advertising

  2. Phil Rayner

    [MSFT] Guest

    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
    [MSFT], Sep 7, 2005
    #2
    1. Advertising

  3. Phil Rayner

    Phil Rayner Guest

    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.

    "[MSFT]" <> wrote in message
    news:...
    > 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
    >
    Phil Rayner, Sep 8, 2005
    #3
  4. Phil Rayner

    [MSFT] Guest

    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
    [MSFT], Sep 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liet Kynes
    Replies:
    0
    Views:
    476
    Liet Kynes
    Nov 26, 2003
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Endless loop on delete auth cookie/abandon session

    =?Utf-8?B?QmlsbCBCb3Jn?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    667
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 2, 2005
  3. Alexandre
    Replies:
    4
    Views:
    470
    Lasse Reichstein Nielsen
    Apr 12, 2005
  4. Leo
    Replies:
    1
    Views:
    407
    Thomas Heller
    Jul 4, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    353
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page