class Aspera::TempFileManager

create a temp file name for a given folder files can be deleted on process exit by calling cleanup

Constants

FILE_LIST_AGE_MAX_SEC

assume no transfer last longer than this (garbage collect file list which were not deleted after transfer)

SEC_IN_DAY

Public Class Methods

new() click to toggle source
# File lib/aspera/temp_file_manager.rb, line 15
def initialize
  @created_files=[]
end

Public Instance Methods

cleanup() click to toggle source

call this on process exit

# File lib/aspera/temp_file_manager.rb, line 20
def cleanup
  @created_files.each do |filepath|
    File.delete(filepath) if File.file?(filepath)
  end
  @created_files=[]
end
cleanup_expired(temp_folder) click to toggle source
# File lib/aspera/temp_file_manager.rb, line 42
def cleanup_expired(temp_folder)
  # garbage collect undeleted files
  Dir.entries(temp_folder).each do |name|
    file_path=File.join(temp_folder,name)
    age_sec=(Time.now - File.stat(file_path).mtime).to_i
    # check age of file, delete too old
    if File.file?(file_path) and age_sec > FILE_LIST_AGE_MAX_SEC
      Log.log.debug("garbage collecting #{name}")
      File.delete(file_path)
    end
  end

end
new_file_path_global(base_name) click to toggle source

same as above but in global temp folder

# File lib/aspera/temp_file_manager.rb, line 37
def new_file_path_global(base_name)
  username = Etc.getlogin || Etc.getpwuid(Process.uid).name || 'unknown_user' rescue 'unknown_user'
  return new_file_path_in_folder(Etc.systmpdir,base_name+'_'+username+'_')
end
new_file_path_in_folder(temp_folder,add_base='') click to toggle source

ensure that provided folder exists, or create it, generate a unique filename @return path to that unique file

# File lib/aspera/temp_file_manager.rb, line 29
def new_file_path_in_folder(temp_folder,add_base='')
  FileUtils.mkdir_p(temp_folder) unless Dir.exist?(temp_folder)
  new_file=File.join(temp_folder,add_base+SecureRandom.uuid)
  @created_files.push(new_file)
  return new_file
end