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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
# ConfHelper module
# Copyright (C) 2000 Progeny Linux Systems, Inc.
# Written by John Goerzen <jgoerzen@progenylinux.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
package ConfHelper;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
require AutoLoader;
@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);
$VERSION = '0.9';
# Preloaded methods go here.
# Autoload methods go after =cut, and are processed by the autosplit program.
1;
__END__
# Below is the stub of documentation for your module. You better edit it!
=head1 NAME
ConfHelper - Configuration file management
=head1 SYNOPSIS
use ConfHelper;
$file = new ConfHelper('myprogram', '/etc/myprogram.conf');
=head1 DESCRIPTION
ConfHelper is a module that is designed to help you edit configuration files
in an automated way without trampling on the toes of users or other programs.
It does this by using "chunking"; that is, your program modifies a certain
chunk of the config file and does not touch everything else. While this
approach is not appropriate everywhere, it can be remarkably useful anyway.
The methods that return a chunk of data (an entire configuration area)
return an array. Each element in the array is a single line that has
been run through chomp(). When passed back to set() functions, the newline
character is added back on before being written to disk.
=head1 METHODS
=over 4
=cut
use IO::Handle;
use IO::File;
require Logfile::Rotate;
=item new($package, $filename)
Creates a new object. The I<package> parameter declares the name of your
program or package, and the I<filename> parameter specifies the name of the
configuration file.
=cut
sub new {
my ($class, $package, $filename) = @_;
my $self = {};
my $fd;
$self->{package} = $package;
$self->{startline} =
"### $package DEBCONF AREA. DO NOT EDIT THIS AREA OR INSERT TEXT BEFORE IT.";
$self->{endline} =
"### END OF DEBCONF AREA. PLACE YOUR EDITS BELOW; THEY WILL BE PRESERVED.";
bless $self, $class;
if ($filename) {
$self->{filename} = $filename;
$fd = new IO::File;
sysopen($fd, $filename, O_CREAT | O_RDWR, 0644) ||
die "Can't open $filename: $!";
} else {
die "Filename not passed."
}
$self->{fd} = $fd;
return $self;
}
=item getversion
Returns the version of the ConfHelper module that created the object.
=cut
# Version
sub getversion {
return $VERSION;
}
=item config($variable, $value)
Sets a configuration variable. The following are valid configuration
variables:
=over 4
=item startline
Specifies the complete text of the line that is used to identify the
start of the ConfHelper section of your config file.
=item endline
Specifies the complete text of the line that is used to identify the
end of the ConfHelper section of your config file.
=back
=cut
# Sets a config var.
sub config {
my ($s, $var, $val) = @_;
$s->{$var} = $val;
}
=item hasconfarea
Returns 1 if the file has a configuration area, and 0 otherwise.
=cut
sub hasconfarea {
my $s = shift;
return ($s->getconfarea() ? 1 : 0);
}
=item getconfarea
Returns your configuration area from the configuration file as a large
array. If there is none, it returns undef.
=cut
sub getconfarea {
my $s = shift;
my $fd = $s->{fd};
my @retval;
seek $fd, 0, SEEK_SET;
# Search for the start marker.
return undef unless $s->findline($s->{startline}, 0);
# Search for end marker and return the intermediate result.
return $s->findline($s->{endline}, 1);
}
=item getotherareas
Returns an array that represents all non-ConfHelper areas of your
configuration file.
=cut
sub getotherareas {
my $s = shift;
my $fd = $s->{fd};
my $inline;
my @retval;
seek $fd, 0, SEEK_SET;
# No start marker? Just slurp in everything.
unless ($s->findline($s->{startline}, 0)) {
seek $fd, 0, SEEK_SET;
while (defined($inline = <$fd>)) {
chomp $inline;
push @retval, $inline;
}
return @retval;
}
seek $fd, 0, SEEK_SET;
# Find everything before the top.
@retval = $s->findline($s->{startline}, 1);
# Now, skip to the endline.
$s->findline($s->{endline}, 0) or die "Missing end line";
# Finally, slurp in everything else.
while (defined($inline = <$fd>)) {
chomp $inline;
push @retval, $inline;
}
return @retval;
}
=item setconfarea(@confarea)
This method will set the configuration area to the value of the string
passed as a parameter. ConfHelper will automatically rotate the file, saving
backups of it.
=cut
sub setconfarea {
my ($s, @dcarea) = @_;
my $fd = $s->{fd};
my @otherarea = $s->getotherareas();
# Close the file, rotate it.
close $fd;
my $log = new Logfile::Rotate(File => $s->{filename},
Count => 9,
Gzip => 'no');
$log->rotate();
undef $log;
# Reopen it.
$fd = new IO::File;
sysopen($fd, $s->{filename}, O_CREAT | O_RDWR, 0644) ||
die "Can't open $filename: $!";
$s->{fd} = $fd;
seek $fd, 0, SEEK_SET;
print $fd $s->{startline}, "\n";
print $fd join("\n", @dcarea), "\n";
print $fd $s->{endline}, "\n";
print $fd join("\n", @otherarea), "\n";
truncate($fd, tell($fd));
return 1;
}
=item setotherarea_DANGEROUS(@otherarea)
Sets the non-confarea portion of the file to the value specified.
NOTE: THIS FUNCTION IS DANGEROUS AND SHOULD NOT BE USED UNLESS
ABSOLUTELY NECESSARY! The configuration file is NOT rotated when this
call is used.
=cut
# Sets the other area.
sub setotherarea_DANGEROUS {
my ($s, @otherarea) = @_;
my $fd = $s->{fd};
my @dcarea = $s->getconfarea();
seek $fd, 0, SEEK_SET;
print $fd $s->{startline}, "\n";
print $fd join("\n", @dcarea), "\n";
print $fd $s->{endline}, "\n";
print $fd join("\n", @otherarea), "\n";
truncate($fd, tell($fd));
return 1;
}
#
sub findline {
my ($s, $line, $r) = @_;
my $fd = $s->{fd};
my @retval;
my $inline;
while (defined($inline = <$fd>)) {
chomp $inline;
if ($inline eq $line) {
return @retval if $r;
return 1;
}
push @retval, $inline if $r;
}
return undef;
}
1;
=pod
=back
=head1 AUTHOR
John Goerzen <jgoerzen@progenylinux.com> of Progeny Linux Systems
http://www.progeny.com.
=head1 COPYRIGHT
Copyright (C) 2000 Progeny Linux Systems, Inc.
Written by John Goerzen <jgoerzen@progenylinux.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
=head1 SEE ALSO
debconf(1), perl(1).
=cut
|