Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir File.read returns empty data when accessing /proc/cpuinfo

Tags:

erlang

elixir

When running any thing like

File.read "/proc/cpuinfo"
>> {:ok, ""}

Same for equivalent erlang function. Is there some reason for this pattern?

like image 530
Supreet Sethi Avatar asked Apr 26 '15 07:04

Supreet Sethi


2 Answers

Like @José mentioned the proc fs is special since the file contents are generated on the fly. If you look at the file sizes in /proc you'll see that they have size 0.

I believe this is why the read function fails to return anything, the file is empty!

The workaround is to force-read a number of bytes anyway, in Erlang you can do:

{ok, FD} = file:open("/proc/cpuinfo", [read]).
file:read(FD, 1024).

To read all the content keep reading fixed number of bytes until EOF is returned by read.

like image 117
johlo Avatar answered Nov 09 '22 21:11

johlo


Entries in proc live under a special filesystem called procfs and I believe Erlang does not support reading from it. More info: https://unix.stackexchange.com/questions/121702/what-happens-when-i-run-the-command-cat-proc-cpuinfo

like image 45
José Valim Avatar answered Nov 09 '22 22:11

José Valim