Folders, files and poorly described exceptions

You know, sometimes exceptions returned in C# are so irritatingly vague or indirect that it takes longer than I’d like to solve the problem.

For example, I’ve been attempting to loop through a large set of directories on a network share, for a list of users in Active Directory. I have both an File/Folder indexing library, and an Active Directory library to help speed up development.

foreach (User Account in Accounts) {
    if (!Directory.Exists(Account.HomeDirectory)) continue;

    List<DirectoryInfo> folders = new List<DirectoryInfo>();
    List<FileInfo> files = new List<FileInfo>();

    Folders.Index(
        new DirectoryInfo(Account.HomeDirectory),
        ref folders,
        ref files);

    files.Reverse();
    folders.Reverse();

    foreach (FileInfo fi in files) {
        if (fi.Exists) fi.Delete();
    }

    foreach (DirectoryInfo di in folders) {
        if (di.Exists) di.Delete(true);
    }
}

Irritatingly, it kept returning a “Access is denied” error on the first folder that it attempted to delete. I, of course, have had not nearly enough sleep to deal with this sort of obscure issue, but I plowed on. I ran the code under a domain admin account; that didn’t fix it. I added the existence checks and a bit of debugging output to find where it was failing exactly.

Unable to force the issue, I checked the folder that it was failing on, and the folder was “root\My Videos”. Properties showed “Read-only for files in this folder”.

*facepalm*

Here’s the fixed code. Essentially it sets the FileAttributes of the files and folders it attempts to delete to FileAttributes.Normal, thereby bypassing the “Access is Denied” error (which IMHO should be a “Unable to delete – folder is read-only”!) and allowing the deletion.

foreach (User Account in Accounts) {
    if (!Directory.Exists(Account.HomeDirectory)) continue;

    List<DirectoryInfo> folders = new List<DirectoryInfo>();
    List<FileInfo> files = new List<FileInfo>();

    Folders.Index(
        new DirectoryInfo(Account.HomeDirectory),
        ref folders,
        ref files);

    files.Reverse();
    folders.Reverse();

    foreach (FileInfo fi in files) {
        fi.Attributes = FileAttributes.Normal;
        if (fi.Exists) fi.Delete();
    }

    foreach (DirectoryInfo di in folders) {
        di.Attributes = FileAttributes.Normal;
        if (di.Exists) di.Delete(true);
    }
}

I’m guessing that if I still have issues, I’ll probably have to take ownership of the files/folders to delete them. Not a great start to the day.

Update 1:

I found what looks to be a “Privilege Enabling” library. This is definitely something I shall be adding to my toolkit.

public static void TakeOwnership(DirectoryInfo di) {
    Process p = Process.GetCurrentProcess();

    using (new ProcessPrivileges.PrivilegeEnabler(p, Privilege.TakeOwnership)) {
        DirectorySecurity ds = di.GetAccessControl();
        ds.SetOwner(WindowsIdentity.GetCurrent().User);
        di.SetAccessControl(ds);
    }
}

Update 2:

Wow. After trying to take ownership as above, I still had difficulties with certain “RECYCLER” files/folders, and taking ownership apparently wasn’t enough. Here’s the full-on, brute-force, take-control method:

public static void TakeOwnership(ref DirectoryInfo di) {
    Process p = Process.GetCurrentProcess();
    SecurityIdentifier si = WindowsIdentity.GetCurrent().User;

    // Raise the process' access privileges.
    using (new ProcessPrivileges.PrivilegeEnabler(p, Privilege.TakeOwnership, Privilege.Security)) {
        // Get access rules.
        DirectorySecurity ds = di.GetAccessControl();

        // Take "full control" of everything.        
        FileSystemAccessRule fsaRule = new FileSystemAccessRule(
            si,
            FileSystemRights.Delete | FileSystemRights.Modify,
            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
            PropagationFlags.None,
            AccessControlType.Allow);

        // Remove previous access rules.
        ds.PurgeAccessRules(si);

        // Add the new access rules.
        ds.AddAccessRule(fsaRule);

        // Take ownership
        ds.SetOwner(si);

        // Set the access rules to the object.
        di.SetAccessControl(ds);
    }
}

Comments Off

Revisionist History

I’ve been going through all the old posts and attempting to update them to match both the category list and the styles. It’ll take a bit of time, but until then, here’s my Harry Potter and the Deathly Hallows Part 2 review. Yes, there will be tonnes of spoilers. You’ve been warned. Also, why don’t you know the story yet, anyway? You should already know how this movie ends!

I am both underwhelmed and overwhelmed. Does that just make me whelmed? Is that even a thing? Do the horrible changes they make verses the beautifully told stories cancel each other out?

