[comp.unix.aix] Why remove

ghe@physics.orst.edu (Guangliang He) (05/04/91)

The title pretty much said it all. The remove() system call fails to remove
the sybolic links. The man page of remove() did mention anything special 
about symolic links. 

Here is a little program shows the problem:
------
#include <stdio.h>

main(int argc, char **argv)
{
    if (remove(argv[1]))
	fprintf(stderr, "can't remove %s\n", argv[1]);
}
------

It works fine (no error message) with regular files but prints the 
'can't remove.." message on symbolic links. 

Is it a bug or 'work as designed' :-(???

---
  Guangliang He                |   If anything can go wrong, it will.
  ghe@physics.orst.edu         |            -- Murphy's Law

shore@theory.tn.cornell.edu (Melinda Shore) (05/04/91)

In article <1991May03.174126.13324@lynx.CS.ORST.EDU> ghe@physics.orst.edu writes:
>The remove() system call fails to remove
>the sybolic links.

It should work as advertised.  I *strongly* recommend checking errno
after the system call fails either by using perror(), or looking at the
value of errno yourself.  You do want to know why remove() is failing,
don't you?
-- 
                    Software longa, hardware brevis
Melinda Shore - Cornell Information Technologies - shore@theory.tn.cornell.edu

ghe@physics.orst.edu (Guangliang He) (05/04/91)

In article <1991May3.185244.1275@batcomputer.tn.cornell.edu>, shore@theory.tn.cornell.edu (Melinda Shore) writes:
|> In article <1991May03.174126.13324@lynx.CS.ORST.EDU> ghe@physics.orst.edu writes:
|> >The remove() system call fails to remove
|> >the sybolic links.
|> 
|> It should work as advertised. 

Which way??? should remove or not??? TFM did not mention a single word on
symbolic links.

|>                               I *strongly* recommend checking errno
|> after the system call fails either by using perror(), or looking at the
|> value of errno yourself.  You do want to know why remove() is failing,
|> don't you?

Yes. I DO want to know why. But TFM says nothing about errno. It only
mentioned that 0 is returned upon successful completion, and non-zero
otherwise. 

When I try to call remove(b) ('b' is a symbolic link to a directory 'a'),
perror prints:

remove: Not a directory

I still don't know why.

|> -- 
|>                     Software longa, hardware brevis
|> Melinda Shore - Cornell Information Technologies - shore@theory.tn.cornell.edu


---
  Guangliang He                |   If anything can go wrong, it will.
  ghe@physics.orst.edu         |            -- Murphy's Law

shore@theory.tn.cornell.edu (Melinda Shore) (05/04/91)

In article <1991May03.202209.432@lynx.CS.ORST.EDU> ghe@physics.orst.edu writes:
>Which way??? should remove or not??? TFM did not mention a single word on
>symbolic links.

I just checked MFM and it says that "if the named file is a symbolic
link to another file or directory, unlink [aka remove - ms] removes
the symbolic link, not the file or directory to which it refers."
Note the lengthy listings of error conditions at the end of the man
page.

> But TFM says nothing about errno. It only
>mentioned that 0 is returned upon successful completion, and non-zero
>otherwise. 

errno is a Unix thing, and you need to be familiar with it if you're
writing code that makes system calls and you want that code to be
reasonably robust.

>When I try to call remove(b) ('b' is a symbolic link to a directory 'a'),
>perror prints:
>remove: Not a directory

Probably because there's something wrong with either the pathname
you're passing to your program or the pathname your symlink points
to.  Does your program have the same problem with all symlinks?
-- 
                    Software longa, hardware brevis
Melinda Shore - Cornell Information Technologies - shore@theory.tn.cornell.edu

wangh@beasley.CS.ORST.EDU (Haiyan Wang) (05/04/91)

In article <1991May3.212509.6542@batcomputer.tn.cornell.edu> shore@theory.tn.cornell.edu (Melinda Shore) writes:
>In article <1991May03.202209.432@lynx.CS.ORST.EDU> ghe@physics.orst.edu writes:
>>Which way??? should remove or not??? TFM did not mention a single word on
>>symbolic links.
>
>I just checked MFM and it says that "if the named file is a symbolic
>link to another file or directory, unlink [aka remove - ms] removes
>the symbolic link, not the file or directory to which it refers."
>Note the lengthy listings of error conditions at the end of the man
>page.

Are you sure you're reading AIX3.1 manual of remove()? I saved the man
page into a file and searched the file with grep and could not find the word
symbolic. Maybe the man page on our system is screwed up somehow? :-(. 

>
>> But TFM says nothing about errno. It only
>>mentioned that 0 is returned upon successful completion, and non-zero
>>otherwise. 
>
>errno is a Unix thing, and you need to be familiar with it if you're
>writing code that makes system calls and you want that code to be
>reasonably robust.
>
>>When I try to call remove(b) ('b' is a symbolic link to a directory 'a'),
>>perror prints:
>>remove: Not a directory
>
>Probably because there's something wrong with either the pathname
>you're passing to your program or the pathname your symlink points
>to.  Does your program have the same problem with all symlinks?

It is not likely to have something wrong with the pathname. I used the same
program on other files and work fine. And same problem happened on different
symbolic links. 

>-- 
>                    Software longa, hardware brevis
>Melinda Shore - Cornell Information Technologies - shore@theory.tn.cornell.edu

Guangliang He
ghe@physics.orst.edu

I'm using someonelse's account over the weekend.

web@farpoint.austin.ibm.com (Bill Baker) (05/06/91)

||> In article <1991May03.202209.432@lynx.CS.ORST.EDU> ghe@physics.orst.edu writes:
||> >The remove() system call fails to remove
||> >the sybolic links.
||> 
|
|When I try to call remove(b) ('b' is a symbolic link to a directory 'a'),
|perror prints:
|
|remove: Not a directory
|
|I still don't know why.

Remove is implemented in libc.  It calls unlink or rmdir to remove files or
directories.  To determine which to do, it calls stat.  And therein lies
the problem.  If it's a symlink to a directory, stat follows the link and
the code tries to rmdir the symlink.  Naturally, the rmdir system call fails
with ENOTDIR.  This is also a problem if the object is a dangling symbolic
link.  The code should use lstat, not stat.  BUG!

web
---
-- 
Bill Baker             Internet: web@glasnost.austin.ibm.com
IBM PSP                AWD net: web@farpoint.austin.ibm.com
11400 Burnet Rd.       VNET: WEBAKER AT AUSVMQ
Austin, TX; 78758-2502