Sha256: 0c59e78d6b11ce7b8b2bc15e0e4df5d1ec68d06e1248e9bf43160135978547b5

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

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 'Access denied', 550
      end
    end

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

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

    def ensure_directory(path)
      unless file_system.directory?(path)
        error 'Not a directory', 550
      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

6 entries across 6 versions & 1 rubygems

Version Path
ftpd-2.0.5 lib/ftpd/file_system_helper.rb
ftpd-2.0.4 lib/ftpd/file_system_helper.rb
ftpd-2.0.3 lib/ftpd/file_system_helper.rb
ftpd-2.0.2 lib/ftpd/file_system_helper.rb
ftpd-2.0.1 lib/ftpd/file_system_helper.rb
ftpd-2.0.0 lib/ftpd/file_system_helper.rb