Project

General

Profile

Backport #8109

FileUtils.mv fails across devices on Windows

Added by mitchellh (Mitchell Hashimoto) over 6 years ago. Updated almost 6 years ago.

Status:
Closed
Priority:
Normal
[ruby-core:53492]

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 MOVEFILE_COPY_ALLOWED 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 Backport193 - Backport #8609: FileUtils.mv fails across devices on WindowsClosed03/18/2013Actions

Associated revisions

Revision 35cc29c8
Added by usa (Usaku NAKAMURA) about 6 years ago

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision 40001
Added by usa (Usaku NAKAMURA) about 6 years ago

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

Revision c632c925
Added by nagachika (Tomoyuki Chikanaga) almost 6 years 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 [ruby-core:53492] [Bug #8109]
      reported by mitchellh (Mitchell Hashimoto)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@42375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 42375
Added by nagachika (Tomoyuki Chikanaga) almost 6 years 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 [ruby-core:53492] [Bug #8109]
  reported by mitchellh (Mitchell Hashimoto)

Revision e6716b5e
Added by usa (Usaku NAKAMURA) almost 6 years ago

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

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@42460 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

History

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

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

Updated by usa (Usaku NAKAMURA) about 6 years ago

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

Need network drive, or others?

Updated by luislavena (Luis Lavena) about 6 years 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 MOVEFILE_COPY_ALLOWED combined with MOVEFILE_REPLACE_EXISTING if necessary.

#4

Updated by usa (Usaku NAKAMURA) about 6 years 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 [ruby-core:53492] [Bug #8109] reported by mitchellh (Mitchell Hashimoto)

Updated by nagachika (Tomoyuki Chikanaga) about 6 years 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 usa (Usaku NAKAMURA) almost 6 years 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 nagachika (Tomoyuki Chikanaga)

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

#7

Updated by nagachika (Tomoyuki Chikanaga) almost 6 years 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 [ruby-core:53492] [Bug #8109]
  reported by mitchellh (Mitchell Hashimoto)

Also available in: Atom PDF