def update_cache
do_update = true
if File.file?(cachefile)
cached_size = File.lstat(cachefile).size
cached_mtime = File.lstat(cachefile).mtime
size, mtime = nil
open @url, :content_length_proc => lambda { |size| } do |file|
mtime = file.last_modified
end
if mtime && size
do_update = (size != cached_size || mtime > cached_mtime)
elsif mtime
$stderr.puts "WARNING: size is not available for #{@url}, relying on modification time"
do_update = (mtime > cached_mtime)
elsif size
$stderr.puts "WARNING: modification time is not available for #{@url}, relying on size"
do_update = (size != cached_size)
else
$stderr.puts "WARNING: neither size nor modification time available for #{@url}, will always update"
end
end
if do_update
puts "downloading #{url}"
FileUtils.mkdir_p(cachedir)
File.open(cachefile, 'w') do |cache|
open @url do |file|
while block = file.read(4096)
cache.write block
end
end
end
true
end
end