[erlang-questions] Reading a file before it has been completely written

David Mercer dmercer@REDACTED
Wed Mar 7 19:06:05 CET 2012


I'm not the one writing the file.  I'm the one reading it.  I have no
control over the writing.

 

Thanks for the thoughts, though.

 

DBM

 

From: Tony Rogvall [mailto:tony@REDACTED] 
Sent: Wednesday, March 07, 2012 11:40 AM
To: David Mercer
Cc: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Reading a file before it has been completely
written

 

 

- Create and open a file with a temporary name.

-  Write the file content.

- Close the file.

- Rename the file to the name/place you want.

 

works ?

 

/Tony

 

On 7 mar 2012, at 18:25, David Mercer wrote:





While this isn't an Erlang-specific question, the problem arises from my
using Richard Carlsson's
file_monitor(https://github.com/richcarl/eunit/blob/master/src/file_monitor.
erl), which sends messages when a file or directory is changed.  I have
found that it is not unusual to get a message about a new file before the
file has been completely written.

 

I had thought that by doing a file:open(Filepath, [read]) and making sure I
got back {ok, _} rather than{error, eacces} I could avoid those cases, but
that approach has failed for me: this morning, I got back {ok, _}, but the
file was not completely written yet.

 

Another approach I tried was to attempt to obtain an exclusive lock (I think
it was file:open(Filepath, [read, exclusive])), but in my testing I came
across the bizarre scenario where I would copy a file into the monitored
directory, the file_monitor would send the message, but the Erlang process
that does the file-open didn't see it, so created the file (the
documentation says it creates the file if it does not exist), and then I got
a message in my window where I was copying that the file already exists, do
I want to overwrite it.

 

Another approach I tried was renaming the file to itself.  All my tests
indicated that that approach would work, but all my tests also indicated
that just doing the file:open(Filepath, [read]) would work, too, so I chose
it, as it seemed cleaner.  I could revert to the rename approach, but I'm
not even sure now that that will work.

 

I imagine others among us have encountered this issue, and rather than
reinvent the wheel, what is the favored approach to handling this issue?

 

Cheers,

 

David Mercer

 

 

_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions

 

"Installing applications can lead to corruption over time. Applications
gradually write over each other's libraries, partial upgrades occur, user
and system errors happen, and minute changes may be unnoticeable and
difficult to fix"

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120307/aaeea75a/attachment.htm>


More information about the erlang-questions mailing list