Class: FakeFtp::ServerCommands::List

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_ftp/server_commands/list.rb

Instance Method Summary collapse

Instance Method Details

#run(ctx, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fake_ftp/server_commands/list.rb', line 6

def run(ctx, *args)
  if ctx.active? && ctx.command_state[:active_connection].nil?
    ctx.respond_with('425 Ain\'t no data port!')
    return
  end

  ctx.respond_with('150 Listing status ok, about to open data connection')
  data_client = if ctx.active?
                  ctx.command_state[:active_connection]
                else
                  ctx.data_server.accept
                end

  wildcards = ctx.build_wildcards(args)
  statlines = ctx.matching_files(wildcards).map do |f|
    %W[
      -rw-r--r--
      1
      owner
      group
      #{f.bytes}
      #{f.created.strftime('%b %d %H:%M')}
      #{f.name}
    ].join("\t")
  end
  data_client.write(statlines.join("\n"))
  data_client.close
  ctx.command_state[:active_connection] = nil

  '226 List information transferred'
end