can't get unlink to work on windows

J

jan_buys

Hi,

Hope this is a quickie for you toughened perl gods.

I am using unlink in a CGI script (server currently running under my
own admin account) but I can't get it to remove a file with the unlink
command. The script should be cross-platform, so I can't use a system
command for this :

code :
$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|); # db too
open (DEBUG, "> test.txt"); # db
print DEBUG "$remfn, $cnt, $!"; # db
close (DEBUG); # db

my debug file then shows :
c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads\UPL_8848.upl
, 0, No such file or directory

Which is strange, because the file IS there.

btw : tried these variants too :
unlink "$remfn";
unlink ("$remfn");

Thanks for any help !
 
C

Chris Mattern

Hi,

Hope this is a quickie for you toughened perl gods.

I am using unlink in a CGI script (server currently running under my
own admin account) but I can't get it to remove a file with the unlink
command. The script should be cross-platform, so I can't use a system
command for this :

code :
$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|); # db too
open (DEBUG, "> test.txt"); # db
print DEBUG "$remfn, $cnt, $!"; # db
close (DEBUG); # db

my debug file then shows :
c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads\UPL_8848.upl
, 0, No such file or directory

Which is strange, because the file IS there.

btw : tried these variants too :
unlink "$remfn";
unlink ("$remfn");

Thanks for any help !

Are you word wrapping stuff? What you claim your debug file shows has line
feeds in it. Are they in the debug file? If they are, are they in the file
name? In particular, is it possible that $NFN has a trailing line feed as
your report from the debug file seems to show? Perhaps you need to chomp
$NFN after you get from wherever you get it from.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
T

Thomas Kratz

Hi,

Hope this is a quickie for you toughened perl gods.

I am using unlink in a CGI script (server currently running under my
own admin account) but I can't get it to remove a file with the unlink
command. The script should be cross-platform, so I can't use a system
command for this :

code :
$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|); # db too
open (DEBUG, "> test.txt"); # db
print DEBUG "$remfn, $cnt, $!"; # db
close (DEBUG); # db

my debug file then shows :
c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads\UPL_8848.upl
, 0, No such file or directory

Which is strange, because the file IS there.

Are you sure? I don't think the filename has newlines in it. Your debug
output does.
Do you read the paths and filenames from a file? Did you remove the
newlines after reading the lines from the file?

Many questions that could be answered easier had you shown a *short* but
*complete* script that produces the behaviour you mentioned. Please have a
look at the posting guidelines.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
J

jan_buys

Hi again,

Sorry for not posting a complete script. I can assure you that there
are no newlines within the pathnames. I create a small testscript that
still gives me the same problem :

#!c:\perl\bin\perl.exe
my $FilePath = 'c:\\Program Files\\Apache
Group\\Apache\\htdocs\\noticeboard\\uploads';
my $PlatformSlash = '\\';
$NFN = 'UPL_6229858.upl';

## start interpreting the request

use CGI qw/:standard :cgi-lib/;
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|);
print "$remfn, $cnt, $!<br>";

This script will print to following to the browser screen :
c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads\UPL_6229858.upl, 0, No such
file or directory
(all on one line... google groups seems to add newlines)

When I go and look at the location of the files :

Microsoft Windows 2000 [versie 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

c:\Program Files\Apache Group\Apache\htdocs\noticeboard\uploads>ls
UPL_5328979.upl UPL_6229858.upl

c:\Program Files\Apache Group\Apache\htdocs\noticeboard\uploads>dir
Het volume in station C heeft geen naam.
Het volumenummer is 20A6-6074

Map van c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads

29/12/2004 17:59 <DIR> .
29/12/2004 17:59 <DIR> ..
29/12/2004 17:59 12.538 UPL_5328979.upl
29/12/2004 17:58 9.566 UPL_6229858.upl
2 bestand(en) 22.104 bytes
2 map(pen) 29.322.977.280 bytes beschikbaar

c:\Program Files\Apache Group\Apache\htdocs\noticeboard\uploads>

(sorry for the dutch version of windows :) )
I am using ActivePerl 5.8.0, win32 btw...
 
G

Gunnar Hjalmarsson

I am using unlink in a CGI script (server currently running under my
own admin account) but I can't get it to remove a file with the unlink
command.

my $cnt = unlink (qw|"$remfn"|); # db too

Please consider:

my $remfn = 'C:\somedir\UPL_8848.upl';
print qw|"$remfn"|, "\n";

Outputs:
"$remfn"

Not very surprising that your unlink command doesn't work, right? The
qw// operator is apparently not what you want. Try:

my $cnt = unlink $remfn;
 
T

Thomas Kratz

Hi again,

Sorry for not posting a complete script. I can assure you that there
are no newlines within the pathnames. I create a small testscript that
still gives me the same problem :

