Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

File::CounterFile problems : access from mulitple scripts and zeroing counter

by hmbscully (Scribe)
on Sep 26, 2005 at 15:52 UTC ( #495149=perlquestion: print w/replies, xml ) Need Help??

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

Dearest Monks,

I have the following code in a larger script which writes a form submission to a flatfile, called each time the form is submitted:

#increment file for count my $asrnumber = File::CounterFile->new("./asr_counter.txt"); $asrnumber->lock(); $asrnumber->inc; $asrnumber->unlock();

I then have another script that will take the flatfile, check the counter for number of submissions, and email the file to some waiting party:

#!/usr/bin/perl # # script to email and archive ASR submissions for backup use warnings; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; use MIME::Lite; use POSIX; use File::CounterFile; require '../code_paths.conf'; $from_email = "webmaster\"; $to_email = "wendy\"; $orig_file = "asr_submissions.txt"; $file_date = POSIX::strftime("%d%m%Y_%H%M", localtime); $new_file = "asr_submissions_$file_date.txt"; system "mv $orig_file $new_file"; #rename the flatfile with date system "mv $asr_data/$new_file $asr_backup"; #move flatfile to backup +directory my $count = File::CounterFile->new("./asr_counter.txt"); $count->lock(); $count->value; $count->unlock(); &Email_Results; #email flatfile sub Email_Results{ $msg = MIME::Lite->new(From => $from_email, To => $to_email, Subject => "$count ASR submissions - $file_date +", Type => 'multipart/mixed'); $msg->attach(Type => 'application/octet-stream', Path => "$asr_backup/$new_file", Filename => $new_file ); $msg->send(); }

The problem I am running into is that if I let the File::CounterFile module create the counter file on its own, it ends up with a different owner than the script (or that I have access to) and I cannot access it from this script. I copied the module-created file and renamed it to the file I am now calling, which seems to work.

The problem is I need to zero out the counter each time the above script is run and I can't quite figure out how to do that other than to just delete the file... and when the original script would recreate the file it would have the bad permissions again...

Hoping there is both a quick and dirty and quick and neat solution to this... thanks.

Replies are listed 'Best First'.
Re: File::CounterFile problems : access from mulitple scripts and zeroing counter
by scmason (Monk) on Sep 26, 2005 at 17:11 UTC
    Something quick and dirty? How about this in the script that creates the file:

    `chmod 666 ./asr_counter.txt`

    Of course, there are better ways, like making the scripts that write it and read it members of the same group, and then setting your up their umask accordiningly. Alternatively, you could have the reading script suid to the user that write the file...

    "Never take yourself too seriously, because everyone knows that fat birds dont fly" -FLC
Re: File::CounterFile problems : access from mulitple scripts and zeroing counter
by Kanji (Parson) on Sep 26, 2005 at 18:21 UTC

    You could extend F::CF to support zeroing out the counter.

    Something like...

    package File::CounterFile::Resettable; use base qw( File::CounterFile ); sub reset { my($self) = @_; if ($self->locked) { $self->{'value'} = 0; $self->{updated} = 1; } else { $self->lock; $self->{'value'} = 0; $self->{updated} = 1; $self->unlock; } $self->{'value'}; # return value }

    ...and then later in your script...

    my $count = File::CounterFile::Resettable->new("./asr_counter.txt"); $count->lock(); $count->reset(); $count->unlock();
    (Code is untested, but you hopefully get the idea!)


    Update: $code =~ s/\t/ /g; # for readability.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://495149]
Approved by sauoq
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-12-06 15:19 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (32 votes). Check out past polls.