I am overwhelmed simply because it is the last movie and we’re pretty much leaving this world behind now. The CG, practical and special effects were all appropriately awesome. The acting has gotten better and better over the years.You really get a good feel of how far they’ve all come on their journey this last decade.

However, I am underwhelmed by all of the changes they made. Much of what made the story so great in the book was gutted, and while this can’t be avoided when the book is that long, some of their edits made absolutely no sense.

For example, the scene in which Harry and Voldemort fight for the last time is completely and utterly pointless and anti-climatic. In the book, this scene takes place in front of the whole (or what’s left of the) school. The real feeling of accomplishment and the weight of the victory can be felt. In the movie, the character split up so much that nothing important happens between more than three people. Harry Potter and Voldy fight by themselves in a stretched out mano-a-snako wand-lightning battle and flying around randomly in smoke. Neville doesn’t get to chop off Nagini‘s head until the dynamic duo Hermione Granger and Ron Weasley battle it on some stairs and get to cower appropriately. The “NOT MY DAUGHTER YOU BITCH” scene in the book becomes an almost whispered line (relative to all the other sounds happening around them) and the emotion of the fight between Molly Weasley and Bellatrix fails completely. There were so many times that I thought the audience in the theatre was going to cheer, but then it seemed like everyone was embarrassed because the movie didn’t really hit the emotional beats it deserved.

The changes they made baffles me, because they would have been so much better literally just cribbed straight from the books. Why the script writer (and/or the actress) changed the feel of the Molly/Bellatrix fight astounds me. Why they felt Harry needed to fight Voldy alone, where no one could see and/or verify his victory over the greatest evil wizard ever is completely bonkers! The extra scenes add literally nothing but extra time wasted to the story, and it degrades the source material.

Other baffling changes arrive in the story of Dumbledore‘s family. I get why they didn’t have time, but why would you bring it up at all if you weren’t going to explain it even a little?. A name is dropped, an accusation flung, but we have no idea why these things are there. And the elves, obviously minus Dobby, show up nowhere in the movie, where they are a major part of the book’s big fight. In the opposite of the last, it would have made sense to add at least a passing reference to them, considering that they made so much a point of them in several movies, because otherwise it is as if Dobby’s death and Treacher’s change from villian to servant to savior completely worthless now.

Now, it’s not as if this is a complete waste of a movie – on the contrary, in every other respect it excelled my expectations. Maggie Smith‘s acting is supurb, knowing that the whole time she was battling cancer during production, and especially the line where she says, in a very girl-ish way, “I’ve always wanted to say that spell!”. Alan Rickman‘s Snape is probably the highlight of the whole movie, as you see every single bit of him shattered in the pensive sequence. Also, his death scene was respectably brutal and hidden tactfully behind frosted glass, but the full weight of it is completely felt. The epilogue scene, while short and not truly important is any way, was wonderfully done, especially the almost lack of makeup and body modifications needed to make it seem legitimate. (I only wish they’d mentioned “Prof. Longbottom”, even if in passing) I guess it helps that the actors themselves have grown enough for it.

If I had to rate the movie, I’d say definitely a 3 out of 5, it was both awesome and infuriating in it’s execution. I only wish they’d consulted the book a little more for ideas instead of padding or trimming things out improperly.

Comments Off

Forgetfulness is… wait, what was I saying?

Unfortunately, I forgot a small detail with the switch-over. All of the media is still on the old server. So apparently all the old posts I imported will be pointing to incorrect media locations. I’ve got a local copy of it, now, as I was able to FTP in via IP Address (since jstorer2.com now points here), so eventually I’ll either have to edit the post or upload the new media.

Now back to your regularly scheduled ignoring of my posts. ;)

Comments Off

The new blog, same as the old blog.

So, here’s the rub. My old website, jstorer2.com, will be ending it’s three year run in August. So I will be moving my blog here, which seems sensible considering how much work/money it costs to host my own. So ignore the ads and enjoy the new location of my blog.

Please realize that all of the links to media will probably be broken. I no longer have access to the old stuff, so I’ll try to fix what I can.

Comments Off

Thunderstorms

We’re actually going through a thunderstorm right now, so I thought I’d get some audio together of the bolts-in-question’s passage through the atmosphere. Here’s an MP3 of some of the best ones, edited to remove as much noise and background rain as possible, without destroying the best lightning-strikes heard today.

[Edit: Of course, now that I've switched to wordpress.com, I am no longer allowed to upload MP3s without paying a little extra a month. We'll see if I can't scrounge up a few bucks to enjoy the privilege.]

Comments Off