6 use lib
"$FindBin::Bin/../lib";
9 use File
::Copy qw
/copy move/;
12 use NonameTV
::DataStore
;
13 use NonameTV
::Config qw
/ReadConfig/;
14 use NonameTV
::Log qw
/progress error d p w f/;
18 my $opt = { 'verbose' => 0,
24 my $res = GetOptions
( $opt, qw
/verbose all xmltvid=s leavedays=s/ );
26 if( not $opt->{xmltvid
} and not $opt->{all
} )
29 nonametv
-filestore
-moveold
--xmltvid
<xmltvid
>
32 Remove old files only
for the channel specified
35 Remove old files
for all channels
38 Leave all files that were touched
in less then
<days
> days
43 The tool is used
for filestore directories cleanup
. It creates
'old' subdirectory
44 in each channel directory inside of filestore
and moves there all files
45 that were modified before the date
given by leavedays argument
.
52 my( $xmltvid ) = $opt->{xmltvid
};
53 my( $leavedays ) = $opt->{leavedays
};
55 $leavedays = 90 if not $leavedays;
57 NonameTV
::Log
::SetVerbosity
( $opt->{verbose
} );
60 my $conf = ReadConfig
();
61 my $ds = NonameTV
::DataStore
->new( $conf->{DataStore
} );
62 my $filestore = $conf->{FileStore
};
65 my $date = localtime( $now - ( 86400 * $leavedays ) );
66 print "Moving all files modified before $date\n";
68 print "Reading directories in $filestore\n";
72 my($f) = File
::Util
->new();
73 # option --no-fsdots excludes "." and ".." from the list
74 @fsfiles = $f->list_dir( $filestore, '--no-fsdots' );
76 push( @fsfiles, $xmltvid );
79 foreach my $fsf (@fsfiles)
83 # check if the directory name is the xmltv name of a valid channel
85 my $channel_id = $ds->{sa
}->Lookup( 'channels', { xmltvid
=> $xmltvid }, 'id' );
86 if( not $channel_id ){
87 progress
( "There is no channel with xmltvid $xmltvid - skipping" );
91 # check if there is a directory for this xmltvid
92 if( not File
::Util
->existent( "$filestore/$fsf" ) ){
93 progress
( "The directory for $fsf does not exist. You should create it manually." );
97 # skip if the file is not a directory
98 my $fsftype = join(',', File
::Util
->file_type( "$filestore/$fsf" ) );
99 if( $fsftype !~ /DIRECTORY/ ){
100 progress
( "File $fsf is $fsftype - skipping" );
104 # check if there is a subdirectory named 'old' inside of the channel directory
105 if( not File
::Util
->existent( "$filestore/$fsf/old" ) ){
106 progress
( "Creating the subdirectory $fsf/old" );
107 File
::Util
->make_dir( "$filestore/$fsf/old", 0755 );
110 # list files inside of the channel directory and check when they have been touched
111 my( $cf ) = File
::Util
->new();
112 my( @cffiles ) = $cf->list_dir( "$filestore/$fsf", '--no-fsdots' );
113 foreach my $cff (@cffiles)
115 next if ( $cff =~ /^old$/ );
117 my $cftype = join(',', File
::Util
->file_type( "$filestore/$fsf/$cff" ) );
119 if( $cftype !~ /PLAIN/ ){
120 progress
( "File $cff is $fsftype - skipping" );
124 my $modified = File
::Util
->last_modified( "$filestore/$fsf/$cff" );
126 print " -> $cff (" . gmtime( $modified ) . ") ... ";
128 my $oktomove = isToMove
( $modified, $leavedays );
130 move
( "$filestore/$fsf/$cff", "$filestore/$fsf/old/$cff" );
131 print "moved to old\n";
143 my( $modified , $leavedays ) = @_;
145 return 0 if not $leavedays;
148 my $thedate = $now - ( 86400 * $leavedays );
150 if( $modified lt $thedate ){