#------------------------------------------------------------------------------------
use Data::Dumper;
-use POSIX qw( mktime );
+use POSIX qw( mktime strftime );
use Cwd qw(cwd getcwd abs_path);
use LogRotate::Common;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
-use constant default_firstline_statusfile => "Logrotate State -- Version 3";
+use constant default_firstline_statusfile_v2 => "Logrotate State -- Version 2";
+use constant default_firstline_statusfile_v3 => "Logrotate State -- Version 3";
-my @ValidFields = qw( parent file test );
+my @ValidFields = qw( parent file test file_version fileversion file-version );
for my $attr ( @ValidFields ) {
$ok_field{$attr}++;
my ( $res, $cmd );
my $self = {
- 'test' => 0,
- 'file' => '/val/lib/logrotate.status',
+ 'file' => '/val/lib/logrotate.status',
+ 'file_version' => 3,
+ 'test' => 0,
@_
};
croak "Ungueltige Attributmethode ->$attr()" unless $ok_field{$attr};
- return $self->file(@_) if $attr eq "file";
- return $self->test(@_) if $attr eq "test";
+ return $self->file(@_) if $attr eq "file";
+ return $self->test(@_) if $attr eq "test";
+ return $self->file_version(@_) if $attr =~ /^file[\-_]*version$/i;
if ( @_ ) {
$val = shift;
print $p . "Aufgerufen mit '" . ( defined $file ? $file : "<undef>" ) . "'.\n" if verbose() > 2;
- my ( $f, $dir, $real_dir, $logfile, $date, $time_t );
+ my ( $f, $dir, $real_dir, $logfile, $date, $time_t, $i );
$file = $self->{'file'} unless $file;
unless ( $file ) {
close FILE;
return undef;
}
- $time_t = mktime( $Date[5] || 0, $Date[4] || 0, $Date[3] || 0, $Date[2], $Date[1] - 1, $Date[0] - 1900 );
+ for $i ( 0 .. 5 ) {
+ $Date[$i] += 0;
+ }
+ printf( $p . "Gelesenes Datum '%-20s': %04d-%02d-%02d %02d:%02d:%02d (%s)\n", $date, @Date, $logfile ) if verbose() > 4;
+ $time_t = mktime( $Date[5], $Date[4], $Date[3], $Date[2], $Date[1] - 1, $Date[0] - 1900 );
unless ( $time_t ) {
warn $p . "Unbekanntes Datumsformat: '$date' (Datei '$f', Zeile $.).\n";
close FILE;
#------------------------------------------------------------------------------------------
+=head2 file_version()
+
+Setzt bzw. gibt die Version der zu schreibenden Status-Datei dieses Moduls zurueck.
+
+=cut
+
+sub file_version($;$) {
+
+ my $self = shift;
+ my $p = verbose() ? __PACKAGE__ . "::file_version(): " : "";
+ my ( $nv, $old_version );
+ if ( @_ ) {
+ $nv = shift;
+ $old_version = defined $nv ? $nv : "<undef>";
+ $nv = to_int( $nv );
+ if ( defined $nv ) {
+ unless ( $nv == 2 or $nv == 3 ) {
+ $nv = undef;
+ }
+ }
+ if ( defined $nv ) {
+ $self->{'file_version'} = $nv;
+ } else {
+ warn $p . "Ungueltige Versionsnummer: '$old_version', gueltig sind nur '2' oder '3'.\n";
+ }
+ }
+ return $self->{'file_version'};
+
+}
+
+#------------------------------------------------------------------------------------------
+
=head2 test()
Setzt bzw. gibt den Test-Modus dieses Moduls zurueck.
#------------------------------------------------------------------------------------------
+=head2 write_logfile( $filename, [$unix_timestamp] )
+
+Schreibt in die aktuelle Statusdatei das Rotations-Datum der angegebenen Logdatei hinein.
+Vorhandene Eintraege in dieser Datei, die andere Logdateien betreffen, werden unveraendert
+wieder eingetragen.
+
+Die Funktion gibt zurueck, ob erfolgreich oder nicht.
+
+=cut
+
+sub write_logfile($$;$) {
+
+ my $self = shift;
+ my $logfile = shift;
+ my $timestamp = shift || time();
+ my $p = verbose() ? __PACKAGE__ . "::write_logfile(): " : "";
+ my $ver = $self->file_version();
+
+ unless ( defined($logfile) and $logfile !~ /^\s*$/ ) {
+ warn $p . "Ungueltiger Dateiname uebergeben zum Schreiben in Statusdatei.\n";
+ return undef;
+ }
+
+ my ( @Date );
+ my ( $f, $d );
+ my $file = $self->{'file'};
+
+ my $files = $self->read();
+ return undef unless $files;
+
+ $files->{$logfile} = $timestamp;
+
+ print $p . "Schreibe Statusdatei '$file' ...\n" if verbose();
+
+ unless ( $self->test() ) {
+ unless ( open FILE, ">$file" ) {
+ warn $p . "Konnte Statusdatei nicht zum Schreiben oeffnen: " . $! . "\n";
+ return undef;
+ }
+ print FILE ( $ver == 2 ? default_firstline_statusfile_v2 : default_firstline_statusfile_v3 ) . "\n";
+ foreach $f ( sort { lc($a) cmp lc($b) } keys %$files ) {
+ @Date = localtime($files->{$f});
+ if ( $ver == 2 ) {
+ $d = sprintf "%d-%d-%d", $Date[5] + 1900, $Date[4] + 1, $Date[3];
+ } else {
+ $d = strftime( '%Y-%m-%d_%H:%M:%S', @Date );
+ }
+ print FILE '"' . $f . '" ' . $d . "\n";
+ }
+ close FILE;
+ }
+ return 1;
+
+}
+
+#------------------------------------------------------------------------------------------
+
1;
__END__
use Getopt::Long;
use Data::Dumper;
+use POSIX qw(setlocale LC_ALL LC_CTYPE LC_NUMERIC LC_TIME LC_MESSAGES);
use LogRotate;
use LogRotate::Common;
+# Ruecksetzen aller locale-Einstellungen auf Standard
+setlocale( LC_ALL, "C" );
+setlocale( LC_CTYPE, "C" );
+setlocale( LC_NUMERIC, "C" );
+setlocale( LC_TIME, "C" );
+setlocale( LC_MESSAGES, "C" );
+
+$ENV{'TZ'} = 'GMT';
+
my $Revis = <<'ENDE';
$Revision$
ENDE
print "\n" . $p . "Gelesene Konfiguration: " . Dumper( $lr ) if $verbose > 3;
+unless ( scalar keys %{$lr->{'c'}{'logfiles'}} ) {
+ warn $p . "Keine Logdateien zum Rotieren gefunden, exit.\n";
+ exit 4;
+}
+
+exit ( $res ? 0 : 2 ) if $config_check;
+
+
+
+
+
$lr = undef;
=pod
-$lr->read_state_file();
-
-print Dumper( $lr ) if $verbose > 2;
-
-unless ( scalar keys %{$lr->{'logfiles'}} ) {
- warn "No files for logrotating found, exit.\n";
- exit 4;
-}
-
-exit ( $res ? 0 : 2 ) if $config_check;
-
$lr->rotate();
$lr->write_status();