file:read_file/1 - bug or feature?

Raimo Niskanen raimo@REDACTED
Mon Oct 20 09:16:57 CEST 2003


The reason the file driver does this is that it is about to return a 
binary, and want to know how large binary to allocate to read the file 
straight into. It is an optimization.

It is correct that the file driver (and therefore the file module) is 
only intended for regular files.

-- 
/ Raimo Niskanen, Erlang/OTP, Ericsson AB



Matthias Lang wrote:
>  > On Sun, 19 Oct 2003, david wallin wrote:
>  > 
>  > > file:read_file("/proc//loadavg").
>  > > and got '{ok, <<>>}' as a reply.
> 
> Try
> 
>   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.
> 
> Matthias




More information about the erlang-questions mailing list