Backport #8109

FileUtils.mv fails across devices on Windows

Added by Mitchell Hashimoto about 1 year ago. Updated 9 months ago.

[ruby-core:53492]
Status:Closed
Priority:Normal
Assignee:Tomoyuki Chikanaga

Description

Reproduction code: https://gist.github.com/luislavena/5179408

The issue is that Ruby is using MoveFile under the covers: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx

Quote directly from the MoveFile docs: "The one caveat is that the MoveFile function will fail on directory moves when the destination is on a different volume."

Instead, MoveFileEx should be used with the MOVEFILECOPYALLOWED flag. This function has existed since XP, and since Ruby 1.9 is XP+, we can safely switch to this: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx

The MoveFileEx call with the proper flag will just do the right thing.


Related issues

Related to Backport93 - Backport #8609: FileUtils.mv fails across devices on Windows Closed 03/18/2013

Associated revisions

Revision 42375
Added by Tomoyuki Chikanaga 9 months ago

merge revision(s) 40001: [Backport #8109]

* win32/win32.c (wrename): use MoveFileExW instead of MoveFileW,
  because the latter fails on cross device file move of some
  environments.
  fix  [Bug #8109]
  reported by mitchellh (Mitchell Hashimoto)

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

=begin
Why Windows returns (({ERRORACCESSDENIED})) on cross-device link for directory, while (({ERRORNOTSAME_DEVICE})) for file?
=end

#2 Updated by Usaku NAKAMURA about 1 year ago

How reproduce this?
I cannot reproduce on Windows 7 (x64).
(The target volume is FAT32 on USB memory.)

Need network drive, or others?

#3 Updated by Luis Lavena about 1 year ago

  • Category set to platform/windows
  • Assignee set to cruby-windows

usa (Usaku NAKAMURA) wrote:

How reproduce this?
I cannot reproduce on Windows 7 (x64).
(The target volume is FAT32 on USB memory.)

Need network drive, or others?

Dunno about moving from NTFS to FAT32, but I used ImDisk (a ram drive) to create a ntfs drive, then:

require "fileutils"
Dir.mkdir "C:/somedirectory"
FileUtils.mv "C:/somedirectory", "V:/another"

This fails because it relies on File.rename which uses FileMoveW and is not causing cross-device link error, but instead access denied, as "move" does in the Command Prompt

Since we use FileMoveEx anyway if the target file exists, perhaps we can directly use FileMoveEx all the way and use MOVEFILECOPYALLOWED combined with MOVEFILEREPLACEEXISTING if necessary.

#4 Updated by Usaku NAKAMURA about 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40001.
Mitchell, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • win32/win32.c (wrename): use MoveFileExW instead of MoveFileW, because the latter fails on cross device file move of some environments. fix [Bug #8109] reported by mitchellh (Mitchell Hashimoto)

#5 Updated by Tomoyuki Chikanaga about 1 year ago

Hello,

usa san, it seems a long-standing bug, isn't it? Do you want to backport r40001 to 1.9.3 branch? I will take a leaf from your opinion for 2.0.0.

Thanks.

#6 Updated by Usaku NAKAMURA 9 months ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport200
  • Category deleted (platform/windows)
  • Status changed from Closed to Assigned
  • Assignee changed from cruby-windows to Tomoyuki Chikanaga

Sorry for late reply.
I should backport it to 1.9.3 (see #8609).

#7 Updated by Tomoyuki Chikanaga 9 months ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r42375.
Mitchell, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 40001: [Backport #8109]

* win32/win32.c (wrename): use MoveFileExW instead of MoveFileW,
  because the latter fails on cross device file move of some
  environments.
  fix  [Bug #8109]
  reported by mitchellh (Mitchell Hashimoto)

Also available in: Atom PDF