Sha256: 3e978f1323ea061f1e3911ba73970faa4cd3d833b9555849f9bddfcd0c80d8ce

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

require_relative 'command_handler'

module Ftpd

  module FileSystemHelper

    def path_list(path)
      if file_system.directory?(path)
        path = File.join(path, '*')
      end
      file_system.dir(path).sort
    end

    def ensure_file_system_supports(method)
      unless file_system.respond_to?(method)
        unimplemented_error
      end
    end

    def ensure_accessible(path)
      unless file_system.accessible?(path)
        error '550 Access denied'
      end
    end

    def ensure_exists(path)
      unless file_system.exists?(path)
        error '550 No such file or directory'
      end
    end

    def ensure_does_not_exist(path)
      if file_system.exists?(path)
        error '550 Already exists'
      end
    end

    def ensure_directory(path)
      unless file_system.directory?(path)
        error '550 Not a directory'
      end
    end

    def unique_path(path)
      suffix = nil
      100.times do
        path_with_suffix = [path, suffix].compact.join('.')
        unless file_system.exists?(path_with_suffix)
          return path_with_suffix
        end
        suffix = generate_suffix
      end
      raise "Unable to find unique path"
    end

    private

    def generate_suffix
      set = ('a'..'z').to_a
      8.times.map do
        set[rand(set.size)]
      end.join
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ftpd-0.16.0 lib/ftpd/file_system_helper.rb
ftpd-0.15.0 lib/ftpd/file_system_helper.rb
ftpd-0.14.0 lib/ftpd/file_system_helper.rb
ftpd-0.13.0 lib/ftpd/file_system_helper.rb
ftpd-0.12.0 lib/ftpd/file_system_helper.rb