views:

127

answers:

3

I've got a large folder on an offsite backup machine that gets populated with files by rsync (through deltacopy) every night (running windows xp) from the main work site. I've discovered some annoying folders that cannot be opened, or deleted, or even checked for file sizes. I get the such and such a folder is not accessible, access is denied message when i try to click on it in windows explorer. According to the windows explorer tooltip they are also "empty" and the properties of these folders say 0 bytes and 0 files.

I currently have a c# program that goes through every folder and file and tries to copy the whole backup directory to a dated backup-backup directory, which is how i discovered this problem in the first place. The regular System.IO library seems helpless against these blasted folders. Exceptions are thrown when I even try to access the folder path.

Does anyone have any clue how I could, say, on an access denied exception in my existing copy code, force the delete of these folders so rysnc can recreate the directory again and get the whole thing synced again?

A: 

Yes, try the awesome "Process Explorer" from Microsoft (formerly SysInternals).

Although it's for the processes in the windows filesystem, you could search for your folder in the explorer window & it will tell you who is locking it.

Once you release the process, your program would be able to delete the folder.

If that doesn't work, see if you can specify additional parameters to bruteforce the delete in your program.

SoftwareGeek
He wants a programmatic method of dealing with it, therefore, it needs to be done via code automatically, not a manual process using a third party app.
Dan McGrath
Yes, only if he can kill the process that's keeping the folder, will his program be able to delete it. what's wrong with that approach?
SoftwareGeek
Tried this, I searched for the folder in the process explorer search with no results. Besides, assuming this keeps happening once in awhile I need to have a procedure to deal with it automatically, hence the plea for C# help.
IsaacB
ok understood but my answer doesn't deserver a downvote.
SoftwareGeek
He needs to be able to do it without human intervention. Your method requires a user to perform a task with a system utility. Hardly appropriate for an end-user application
Dan McGrath
-1. As others have said, he said "programmatic". This requires him to run the application; he wants to automate it.
Adam Robinson
Jeez, give me a break, will ya?
SoftwareGeek
@SoftwareGeek regarding downvotes. They are nothing personal, they are a method of showing the helpfulness of an answer. Since the answer does not address the question asked, the -1. If this was a question on Super, I would have given a +1
Dan McGrath
@Dan McG - appreciate your response, thanks.
SoftwareGeek
Wow, can't believe this got down voted. Perhaps there is a process keeping the folders locked. If the same program is consistently locking these folders you could surely programmaticaly kill it but until he finds out if this is the case....Seems like a reasonable suggestion to me.
jwsample
@jwsample - I coudn't believe it either.
SoftwareGeek
@SoftwareGeek et. al. If you hover over the down arrow, you'll see it say "This answer is not useful". Since the OP asked for a programmatic method, and you didn't give one, that deserves a downvote (and you got mine).
John Saunders
@John Saunders - he he thanks, i shall return the favor.
SoftwareGeek
Quick! Someone downvote the chkdsk suggestion as that also might help narrow down the problem! Strange place this.
jwsample
@SoftwareGeek: be as honest about it as I am, and I'll be pleased to receive your downvotes.
John Saunders
SoftwareGeek
@SoftwareGeek: first of all, my comment should be a hint that your answer would have been better as a comment, since it doesn't answer the question. Also, my comment is different: I say "find out what's wrong, then maybe you can write a program to fix it". I meant, find out once, then the program would be able to fix it many times.
John Saunders
+2  A: 

First thing I think of when I see this is time to do a checkdisk. From the sounds of it, it feels more like a file system problem than something solvable the way you want to go about it.

Joel Rondeau
chkdsk did find some relevant irregularities, in fact. But it did not fix the problem for me. <sigh> I guess the move to linux is unavoidable.
IsaacB
@IssacB: You can move to Linux if you wish, but do consider the fact that, somehow, people manage to work with Windows and NTFS without being blocked by the problems you're encountering.
Steven Sudit
I think you are right Joel.My solution to this will be to use my copy program to identify broken files and directories and email them to me. Then I'll use unlocker to delete them manually. This probably happened because I'm using a very unreliable POS computer for this job, and it decided to crash in the middle of a write or something.Thanks for recommending unlocker again steven, that is a pretty useful program!
IsaacB
Sorry, this was not the problem. I have copied the same folder over again through rsync and I have the same problems. I can access the files fine on the main fileserver but not on the backup. The mystery continues.
IsaacB
A: 

It sounds like the filenames are either bad or contain characters that are invalid in Win32. Did you try to delete the directories with rd /r? Did you do a dir /x on them and try to delete the files/directories using their short names?

I would say that you first have to figure out why you can't delete the folders. Once you figure that out, you can write a program to fix it.

OK, so now that you know it's a permissions problem, the first step is to take ownership of the files (so you can set the permissions), then change the permissions so that you can delete the files.

Here's code to take ownership of a file:

WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); 

FileSecurity acl = File.GetAccessControl(filename); 
acl.SetOwner(currentUser.User); 

File.SetAccessControl(filename, security); 
Gabe
access denied. access denied.
IsaacB
What does `cacls` say? Is it possible the directory is owned by another use and you don't have permissions for it?
Gabe
Okay, I was led on a wild goose chase. I don't know why my thinking was so unclear today. Anyhow, it was a permissions issue. System owned those files. chkdsk led me astray by finding orphaned files in that directory. Grrr. I will look into how to change the permissions on folders in C# if that is possible.
IsaacB
Changing permissions is easy in .NET, but you have to actually understand ACE's, DACL's, SDDL and the rest. Start here: http://msdn.microsoft.com/en-us/magazine/cc135979.aspx
Steven Sudit
I ended up making deltacopy server run on the administrator account. Now there aren't anymore permissions problem. Thanks for everyone's help!
IsaacB