confusing error message in os.rename (bug?)

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

confusing error message in os.rename (bug?)

Wolfram Ladurner
$ touch testfile
$ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
lua: <command line>:1: testfile: No such file or directory
stack traceback:
         [C]: in function `assert'
         <command line>:1: in main chunk
         [C]: ?

The error message looks like the testfile does not exist, while it is
the new name that is causing the error.

--
Wolfram Ladurner

Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Rob Kendrick-2
On Thu, Oct 18, 2012 at 03:00:36PM +0200, Wolfram Ladurner wrote:

> $ touch testfile
> $ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
> lua: <command line>:1: testfile: No such file or directory
> stack traceback:
>         [C]: in function `assert'
>         <command line>:1: in main chunk
>         [C]: ?
>
> The error message looks like the testfile does not exist, while it
> is the new name that is causing the error.

Lua has no way of knowing this, it is just reporting the error that the
ANSI C function has signalled.

B.

Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Luiz Henrique de Figueiredo
In reply to this post by Wolfram Ladurner
> $ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
> lua: <command line>:1: testfile: No such file or directory

The message comes from the OS. There is nothing we can do about it.
Try it on the command line:
        mv testfile /does/not/exist
You'll get
        mv: rename testfile to /does/not/exist: No such file or directory

Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Wolfram Ladurner
In reply to this post by Rob Kendrick-2
Am 18.10.2012 15:04, schrieb Rob Kendrick:

> On Thu, Oct 18, 2012 at 03:00:36PM +0200, Wolfram Ladurner wrote:
>> $ touch testfile
>> $ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
>> lua: <command line>:1: testfile: No such file or directory
>> stack traceback:
>>          [C]: in function `assert'
>>          <command line>:1: in main chunk
>>          [C]: ?
>>
>> The error message looks like the testfile does not exist, while it
>> is the new name that is causing the error.
>
> Lua has no way of knowing this, it is just reporting the error that the
> ANSI C function has signalled.
>
> B.
>

I know, but Lua always displays the old name at the beginning of the
error message, regardless of whether the old or the new name cause the
error.  IMHO it would be better to show no name at all, unless it can be
determined which of them is responsible for the error.

--
Wolfram Ladurner


Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Roberto Ierusalimschy
> I know, but Lua always displays the old name at the beginning of the
> error message, regardless of whether the old or the new name cause
> the error.  IMHO it would be better to show no name at all, unless
> it can be determined which of them is responsible for the error.

It cannot :(  The error codes from 'rename' are the same; for instance:

   EACCES Write  permission is denied for the directory containing oldpath
          or newpath, or, search permission  is  denied  for  one  of  the
          directories in the path prefix of oldpath or newpath, or oldpath
          is a directory and does not allow write  permission  (needed  to
          update the ..  entry).  (See also path_resolution(7).)

   ENOENT The link named by oldpath does not exist; or, a directory compo‐
          nent  in  newpath  does  not exist; or, oldpath or newpath is an
          empty string.

Probably not to show any file name is the best approach.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Wolfram Ladurner
In reply to this post by Luiz Henrique de Figueiredo
Am 18.10.2012 15:05, schrieb Luiz Henrique de Figueiredo:
>> $ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
>> lua: <command line>:1: testfile: No such file or directory
>
> The message comes from the OS. There is nothing we can do about it.
> Try it on the command line:
> mv testfile /does/not/exist
> You'll get
> mv: rename testfile to /does/not/exist: No such file or directory
>

You are right, "No such file or directory" comes from the OS, but the
"testfile: " comes from Lua.  I can live with that, but I still think
it's misleading.

(mv also shows different messages, depending on whether the old or the
new name is incorrect:)

$ mv testfile /does/not/exists
mv: cannot move `testfile' to `/does/not/exists': No such file or directory
$ mv /does/not/exist testfile
mv: cannot stat `/does/not/exist': No such file or directory

--
Wolfram Ladurner


Reply | Threaded
Open this post in threaded view
|

Re: confusing error message in os.rename (bug?)

Sean Conner
It was thus said that the Great Wolfram Ladurner once stated:

> Am 18.10.2012 15:05, schrieb Luiz Henrique de Figueiredo:
> >>$ lua -e 'assert( os.rename( "testfile", "/does/not/exist" ) )'
> >>lua: <command line>:1: testfile: No such file or directory
> >
> >The message comes from the OS. There is nothing we can do about it.
> >Try it on the command line:
> > mv testfile /does/not/exist
> >You'll get
> > mv: rename testfile to /does/not/exist: No such file or directory
> >
>
> You are right, "No such file or directory" comes from the OS, but the
> "testfile: " comes from Lua.  I can live with that, but I still think
> it's misleading.
>
> (mv also shows different messages, depending on whether the old or the
> new name is incorrect:)
>
> $ mv testfile /does/not/exists
> mv: cannot move `testfile' to `/does/not/exists': No such file or directory
> $ mv /does/not/exist testfile
> mv: cannot stat `/does/not/exist': No such file or directory

  What could be happening here is:

        char        *src = argv[1];
        char        *dest = argv[2];
        char        *dir;
        struct stat  status;

        dir = dirname(argv[2]);
        if (stat(dir,&status) < 0)
        {
          fprintf(
                stderr,
                "%s: cannot move '%s' to '%s': %s\n",
                argv[0],
                argv[1],
                argv[2],
                strerror(errno)
          );
          exit(EXIT_FAILURE);
        }

        if (stat(argv[1],&status) < 0)
        {
          fprintf(
                stderr,
                "%s: cannot stat '%s': %s\n",
                argv[0],
                argv[1],
                strerror(errno)
          );
          exit(EXIT_FAILURE);
        }

i.e. the "mv" command is manually checking the source and destination prior
to calling the rename() function for better error checking.  It's also
probably checks to see if the destination is a directory and if so, creates
a destination filename (since rename() will fail if given a file and a
directory).  [1].

  -spc (Leaky abstractions and all that ... )

[1] "mv" does a whole bunch of functionality that rename() lacks, such
        as moving a file between filesystems (say, an NFS mount and a local
        disk, which becomes a copy, delete and touch).