/usr/share/perl5/File/CounterFile.pm is in libfile-counterfile-perl 1.04-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | package File::CounterFile;
# $Id: CounterFile.pm,v 0.23 2004/01/23 08:37:18 gisle Exp $
require 5.004;
use strict;
use Carp qw(croak);
use Symbol qw(gensym);
use Fcntl qw(LOCK_EX O_RDWR O_CREAT);
BEGIN {
# older version of Fcntl did not know about SEEK_SET
if ($] < 5.006) {
*SEEK_SET = sub () { 0 };
}
else {
Fcntl->import("SEEK_SET");
}
}
use vars qw($VERSION $MAGIC $DEFAULT_INITIAL $DEFAULT_DIR);
sub Version { $VERSION; }
$VERSION = "1.04";
$MAGIC = "#COUNTER-1.0\n"; # first line in counter files
$DEFAULT_INITIAL = 0; # default initial counter value
# default location for counter files
$DEFAULT_DIR = $ENV{TMPDIR} || "/var/tmp";
# Experimental overloading.
use overload ('++' => \&inc,
'--' => \&dec,
'""' => \&value,
fallback => 1,
);
sub new
{
my($class, $file, $initial) = @_;
croak("No file specified\n") unless defined $file;
$file = "$DEFAULT_DIR/$file" unless $file =~ /^[\.\/]/;
$initial = $DEFAULT_INITIAL unless defined $initial;
my $value;
local($/, $\) = ("\n", undef);
local *F;
sysopen(F, $file, O_RDWR|O_CREAT) or croak("Can't open $file: $!");
flock(F, LOCK_EX) or croak("Can't flock: $!");
my $first_line = <F>;
if (defined $first_line) {
croak "Bad counter magic '$first_line' in $file" unless $first_line eq $MAGIC;
$value = <F>;
chomp($value);
}
else {
seek(F, 0, SEEK_SET);
print F $MAGIC;
print F "$initial\n";
$value = $initial;
}
close(F) || croak("Can't close $file: $!");
bless { file => $file, # the filename for the counter
'value' => $value, # the current value
updated => 0, # flag indicating if value has changed
# handle => XXX, # file handle symbol. Only present when locked
};
}
sub locked
{
exists shift->{handle};
}
sub lock
{
my($self) = @_;
$self->unlock if $self->locked;
my $fh = gensym();
my $file = $self->{file};
open($fh, "+<$file") or croak "Can't open $file: $!";
flock($fh, LOCK_EX) or croak "Can't flock: $!"; # 2 = exlusive lock
local($/) = "\n";
my $magic = <$fh>;
if ($magic ne $MAGIC) {
$self->unlock;
croak("Bad counter magic '$magic' in $file");
}
chomp($self->{'value'} = <$fh>);
$self->{handle} = $fh;
$self->{updated} = 0;
$self;
}
sub unlock
{
my($self) = @_;
return unless $self->locked;
my $fh = $self->{handle};
if ($self->{updated}) {
# write back new value
local($\) = undef;
seek($fh, 0, SEEK_SET) or croak "Can't seek to beginning: $!";
print $fh $MAGIC;
print $fh "$self->{'value'}\n";
}
close($fh) or warn "Can't close: $!";
delete $self->{handle};
$self;
}
sub inc
{
my($self) = @_;
if ($self->locked) {
$self->{'value'}++;
$self->{updated} = 1;
} else {
$self->lock;
$self->{'value'}++;
$self->{updated} = 1;
$self->unlock;
}
$self->{'value'}; # return value
}
sub dec
{
my($self) = @_;
if ($self->locked) {
unless ($self->{'value'} =~ /^\d+$/) {
$self->unlock;
croak "Autodecrement is not magical in perl";
}
$self->{'value'}--;
$self->{updated} = 1;
}
else {
$self->lock;
unless ($self->{'value'} =~ /^\d+$/) {
$self->unlock;
croak "Autodecrement is not magical in perl";
}
$self->{'value'}--;
$self->{updated} = 1;
$self->unlock;
}
$self->{'value'}; # return value
}
sub value
{
my($self) = @_;
my $value;
if ($self->locked) {
$value = $self->{'value'};
}
else {
$self->lock;
$value = $self->{'value'};
$self->unlock;
}
$value;
}
sub DESTROY
{
my $self = shift;
$self->unlock;
}
1;
__END__
=head1 NAME
File::CounterFile - Persistent counter class
=head1 SYNOPSIS
use File::CounterFile;
$c = File::CounterFile->new("COUNTER", "aa00");
$id = $c->inc;
open(F, ">F$id");
=head1 DESCRIPTION
This module implements a persistent counter class. Each counter is
represented by a separate file in the file system. File locking is
applied, so multiple processes can attempt to access a counter
simultaneously without risk of counter destruction.
You give the file name as the first parameter to the object
constructor (C<new>). The file is created if it does not exist.
If the file name does not start with "/" or ".", then it is
interpreted as a file relative to C<$File::CounterFile::DEFAULT_DIR>.
The default value for this variable is initialized from the
environment variable C<TMPDIR>, or F</var/tmp> if no environment
variable is defined. You may want to assign a different value to this
variable before creating counters.
If you pass a second parameter to the constructor, it sets the
initial value for a new counter. This parameter only takes effect
when the file is created (i.e. it does not exist before the call).
When you call the C<inc()> method, you increment the counter value by
one. When you call C<dec()>, the counter value is decremented. In both
cases the new value is returned. The C<dec()> method only works for
numerical counters (digits only).
You can peek at the value of the counter (without incrementing it) by
using the C<value()> method.
The counter can be locked and unlocked with the C<lock()> and
C<unlock()> methods. Incrementing and value retrieval are faster when
the counter is locked, because we do not have to update the counter
file all the time. You can query whether the counter is locked with
the C<locked()> method.
There is also an operator overloading interface to the
File::CounterFile object. This means that you can use the C<++>
operator for incrementing and the C<--> operator for decrementing the counter,
and you can interpolate counters directly into strings.
=head1 COPYRIGHT
Copyright (c) 1995-1998,2002,2003 Gisle Aas. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Gisle Aas <gisle@aas.no>
=cut
|