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
|
#-*- perl -*-
#
# Copyright (C) 2004,2005 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Rotate.pm,v 1.5 2004/07/23 15:59:05 fukachan Exp $
#
package FML::File::Rotate;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Carp;
BEGIN {}
END {}
=head1 NAME
FML::File::Rotate - file rotatation utilities.
=head1 SYNOPSIS
my $logrotate = new FML::File::Rotate $curproc;
if ($logrotate->is_time_to_rotate($file)) {
$logrotate->rotate($file);
}
=head1 DESCRIPTION
Utility functions for file rotate operations.
It turns over the given C<file> by some condition.
Typical condition is given as the number of files or how old they are.
C<rotation> renames and rearranges files like this:
rm file.4
mv file.3 file.4
mv file.2 file.3
mv file.1 file.2
mv file.0 file.1
mv file file.0
In old age, a shell script does this but
in modern unix,
some programs such as /usr/bin/newsyslog (MIT athena project) do it.
=head1 METHODS
=head2 new($curproc)
constructor.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self, $curproc) = @_;
my ($type) = ref($self) || $self;
my $me = { _curproc => $curproc };
return bless $me, $type;
}
=head1 PARAMETERS
=head2 set_max_size($size)
set the maximum size.
=head2 get_max_size()
get the maximum size.
=head2 set_num_backlog($num)
set the number of backlog files.
=head2 get_num_backlog()
get the number of backlog files.
=cut
# Descriptions: set max_size.
# Arguments: OBJ($self) NUM($size)
# Side Effects: update $self.
# Return Value: none
sub set_max_size
{
my ($self, $size) = @_;
my $curproc = $self->{ _curproc };
if (defined $size && $size =~ /^\d+$/o) {
$self->{ _max_size } = $size;
}
else {
$curproc->logerror("set_max_size: invalid data: $size");
}
}
# Descriptions: get max_size.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM
sub get_max_size
{
my ($self) = @_;
return( $self->{ _max_size } || 300*1024 );
}
# Descriptions: set number of backlog files.
# Arguments: OBJ($self) NUM($num)
# Side Effects: update $self
# Return Value: none
sub set_num_backlog
{
my ($self, $num) = @_;
my $curproc = $self->{ _curproc };
if (defined $num && $num =~ /^\d+$/o) {
$self->{ _num_backlog } = $num || 4;
}
else {
$curproc->logerror("set_num_backlog: invalid data: $num");
}
}
# Descriptions: get number of backlog files.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM
sub get_num_backlog
{
my ($self) = @_;
return( $self->{ _num_backlog } || 4 );
}
=head2 is_time_to_rotate()
C<stat()> the file correspoinding to the object and
determine whether the time to rotate comes or not.
=cut
# Descriptions: determine if the time to rotate comes.
# Arguments: OBJ($self) STR($file)
# Side Effects: none
# Return Value: NUM(1 (time comes!) or 0)
sub is_time_to_rotate
{
my ($self, $file) = @_;
my $size = $self->get_max_size();
use File::stat;
my $st = stat($file);
return 1 if $st->size > $size;
return 0;
}
=head2 rotate($file)
rename files to rotate.
rm file.4
mv file.3 file.4
mv file.2 file.3
mv file.1 file.2
mv file.0 file.1
mv file file.0
=cut
# Descriptions: rotate files.
# Arguments: OBJ($self) STR($file)
# Side Effects: rename in proper way and unlink the oldest file if needed.
# Return Value: none
sub rotate
{
my ($self, $file) = @_;
my $size = $self->get_max_size();
my $max = $self->get_num_backlog();
# remove oldest file
my $maxfile = sprintf("%s.%s", $file, $max);
if (-f $maxfile) { unlink $maxfile;}
# mv var/log/file.3 -> var/log/file.4 ...;
do {
my $old = sprintf("%s.%s", $file, ($max - 1 > 0 ? $max - 1 : 0));
my $new = sprintf("%s.%s", $file, $max);
-f $old && rename($old, $new);
$max--;
} while ($max > 0);
my $bak = sprintf("%s.%s", $file, 0);
rename($file, $bak);
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2004,2005 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::File::Rotate first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
FML::File::Rotate (2001-2003) is renamed from File::Rotate class in
2004.
=cut
1;
|