# Atomic File.copy from Linux to Windows shared drive

**URL:** <https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675>\
**Category:** ruby-talk\
**Created:** [23 May 2006 21:26 UTC](https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675 "2006-05-23T21:26:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [23 May 2006 21:26 UTC](https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675/1 "2006-05-23T21:26:19Z")

</div>

Whoops! Yeah, that worked. I still don't know what will happen if the  
share goes down, though.

Regards,

Dan

This communication is the property of Qwest and may contain confidential or  
privileged information. Unauthorized use of this communication is strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and destroy  
all copies of the communication and any attachments.

> **···**
>
> > -----Original Message-----  
> > From: frank.cameron@gmail.com  
> > [mailto:frank.cameron@gmail.com] On Behalf Of brabuhr@gmail.com  
> > Sent: Tuesday, May 23, 2006 3:18 PM  
> > To: ruby-talk ML  
> > Subject: Re: Atomic File.copy from Linux to Windows shared drive
> > 
> > \> require 'ftools'  
> > \> require 'timeout'  
> > \>  
> > \> begin  
> > \> Timeout.timeout(3){  
> > \> File.copy('temp.txt', 'temp.bak')  
> > \> File.rename('temp.bak', '/X/temp.txt')  
> > \> File.unlink('temp.bak')  
> > \> }  
> > \> rescue Errno::EPERM  
> > \> # Ignore bogus EPERM errors - harmless  
> > \> end  
> > \>  
> > \> The problem is that File.rename is raising Errno::EXDEV errors, and  
> > \> simply ignoring those won't work.
> > 
> > Shouldn't it be:  
> > &nbsp;&nbsp;File.copy('temp.txt', '/X/temp.bak')  
> > &nbsp;&nbsp;File.rename('/X/temp.bak', '/X/temp.txt')

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [23 May 2006 21:58 UTC](https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675/2 "2006-05-23T21:58:04Z")

</div>

this \_\_should\_\_ depend on how the drive is mounted. for instance, with an  
nfs 'hard' mount your script would hang until the remote server came back up -  
and then proceed. as amazing as this sounds it routinely works for us with 30  
nodes hits giant nfs mounted raids. i assume there are similar options for  
mounting windows shared drives but you'll have to consult the docs for that...  
in any case you are looking for something like 'hard' mount.

cheers.

-a

> **···**
>
> On Wed, 24 May 2006, Berger, Daniel wrote:
> 
> > > -----Original Message-----  
> > > From: frank.cameron@gmail.com  
> > > [mailto:frank.cameron@gmail.com] On Behalf Of brabuhr@gmail.com  
> > > Sent: Tuesday, May 23, 2006 3:18 PM  
> > > To: ruby-talk ML  
> > > Subject: Re: Atomic File.copy from Linux to Windows shared drive
> > > 
> > > > require 'ftools'  
> > > > require 'timeout'
> > > > 
> > > > begin  
> > > > &nbsp;&nbsp;&nbsp;Timeout.timeout(3){  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.copy('temp.txt', 'temp.bak')  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename('temp.bak', '/X/temp.txt')  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.unlink('temp.bak')  
> > > > &nbsp;&nbsp;&nbsp;}  
> > > > rescue Errno::EPERM  
> > > > &nbsp;&nbsp;&nbsp;# Ignore bogus EPERM errors - harmless  
> > > > end
> > > > 
> > > > The problem is that File.rename is raising Errno::EXDEV errors, and  
> > > > simply ignoring those won't work.
> > > 
> > > Shouldn't it be:  
> > > &nbsp;&nbsp;File.copy('temp.txt', '/X/temp.bak')  
> > > &nbsp;&nbsp;File.rename('/X/temp.bak', '/X/temp.txt')
> > 
> > Whoops! Yeah, that worked. I still don't know what will happen if the  
> > share goes down, though.
> > 
> > Regards,
> > 
> > Dan
> 
> --  
> be kind whenever possible... it is always possible.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)\
**Post date:** [23 May 2006 22:35 UTC](https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675/3 "2006-05-23T22:35:00Z")

</div>

You may need some way of checking the files on the other side to see  
if they check out. If not, try and get the report generator to copy  
them again.

You could write a small Ruby script on the report-sending side that  
makes an MD5 sum and puts in a "latest.md5" file, here's some help:

require 'md5'  
puts MD5.new(File.open('filename', 'rb').read).hexdigest

Then you'd need a script on the other side to pick up the latest file,  
make sure the date is right and check the MD5 sum to be sure the  
report is intact. If not, signal the report writer to recopy and then  
check again in a a few minutes. When everything checks out, run the  
report reader program.

