File.link() error message

Why does File.link() report that the source file exists? It really
ought to only report that the destination file exists. It isn't an
error for the source to exist. If it didn't, we'd get ENOENT, not
EEXIST.

irb(main):018:0> File.open("foop", "w") {}
=> nil
irb(main):019:0> File.open("barp", "w") {}
=> nil
irb(main):020:0> File.link("foop", "barp")
Errno::EEXIST: File exists - foop or barp
  from (irb):20:in `link'
  from (irb):20
  from :0

-Loris

ENOENT is returned only if

   harp:~ > man 2 link
   ...
   SYNOPSIS
          #include <unistd.h>

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

   ...

          ENOENT A directory component in oldpath or newpath does not exist or
                        is a dangling symbolic link.

otherwise ENOEXIST is returned as this shows

   harp:~ > cat a.c
   #include <stdlib.h>
   #include <stdio.h>
   #include <string.h>
   #include <errno.h>
   #include <unistd.h>
   main (int argc, char **argv)
   {
       char command[256];
         link (argv[1], argv[2]);
           sprintf (command, "ruby -e'p(SystemCallError::new(%d))'", errno);
             system(command);
   }

   harp:~ > gcc a.c

   harp:~ > ls foo
   ls: foo: No such file or directory

   harp:~ > ls bar
   ls: bar: No such file or directory

   harp:~ > a.out foo bar
   #<Errno::ENOENT: No such file or directory>

ENOENT is returned if we are dealing with bad directories only

   harp:~ > a.out /no/such/path/foo bar
   #<Errno::ENOENT: No such file or directory>

   harp:~ > a.out foo /no/such/path/bar
   #<Errno::ENOENT: No such file or directory>

but two good dirs always result in ENOEXIST

   harp:~ > a.out /usr/foo /usr/bar
   #<Errno::ENOENT: No such file or directory>

and a subsequent stat to determine which pathname did not exist would be a
race condition so i think you are stuck with this.

cheers.

-a

···

On Sun, 12 Jun 2005, a slow loris wrote:

Why does File.link() report that the source file exists? It really
ought to only report that the destination file exists. It isn't an
error for the source to exist. If it didn't, we'd get ENOENT, not
EEXIST.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso

===============================================================================

Hi,

Why does File.link() report that the source file exists? It really
ought to only report that the destination file exists. It isn't an
error for the source to exist. If it didn't, we'd get ENOENT, not
EEXIST.

I guess it's a matter of description.

"Errno::EEXIST: File exists - foop or barp" means that an EEXIST error
happened either for foop or barp. But I don't want to treat every
kind of system call errors individually.

If you (or anyone else) have any suggestion for better description, we
are always open.

              matz.

···

In message "Re: File.link() error message" on Sun, 12 Jun 2005 12:25:30 +0900, lorisx@gmail.com (a slow loris) (with) (poison) (elbows) writes: