Picon

require bug?

Hi all.
I was having this strange problem with my program.
An error happened in the code: require 'corr'

Error: error loading module 'corr' from file 
'app/geresim/prerock/lua/corr': cannot read 
app/geresim/prerock/lua/corr: Is a directory

This error does not happen in Windows but is happening in my Linux machine.
The problem is, the 'app/geresim/prerock/lua' directory contains both a 
'corr' directory and a 'corr.lua' file.
In loadlib.c, 'require' uses this 'readable' function to search 
package.path for existent lua files:

332 static int readable (const char *filename) {
333   FILE *f = fopen(filename, "r");  /* try to open file */
334   if (f == NULL) return 0;  /* open failed */
335   fclose(f);
336   return 1;
337 }

The problem is, apparently fopen succeeds when trying to open a 
directory but later on, when the file is actually fread from, this error 
happens.
I changed 'readable' so that it will fail if reading from the file fails:

332 static int readable (const char *filename) {
333   int ret = 1;
334   FILE *f = fopen(filename, "r");  /* try to open file */
335   if (f == NULL) return 0;  /* open failed */
(Continue reading)

Picon

Re: require bug?

> The problem is, the 'app/geresim/prerock/lua' directory contains both a 
> 'corr' directory and a 'corr.lua' file.

What is your package.path?

> The problem is, apparently fopen succeeds when trying to open a
> directory but later on, when the file is actually fread from, this
> error happens. I changed 'readable' so that it will fail if reading
> from the file fails:

That should work in modern Unix systems. luaL_loadfile has similar code.
In old Unix systems, directories could be read like ordinary files.
(Ah, the good old days...)

Picon

Re: require bug?

Luiz Henrique de Figueiredo wrote:
The problem is, the 'app/geresim/prerock/lua' directory contains both a 'corr' directory and a 'corr.lua' file.
What is your package.path?

something like ?;?.lua;?.lo

since 'corr' exists as a directory, the original version of 'readable' is returning 1.

The problem is, apparently fopen succeeds when trying to open a directory but later on, when the file is actually fread from, this error happens. I changed 'readable' so that it will fail if reading from the file fails:
That should work in modern Unix systems. luaL_loadfile has similar code. In old Unix systems, directories could be read like ordinary files. (Ah, the good old days...)
yeah.

Picon

Re: require bug?

 	Hi Frederico

>> What is your package.path?
> 
> something like ?;?.lua;?.lo
 	That's your problem.  Try ?.lua;?.lo instead (without the
preceeding `?;').
 		Tomás
Picon

Re: require bug?

>     Hi Frederico
>
>>> What is your package.path?
>>
>> something like ?;?.lua;?.lo
>     That's your problem.  Try ?.lua;?.lo instead (without the
> preceeding `?;').
>

Yeah, I know. I'm just questioning the semantics of the 
'readable(filename)' function. To me it seems to be used as "is filename 
a readable file?"
-- Fred

Picon

Re: require bug?

> I'm just questioning the semantics of the 'readable(filename)'
> function. To me it seems to be used as "is filename a readable file?"

Unfortunately, this cannot be done reliably in ANSI C, except by the
roundabout way you suggested.


Gmane