]> gitweb.michael.orlitzky.com - dead/janitor.git/blob - src/janitor.rb
Initial commit.
[dead/janitor.git] / src / janitor.rb
1 require('src/apache_conf_file')
2
3 class Janitor
4
5 attr_accessor :apache_vhosts_directory
6
7 SECONDS_PER_MINUTE = 60
8 MINUTES_PER_HOUR = 60
9 HOURS_PER_DAY = 24
10 SECONDS_PER_DAY = SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY
11
12
13 def initialize()
14 @apache_vhosts_directory = nil
15 end
16
17
18 protected;
19
20 def seconds_to_whole_days(seconds)
21 # Given some number of seconds, computer how many
22 # entire days are contained in that number of seconds.
23 # Fractions of days don't count!
24
25 if (seconds < 0)
26 raise(ArgumentException.new('Negative seconds?'))
27 end
28
29 return (seconds / SECONDS_PER_DAY).floor
30 end
31
32 public;
33
34 # Get a list of all the temporary directories that
35 # we'd like to clean out.
36 def get_temporary_directories(remove_duplicates = true)
37 temp_dirs = []
38
39 # Get the PHP temp dirs if we've been given a vhosts path
40 if !@apache_vhosts_directory.nil? && File.directory?(@apache_vhosts_directory)
41 Dir.glob(@apache_vhosts_directory + '/*.conf').each do |conf_file|
42 acf = ApacheConfFile.new(conf_file)
43
44 # Don't remove the duplicates here since we have
45 # to do it afterwards anyway.
46 temp_dirs += acf.get_php_temporary_directory_list(false)
47 end
48 end
49
50 # We expect these to have been strip()ed already
51 if remove_duplicates
52 return temp_dirs.uniq
53 else
54 return temp_dirs
55 end
56 end
57
58
59 def clean_directory(directory, max_age, file_pattern = '*')
60 glob_pattern = File.join(directory, file_pattern)
61
62 Dir.glob(glob_pattern).each do |f|
63 # Obviously, we should bail if it's not a file.
64 next if not File.file?(f)
65
66 # Calculate how old it is
67 days_old = seconds_to_whole_days(Time.now - File.mtime(f))
68
69 # And then delete the file (with a notice) if it's too old.
70 if days_old > max_age
71 puts "Deleting file: #{f} (#{days_old} days old)"
72 File.delete(f)
73 end
74 end
75
76 end
77 end