Forum: Ruby-core [ruby-trunk - Bug #7246][Open] FileUtils.cp_r does not preserve attributes of directories

Posted by wedesoft (Jan Wedekind) (Guest)
on 2012-10-30 16:43
(Received via mailing list)
Issue #7246 has been reported by wedesoft (Jan Wedekind).

----------------------------------------
Bug #7246: FileUtils.cp_r does not preserve attributes of directories
https://bugs.ruby-lang.org/issues/7246

Author: wedesoft (Jan Wedekind)
Status: Open
Priority: Normal
Assignee:
Category: lib
Target version:
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) 
[x86_64-darwin11.3.0]


(({FileUtils.cp_r})) does not preserve the file attributes of 
directories because the attributes are set before copying to the 
directory.
The following monkey patch fixes this behaviour. However the monkey 
patch relies on the contents of the source directory not changing during 
the copy operation.

(({require 'fileutils'

  def copy_entry(src, dest, preserve = false, dereference_root = false, 
remove_destination = false)
    Entry_.new(src, nil, dereference_root).traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      File.unlink destent.path if remove_destination && 
File.file?(destent.path)
      ent.copy destent.path
    end
    Entry_.new(src, nil, dereference_root).postorder_traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      ent.copy_metadata destent.path if preserve
    end
  end
  module_function :copy_entry

end}))
Posted by wedesoft (Jan Wedekind) (Guest)
on 2012-10-30 16:58
(Received via mailing list)
Issue #7246 has been updated by wedesoft (Jan Wedekind).


The following code might be a proper bugfix.

(({require 'fileutils'

module FileUtils

  class Entry_

    def wrap_traverse(pre, post)
      pre.call self
      if directory?
        entries.each do |ent|
          ent.wrap_traverse pre, post
        end
      end
      post.call self
    end
  end

  def copy_entry(src, dest, preserve = false, dereference_root = false, 
remove_destination = false)
    Entry_.new(src, nil, dereference_root).wrap_traverse(proc do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      File.unlink destent.path if remove_destination && 
File.file?(destent.path)
      ent.copy destent.path
    end, proc do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      ent.copy_metadata destent.path if preserve
    end)
  end
  module_function :copy_entry

end}))

----------------------------------------
Bug #7246: FileUtils.cp_r does not preserve attributes of directories
https://bugs.ruby-lang.org/issues/7246#change-32017

Author: wedesoft (Jan Wedekind)
Status: Open
Priority: Normal
Assignee:
Category: lib
Target version:
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) 
[x86_64-darwin11.3.0]


(({FileUtils.cp_r})) does not preserve the file attributes of 
directories because the attributes are set before copying to the 
directory.
The following monkey patch fixes this behaviour. However the monkey 
patch relies on the contents of the source directory not changing during 
the copy operation.

(({require 'fileutils'

  def copy_entry(src, dest, preserve = false, dereference_root = false, 
remove_destination = false)
    Entry_.new(src, nil, dereference_root).traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      File.unlink destent.path if remove_destination && 
File.file?(destent.path)
      ent.copy destent.path
    end
    Entry_.new(src, nil, dereference_root).postorder_traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      ent.copy_metadata destent.path if preserve
    end
  end
  module_function :copy_entry

end}))
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-25 16:53
(Received via mailing list)
Issue #7246 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to mame (Yusuke Endoh)
Target version set to 2.0.0

Thanks, I'll import your fix unless there is objection.

--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
Bug #7246: FileUtils.cp_r does not preserve attributes of directories
https://bugs.ruby-lang.org/issues/7246#change-33862

Author: wedesoft (Jan Wedekind)
Status: Assigned
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: lib
Target version: 2.0.0
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) 
[x86_64-darwin11.3.0]


(({FileUtils.cp_r})) does not preserve the file attributes of 
directories because the attributes are set before copying to the 
directory.
The following monkey patch fixes this behaviour. However the monkey 
patch relies on the contents of the source directory not changing during 
the copy operation.

(({require 'fileutils'

  def copy_entry(src, dest, preserve = false, dereference_root = false, 
remove_destination = false)
    Entry_.new(src, nil, dereference_root).traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      File.unlink destent.path if remove_destination && 
File.file?(destent.path)
      ent.copy destent.path
    end
    Entry_.new(src, nil, dereference_root).postorder_traverse do |ent|
      destent = Entry_.new(dest, ent.rel, false)
      ent.copy_metadata destent.path if preserve
    end
  end
  module_function :copy_entry

end}))
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.