CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
File creation

 



BikerHQ
Deleted

Mar 4, 2001, 2:37 PM

Post #1 of 7 (601 views)
File creation Can't Post

Is there no end to my infernal questioning...... :)

OK. Last one for today. Honest. I tried writing a little script which checked for the existance of a file and if it wasn't there, to create a blank file. For some reason, it just doesn't work. It gives me this error (this is whilst testing in my script editor).....

Insecure dependency in open while running with -T switch at line 128.

Can anyone give me a code chunk for checking the existance of a file, and it it isn't there - creating a blank file with it's name, or at least point me in the right direction. As far as I know, my code is right - I've opened files many times before. Can't see what is different this time (except I normally have a file already to open instead of creating a new one from within perl)

p.s. Are there any hard and fast rules I should know about when using the Open command?

Thanks,
BHQ



wickedxter
User

Mar 4, 2001, 3:04 PM

Post #2 of 7 (599 views)
Re: File creation [In reply to] Can't Post

use this code to check if the file exist..

if(-e "/dir/to/file/name"){
print ("File exists!");
}

or you can get it to return a $value than printing something..



Jean
User


Mar 4, 2001, 11:01 PM

Post #3 of 7 (591 views)
Re: File creation [In reply to] Can't Post

Here's exactly what you need:


Code
my $filename = "/path/filename"; 
if ( ! -e($filename) ) {
open(FILE, ">$filename") or die "Unable to create file $filename";
close(FILE);
}

Just to remind in case that was your problem:
1. open(FILE, "$filename") - opens file for reading (only existing one);
2. open(FILE, "<$filename") - exactly as N1 above;
3. open(FILE, ">$filename") - opens file for writing (existing file is rewritten, i.e. emptied, in case file doesn't exist it is created automatically);
4. open(FILE, ">>$filename") - opens file for appending (data is added to the end of the existing file, in case file doesn't exist it is created automatically);


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


BikerHQ
Deleted

Mar 5, 2001, 12:16 AM

Post #4 of 7 (590 views)
Re: File creation [In reply to] Can't Post

Thanks for your help again peeps.....

You know, it's strange but that is what I did (not quite as tidy, but the same method). I tried this one in place of my own code and it still reports this error. I would have thought this would work perfectly. I'm wondering whether it's something to do with the naming convention of my filenames. So I have one last question.....

Say I have a filename which always ends in either .JPG, .BMP or .GIF. I want to take that filename and change the extension to .TXT. How would I go about this.

(Last question. Honest!! - ...kindof.....)



Jean
User


Mar 5, 2001, 1:10 AM

Post #5 of 7 (589 views)
Re: File creation [In reply to] Can't Post

The following is working example that should help you.


Code
my $filename = "myfile.JPG"; 
my $newname = $filename;
my $command;

$newname =~ s/\.(jpg|bmp|gif)$/.txt/i;
if ( $^O =~ /MSWin/i ) {
# MS Windows OS
$command = "ren $filename $newname";
}
else {
# Non-Windows OS (Unix-like command is used)
$command = "mv $filename $newname";
}
eval `$command`;

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


japhy
Enthusiast

Mar 5, 2001, 6:07 AM

Post #6 of 7 (586 views)
Re: File creation [In reply to] Can't Post

First, you don't need to use eval() here at all. Second, there's no reason not to use Perl's rename() function:


Code
rename $old => $new or warn "can't rename $old to $new: $!";

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


japhy
Enthusiast

Mar 5, 2001, 6:29 AM

Post #7 of 7 (586 views)
Re: File creation [In reply to] Can't Post

This creates a race condition, which is a terrible security hole. Imagine this situation from a movie: the bad guy is running away from the good guy. He escapes, and is walking down a street somewhere. He comes to a corner. He peers around the corner to make sure no one's there -- there isn't. He brings his head back, sighs, and then walks around the corner. *BAM* The good guy's fist punches the bad guy.

That's what race conditions can do to you. Punch you in the face.

Here's where you get punched:


Code
# here, you peek around the corner to see if anyone's coming... 
if (-e $filename) {
# here, you duck your head back, and sigh
# and RIGHT NOW is when the good guy appears
open FILE, "> $filename";
# and THAT'S the punch in the face
}

In the instant between the time you check for the file's existence, and the time you create the file, that file MIGHT appear -- and you've just wiped it out. Or worse, some malicious cracker could make that filename a symbolic link to a file you own, and you've just wiped out that file.

How do you avoid this? You do it in ONE step, not TWO. That's when race conditions occur -- in non-atomic processes.


Code
use Fcntl qw( O_CREAT O_RDWR ); 
sysopen(FILE, $filename, O_CREAT | O_RDWR, 0644) or
die "can't sysopen $filename: $!";

What that does is opens the file for read/write access, AND creates it IF it needs to.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives