Re: Recursive delete of directory - Directory not empty error



On Apr 15, 3:27 pm, The Natural Philosopher <a...@xxx> wrote:
Gordon wrote:
On Apr 15, 2:28 pm, The Natural Philosopher <a...@xxx> wrote:
Álvaro G. Vicario wrote:
Gordon escribió:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.
In the manual page for unlink() there's a user comment with sample code
for recursive deletion. I haven't tried it but who knows:
http://php.net/unlink
Normally thats because there are hidden or wrong permission files in it..

I suspect you need more subtle code, and if in Linux etc, some form of
attention to permissions if these are the problem.

I don't think permissions are the problem, because the development
machine is Windows, and the error message being given is Directory Not
Empty. No Permission Denied errors are popping up in the output. I
also mentioned that the problem went away when I introduced a line of
code for debugging purposes into CmsItem::deleteItem(), which echos
the path being deleted to the output. My suspicion is that the calls
to rmdir are happening more rapidly than the filesystem can cope with
them. The debug code introduced just enough of a delay for the code
to work properly but without it attempts to delete a parent directory
can occur before the deletion of its children has completed, causing
the not empty error.

Ah. Caching.

I've had a similar problem with PHP creating a file that didn't actually
exist on disk till PHP exited.

So your are suggesting that PHP doesn't actually delete the files until
after the call to unlink the directory is received?

I would NOT be surprised. PHP'S file handling seems to be a bit flaky in
this area.

Windows should be capable of processing the calls as fast as they can be
sent: IT *should* simply block if the directory is still busy.

My guess - and its only a guess - is that PHP itself is caching the file
deletes. But not the directory deletes.

Or spitting them out in random order.

Maybe its possible to force a buffer flush in PHP.

Yes, I'm caching database-stored content to disk as HTML in a
directory structure so only the first view of a page causes the actual
script to run, do the DB lookup, build the page and all the other time-
consuming jobs involved in presenting the content to the user (My 404
page is actually a PHP script that takes care of all this).

Like you said, I think that PHP is simply dispatching filesystem
commands to a queue of some sort rather than waiting for each one to
complete before moving on to the next, resulting in directories not
being empty when an unlink attempt is made on them, even though all
their contents has already also been unlinked.

I had thought of the possibility that there was a buffer problem
somewhere, and I tried adding clearstatcache calls in strategic places
in the code but that doesn't seem to have helped.

.