Search code examples
common-lispfilepathlispworks

How to use LispWorks' fast-directory-files with fdf-handle-* functions?


LispWorks' fast-directory-files with fdf-handle-* functions seem very promising. The Fast access to files in a directory page in the LispWorks manual says,

fast-directory-files gives a faster way to access files than directory, especially in situations when you need to filter based on simple features such as size and access time, or filter based on the name in a more complex way than directory can.

I am trying to understand how they can be used, but I am meeting with a bit of opacity in documentation, beside the fact I am quite new to Common Lisp.

Let's say as an example, one would wish to fetch the file size of a file, using fdf-handle-size. The detailed manual page offers no examples and the text seems a bit terse. For example,

The fdf-handle can be accessed by the following readers. Functions named in parentheses would return the same value when called on the full path of the file:

fdf-handle-size returns the size of the file in bytes.

[etc.]

Notes says,

The fdf-handle can be used only within the dynamic scope of the callback to which it was passed.

By trial and error I got to this point (of course wrong):

CL-USER 1 > (let (file-size)
  (fast-directory-files "/temp/a.txt"
                        #'(lambda (path handle)
                           (push (fdf-handle-size handle) file-size))))
("save.lisp" "a.txt" "a.lisp")

How could one get the file size for /temp/a.txt? More importantly, how one is supposed to use LispWorks' fast-directory-files with fdf-handle-* functions in general?


Solution

  • The Snippet above does not return the output of the callback. Hence

    (let (file-size)
      (fast-directory-files "/temp/a.txt"
                            #'(lambda (path handle)
                               (push (fdf-handle-size handle) file-size)))
      file-size)
    

    Will result in a list of file sizes for all files in the directory.

    Note also that fast-directory-files accepts a dir-pathname - the intention is to deal with all files in a directory hence the a.txt is ignored.

    If you just need the size of a single file - open it and use file-length (paying attention to the element size of course).