lua-users home
lua-l archive

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


On Thu, Sep 23, 2004 at 05:40:02AM +0200, Ashwin Hirschi wrote:
> If you're serious about checking for file/directory existence (and such), 
> you'd do well to look into Luiz' posix library. You can find it here:
> 
> 	http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
> 
> Among other things it'll give you functions like "access" and "stat". These 
> are more functional and in all likelihood more efficient as well.

Thanks, I'm aware of posix module.

The problem is that you are perfectly able to fopen() a directory (at
least on a Linux box), but you cannot neither fread() nor fgets() nor
getc() from it.  So fopen() check is not sufficient.

I noticed that lua-5.1-work1 base library is affected with this
problem.  Consider lcurses-0.1-devel module (available at
http://mega.ist.utl.pt/~tngd/lua/):

$ ls -dlF lcurses-0.1-devel/cui*
drwxr-x---  2 at at 544 Sep 21 04:18 lcurses-0.1-devel/cui/
-rw-r-----  1 at at 128 May 22 21:04 lcurses-0.1-devel/cui.lua
$ cd lcurses-0.1-devel
$ lua -lcui -e 'print(cui)'
$ lua: cannot read cui: Is a directory

Because LUA_PATH defaults to "?;?.lua" and because cui directory
matches first and passes fopen() check, cui.lua cannot be loaded.

I had no such problem with lua-5.0.2, though.

I believe the following patch should fix this particular problem:

$ lua -lcui -e 'print(cui)'
table: 0x80691b0
$


--- lua-5.1-work1/src/lib/lauxlib.c-	2004-08-30 22:35:14 +0400
+++ lua-5.1-work1/src/lib/lauxlib.c	2004-09-23 11:27:06 +0400
@@ -372,6 +372,7 @@
 LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
                                                       const char *path) {
   FILE *f;
+  int err;
   const char *p = path;
   for (;;) {
     const char *fname;
@@ -383,8 +384,11 @@
     lua_remove(L, -2);  /* remove path template */
     f = fopen(fname, "r");  /* try to read it */
     if (f) {
+      getc(f);
+      err = ferror(f);
       fclose(f);
-      return fname;
+      if (err == 0)
+        return fname;
     }
     lua_pop(L, 1);  /* remove file name */ 
   }


> Ashwin.
> -- 
> no signature is a signature.

-- 
Alexey Tourbin
ALT Linux Team