Added ability to set lock screen to setbg
[clearscm.git] / bin / cleantmp.pl
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use v5.22;
5
6 =pod
7
8 =head1 NAME $RCSfile: cleantmp.pl,v $
9
10 Keep /tmp clean based on filesets
11
12 =head1 VERSION
13
14 =over
15
16 =item Author
17
18 Andrew DeFaria <Andrew@DeFaria.com>
19
20 =item Revision
21
22 $Revision: 1.0 $
23
24 =item Created:
25
26 Wed Feb 21 20:43:58 PST 2018
27
28 =item Modified:
29
30 $Date: $
31
32 =back
33
34 =head1 SYNOPSIS
35
36  Usage: cleantmp.pl [-u|sage] [-h|elp] [-v|erbose] [-d|ebug]
37                     [-t|mp <dir>] [-c|onf <file>] [-l|ogpath <path>]
38                     [-s|leep <secs>]
39
40  Where:
41    -u|sage           Print this usage
42    -h|elp            Detailed help
43    -v|erbose:        Verbose mode
44    -d|ebug:          Print debug messages
45    -t|mp <dir>:      Tmp directory to monitor (Default: /tmp)
46    -c|onf <file>:    Config file holding patterns to match (Default: 
47                      .../etc/cleantmp.conf)
48    -l|ogpath <path>: Path to logfile (Default: /var/log)
49    -s|leep <secs>:   How many seconds to sleep between polls (Default: 60)
50
51 =head1 DESCRIPTION
52
53 This script will run in the background and keep /tmp clean. It will read in a 
54 list of Perl regexs from the config file. When new files are created in tmp they
55 will be compared against the list of regexs and if there's a match the file or
56 directory will be removed. 
57
58 The sleep parameter tells us how long to wait before polling for changes again
59
60 =cut
61
62 use FindBin;
63 use Getopt::Long;
64 use Pod::Usage;
65 use File::Monitor;
66 use File::Spec;
67 use File::Path qw/remove_tree/;
68
69 use lib "$FindBin::Bin/../lib";
70
71 use Display;
72 use Logger;
73 use Utils;
74
75 my ($script) = ($FindBin::Script =~ /^(.*)\.pl/);
76 my $log;
77
78 my %opts = (
79   usage   => sub { pod2usage },
80   help    => sub { pod2usage(-verbose => 2)},
81   verbose => sub { set_verbose },
82   debug   => sub { set_debug },
83   tmp     => File::Spec->tmpdir(),
84   conf    => "$FindBin::Bin/../etc/$script.conf",
85   logpath => '/var/log',
86   sleep   => 60,
87 );
88
89 sub loadConfig() {
90   my @patterns;
91
92   open my $patterns, '<', $opts{conf}
93     or $log->err("Unable to open $opts{conf} - $!", 1);
94
95   while (<$patterns>) {
96     next if /^\s*#/; # Skip comments
97
98     chomp;
99
100     push @patterns, $_;
101   } # while
102
103   close $patterns;
104
105   return @patterns;
106 } # loadConfig
107
108 sub FileCreated {
109   my ($name, $event, $change) = @_;
110
111   opendir my $dir, $opts{tmp}
112     or $log->err("Unable to open $opts{tmp} - $!", 1);
113
114   while (my $createdFile = readdir $dir) {
115     next if $createdFile =~ /^\./; # Skip all hidden files
116
117     for my $pattern (loadConfig) {
118       debug "Processing pattern $pattern";
119
120       if ($createdFile =~ /$pattern/) {
121         debug "Matched $createdFile to $pattern";
122
123         if (-d "$opts{tmp}/$createdFile") {
124           remove_tree ("$opts{tmp}/$createdFile", {error => \my $err});
125
126           if (@$err) {
127             for my $diag (@$err) {
128               my ($file, $message) = %$diag;
129
130               if ($file eq '') {
131                 $log->err("General error: $message");
132               } else {
133                 $log->err("Unable to delete $file: $message");
134               } # if
135             } # for
136           } else {
137             $log->msg("$opts{tmp}/$createdFile removed");
138           } # if
139         } else {
140           unless (unlink "$opts{tmp}/$createdFile") {
141             $log->err("Unable to remove $opts{tmp}/$createdFile - $!");
142           } else {
143             $log->msg("$opts{tmp}/$createdFile removed");
144           } # if
145         } # if
146
147
148         last;
149       } # if
150     } # for
151   } # while
152
153   return;
154 } # FileCreated
155
156 ## Main
157 GetOptions (
158   \%opts,
159   'usage',
160   'help',
161   'verbose',
162   'debug',
163   'tmp=s',
164   'logpath=s',
165   'conf=s',
166   'sleep=i'
167 ) or pod2usage;
168
169 $log = Logger->new(path => $opts{logpath}, timestamped => 1);
170
171 $log->msg("Starting $FindBin::Script");
172
173 # First run through whatever junk is in /tmp
174 for (glob "$opts{tmp}/*") {
175   FileCreated($_);
176 } # for
177
178 my $monitor = File::Monitor->new;
179
180 $monitor->watch({
181   name     => $opts{tmp},
182   files    => 1,
183   callback => {files_created => \&FileCreated}
184 });
185
186 set_debug if $DB::OUT;
187
188 EnterDaemonMode unless $DB::OUT or get_debug;
189
190 while () {
191   $monitor->scan;
192
193   sleep $opts{sleep};
194 } # while
195