[erlang-questions] file:read_file won't read from /proc

Richard O'Keefe <>
Fri Mar 5 03:00:47 CET 2010


On Mar 5, 2010, at 5:17 AM, Attila Rajmund Nohl wrote:

> 2010/3/4, Jachym Holecek <>:
> [...]
>> The read_file/1 thing check file size and then attempts to read  
>> exactly
>> that many bytes. The size of /proc/cpuinfo is zero[*], thus  
>> read_file/1
>> correctly reads zero bytes and represents the result with empty  
>> binary.
>>
>> Gotta love UN*X for its sense of consistency, right? ;-)
>
> Even the classic UNIX utilities can't get it right occasionally:
>
> : ~>wc /proc/sys/kernel/panic
> 1 1 2 /proc/sys/kernel/panic
> : ~>wc -c /proc/sys/kernel/panic
> 0 /proc/sys/kernel/panic
> : ~>cat /proc/sys/kernel/panic | wc -c

Here's what the Single Unix Specification, 4th edition says:
     [st_size is set for "shared memory object"s and "typed memory  
object"s]

     For symbolic links, the ... value of the st_size member shall be  
set
     to the length of the pathname contained in the symbolic link not
     including any terminating null byte.

     For all other file types defined in this volume of POSIX.1-2008,
     the structure members st_mode, st_ino, st_dev, st_uid, st_gid,
     st_atim, st_ctim, and st_mtim shall have meaningful values and
     the value of the member st_nlink shall be set to the number of
     links to the file.

What's missing is *any* statement at all about what st_size should
be for anything other than a symbolic link, a shared memory object,
or a typed memory object.

If instead of looking at 'stat' you look at '<sys/stat.h>', you find

off_t st_size
     For regular files, the file size in bytes.
     For symbolic links, the length in bytes of the pathname
     contained in the symbolic link.
     [SHM]
-------------- next part --------------
  For a shared memory object, the length in bytes. 
-------------- next part --------------

     [TYM]
-------------- next part --------------
  For a typed memory object, the length in bytes. 
-------------- next part --------------

     For other file types, the use of this field is unspecified.
Now that tells us what st_size means *if* it is set, but the
specification of stat() doesn't that it it *is* set.  And this has
not changed since SUSv3.
I don't know what kind of file /proc/cpuinfo is, since neither
Solaris nor Mac OS X has it, but if it's _not_ a regular file,
symbolic link, shared memory object, or typed memory object,
its st_size doesn't have to mean anything.

If it _is_ reported as a regular file, and its st_size does not
match its contents, that's nothing but a bug.



More information about the erlang-questions mailing list