#!c:\perl\bin\perl.exe
my $FilePath = 'c:\\Program Files\\Apache
Group\\Apache\\htdocs\\noticeboard\\uploads';
my $PlatformSlash = '\\';

Don't do that. If you want to be platform independent use File::Spec,
File::Basename and other Modules created for that purpose.
$NFN = 'UPL_6229858.upl';

## start interpreting the request

use CGI qw/:standard :cgi-lib/;
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|);

Ah, there it is! You try to unlink a file with double quotes around it.
This file does not exist. The double quotes are needed by the shell, not
by unlink.

Just use

my $cnt = unlink ($remfn);

instead.
[rest snipped]

Thomas


--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
B

Brian McCauley

I am using unlink in a CGI script (server currently running under my
own admin account) but I can't get it to remove a file with the unlink
command. The script should be cross-platform, so I can't use a system
command for this :

code :
$remfn = $FilePath.$PlatformSlash.$NFN;
my $cnt = unlink (qw|"$remfn"|); # db too

This is trying to delete a file that actually has double quote
characters in its name.

I suspect you meant to say:

my $cnt = unlink ($remfn); # db too

Note it is unecessary to use the platform specific slash. You can
simply use '/'.
open (DEBUG, "> test.txt"); # db
print DEBUG "$remfn, $cnt, $!"; # db
close (DEBUG); # db

my debug file then shows :
c:\Program Files\Apache
Group\Apache\htdocs\noticeboard\uploads\UPL_8848.upl
, 0, No such file or directory

Which is strange, because the file IS there.

btw : tried these variants too :
unlink "$remfn";
unlink ("$remfn");

Well, those two should work (but they are suboptimal (see FAQ: What's
wrong with always quoting "$vars"?)).

Do you get the same error?

Did you try from the command line instead of as a CGI script?

I think you may be able to get "No such file or directory" on Windows
when the real problem is permission denied on a parent directory

Are you sure the webserver is running under the admin account?

You how have any idea how bad an idea this is?
 
J

jan_buys

Ah Thomas,

You made my day. Now it works indeed :) Thanks a lot. I will go for
the filename modules approach as well. Quick and dirty WAS my name ;-)
Thanks to all for contributing.
 
J

Jürgen Exner

Sorry for not posting a complete script. I can assure you that there
are no newlines within the pathnames. I create a small testscript
that still gives me the same problem :

#!c:\perl\bin\perl.exe
my $FilePath = 'c:\\Program Files\\Apache
Group\\Apache\\htdocs\\noticeboard\\uploads';

Don't know what else is wrong with your script, but single quotes combined
with double backslash as path seperator is not going to work.

Use
- double quotes if you want "\\" interpolated into a single backslash
- or single backslash if you want a single backslash inside of single quotes
- or best of all: a single forward slash

jue
 
J

jan_buys

Hi Brian,

The problem is solved now. Drop the quotes and it works indeed :)

about the server running under the admin account : It should say 'my
test environment server is running under the admin account'. No
particle in my otherwise sane mind would think of doing that on a
production server machine ;-)
 
P

Paul Lalli

Jürgen Exner said:
Don't know what else is wrong with your script, but single quotes combined
with double backslash as path seperator is not going to work.

Use
- double quotes if you want "\\" interpolated into a single backslash
- or single backslash if you want a single backslash inside of single
quotes

While true, the double backslash does indeed 'work'. Within single
quotes, a backslash will escape either a single quote or a backslash:

#!/usr/bin/perl
use strict;
use warnings;
my $foo = ' \\ ';
my $bar = ' \ ';
print "Foo: !$foo! Bar: !$bar!\n";
__END__

Foo: ! \ ! Bar: ! \ !
- or best of all: a single forward slash

Paul Lalli
 
S

Sam Holden

Hi Brian,

The problem is solved now. Drop the quotes and it works indeed :)

about the server running under the admin account : It should say 'my
test environment server is running under the admin account'. No
particle in my otherwise sane mind would think of doing that on a
production server machine ;-)

Why dos you otherwise sane mind have such a large difference in
configuration of the production and test servers?

Doesn't that make life hard when putting something into production?
 
J

jan_buys

Sam said:
Why dos you otherwise sane mind have such a large difference in
configuration of the production and test servers?

Doesn't that make life hard when putting something into production?

It's just the way I work. There are a billion of ways in which
security can be in the way for the script to work properly (and
security is a big word here). Just to separate the errors caused by
that from errors caused by improper Perling, I use a very open dev.
environment. Once things work there, I move to a strict environment,
knowing that if problems pop up there, they will be related to
permissions and nothing else.

Have a look over the newsgroups and count the number of people crying
about their broken script while they actually had a permissions problem
at hand.

It's just a way of working I like, not better than yours, but more
suitable to me ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top