c has asked for the wisdom of the Perl Monks concerning the following question:

I am using File::Copy and I think I am either misreading the CPAN doc or missing something in my code. When I am copying files, ownership and permissions do not remain intact, but rather take on the ownership and permissions of the user running the script.
The docs talk about syscopy but since I am running the code on a unix variant (linux) I had thought that copy would suffice.
The code:

#!/usr/bin/perl -w use strict; use File::Copy; my $from = "/root/yim"; my $to = "/root/perl/yim"; if ( -d $from ) { mkdir "$to"; } opendir(DIR,"$from"); my @contents = readdir(DIR); for my $i(@contents) { if ( -f "$from/$i" ) { copy("$from/$i","$to/$i"); } } close(DIR); print @contents;

Replies are listed 'Best First'.
Re: Misinterpretation of File::Copy
by lemming (Priest) on Jul 23, 2001 at 14:28 UTC
    The doc refers to syscopy preserving file attributes if Win32, VMS, or OS/2. As a non-perl solution:
    cd /root/yim; find . -depth -print | cpio -pdumv /root/perl/yim
    though only if root for preserving ownership, but perl solutions would have the same issue.
Re: Misinterpretation of File::Copy
by wine (Scribe) on Jul 23, 2001 at 14:34 UTC
    This is the default cp behaviour. You can see the same behaviour from the commandline:

    [wine@guppie tmp_]$ touch boe [wine@guppie tmp_]$ chmod u+x boe [wine@guppie tmp_]$ su Password: guppie:/home/wine/tmp_# cp boe boe.2 guppie:/home/wine/tmp_# ls -l total 0 -rwxr--r-- 1 wine wine 0 Jul 23 12:35 boe -rw-r--r-- 1 root wine 0 Jul 23 12:36 boe.2 guppie:/home/wine/tmp_#

    This happens when a user (or perl in this case) copies files that do not belong to himself. The copies get chown-ed to the user.

    I guess the only way to circumvent this is to restore the properties manually, if you have the permission to do that, that is.

Re: Misinterpretation of File::Copy
by PetaMem (Priest) on Jul 23, 2001 at 14:43 UTC
    Hi,

    see e.g. man cp:

           -p, --preserve
                  preserve file attributes if possible
    
    Now this "if possible" says exactly, that only if you have
    the necessary rights it will preserve attributes. That is in
    your case "being root".

    Itīs an inherent Unix feature. Canīt say anything about the Win
    behaviour.

    Ciao

      It kind of seems like in order to achieve what i want i'll need to use an exec() or system() call for what I am wanting to do, but I was hoping to keep this script as pure perl if possible.

      humbly -c

Re: Misinterpretation of File::Copy
by grinder (Bishop) on Jul 23, 2001 at 17:46 UTC
    for my $i(@contents) { if ( -f "$from/$i" ) { copy("$from/$i","$to/$i"); } }
    If you want to keep it pure perl (but non perl cpio solution ++ :-), you could do the following:

    my( $uid, $gid ) = (stat $i)[4,5]; copy "from/$i", "$to/$i"; chown $uid, $gid, "$to/$i" or die "chown $to/$i (uid=$uid, gid=$gid) + failed: $!";

    Pretty please with sugar on top, check your fscking return codes, okay? ([tm] Reservoir Dogs).

    --
    g r i n d e r