diff --git a/modules/tftp/server.rb b/modules/tftp/server.rb index 98218d8bd..473841763 100644 --- a/modules/tftp/server.rb +++ b/modules/tftp/server.rb @@ -63,6 +63,13 @@ def delete_file(file) logger.debug "TFTP: Skipping a request to delete a file which doesn't exists" end end + + def self.outdated_files(age) + delete_before = Time.now - age + Dir.glob(File.join(Proxy::TFTP::Plugin.settings.tftproot, "boot", "*-{vmlinuz,initrd.img}")).filter do |file| + File.file?(file) && File.mtime(file) < delete_before + end + end end class Syslinux < Server diff --git a/modules/tftp/tftp_api.rb b/modules/tftp/tftp_api.rb index 1bc6276c7..9b30083a7 100644 --- a/modules/tftp/tftp_api.rb +++ b/modules/tftp/tftp_api.rb @@ -71,5 +71,24 @@ def create_default(variant) get "/serverName" do {"serverName" => (Proxy::TFTP::Plugin.settings.tftp_servername || "")}.to_json end + + # Purge old entries + post "/prune" do + # how old a file can be, in seconds + age = params[:age] + + if age.nil? || age.to_i <= 0 + log_halt 400, "Invalid age; needs to be a valid integer > 0" + end + + to_prune = Proxy::TFTP::Server.outdated_files(age.to_i) + + to_prune.each do |file| + logger.debug("Removing outdated file", file) + File.unlink(file) + end + + pruned.length + end end end