rename() doesn't return -1 when it does not really rename

rename() doesn't return -1 when it does not really rename

Post by L » Wed, 05 Mar 2003 19:28:29



Hello there.

I tested the following in hp unix as well as Red hat linux.

I expect rename to fail in the following scenario.

===C program====
main ()
{
printf ("rename returns %d\n",
rename ("a", "b"));

Quote:}

let us say the compiled output a.out is available
======================

$ touch a
$ ln a b
$ mv a b <== This fails and echo $? gives 1
$ ./a.out <== The output is "rename returns 0"
Both the files are availble as it is. Thus rename() in a.out should
have returned -1 as per the man page. Why is this not happening?

How does "mv" command identifies this problem and exits with 1?

Any sure way to rename "a" to "b" irrespective of what "b" is, using a
C program?

Thank you,
LV
04-Mar-2003

 
 
 

rename() doesn't return -1 when it does not really rename

Post by Chuck Dillo » Wed, 05 Mar 2003 23:54:45



> Hello there.

> I tested the following in hp unix as well as Red hat linux.

> I expect rename to fail in the following scenario.

> ===C program====
> main ()
> {
> printf ("rename returns %d\n",
> rename ("a", "b"));
> }
> let us say the compiled output a.out is available
> ======================

> $ touch a
> $ ln a b
> $ mv a b <== This fails and echo $? gives 1
> $ ./a.out <== The output is "rename returns 0"
> Both the files are availble as it is. Thus rename() in a.out should
> have returned -1 as per the man page. Why is this not happening?

rename != mv

Although the RH manpage/infopage doesn't seem to mention it, after a
quick scan, the Solaris manpage specifically states:

      If old and new both refer to the  same  existing  file,  the
      rename() function returns successfully and performs no other
      action.

Quote:

> How does "mv" command identifies this problem and exits with 1?

man stat/fstat

Quote:

> Any sure way to rename "a" to "b" irrespective of what "b" is, using a
> C program?

In general you have to figure out what "b" is and blow it away, if you
can.  If it's a file you unlink it.  If it's a directory you have to
decide what to do with it.  Once you've removed whatever it is the name
is available.

-- ced

Quote:

> Thank you,
> LV
> 04-Mar-2003

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

rename() doesn't return -1 when it does not really rename

Post by Marc Rochkin » Thu, 06 Mar 2003 00:19:26



[snip]

Quote:> Both the files are availble as it is. Thus rename() in a.out should
> have returned -1 as per the man page. Why is this not happening?

My documentation says: "If the old argument and the new argument resolve to
the same existing file, rename() shall return successfully and perform no
other action."

Quote:> How does "mv" command identifies this problem and exits with 1?

On a couple of systems I tried (Solaris and FreeBSD), the behavior of mv
when the files are identical varies. Solaris complained about it and gave an
exit code of 2; FreeBSD didn't complain and gave an exit code of 0.

Quote:

> Any sure way to rename "a" to "b" irrespective of what "b" is, using a
> C program?

You need to more precisely define "rename," expecially what you want to
happen with existing hard and symbolic links.
--Marc
 
 
 

rename() doesn't return -1 when it does not really rename

Post by Rekcuf Ykaen » Thu, 06 Mar 2003 06:59:37


On Tue, 4 Mar 2003 08:19:26 -0700, "Marc Rochkind"




> [snip]
> > Both the files are availble as it is. Thus rename() in a.out should
> > have returned -1 as per the man page. Why is this not happening?

> My documentation says: "If the old argument and the new argument resolve to
> the same existing file, rename() shall return successfully and perform no
> other action."

> > How does "mv" command identifies this problem and exits with 1?

> On a couple of systems I tried (Solaris and FreeBSD), the behavior of mv
> when the files are identical varies. Solaris complained about it and gave an
> exit code of 2; FreeBSD didn't complain and gave an exit code of 0.

> > Any sure way to rename "a" to "b" irrespective of what "b" is, using a
> > C program?

> You need to more precisely define "rename," expecially what you want to
> happen with existing hard and symbolic links.

> --Marc

from the mv.c source....

static int is_real_dir (const char *path) {
  struct stat stats;

  return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);

Quote:}

Yes this example is not what you asked for, but I am sure you can
modify it to something that suits your specific needs..

J.

 
 
 

rename() doesn't return -1 when it does not really rename

Post by Lew Pitche » Fri, 07 Mar 2003 10:50:55



> Hello there.

> I tested the following in hp unix as well as Red hat linux.

> I expect rename to fail in the following scenario.

> ===C program====
> main ()
> {
> printf ("rename returns %d\n",
> rename ("a", "b"));
> }
> let us say the compiled output a.out is available
> ======================

> $ touch a
> $ ln a b
> $ mv a b <== This fails and echo $? gives 1
> $ ./a.out <== The output is "rename returns 0"
> Both the files are availble as it is. Thus rename() in a.out should
> have returned -1 as per the man page. Why is this not happening?

The manpage does not agree with you. See below...

SYNOPSIS
        #include <stdio.h>

        int rename(const char *oldpath, const char *newpath);

DESCRIPTION
        rename  renames  a  file, moving it between directories if
        required.

        Any other  hard  links  to  the  file  (as  created  using
        link(2)) are unaffected.

        If  newpath  already exists it will be atomically replaced
        (subject to a few conditions - see ERRORS below), so  that
        there  is  no point at which another process attempting to
        access newpath will find it missing.

        [snip]

Quote:> How does "mv" command identifies this problem and exits with 1?

rename doesn't recognize that there is a problem. It _replaces_ the
existing "b" file with the existing "a" file, renamed to "b".

Quote:> Any sure way to rename "a" to "b" irrespective of what "b" is, using a
> C program?

rename(2)

--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

 
 
 

1. sockets. send doesn't return -1

hallo,
I have a trouble with sockets under LINUX.

I've opened a TCP-socket:
socket(PF_INET, SOCK_STREAM, 0)

write, send etc. does work without problems.
But if the physical communication (cable) between two partners is
broken,
no one of this functions notice it! They doesn't return error!

I tried to configure the socket:
setsockopt(Descr, SOL_SOCKET, SO_KEEPALIVE, ...)
setsockopt(Descr, SOL_SOCKET, SO_SNDBUF, ...)

and I ignore SIGPIPE
but send() doesn't return -1 as I thought.

has anybody experience in this subject?
gemal

2. root login with telnet into Redhat linux 2.0.29 ?

3. 2.2.18+ adaptec 29160 (scsi0:-1:-1:-1) Referenced SCB 0 not valid

4. HELP!:mars_nwe(netware emulator install)

5. bug in ext3 htree rename: doesn't delete old name, leaves ino with bad nlink

6. Loosers on Linux Advocacy!

7. Solaris -1.-1.-1

8. Counting byte traffic of modem

9. poll() returns -1 but errno not set!

10. MAC 'fetch' renames '.htaccess'

11. sem_timedwait returns 110 not -1

12. Why doesn't kill(pid,0) = -1?

13. Can't get 'rename' command to work!