Finding out if a directory is empty ...

Kenichi Okuyama okuyamak at
Tue Mar 27 06:14:23 GMT 2001

>>>>> "SL" == Steve Langasek <vorlon at> writes:

SL> On Tue, 27 Mar 2001, Kenichi Okuyama wrote:
>> Okey. But! At least, what you should first do is rmdir().

>> If you succeed, then it's case 4. No doubt.
>> If errno == 'EPERM', then it's write permission that you didn't have.
>> This is one of the case 3.
>> If errno == 'EFAULT' or 'ENOENT' then it's case 5.
>> If errno != 'EACCESS' than it's case 6.

>> Now we get to point where we have to decide whether it's case 1 or
>> 2.
>> But according to whay you've mentioned, we do not really have to
>> check if it's case 1 or 2, for ether cases, we're going to return

>> # Am I reading your sentence correctly?
SL> But you need to distinguish between cases 2 and 3: you've suggested returning
SL> EACCES whenever the user does not have permission to delete the directory, but
SL> Richard says he needs to ONLY return EACCES when the user does not have
SL> permission to delete the directory AND the directory is truly empty.  If the
SL> directory is not empty, he returns a different error, so clearly he needs to
SL> know if the directory is empty or not.

.... It seems like this is lot more difficult than I thought.
man page of rmdir(2) on Linux and FreeBSD4.2, says that there's
error called ENOTEMPTY.

It says that 'EACCESS' will be returned only if we do not have
permission to "SEARCH" that path. So, if what this says is right,
ENOTEMPTY should be returned for both case 1 and 2. So, if this is
being returned, I think we need not to look more deeply.

The un-understandable case is:

Let's suppose that we did not have +x permission against target
directory. In this case, we have no right to look into the
directory, and check if it's empty or not.

So, in this case we'll get EACCESS error. But according to rule:

1. Directory is not empty, and you do not have sufficient access to remove it

... Now, we have no right to check if Directory is empty or not.

Can't we just return EACCESS in case like this?

# I mean, are you really sure about case lists? > Sharpe

If we can, Story is lot simpler. Just run rmdir(2).

1) If no error return 0.
2) If errno == ENOTEMPTY, then return ENOTEMPTY.
3) If errno == EACCESS, then return EACCESS.
4) If errno is something else, it's system error.

Kenichi Okuyama at Tokyo-Research Lab. IBM-Japan, Co.

More information about the samba-technical mailing list