Finding out if a directory is empty ...

Kenichi Okuyama okuyamak at dd.iij4u.or.jp
Tue Mar 27 05:36:41 GMT 2001


Dear Sharpe,

>>>>> "RS" == Richard Sharpe <sharpe at ns.aus.com> writes:
RS> 1. Directory is not empty, and you do not have sufficient access to remove it
RS> 2. Directory is not empty, and you do have sufficient access to remove it
RS> 3. Directory is empty, and you do not have sufficient access to remove it
RS> 4. Directory is empty, and you do have sufficient access to remove it.

RS> I have to return the following:

RS> 1. ENOTEMPTY
RS> 2. ENOTEMPTY
RS> 3. EACCES
RS> 4. 0

RS> As you say, if I try to delete the directory, and it is not empty, I get
RS> back EACCES from a Samba mapping function, but I then have to determine if
RS> the problem was an empty directory to which the caller truly does not have
RS> access or the directory is not empty.

Wait, I think you have more possibilities.


RS> 1. Directory is not empty, and you do not have sufficient access to remove it
RS> 2. Directory is not empty, and you do have sufficient access to remove it
RS> 3. Directory is empty, and you do not have sufficient access to remove it
RS> 4. Directory is empty, and you do have sufficient access to remove it.

    5. There's no such directory.
    6. System error.


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
"ENOTEMPTY".

# Am I reading your sentence correctly?


Just in case you need to find out whether it's case 1 or 2,
all we need to check is whether we have write permission against
that directory. To check this you need to call stat(2).
Still, this is lot lighter than looking inside directory.

isn't it?
---- 
Kenichi Okuayma at Tokyo Research Lab. IBM-Japan, Co.




More information about the samba-technical mailing list