[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fileutils::copy_entry() won't copy symlinks without dereferencing

Andrew Walrond

1/9/2005 8:12:00 PM

Am I doing something stupid?? ruby-doc says

"copy_entry(src, dest, preserve = false, dereference = false)

Copies a file system entry src to dest. This method preserves
file types, c.f. FIFO, device files, directory.â?¦

Both of src and dest must be a path name. src must exist,
dest must not exist.

If preserve is true, this method preserves owner, group and
permissions. If dereference is true, this method copies a
target of symbolic link instead of a symbolic link itself. "

But I cannot get a symlink to copy as a symlink. Example:

andrew@orac apw $ ruby --version
ruby 1.8.2 (2004-12-15) [x86_64-linux]
andrew@orac apw $ ls -l /etc/ld.so.conf
lrwxrwxr-x 1 root root 27 Dec 16 23:28 /etc/ld.so.conf -> /pkg/glibc.1/etc/ld.so.conf
andrew@orac apw $ irb
irb(main):001:0> require "fileutils"
=> true
irb(main):002:0> FileUtils.copy_entry("/etc/ld.so.conf","ld.so.conf")
=> nil
irb(main):003:0> quit
andrew@orac apw $ ls -l
total 4
-rw-r--r-- 1 andrew users 2469 Jan 9 20:07 ld.so.conf
andrew@orac apw $

Any suggestions?

Andrew Walrond



6 Answers

leon breedt

1/9/2005 8:37:00 PM

0

On Mon, 10 Jan 2005 05:12:18 +0900, Andrew Walrond <andrew@walrond.org> wrote:
> But I cannot get a symlink to copy as a symlink. Example:
This appears to be a bug.

In my version of Ruby, File.stat('/some/symlink').file? returns true
if the symlink points to a file.

If you look at fileutils.rb, you'll see the case statement in
CopyContext_#_copy_entry puts the st.file? condition before the
st.symlink? condition, so st.symlink? will never be true, and
#_copy_content will always be used.

Leon


leon breedt

1/9/2005 8:39:00 PM

0

On Mon, 10 Jan 2005 09:36:52 +1300, leon breedt <bitserf@gmail.com> wrote:
> On Mon, 10 Jan 2005 05:12:18 +0900, Andrew Walrond <andrew@walrond.org> wrote:
> so st.symlink? will never be true
Sorry, that would be "will never be matched".


Kent Dahl

1/9/2005 8:56:00 PM

0

Andrew Walrond wrote:
> "copy_entry(src, dest, preserve = false, dereference = false)
>
[...]
>
> But I cannot get a symlink to copy as a symlink. Example:

Same problem here on ruby 1.8.2 (2004-12-25) [x86_64-linux].

FileUtils appeared to be correct at first glance, the problem was due to
File.stat(filename).symlink? not returning true for symlinks.

But then I saw this tidbit in file.c:

* call-seq:
* stat.symlink? => true or false
*
* Returns <code>true</code> if <i>stat</i> is a symbolic link,
* <code>false</code> if it isn't or if the operating system doesn't
* support this feature. As <code>File::stat</code> automatically
* follows symbolic links, <code>symlink?</code> will always be
* <code>false</code> for an object returned by
* <code>File::stat</code>.
*
* File.symlink("testfile", "alink") #=> 0
* File.stat("alink").symlink? #=> false
* File.lstat("alink").symlink? #=> true

Should FileUtils use lstat everywhere? (It does for the most part.)

As for a quick hack/workaround, just change one method after fetching the
FileUtils module:

require 'fileutils'
class FileUtils::CopyContext_
def stat(path)
@stat ||= ::File.lstat(path)
end
end
FileUtils.copy_entry( 'link', 'link2' )

This script makes another symlink to whatever 'link' pointed to, on my
system.

HTH

--
Kent Dahl

Minero Aoki

1/10/2005 7:04:00 AM

0

Andrew Walrond

1/10/2005 9:47:00 AM

0

On Monday 10 January 2005 07:04, Minero Aoki wrote:
>
> Sorry, this is a bug of fileutils.
> It was already fixed in HEAD, but was too late for 1.8.2.
> Please do not use copy_entry in this version.
>

Ok. Has it reached a stable snapshot yet?

Andrew


Minero Aoki

1/10/2005 7:16:00 PM

0