| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::DbDriver::DirTree - store database in a directory hierarchy |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::DbDriver::DirTree; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Log qw(:all); |
| 13 |
use base 'Debconf::DbDriver::Directory'; |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
This is an extension to the Directory driver that uses a deeper directory |
| 18 |
tree. I find such a tree easier to navigate, and it will also scale better |
| 19 |
for huge databases on ext2. It does use a little more disk space/inodes |
| 20 |
though. |
| 21 |
|
| 22 |
=head1 FIELDS |
| 23 |
|
| 24 |
=over 4 |
| 25 |
|
| 26 |
=item extension |
| 27 |
|
| 28 |
This field is mandatory for this driver. If it is not set, it will be set |
| 29 |
to ".dat" by default. |
| 30 |
|
| 31 |
=back |
| 32 |
|
| 33 |
=head1 METHODS |
| 34 |
|
| 35 |
Note that the extension field is mandatory for this driver, so it checks |
| 36 |
that on initialization. |
| 37 |
|
| 38 |
=cut |
| 39 |
|
| 40 |
sub init { |
| 41 |
my $this=shift; |
| 42 |
if (not defined $this->{extension} or not length $this->{extension}) { |
| 43 |
$this->{extension}=".dat"; |
| 44 |
} |
| 45 |
$this->SUPER::init(@_); |
| 46 |
} |
| 47 |
|
| 48 |
=head2 save(itemname,value) |
| 49 |
|
| 50 |
Before saving as usual, we have to make sure the subdirectory exists. |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
sub save { |
| 55 |
my $this=shift; |
| 56 |
my $item=shift; |
| 57 |
|
| 58 |
return unless $this->accept($item); |
| 59 |
return if $this->{readonly}; |
| 60 |
|
| 61 |
my @dirs=split(m:/:, $this->filename($item)); |
| 62 |
pop @dirs; # the base filename |
| 63 |
my $base=$this->{directory}; |
| 64 |
foreach (@dirs) { |
| 65 |
$base.="/$_"; |
| 66 |
next if -d $base; |
| 67 |
mkdir $base or $this->error("mkdir $base: $!"); |
| 68 |
} |
| 69 |
|
| 70 |
$this->SUPER::save($item, @_); |
| 71 |
} |
| 72 |
|
| 73 |
=head2 filename(itemname) |
| 74 |
|
| 75 |
We actually use the item name as the filename, subdirs and all. |
| 76 |
|
| 77 |
We also still append the extension to the item name. And the extension is |
| 78 |
_mandatory_ here; otherwise this would try to use filenames and directories |
| 79 |
with the same names sometimes. |
| 80 |
|
| 81 |
=cut |
| 82 |
|
| 83 |
sub filename { |
| 84 |
my $this=shift; |
| 85 |
my $item=shift; |
| 86 |
$item =~ s/\.\.//g; |
| 87 |
return $item.$this->{extension}; |
| 88 |
} |
| 89 |
|
| 90 |
=head2 iterator |
| 91 |
|
| 92 |
Iterating over the whole directory hierarchy is the one annoying part of |
| 93 |
this driver. |
| 94 |
|
| 95 |
=cut |
| 96 |
|
| 97 |
sub iterator { |
| 98 |
my $this=shift; |
| 99 |
|
| 100 |
# Stack of pending directories. |
| 101 |
my @stack=(); |
| 102 |
my $currentdir=""; |
| 103 |
my $handle; |
| 104 |
opendir($handle, $this->{directory}) or |
| 105 |
$this->error("opendir: $this->{directory}: $!"); |
| 106 |
|
| 107 |
my $iterator=Debconf::Iterator->new(callback => sub { |
| 108 |
my $i; |
| 109 |
while ($handle or @stack) { |
| 110 |
while (@stack and not $handle) { |
| 111 |
$currentdir=pop @stack; |
| 112 |
opendir($handle, "$this->{directory}/$currentdir") or |
| 113 |
$this->error("opendir: $this->{directory}/$currentdir: $!"); |
| 114 |
} |
| 115 |
$i=readdir($handle); |
| 116 |
if (not defined $i) { |
| 117 |
closedir $handle; |
| 118 |
$handle=undef; |
| 119 |
next; |
| 120 |
} |
| 121 |
next if $i eq '.lock' || $i =~ /-old$/; |
| 122 |
if (-d "$this->{directory}/$currentdir$i") { |
| 123 |
if ($i ne '..' and $i ne '.') { |
| 124 |
push @stack, "$currentdir$i/"; |
| 125 |
} |
| 126 |
next; |
| 127 |
} |
| 128 |
# Ignore files w/o our extension, and strip it. |
| 129 |
next unless $i=~s/$this->{extension}$//; |
| 130 |
return $currentdir.$i; |
| 131 |
} |
| 132 |
return; |
| 133 |
}); |
| 134 |
|
| 135 |
$this->SUPER::iterator($iterator); |
| 136 |
} |
| 137 |
|
| 138 |
=head2 remove(itemname) |
| 139 |
|
| 140 |
Unlink a file. Then, rmdir any empty directories. |
| 141 |
|
| 142 |
=cut |
| 143 |
|
| 144 |
sub remove { |
| 145 |
my $this=shift; |
| 146 |
my $item=shift; |
| 147 |
|
| 148 |
# Do actual remove. |
| 149 |
my $ret=$this->SUPER::remove($item); |
| 150 |
return $ret unless $ret; |
| 151 |
|
| 152 |
# Clean up. |
| 153 |
my $dir=$this->filename($item); |
| 154 |
while ($dir=~s:(.*)/[^/]*:$1: and length $dir) { |
| 155 |
rmdir "$this->{directory}/$dir" or last; # not empty, I presume |
| 156 |
} |
| 157 |
return $ret; |
| 158 |
} |
| 159 |
|
| 160 |
=head1 AUTHOR |
| 161 |
|
| 162 |
Joey Hess <[email protected]> |
| 163 |
|
| 164 |
=cut |
| 165 |
|
| 166 |
1 |