lua-users home
lua-l archive

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

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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).

AltStyle によって変換されたページ (->オリジナル) /