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

Hello Monks,

Good Morning to all of you !

I am trying to run cgi script, which copies file from source to target location with UNIX cp command or copy function from File::Copy.

in the cgi script i have line like this:

$cp="cp-f"; $rc=system("$cp $source_file $target_file"); if(!$rc) { print "error: $! \n"; exit 2; }

Above copy command is copying to target location, but is modifying the file permissions as:

Source file permissions = -rw-r--r-- 1 httpd httpd

Target file permissions = -rw-r----- 1 httpd httpd

Why is that ?

Please help me regarding this.

Thanking you in advance.

  • Comment on copying files from cgi perl script is modifying target file permissions
  • Download Code

Replies are listed 'Best First'.
Re: copying files from cgi perl script is modifying target file permissions
by choroba (Cardinal) on Jun 20, 2012 at 06:43 UTC
    What does cp-f do? It is not a standard command.

    Also note that system returns the exit status, so normally you should use system "..." and die instead of or.

      taint% man cp 1
      CP(1) FreeBSD General Commands Manual CP(1) ... -f For each existing destination pathname, remove it and create a new file, without prompting for confirmation regardless of its permis- sions. (The -f option overrides any previous -i or -n options.)

      I this this option may have led to your problem.

      use perl::always;
      my $perl_version( 5.12.4 );
      print $perl_version;
        taint% man cp 1

        Nice. But "cp-f" ne "cp", and "cp-f" ne "cp -f".

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: copying files from cgi perl script is modifying target file permissions
by frozenwithjoy (Priest) on Jun 20, 2012 at 06:38 UTC
    use File::Copy "cp" is supposed to maintain permissions of the original file. If that isn't working, perhaps give cp -p a shot? The -p flag preserves mode/ownership/timestamps.
Re: copying files from cgi perl script is modifying target file permissions
by cheekuperl (Monk) on Jun 20, 2012 at 08:22 UTC
    Are the source and target directories different? I think this is happening because of umask.
    From perldoc for File::Copy
    You may use the syntax use File::Copy "cp" to get at the cp alias for this function. The syntax is exactly the same. The behavior is nearly the same as well: as of version 2.15, <cp> will preserve the source file's permission bits like the shell utility cp(1) would do, while copy uses the default permissions for the target file (which may depend on the process' umask, file ownership, inherited ACLs, etc.).
Re: copying files from cgi perl script is modifying target file permissions
by Don Coyote (Hermit) on Jun 20, 2012 at 08:32 UTC

    This is more likely as a result of your httpd configuration. This occurs when I upload through ftp too. The source file will take on the permissions of the existing file.

    cp -f in 'man cp' will remove existing files first if it cannot open them. In lieu of you having set the user or mode in the script, the system commands will operate under the user mode of the user of the script, most likely to be a user named nobody or something similar. A specific user that is essentially the httpd.

    What your source file mode says is 'anyone can read me'. What 'nobody' does in your configuration, is to set the destination file permissions so only the system admin and itself have permissions on the file. This basically means that not anyone can read your httpd, only no-one can.*

    *Differing transalations of The Odyssey also delight in the nobody/noman/no-one interpretation complex .

    Coyote

Re: copying files from cgi perl script is modifying target file permissions
by Happy-the-monk (Canon) on Jun 20, 2012 at 07:35 UTC

    $cp="cp-f";

    Although not related to Perl, cp -p is the option to preserve permissions while copying files as mentioned above by frozenwithjoy.
    (sorry, didn't see that before I posted, I keep too many firefox tabs open for too long).
    See man cp

    Cheers, Sören