Oh, I just noticed that the server is not available to you. Never mind then.

> **···**
>
> On 5/23/06, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Wed, 24 May 2006, Berger, Daniel wrote:
> > 
> > \>\> -----Original Message-----  
> > \>\> From: frank.cameron@gmail.com  
> > \>\> [mailto:frank.cameron@gmail.com] On Behalf Of brabuhr@gmail.com  
> > \>\> Sent: Tuesday, May 23, 2006 3:18 PM  
> > \>\> To: ruby-talk ML  
> > \>\> Subject: Re: Atomic File.copy from Linux to Windows shared drive  
> > \>\>  
> > \>\>\> require 'ftools'  
> > \>\>\> require 'timeout'  
> > \>\>\>  
> > \>\>\> begin  
> > \>\>\> Timeout.timeout(3){  
> > \>\>\> File.copy('temp.txt', 'temp.bak')  
> > \>\>\> File.rename('temp.bak', '/X/temp.txt')  
> > \>\>\> File.unlink('temp.bak')  
> > \>\>\> }  
> > \>\>\> rescue Errno::EPERM  
> > \>\>\> # Ignore bogus EPERM errors - harmless  
> > \>\>\> end  
> > \>\>\>  
> > \>\>\> The problem is that File.rename is raising Errno::EXDEV errors, and  
> > \>\>\> simply ignoring those won't work.  
> > \>\>  
> > \>\> Shouldn't it be:  
> > \>\> File.copy('temp.txt', '/X/temp.bak')  
> > \>\> File.rename('/X/temp.bak', '/X/temp.txt')  
> > \>  
> > \> Whoops! Yeah, that worked. I still don't know what will happen if the  
> > \> share goes down, though.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [24 May 2006 00:36 UTC](https://rubytalk.org/t/atomic-file-copy-from-linux-to-windows-shared-drive/27675/4 "2006-05-24T00:36:34Z")

</div>

Unfortunately I know very little about the setup, so I'm not sure what I'm dealing with.

However, after I wrote that I noticed something odd. The File.copy from ftools was sometimes failing, even when there didn't appear to be a problem with the shared drive! I was getting partial copies, or copies that appeared to be intact but had slightly different file sizes.

I switched to FileUtils.cp and things seem to work much better. I haven't dug into the implementations too much, so I'm curious as to why that might be.

Anyway, between that and a timeout wrapper, I'm hoping to catch the next failure.

Thanks,

Dan

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> > On Wed, 24 May 2006, Berger, Daniel wrote:
> > 
> > > > -----Original Message-----  
> > > > From: frank.cameron@gmail.com  
> > > > [mailto:frank.cameron@gmail.com] On Behalf Of brabuhr@gmail.com  
> > > > Sent: Tuesday, May 23, 2006 3:18 PM  
> > > > To: ruby-talk ML  
> > > > Subject: Re: Atomic File.copy from Linux to Windows shared drive
> > > > 
> > > > > require 'ftools'  
> > > > > require 'timeout'
> > > > > 
> > > > > begin  
> > > > > &nbsp;&nbsp;&nbsp;Timeout.timeout(3){  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.copy('temp.txt', 'temp.bak')  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename('temp.bak', '/X/temp.txt')  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.unlink('temp.bak')  
> > > > > &nbsp;&nbsp;&nbsp;}  
> > > > > rescue Errno::EPERM  
> > > > > &nbsp;&nbsp;&nbsp;# Ignore bogus EPERM errors - harmless  
> > > > > end
> > > > > 
> > > > > The problem is that File.rename is raising Errno::EXDEV errors, and  
> > > > > simply ignoring those won't work.
> > > > 
> > > > Shouldn't it be:  
> > > > &nbsp;&nbsp;File.copy('temp.txt', '/X/temp.bak')  
> > > > &nbsp;&nbsp;File.rename('/X/temp.bak', '/X/temp.txt')
> > > 
> > > Whoops! Yeah, that worked. I still don't know what will happen if the  
> > > share goes down, though.
> > > 
> > > Regards,
> > > 
> > > Dan
> > 
> > this \_\_should\_\_ depend on how the drive is mounted. for instance, with an  
> > nfs 'hard' mount your script would hang until the remote server came back up -  
> > and then proceed. as amazing as this sounds it routinely works for us with 30  
> > nodes hits giant nfs mounted raids. i assume there are similar options for  
> > mounting windows shared drives but you'll have to consult the docs for that...  
> > in any case you are looking for something like 'hard' mount.
