Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Reading a file before it has been completely written
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
David Mercer  
View profile  
 More options Mar 7 2012, 12:25 pm
From: "David Mercer" <dmer...@gmail.com>
Date: Wed, 7 Mar 2012 11:25:33 -0600
Local: Wed, Mar 7 2012 12:25 pm
Subject: [erlang-questions] Reading a file before it has been completely written

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-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogvall  
View profile  
 More options Mar 7 2012, 12:39 pm
From: Tony Rogvall <t...@rogvall.se>
Date: Wed, 7 Mar 2012 18:39:41 +0100
Local: Wed, Mar 7 2012 12:39 pm
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:

"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"

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Carlsson  
View profile  
 More options Mar 7 2012, 12:54 pm
From: Richard Carlsson <carlsson.rich...@gmail.com>
Date: Wed, 07 Mar 2012 18:54:08 +0100
Local: Wed, Mar 7 2012 12:54 pm
Subject: Re: [erlang-questions] Reading a file before it has been completely written
On 03/07/2012 06:25 PM, David Mercer wrote:

Hey, a user! I haven't had any reports about this module before (and the
fact that it's still in my development branch of eunit is more of a
historical accident; it's not shipped with OTP). I don't know of any
real issues with it though.

In this case, I think the problem is just the underlying file system
semantics. I presume it's Linux, and in Unix:y file systems a file can
be seen to exist and can be opened for reading as soon as it has been
created. Trying to fiddle with exclusive locks is probably always going
to have corner cases. The only techniques you can trust to practically
always work and be portable across file systems are directory creation
and file renaming. So what Tony suggested is likely to be the best
solution: create the file under another name or in a separate directory,
and when it's completely written, rename it.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mercer  
View profile  
 More options Mar 7 2012, 1:06 pm
From: "David Mercer" <dmer...@gmail.com>
Date: Wed, 7 Mar 2012 12:06:05 -0600
Local: Wed, Mar 7 2012 1:06 pm
Subject: Re: [erlang-questions] Reading a file before it has been completely written

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:t...@rogvall.se]
Sent: Wednesday, March 07, 2012 11:40 AM
To: David Mercer
Cc: erlang-questi...@erlang.org
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-questi...@erlang.org
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"

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mercer  
View profile  
 More options Mar 7 2012, 1:08 pm
From: "David Mercer" <dmer...@gmail.com>
Date: Wed, 7 Mar 2012 12:08:44 -0600
Local: Wed, Mar 7 2012 1:08 pm
Subject: Re: [erlang-questions] Reading a file before it has been completely written

On Wednesday, March 07, 2012, Richard Carlsson wrote:
> Hey, a user! I haven't had any reports about this module before (and
> the
> fact that it's still in my development branch of eunit is more of a
> historical accident; it's not shipped with OTP). I don't know of any
> real issues with it though.

It works fine.  If you know of a better one, I'd be OK switching.  This was
just the one that came up when I Googled.

> In this case, I think the problem is just the underlying file system
> semantics. I presume it's Linux, and in Unix:y file systems a file can
> be seen to exist and can be opened for reading as soon as it has been
> created. Trying to fiddle with exclusive locks is probably always going
> to have corner cases. The only techniques you can trust to practically
> always work and be portable across file systems are directory creation
> and file renaming. So what Tony suggested is likely to be the best
> solution: create the file under another name or in a separate
> directory,
> and when it's completely written, rename it.

I might go back to my renaming approach, which also had no failures during
testing.  I just attempt to rename the file to itself.  If it fails, I try
again 5 seconds later.

Thanks.

Cheers,

DBM

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Luna  
View profile  
 More options Mar 7 2012, 1:09 pm
From: Daniel Luna <dan...@lunas.se>
Date: Wed, 7 Mar 2012 13:09:32 -0500
Local: Wed, Mar 7 2012 1:09 pm
Subject: Re: [erlang-questions] Reading a file before it has been completely written
(Sorry, Richard for getting this multiple times, forgot to include the list)

Just as an added caveat to what Richard mentions: you probably want to
make sure that the temp file is created on the same file system as
your test directory.  A rename between file systems is really just a
copy and delete, with the exact same problems you had previously
(depending on OS I guess), while a rename within a file system is a
link/unlink with the contents being untouched.

/Daniel

On 7 March 2012 12:54, Richard Carlsson <carlsson.rich...@gmail.com> wrote:

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Carlsson  
View profile  
 More options Mar 7 2012, 1:30 pm
From: Richard Carlsson <carlsson.rich...@gmail.com>
Date: Wed, 07 Mar 2012 19:30:06 +0100
Local: Wed, Mar 7 2012 1:30 pm
Subject: Re: [erlang-questions] Reading a file before it has been completely written
On 03/07/2012 07:08 PM, David Mercer wrote:

> On Wednesday, March 07, 2012, Richard Carlsson wrote:

>> Hey, a user! I haven't had any reports about this module before (and
>> the
>> fact that it's still in my development branch of eunit is more of a
>> historical accident; it's not shipped with OTP). I don't know of any
>> real issues with it though.

> It works fine.  If you know of a better one, I'd be OK switching.  This was
> just the one that came up when I Googled.

No, I think it's pretty good and I don't know any other portable
implementation. I'd just like to add optional inotify support (and
whatever it's called on Windows) on supported platforms. Right now it
only works by polling. Which is actually good enough in a lot of cases.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mercer  
View profile  
 More options Mar 8 2012, 9:23 am
From: "David Mercer" <dmer...@gmail.com>
Date: Thu, 8 Mar 2012 08:23:30 -0600
Local: Thurs, Mar 8 2012 9:23 am
Subject: Re: [erlang-questions] Reading a file before it has been completely written

For closure here, I went back to my approach of attempting to rename the
file to itself before reading it.  I'll let y'all know if I encounter any
more corner cases.

Cheers,

DBM

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »