lib/zold/copies.rb
def clean(max: 24 * 60 * 60)
      Futex.new(file, log: @log).open do
        list = load
        list.reject! do |s|
          if s[:time] >= Time.now - max
            false
          else
            @log.debug("Copy ##{s[:name]}/#{s[:host]}:#{s[:port]} is too old, over #{Age.new(s[:time])}")
            true
          end
        end
        save(list)
        deleted = 0
        files.each do |f|
          next unless list.find { |s| s[:name] == File.basename(f, Copies::EXT) }.nil?
          file = File.join(@dir, f)
          size = File.size(file)
          File.delete(file)
          @log.debug("Copy at #{f} deleted: #{Size.new(size)}")
          deleted += 1
        end
        list.select! do |s|
          cp = File.join(@dir, "#{s[:name]}#{Copies::EXT}")
          wallet = Wallet.new(cp)
          begin
            wallet.refurbish
            raise "Invalid protocol #{wallet.protocol} in #{cp}" unless wallet.protocol == Zold::PROTOCOL
            true
          rescue StandardError => e
            FileUtils.rm_rf(cp)
            @log.debug("Copy at #{cp} deleted: #{Backtrace.new(e)}")
            deleted += 1
            false
          end
        end
        save(list)
        deleted
      end
    end
def clean(max: 24 * 60 * 60)
      Futex.new(file, log: @log).open do
        list = load
        list.reject! do |s|
          if s[:time] >= Time.now - max
            false
          else
            @log.debug("Copy ##{s[:name]}/#{s[:host]}:#{s[:port]} is too old, over #{Age.new(s[:time])}")
            true
          end
        end
        save(list)
        deleted = 0
        files.each do |f|
          next unless list.find { |s| s[:name] == File.basename(f, Copies::EXT) }.nil?
          file = File.join(@dir, f)
          size = File.size(file)
          File.delete(file)
          @log.debug("Copy at #{f} deleted: #{Size.new(size)}")
          deleted += 1
        end
        list.select! do |s|
          cp = File.join(@dir, "#{s[:name]}#{Copies::EXT}")
          wallet = Wallet.new(cp)
          begin
            wallet.refurbish
            raise "Invalid protocol #{wallet.protocol} in #{cp}" unless wallet.protocol == Zold::PROTOCOL
            true
          rescue StandardError => e
            FileUtils.rm_rf(cp)
            @log.debug("Copy at #{cp} deleted: #{Backtrace.new(e)}")
            deleted += 1
            false
          end
        end
        save(list)
        deleted
      end
    end
def clean ( max : 24 * 60 * 60 ) Futex . new ( file , log : @log ) . open do list = load list . reject! do | s | if s [ :time ] >= Time . now - max false else @log . debug ( "Copy ##{s[:name]}/#{s[:host]}:#{s[:port]} is too old, over #{Age.new(s[:time])}" ) true end end save ( list ) deleted = 0 files . each do | f | next unless list . find { | s | s [ :name ] == File . basename ( f , Copies :: EXT ) } . nil? file = File . join ( @dir , f ) size = File . size ( file ) File . delete ( file ) @log . debug ( "Copy at #{f} deleted: #{Size.new(size)}" ) deleted += 1 end list . select! do | s | cp = File . join ( @dir , "#{s[:name]}#{Copies::EXT}" ) wallet = Wallet . new ( cp ) begin wallet . refurbish raise "Invalid protocol #{wallet.protocol} in #{cp}" unless wallet . protocol == Zold :: PROTOCOL true rescue StandardError => e FileUtils . rm_rf ( cp ) @log . debug ( "Copy at #{cp} deleted: #{Backtrace.new(e)}" ) deleted += 1 false end end save ( list ) deleted end end
Delete all copies that are older than the "max" age provided, in seconds.
Delete all copies that are older than the max age provided in seconds .