lua-users home
lua-l archive

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


It was thus said that the Great Jerome Vuarand once stated:
> On Tue, 21 Jan 2020 at 05:38, Sean Conner <sean@conman.org> wrote:
> >   Okay, let's see if the community can just come together to define the
> > minimum viable module for directory functions.  I'm looking at three
> > different modules that implement such functionality, LuaFileSystem, luaposix
> > and my own org.conman.fsys module.
> >
> > [...]
> >
> >         okay = mkdir(name)
> >         okay = rmdir(name)
> >         okay = getcwd()
> >         okay = setcwd(name)
> >         cat = list([name])
> >         cat = expand(glob)
> >         fun,state,var = glist([name])
> >         fun,state,var = gexpand(glob)
> >         file = filename(name)
> >         dir = dirname(name)
> >
> >   And that's it.  Additional information about files (like timestamps, size,
> > type) is not in the scope for this module---this is just about directory
> > manipulation.  Now imaging having to go through all this for other possible
> > batteries, like additional file operations, network API, events, etc.
> 
> A couple obvious omissions (to me) are a way to test if a
> file/directory exists (io.open might work on files, does it work
> portably on directories?), and a way to tell if a filename is a
> regular file, a directory (or something else). It could be a combined
> fs.type that says nil (doesn't exist), "file", "directory", or
> "other". I usually use lfs.attributes(path, 'mode') for these.

  I was thinking about that, and I initially had a function like that, but
as I was writing it, I felt that was more a "file" type operation. 
Afterwards, I realized I needed such a function.  My proposal is:

	info = status(name)

		Returns a table (or nil on error) with the following fields:

			* name - name of file (not really needed, but nice to have)
			* type - string
					'file'
					'directory'
					some other system specific type
			* size - integer, size of file
			* time - timestamp on file

> And on the other hand expand, gexpand, filename and dirname feel
> redundant. 

  Yes, you really only need one of expand (returns a table) or gexpand
(returns an interator).  The same with list and glist.  I included both but
if I were to remove one, I'd remove the ones that return a table.  You can
construct a table from the interator (both of which don't create Lua
tables).

> I can see how they can be useful (I'd probably use them),
> but that's basic string manipulation which Lua already excels at.

  filename() and dirname() do complimentary things---if you want a filename
from a path, filename() provides a way to do it without having to know
details; same with getting a directory name from a pathname.

  -spc