[erlang-questions] file:read_file/2 bug?
Per Hedeland
per@REDACTED
Thu May 1 20:43:56 CEST 2008
"Christian S" <chsu79@REDACTED> wrote:
>
>I dont understand why everything is done in efile_openfile that is
>done, but my guess is that if one first open() the file then use
>fstat() on the file descriptor, then the proc filesystem would report
>nonzero file length.
Your guess is wrong:
$ cat stdin_size.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
main()
{
struct stat buf;
fstat(0, &buf);
printf("size=%d\n", buf.st_size);
}
$ ./stdin_size < /proc/self/environ
size=0
>The linux kernel generates the proc file's content as a snapshot at
>the instance the file is opened. Before open() it doesnt know how long
>the content will be.
AFAIK, the snapshotting happens when you read(2) the file, which can
have a variety of interesting effects (probably not in this particular
case, but in general). Basically, the Linux /proc file system is handy
for the "I just want to have a peek at some system stuff" scenario, but
if you want to use it programmatically and get dependable results, you
need to be *very* careful. I don't think any of the Erlang file(3)
functionality is quite up to the general task.
--Per
More information about the erlang-questions
mailing list