file:read_file/1 - bug or feature?

Matthias Lang matthias@REDACTED
Mon Oct 20 08:20:55 CEST 2003

 > On Sun, 19 Oct 2003, david wallin wrote:
 > > file:read_file("/proc//loadavg").
 > > and got '{ok, <<>>}' as a reply.


  os:cmd("cat /proc/loadavg").

Jimmy's earlier mail was right on the money about the size.

Given that the length of that file (according to fstat) is zero, the
null result isn't altogether unreasonable. If you trace the emulator
(e.g. with strace or strut):

  open("loadavg", O_RDONLY)               = 3
  fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
  read(3, "", 0)                          = 0
  close(3)                                = 0

you see what's happening. Here's what it looks like for a real file:

  open("books", O_RDONLY)                 = 3
  fstat64(3, {st_mode=S_IFREG|0664, st_size=114, ...}) = 0
  read(3, "The Anatomy of a High-Performanc"..., 114) = 114
  close(3)                                = 0
  write(0, "{ok,<<84,104,101,32,65,110,97,11"..., 111) = 111

People run into trouble every so often by assuming more unix-isms in
the 'file' module/ERTS than they actually have. E.g. you can't open
devices such as serial ports. It'd might help if the 'file' module
documentation had a warning about 'no funny file-like things' and I
should probably add a question to the FAQ about it too.


More information about the erlang-questions mailing list