This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
/
SessionFile.pm
135 lines (119 loc) · 3.11 KB
/
SessionFile.pm
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
# SessionFile.pm: stores session information in files
#
# $Id: SessionFile.pm,v 1.3 2000/04/02 10:20:34 mike Exp $
#
# Copyright 1996-2000 by Michael J. Heins <mikeh@minivend.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA.
# $Id: SessionFile.pm,v 1.3 2000/04/02 10:20:34 mike Exp $
package Vend::SessionFile;
require Tie::Hash;
@ISA = qw(Tie::Hash);
use Symbol;
use strict;
use Vend::Util;
use vars qw($VERSION);
$VERSION = substr(q$Revision: 1.3 $, 10);
my $SessionDir;
my $SessionFile;
my $SessionLock;
my %HaveLock;
my $Lh;
my $Fh;
my $Last;
my @Each;
sub TIEHASH {
my($self, $dir) = @_;
die "Vend::SessionFile: directory name\n"
unless $dir;
$SessionDir = $dir;
bless {}, $self;
}
sub keyname {
return Vend::Util::get_filename(shift, 2, 1, $SessionDir);
}
sub FETCH {
my($self, $key) = @_;
$SessionFile = keyname($key);
$SessionLock = $SessionFile . ".lock";
return undef unless -f $SessionFile;
my $str;
unless ($HaveLock{$SessionFile} || $Global::Windows) {
$Lh = gensym();
open($Lh, "+>>$SessionLock")
or die "Can't open '$SessionLock': $!\n";
lockfile($Lh, 1, 1)
and $HaveLock{$SessionFile} = 1;
}
my $ref = Vend::Util::eval_file($SessionFile);
#::logDebug("retrieving from $SessionFile: " . ::uneval($ref));
return $ref;
}
sub FIRSTKEY {
my ($self) = @_;
require File::Find
or die "No standard Perl library File::Find!\n";
@Each = ();
File::Find::find( sub {
return if ! -f $File::Find::name;
return if $File::Find::name =~ /\.lock$/;
push @Each, $File::Find::name;
},
$SessionDir,
);
&NEXTKEY;
}
sub NEXTKEY {
my $key = shift @Each;
my $last = $Last;
$Last = $key;
return $key;
}
sub EXISTS {
return Vend::Util::exists_filename($_[1], 2, 1, $SessionDir);
}
sub DELETE {
my($self,$key) = @_;
my $filename = keyname($key);
unlink $filename;
return 1 if $Global::Windows;
my $lockname = $filename . ".lock";
unlink $lockname;
}
sub STORE {
my($self, $key, $ref) = @_;
$SessionFile = keyname($key);
$SessionLock = $SessionFile . ".lock";
unlink $SessionFile;
unless ($HaveLock{$SessionFile} || $Global::Windows) {
$Lh = gensym();
open($Lh, "+>>$SessionLock")
or die "Can't open '$SessionLock': $!\n";
lockfile($Lh, 1, 1)
and $HaveLock{$SessionFile} = 1;
}
#::logDebug("storing in $SessionFile: " . ::uneval($ref));
Vend::Util::uneval_file($ref,$SessionFile);
}
sub DESTROY {
my($self) = @_;
unlockfile($Lh)
and delete $HaveLock{$SessionFile};
close($Lh);
undef $self;
}
1;
__END__