Finding out if a directory is empty ...

Richard Sharpe sharpe at ns.aus.com
Tue Mar 27 06:13:43 GMT 2001


At 02:36 PM 3/27/01 +0900, Kenichi Okuyama wrote:
>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.

Yes, you are correct, now that I think of it ...

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

Yes. However, SMB servers return a different error than Permission denied.

>    6. System error.

Yes, and I think I return a different error in that case.

>Okey. But! At least, what you should first do is rmdir().

Yes, that is what I do.

>If you succeed, then it's case 4. No doubt.

Yes, that is correct.

>If errno == 'EPERM', then it's write permission that you didn't have.

I never seem to get this back ...

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

Yes, I return ENOTEMPTY if the directory is not empty. But you see, I will
get EACCES back if the directory was not empty, in both cases, and the best
I can do is map it to ENOTEMPTY, as that is the case. I cannot easily
distinguish between cases 1 and 2 above.

># 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.

Hmmm, maybe I can do that. I will check what the situation is ... I would
have to stat the parent to see what access I have ...

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

Regards
-------
Richard Sharpe, sharpe at ns.aus.com
Samba (Team member, www.samba.org), Ethereal (Team member, www.ethereal.com)
Contributing author, SAMS Teach Yourself Samba in 24 Hours
Author, Special Edition, Using Samba






More information about the samba-technical mailing list