Config-Writer
view release on metacpan or search on metacpan
**Config::Writer** - a module to write configuration files
in an easy and safe way.
# DESCRIPTION
This module is intended to perform the next operations:
- safe temporary configuration file creation, ownership and
access mode setting;
- creation of backup file(-s) of the target configuration file;
- automatic cleanup of outdated or surplus backup files.
Now you are able to restore configuration file even if you
forgot to create a backup file before editing it!
# CAVEATS
- This module is written using \`signatures\` feature. As for me,
it makes code clearer. However, it requires perl 5.10+. All
more or less modern OSes has much more newer perl included, so
don't think it will be a problem.
# **SYNOPSIS**
'group' => 'bird',
'permissions' => 0640
});
Configuration file to be created or replaced name can contain either absolute or
relative path part. Path part handling is described in **workdir** option description
below.
New temporary file will be created on success and all write operations will be
performed on this temporary file. On \`close\` method invocation existing configuration
file can be moved to a backup file (see descrition of **overwrite** option below) and
temporary file is renamed in place of the original configuration file.
- **FILENAME**
Configuration file to be created or replaced name. Can contain either absolute or
relative path part. Path part handling is described in **workdir** option description below.
New temporary file will be created on success and all write operations will be performed
on this temporary file. On **close()** method invocation existing configuration file can
be moved to a backup file (see descrition of **overwrite** option below) and temporary file
is renamed in place of the original configuration file.
- **format** = STRING
Configuration file format. Currently unused.
- **workdir** = STRING
If filename contains absolute path, work directory is set to a **dirname(1)**
implicitly regardless of whether **workdir** option is set or not.
If **workdir** is not set, work directory defaults to **getcwd(3)**.
If filename contains relative path, it is appended to a work directory name,
provided either in **workdir** option or returned by **getcwd(3)**.
Work directory existence check is performed. If work directory does not exist, \`undef\`
is returned and error flag is set!
- **retain** = INTEGER
Quantity of configuration file backups to retain. Default is 0 - do not retain any.
- **overwrite** = BOOLEAN
Existing backup file will be either overwritten if the flag is set to true
(overwrite = 1) or stayed untouched (overwrite = 0). E. g. if you choose to
store single backup per day, you'll get either the latest configuration version
before it being updated, or the configuration you've got at the beginning of the
day.
Default is 0.
- **extension** = STRING
Configuration file backup extension format as described in POSIX strftime function
documentation. The new extension will replace original one, so the backup files
should not be loaded even in case wildcards (e. g. '**\*.conf**') are used to include
configuration from a several files. Existing backup files will either stay untouched
or overwritten depending on **overwrite** flag value.
Default is '-%Y-%m-%d'.
- **owner** = STRING
Configuration file owner name. If file owner can not be changed, error flag is set.
Defaults to process EUID.
- **printf(STRING, ARRAY)**
Prints formatted string to the temporary file. See **printf(3)** for
more details.
- **close()**
When called:
- closes temporary configuration file;
- tries to rename target configuration file to a backup file (if \`retain\`
option is non-zero);
- tries to remove surplus (oldest) backup files (if \`retain\` option is non-zero);
- tries to rename temporary configuration file to a target name.
If any errors occurs, \`error\` flag is set.
# **AUTHORS**
- Volodymyr Pidgornyi, vp<at>dtel-ix.net;
# **CHANGELOG**
lib/Config/Writer.pm view on Meta::CPAN
=over 4
=item *
safe temporary configuration file creation, ownership and
access mode setting;
=item *
creation of backup file(-s) of the target configuration file;
=item *
automatic cleanup of outdated or surplus backup files.
=back
Now you are able to restore configuration file even if you
forgot to create a backup file before editing it!
=head1 CAVEATS
=over 4
=item *
This module is written using `signatures` feature. As for me,
it makes code clearer. However, it requires perl 5.10+. All
more or less modern OSes has much more newer perl included, so
lib/Config/Writer.pm view on Meta::CPAN
'group' => 'bird',
'permissions' => 0640
});
Configuration file to be created or replaced name can contain either absolute or
relative path part. Path part handling is described in B<workdir> option description
below.
New temporary file will be created on success and all write operations will be
performed on this temporary file. On `close` method invocation existing configuration
file can be moved to a backup file (see descrition of B<overwrite> option below) and
temporary file is renamed in place of the original configuration file.
=over 4
=item B<FILENAME>
Configuration file to be created or replaced name. Can contain either absolute or
relative path part. Path part handling is described in B<workdir> option description below.
New temporary file will be created on success and all write operations will be performed
on this temporary file. On B<close()> method invocation existing configuration file can
be moved to a backup file (see descrition of B<overwrite> option below) and temporary file
is renamed in place of the original configuration file.
=item B<format> = STRING
Configuration file format. Currently unused.
=item B<workdir> = STRING
If filename contains absolute path, work directory is set to a B<dirname(1)>
implicitly regardless of whether B<workdir> option is set or not.
lib/Config/Writer.pm view on Meta::CPAN
If B<workdir> is not set, work directory defaults to B<getcwd(3)>.
If filename contains relative path, it is appended to a work directory name,
provided either in B<workdir> option or returned by B<getcwd(3)>.
Work directory existence check is performed. If work directory does not exist, `undef`
is returned and error flag is set!
=item B<retain> = INTEGER
Quantity of configuration file backups to retain. Default is 0 - do not retain any.
=item B<overwrite> = BOOLEAN
Existing backup file will be either overwritten if the flag is set to true
(overwrite = 1) or stayed untouched (overwrite = 0). E. g. if you choose to
store single backup per day, you'll get either the latest configuration version
before it being updated, or the configuration you've got at the beginning of the
day.
Default is 0.
=item B<extension> = STRING
Configuration file backup extension format as described in POSIX strftime function
documentation. The new extension will replace original one, so the backup files
should not be loaded even in case wildcards (e. g. 'B<*.conf>') are used to include
configuration from a several files. Existing backup files will either stay untouched
or overwritten depending on B<overwrite> flag value.
Default is '-%Y-%m-%d'.
=item B<owner> = STRING
Configuration file owner name. If file owner can not be changed, error flag is set.
Defaults to process EUID.
lib/Config/Writer.pm view on Meta::CPAN
When called:
=over 4
=item *
closes temporary configuration file;
=item *
tries to rename target configuration file to a backup file (if `retain`
option is non-zero);
=item *
tries to remove surplus (oldest) backup files (if `retain` option is non-zero);
=item *
tries to rename temporary configuration file to a target name.
=back
If any errors occurs, `error` flag is set.
=back
=cut #}}}
if (fileno $self->{'fh'} != -1 and fileno $self->{'fh'} != fileno STDOUT) {
undef $self->{'fh'};
unless ($self->{'retain'} == 0) {
my $backup = $self->{'workdir'} . '/' . $self->{'filename'} . POSIX::strftime($self->{'extension'}, localtime time);
if (! -f $backup or isTrue $self->{'overwrite'}) {
rename($self->{'fullname'}, $backup) or $self->{'error'} = boolean::true;
}
opendir(DH, $self->{'workdir'}) or $self->{'error'} = boolean::true;
my $tmpfiles = {};
foreach my $filename (readdir DH) {
next if $filename !~ /^$self->{'filename'}(?!$|\.[_\w]{6}$)/;
$filename = Cwd::realpath($self->{'workdir'} . '/' . $filename);
$tmpfiles->{$filename} = (stat $filename)[9];
}
closedir DH;
my @tmpfiles = map { $_ } sort { $tmpfiles->{$b} <=> $tmpfiles->{$a} } keys %{$tmpfiles};
( run in 0.594 second using v1.01-cache-2.11-cpan-ff066701436 )