• Re: Is there an efficient way to get file length w/o reading file?

    From B. Pym@21:1/5 to Pascal Bourguignon on Mon Sep 2 19:58:56 2024
    Pascal Bourguignon wrote:

    An intelligent implementation could avoid reading the file.
    But otherwise, the answer is that it's not possible portably.
    Drop down to the POSIX API.

    How does one do that? Is there an example somewhere?

    (defun file-length-in-bytes (file)
    ;; Note the "EXT" implementation specific package.
    (with-open-stream (in (ext:run-program "/bin/ls"
    :arguments (list "-l" (namestring file))
    :output :stream))
    (read in)(read in)(read in)(read in)(read in)))

    Alternatively, one could use FFI:

    (defun file-length-in-bytes (file)
    (multiple-value-bind (res stat) (linux:stat (namestring file))
    (linux:|stat-st_size| stat)))

    Gauche Scheme:

    (use file.util)
    (file-size "data.txt")


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)