"copy" from File::Copy fails but returns success

"copy" from File::Copy fails but returns success

am 16.10.2007 10:43:49 von Henry Law

The CGI program, running under Apache, supports download of a file from
a Linux server running Perl 5.8.6. The files are stored compressed, so
the download program first copies the compressed file to a temporary
directory, then uncompresses it and handles the HTTP for the download.
My problem is that the use of "copy" from File::Copy appears to succeed
(with a TRUE return code) but then when I go to uncompress the file it's
not there.

Because of the CGI environment it's hard to give a fully-runnable
fragment, but here's the core of it.

$rc = copy ("/home/nfb/temp.txt", "/tmp/nfb");
my $output = "Return code:$rc
" . `ls -l /tmp/nfb`;
print "Content-type:
text/html\n\n

$output

\n";
exit 0;

The source file exists and is chmod 777. The result from the code above is

Return code:1
-rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb

.... which shows that the copy succeeded (return code 1) but the file is
nevertheless not in the target directory /tmp/nfb. I'm baffled.

Any ideas? If you think it's a Linux or Apache configuration problem
then please say so and I'll take this to the relevant group; but this
code worked a few weeks ago and I've not touched the Apache
configuration since.

--

Henry Law Manchester, England

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 12:14:51 von unknown

Post removed (X-No-Archive: yes)

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 12:47:03 von Josef Moellers

all mail refused wrote:
> On 2007-10-16, Henry Law wrote:
>=20
>>The CGI program, running under Apache, supports download of a file from=
=20
>>a Linux server running Perl 5.8.6. The files are stored compressed, so=
=20
>>the download program first copies the compressed file to a temporary=20
>>directory, then uncompresses it and handles the HTTP for the download. =

>>My problem is that the use of "copy" from File::Copy appears to succeed=
=20
>>(with a TRUE return code) but then when I go to uncompress the file it'=
s=20
>>not there.
>=20
>=20
> Are you sure it's not a 0 return code that indicates success?

From man File::Copy:

RETURN
All functions return 1 on success, 0 on failure. $! will
be set if an error was encountered.

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 12:49:06 von Josef Moellers

Henry Law wrote:
> The CGI program, running under Apache, supports download of a file from=
=20
> a Linux server running Perl 5.8.6. The files are stored compressed, so=
=20
> the download program first copies the compressed file to a temporary=20
> directory, then uncompresses it and handles the HTTP for the download. =

> My problem is that the use of "copy" from File::Copy appears to succeed=
=20
> (with a TRUE return code) but then when I go to uncompress the file it'=
s=20
> not there.
>=20
> Because of the CGI environment it's hard to give a fully-runnable=20
> fragment, but here's the core of it.
>=20
> $rc =3D copy ("/home/nfb/temp.txt", "/tmp/nfb");
> my $output =3D "Return code:$rc
" . `ls -l /tmp/nfb`;
> print "Content-type:=20
> text/html\n\n

$output

\n";
> exit 0;
>=20
> The source file exists and is chmod 777. The result from the code abov=
e is
>=20
> Return code:1
> -rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb
>=20
> ... which shows that the copy succeeded (return code 1) but the file is=
=20
> nevertheless not in the target directory /tmp/nfb.

Obviously, /tmp/nfb isn't a directory.

> Any ideas? If you think it's a Linux or Apache configuration problem=20
> then please say so and I'll take this to the relevant group; but this=20
> code worked a few weeks ago and I've not touched the Apache=20
> configuration since.

That's what they always say ;-)

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 13:55:40 von vbMark

Henry Law wrote:

> The CGI program, running under Apache, supports download of a file from
> a Linux server running Perl 5.8.6. The files are stored compressed, so
> the download program first copies the compressed file to a temporary
> directory, then uncompresses it and handles the HTTP for the download.
> My problem is that the use of "copy" from File::Copy appears to succeed
> (with a TRUE return code) but then when I go to uncompress the file it's
> not there.
>
> Because of the CGI environment it's hard to give a fully-runnable
> fragment, but here's the core of it.
>
> $rc = copy ("/home/nfb/temp.txt", "/tmp/nfb");
> my $output = "Return code:$rc
" . `ls -l /tmp/nfb`;
> print "Content-type:
> text/html\n\n

$output

\n";
> exit 0;
>
> The source file exists and is chmod 777. The result from the code above
> is
>
> Return code:1
> -rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb
>
> ... which shows that the copy succeeded (return code 1) but the file is
> nevertheless not in the target directory /tmp/nfb. I'm baffled.


