-
Notifications
You must be signed in to change notification settings - Fork 27
/
compact
executable file
·85 lines (62 loc) · 1.66 KB
/
compact
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
#!/usr/bin/perl
#
# compact -- compact your already-expired MV session database
#
# Bugs: uses only the default session lock and file names
#
$USAGE = <<EOF;
usage: compact.pl [catalog-directory]
Set to your software and catalog directory
or pass it a directory name
EOF
my $Catdir = shift || die "$USAGE\n";
# I usually do
use strict;
my $type; # GDBM_File or DB_File
my $flags; # How to open file
my $ext; # File extension
# Uncomment for Berkeley
BEGIN {
DBCHECK: {
eval {
die if $ENV{MINIVEND_DBFILE};
require GDBM_File;
import GDBM_File;
$type = 'GDBM_File';
$flags = &GDBM_WRCREAT;
$ext = '.gdbm';
};
last DBCHECK unless $@;
eval {
require DB_File;
import DB_File;
$type = 'DB_File';
$flags = &O_RDWR | &O_CREAT;
$ext = '.db';
};
last DBCHECK unless $@;
die "You don't need this script, not GDBM_File or DB_File.\n";
}
}
use Fcntl qw/:flock/;
my %session1;
my %session2;
my ($k, $v);
my $file1 = "$Catdir/session$ext";
my $file2 = "$Catdir/new_session$ext";
open(LOCK, "+<$Catdir/etc/session.lock")
or open(LOCK, ">>$Catdir/etc/session.lock")
or die "open lockfile: $!\n";
flock(LOCK, LOCK_EX)
or die "lock: $!\n";
tie(%session1, $type, $file1, $flags, 0777)
or die "tie $file1: $!\n";
unlink "$Catdir/new_session$ext";
tie(%session2, $type, $file2, $flags, 0777)
or die "tie $file2: $!\n";
while( ($k, $v) = each %session1) {
$session2{$k} = $v;
}
untie %session1 or die "untie: $!\n";
untie %session2 or die "untie: $!\n";
rename $file2, $file1 or die "rename $file1 to $file2: $!\n";