Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Atomic - Check file exists and create if not
dazzlad
Check file exists and create if not.
Can this be done as an atomic operation in Perl (cross platform).
Thanks.
Darren.
Find more posts tagged with
Comments
Adam Stoller
Perhaps you're not providing enough detail - but are you talking about something like:
if (! -e $fname){
if (open(OUT, '>', $fname)){
# put something in the file?
close(OUT);
}
else {
# error opening file for write
}
}
else {
# file already exists
}
I'm not sure it's completely atomic, but it's pretty close and likely to be close enough without knowing more about the conditions you're worried about.
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
dazzlad
That snippet is exactly what I'm talking about.
I'm just being a perfectionist in looking for the atomic operation equivalent.
Thanks.
Darren.
stefanmaier
Hi Darren,
perhaps you can use
use POSIX;
my $fd = POSIX:
pen("test.html", O_CREAT|O_EXCL|O_WRONLY);
print "FileDescriptor=$fd\n";
If the file already exists, an undef is returned.
If there's no file, the file is created and you get back an file descriptor.
Open: don't know if POSIX:
pen itself is atomic. I assume it is not.
Stefan
birt_test.zip
dazzlad
Thanks for that!
Actually googling
"POSIX:
pen" +atomic
seems to suggest that it is an atomic op!
Darren.