The file *is* /tmp/nfb


--
Brian Wakem

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 14:28:44 von Paul Lalli

On Oct 16, 4:43 am, Henry Law wrote:

> My problem is that the use of "copy" from File::Copy appears to
> succeed (with a TRUE return code) but then when I go to
> uncompress the file it's not there.
>
> Because of the CGI environment it's hard to give a fully-runnable
> fragment, but here's the core of it.
>
> $rc = copy ("/home/nfb/temp.txt", "/tmp/nfb");
> my $output = "Return code:$rc
" . `ls -l /tmp/nfb`;
> print "Content-type:
> text/html\n\n

$output

\n";
> exit 0;
>
> The source file exists and is chmod 777. The result from the
> code above is
>
> Return code:1
> -rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb
>
> ... which shows that the copy succeeded (return code 1) but the
> file is nevertheless not in the target directory /tmp/nfb. I'm
> baffled.

Look at that ls output again. See that first dash? That means that /
tmp/nfb is a file, not a directory. You copied the file /home/nfb/
temp.txt to the *file* /tmp/nfb.

If a directory named /tmp/nfb does not already exist, then you need to
create it first:
unless (-e "/tmp/nfb") {
mkdir "/tmp/nfb" or die $!;
}

(Yes, yes, this is a race condition, and that's potentially bad. Only
you know the internals of your system to know whether or not it's
worth caring about - is /tmp/nfb something that gets repeatedly
created and deleted?)

Paul Lalli

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 15:35:40 von Ben Morrow

Quoth Paul Lalli :
> On Oct 16, 4:43 am, Henry Law wrote:
>
> > My problem is that the use of "copy" from File::Copy appears to
> > succeed (with a TRUE return code) but then when I go to
> > uncompress the file it's not there.
> >
> > Because of the CGI environment it's hard to give a fully-runnable
> > fragment, but here's the core of it.
> >
> > $rc = copy ("/home/nfb/temp.txt", "/tmp/nfb");
> > my $output = "Return code:$rc
" . `ls -l /tmp/nfb`;
> > print "Content-type:
> > text/html\n\n

$output

\n";
> > exit 0;
> >
> > The source file exists and is chmod 777. The result from the
> > code above is
> >
> > Return code:1
> > -rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb
> >
> > ... which shows that the copy succeeded (return code 1) but the
> > file is nevertheless not in the target directory /tmp/nfb. I'm
> > baffled.
>
> Look at that ls output again. See that first dash? That means that /
> tmp/nfb is a file, not a directory. You copied the file /home/nfb/
> temp.txt to the *file* /tmp/nfb.
>
> If a directory named /tmp/nfb does not already exist, then you need to
> create it first:
> unless (-e "/tmp/nfb") {
> mkdir "/tmp/nfb" or die $!;
> }

Better would be

my $dir = '/tmp/nfb';

-e $dir and ! -d $dir and unlink $dir
or die "can't unlink $dir: $!";
-d $dir or mkdir $dir
or die "can't mkdir $dir: $!";

which has the same race condition, but is proof against old copies of
the file hanging around.

Ben

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 21:15:10 von Henry Law

Josef Moellers wrote:

> Obviously, /tmp/nfb isn't a directory.

Danke sehr, Josef; and thanks to Brian, Paul and Ben for the same
observation and for the useful improvements.

Do you know how many times I've looked at that "ls" output, and
_completely_ failed to see that it had no "d" at the beginning? There's
none so blind as them that thinks they know what they see.

But that's the benefit of having other people help you with your problem.

--

Henry Law Manchester, England

Re: "copy" from File::Copy fails but returns success

am 16.10.2007 21:37:59 von Sherm Pendley

Henry Law writes:

> The source file exists and is chmod 777. The result from the code above is
>
> Return code:1
> -rw-r--r-- 1 apache apache 5 Oct 16 09:34 /tmp/nfb
>
> ... which shows that the copy succeeded (return code 1) but the file
> is nevertheless not in the target directory /tmp/nfb. I'm baffled.

The above also shows that /tmp/nfb is not a directory - it's a file. The
last component of the destination path is only interpreted as a directory
if that directory already exists; otherwise it's treated as a filename.

> Any ideas?

Check for the existence of the /tmp/nfb directory before you do the copy,
and create it if necessary.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net