# Atomic File.copy from Linux to Windows shared drive

**URL:** <https://www.ruby-forum.com/t/atomic-file-copy-from-linux-to-windows-shared-drive/95179>\
**Category:** Ruby\
**Created:** [May 23, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/atomic-file-copy-from-linux-to-windows-shared-drive/95179 "2006-05-23T23:01:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 23, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/atomic-file-copy-from-linux-to-windows-shared-drive/95179/1 "2006-05-23T23:01:43Z")

</div>

Hi all,

We’ve got a Linux box that runs some reports. When the reports are  
finished they’re supposed to be copied to a public drive setup via  
Samba, with a Samba client on the Linux box and a Samba server setup on  
a Windows box somewhere (not available to me). At least, that’s my  
understanding of how things are setup. The public drive is mounted as  
/X.

The problem is that sometimes the server (or client?) flakes out and  
weird stuff happens during File.copy attempts. In the last incident  
only parts of files were copied. Some operations seemed to just hang.

Here’s a synopsis of what I attempted most recently:

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.

So, given that the shared drive could go down in the middle of a  
File.copy operation, what are my options here? Would Ara’s posixlock  
package help me here? If so, how would I use it? It wasn’t clear to me  
from the docs.

Thanks,

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.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 23, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/atomic-file-copy-from-linux-to-windows-shared-drive/95179/2 "2006-05-23T23:20:23Z")

</div>

> # 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’)
