Search code examples
linuxacpi

Determining length of ACPI state file


I am writing a tool to log battery charge to a CSV file (I know it'd be a bash one-liner but this is yet another Common Lisp learning exercise for me).

My plan is to slurp the entire battery state file into memory, as it's guaranteed to be a tiny file. But file-length is consistently returning 0 for the file size, while working normally on other files:

* (defun stream-length (path)
    (with-open-file (stream path) 
            (file-length stream)))

STYLE-WARNING: redefining COMMON-LISP-USER::STREAM-LENGTH in DEFUN

STREAM-LENGTH
* (stream-length "/home/duncan/foo")

4
* (stream-length "/proc/acpi/battery/BAT0/state")

0

It turns out it's not only SBCL; Perl behaves the same way:

$ perl -e 'print -s "/home/duncan/foo"; print "\n"'
4
$ perl -e 'print -s "/proc/acpi/battery/BAT0/state"; print "\n"'
0

But the file definitely contains something:

$ cat /proc/acpi/battery/BAT0/state
present:                 yes
capacity state:          ok
charging state:          discharging
present rate:            0 mW
remaining capacity:      3945 mWh
present voltage:         10800 mV

So I'm presuming there's something special about the state file. Could someone please enlighten me as to what it is?


Solution

  • Most /proc files are special. Their contents don't exist stored anywhere, but get produced on demand when a process opens and reads a given file. And the size of the contents may be different on every read. So, they generally don't advertise any particular size.

    The correct approach is to simply open the file and read it until EOF.