|
Home > Archive > Unix Shell > November 2005 > how does rm check if directory is emtpy
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how does rm check if directory is emtpy
|
|
| Frank Dietrich 2005-11-27, 5:52 pm |
| Hi,
on a Red Hat 7.3 I found an intersting behavior from rm.
>mkdir foodir
>touch foodir/barfile
>chmod 100 foodir
>ls -ld foodir
d--x------ 2 Frank.Di users 4096 Nov 27 16:40 foodir/
>rm -rf foodir
rm: cannot open directory `foodir/.': Permission denied
rm: cannot remove directory `foodir': Directory not empty
>chmod 700 foodir
>rm foodir/barfile
>chmod 100 foodir
>rm -rf foodir
rm: cannot open directory `foodir/.': Permission denied
How does rm know about the file in the directory?
Frank
| |
| Janis Papanagnou 2005-11-27, 5:52 pm |
| Frank Dietrich wrote:
> Hi,
>
> on a Red Hat 7.3 I found an intersting behavior from rm.
>
>
>
> d--x------ 2 Frank.Di users 4096 Nov 27 16:40 foodir/
>
>
> rm: cannot open directory `foodir/.': Permission denied
> rm: cannot remove directory `foodir': Directory not empty
>
>
> rm: cannot open directory `foodir/.': Permission denied
>
> How does rm know about the file in the directory?
You specified access permissions for the directory foodir.
man chmod
The letters `rwxXstugo' select the new permissions for the
affected users: read (r), write (w), execute (or access
for directories) (x), [...]
Janis
>
> Frank
| |
| Frank Dietrich 2005-11-27, 5:53 pm |
| Hi Janis,
Janis Papanagnou wrote:
> Frank Dietrich wrote:
>
> You specified access permissions for the directory foodir.
Yes I know this. But I can't "cd foodir/" or "ls -A foodir/". Therefore my
question how does rm know about the files in foodir?
I'm sure that rm doesn't
chmod 500 foodir
# count, however it does
chmod $OLDCHMOD foodir
In man stat I can't find any information about "directory has files" or
"directory has no files". But rm shows there must be a way. ;-)
Frank
| |
| Janis Papanagnou 2005-11-27, 5:53 pm |
| Frank Dietrich wrote:
> Janis Papanagnou wrote:
>
> Yes I know this. But I can't "cd foodir/" or "ls -A foodir/".
The first one puzzles me... If the x-flag for the directory is set then
you should be able to chdir to it. I verified that expected behaviour on
my system. The second one is as I expected; since you have no r-flag set
on the directory permission is denied.
> Therefore my
> question how does rm know about the files in foodir?
rm doesn't know that, it get's the access violation from the system call.
Everything that you reported seems to be consistent, with the exception
of the "cd foodir" which should be possible and which you should verify.
> I'm sure that rm doesn't
> chmod 500 foodir
> # count, however it does
> chmod $OLDCHMOD foodir
>
> In man stat I can't find any information about "directory has files" or
> "directory has no files". But rm shows there must be a way. ;-)
[ re-inserted your original testcase ]
> mkdir foodir
> touch foodir/barfile
> chmod 100 foodir
> ls -ld foodir
> d--x------ 2 Frank.Di users 4096 Nov 27 16:40 foodir/
> rm -rf foodir
> rm: cannot open directory `foodir/.': Permission denied
To remove something from the directory you need read and write permissions
on the directory, thus permission is denied.
> rm: cannot remove directory `foodir': Directory not empty
Since the directory is not empty you get that message. Note that rm has
the permission to enter the directory.
> chmod 700 foodir
> rm foodir/barfile
> chmod 100 foodir
> rm -rf foodir
> rm: cannot open directory `foodir/.': Permission denied
Again missing read permissions cause this error.
You may want to play with setting and removing r- and w-flags.
Janis
| |
| Frank Dietrich 2005-11-27, 5:53 pm |
| Hi Janis,
Janis Papanagnou wrote:
> Frank Dietrich wrote:
>
> The first one puzzles me... If the x-flag for the directory is set then
> you should be able to chdir to it.
Sorry. That's my fault. Change into the diretory works.
> To remove something from the directory you need read and write permissions
> on the directory, thus permission is denied.
No. I don't like to remove something from the directory I look for a way
to know if it is empty or not.
The confusion starts with this testcase (I verified it again, maybe it's
an Red Hat 7.3 oddity)
~>ls -A
~>mkdir foodir
~>chmod 100 foodir
~>ls -l
d--x------ 2 Frank.Di users 4096 Nov 27 22:37 foodir
~>rm -rf foodir
rm: cannot open directory `foodir/.': Permission denied
~>ls -A
# foodir was deleted
~>mkdir foodir
~>touch foodir/barfile
~>chmod 100 foodir
~>ls -l
d--x------ 2 Frank.Di users 4096 Nov 27 22:40 foodir
~>rm -rf foodir
rm: cannot open directory `foodir/.': Permission denied
rm: cannot remove directory `foodir': Directory not empty
# foodir not deleted
I interpret it like: I can step into this directory but can't list the
contained files. Ok, you say rm doesn't know about the file in foodir
because it uses an system function. So I will alter my question: Is it
possible to use this function from within an script. May be there's a
core tool like 'stat' that only return empty or not empty? Or is this
generally impossible to got this information in the way I like to get it?
thanks for your answer
Frank
| |
| Greg Andrews 2005-11-27, 8:49 pm |
| Frank Dietrich <ablesoft@gmx.de> writes:
>
>I interpret it like: I can step into this directory but can't list the
>contained files. Ok, you say rm doesn't know about the file in foodir
>because it uses an system function. So I will alter my question: Is it
>possible to use this function from within an script. May be there's a
>core tool like 'stat' that only return empty or not empty? Or is this
>generally impossible to got this information in the way I like to get it?
>
The short answer is "no".
Rm is not asking the kernel whether the directory is empty. Rm is
asking the kernel to delete the directory. When the directory is
not empty, the kernel returns an error to rm indicating the reason
the delete cannot be performed (directory is not empty). Rm simply
prints the meaning of the error to you, the user.
-Greg
--
::::::::::::: Greg Andrews ::::: gerg@panix.com :::::::::::::
I have a map of the United States that's actual size.
-- Steven Wright
| |
| Enrique Perez-Terron 2005-11-28, 6:04 pm |
| On Sun, 27 Nov 2005 22:49:24 +0100, Frank Dietrich <ablesoft@gmx.de> wrote:
> Hi Janis,
>
> Janis Papanagnou wrote:
> The confusion starts with this testcase (I verified it again, maybe it's
> an Red Hat 7.3 oddity)
>
> ~>ls -A
> ~>mkdir foodir
> ~>chmod 100 foodir
> ~>ls -l
> d--x------ 2 Frank.Di users 4096 Nov 27 22:37 foodir
> ~>rm -rf foodir
> rm: cannot open directory `foodir/.': Permission denied
> ~>ls -A
> # foodir was deleted
> ~>mkdir foodir
> ~>touch foodir/barfile
> ~>chmod 100 foodir
> ~>ls -l
> d--x------ 2 Frank.Di users 4096 Nov 27 22:40 foodir
> ~>rm -rf foodir
> rm: cannot open directory `foodir/.': Permission denied
> rm: cannot remove directory `foodir': Directory not empty
> # foodir not deleted
>
> I interpret it like: I can step into this directory but can't list the
> contained files. Ok, you say rm doesn't know about the file in foodir
> because it uses an system function. So I will alter my question: Is it
> possible to use this function from within an script. May be there's a
> core tool like 'stat' that only return empty or not empty? Or is this
> generally impossible to got this information in the way I like to get it?
I checked on my FC4, and I only get
$ cd /tmp
$ mkdir foodir
$ touch foodir/barfile
$ chmod 100 foodir/
$ ls -ld foodir/
d--x------ 2 quique quique 4096 Nov 28 18:44 foodir/
$ rm -rf foodir
rm: cannot open directory `foodir': Permission denied
I don't get the two messages you get.
I also tried to run rm under strace, and found:
unlink("foodir") = -1 EISDIR (Is a directory)
open(".", O_RDONLY|O_LARGEFILE|O_DIRECTORY) = 3
lstat64("foodir", {st_mode=S_IFDIR|0100, st_size=4096, ...}) = 0
chdir("foodir") = 0
lstat64(".", {st_mode=S_IFDIR|0100, st_size=4096, ...}) = 0
open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECT
ORY) = -1 EACCES (Permission denied)
I further did man 2 rmdir, and found under the error codes:
ENOTEMPTY
pathname contains entries other than . and .. .
which probably explains how the second message was possible in your case.
-Enrique
|
|
|
|
|