Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindFirstFile returns access denied

Tags:

c

windows

winapi

I'm trying to create a robust recursive folder deleter function.

With normal directories works pretty fine.

The problem appears when I create a "hardcore" direcory, like:

C:\test\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x\ ... \x\x\x

The length of this is around 25000 (less then the MSDN limit: 32,767). Basically I created this directory recursively until the CreatDirectory function failed.

Now, the strangest thing is, that my function is able to delete 2 directories then the FindFirstFile fails with 0x5:

\\?\C:\test\x\ ... \x\x\x\*.*    < no error
\\?\C:\test\x\ ... \x\x\*.*      < no error
\\?\C:\test\x\ ... \x\*.*        < access denied

(I can rerun the it, the app is slowly chews up the folder, 2 by 2, probably until the path length gets pretty small)

I'm running FindFirstFile to check if the folder is empty.

  • Is there any sort of limitation that is less documented?
  • The FindFirstFile just simply doesn't work? (buggy?)
  • Am I missing some sort of NTFS permission thing?
  • Something else ...

EDIT: IMPORTANT NOTE: If I run the program step by step slowly ... then nothing will fail.

like image 837
lerosQ Avatar asked May 16 '12 18:05

lerosQ


1 Answers

You are probably experiencing something like a virus scanner, indexer or continuous-backup solution holding a handle to the directory. If the Indexing Service is configured to index that folder for example.

Trying to delete a folder or file which is open other than with FILE_SHARE_DELETE flag will cause ACCESS_DENIED.

To confirm this, use Process Monitor to see opens and closes on anything matching your path.

(Of course also confirm you called FindClose).

like image 104
Ben Avatar answered Oct 08 '22 01:10

Ben