@@ -30,6 +30,18 @@ class Console::CommandDispatcher::Stdapi::Fs
30
30
@@upload_opts = Rex ::Parser ::Arguments . new (
31
31
"-h" => [ false , "Help banner." ] ,
32
32
"-r" => [ false , "Upload recursively." ] )
33
+ #
34
+ # Options for the ls command
35
+ #
36
+ @@ls_opts = Rex ::Parser ::Arguments . new (
37
+ "-h" => [ false , "Help banner." ] ,
38
+ "-S" => [ true , "Search string." ] ,
39
+ "-t" => [ false , "Sort by time" ] ,
40
+ "-s" => [ false , "Sort by size" ] ,
41
+ "-r" => [ false , "Reverse sort order" ] ,
42
+ "-x" => [ false , "Show short file names" ] ,
43
+ "-l" => [ false , "List in long format (default)" ] ,
44
+ "-R" => [ false , "Recursively list subdirectories encountered" ] )
33
45
34
46
#
35
47
# List of supported commands.
@@ -223,22 +235,34 @@ def cmd_rm(*args)
223
235
224
236
alias :cmd_del :cmd_rm
225
237
226
- #
227
- # Move source to destination
228
- #
229
- def cmd_mv ( *args )
230
- if ( args . length < 2 )
231
- print_line ( "Usage: mv oldfile newfile" )
232
- return true
233
- end
238
+ #
239
+ # Move source to destination
240
+ #
241
+ def cmd_mv ( *args )
242
+ if ( args . length < 2 )
243
+ print_line ( "Usage: mv oldfile newfile" )
244
+ return true
245
+ end
246
+ client . fs . file . mv ( args [ 0 ] , args [ 1 ] )
247
+ return true
248
+ end
234
249
235
- client . fs . file . mv ( args [ 0 ] , args [ 1 ] )
250
+ alias :cmd_move :cmd_mv
251
+ alias :cmd_rename :cmd_mv
236
252
237
- return true
238
- end
253
+ #
254
+ # Move source to destination
255
+ #
256
+ def cmd_cp ( *args )
257
+ if ( args . length < 2 )
258
+ print_line ( "Usage: cp oldfile newfile" )
259
+ return true
260
+ end
261
+ client . fs . file . cp ( args [ 0 ] , args [ 1 ] )
262
+ return true
263
+ end
239
264
240
- alias :cmd_move :cmd_mv
241
- alias :cmd_rename :cmd_mv
265
+ alias :cmd_copy :cmd_cp
242
266
243
267
244
268
def cmd_download_help
@@ -387,7 +411,15 @@ def cmd_lpwd(*args)
387
411
388
412
alias cmd_getlwd cmd_lpwd
389
413
390
- def list_path ( path , columns , sort , order , short , recursive = false , depth = 0 )
414
+
415
+ def cmd_ls_help
416
+ print_line "Usage: ls [options]"
417
+ print_line
418
+ print_line "Lists contents of directory or file info, searchable"
419
+ print_line @@ls_opts . usage
420
+ end
421
+
422
+ def list_path ( path , columns , sort , order , short , recursive = false , depth = 0 , search_term = nil )
391
423
392
424
# avoid infinite recursion
393
425
if depth > 100
@@ -398,7 +430,8 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
398
430
'Header' => "Listing: #{ path } " ,
399
431
'SortIndex' => columns . index ( sort ) ,
400
432
'SortOrder' => order ,
401
- 'Columns' => columns )
433
+ 'Columns' => columns ,
434
+ 'SearchTerm' => search_term )
402
435
403
436
items = 0
404
437
@@ -419,8 +452,10 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
419
452
row . insert ( 4 , p [ 'FileShortName' ] || '' ) if short
420
453
421
454
if fname != '.' && fname != '..'
422
- tbl << row
423
- items += 1
455
+ if row . join ( ' ' ) =~ /#{ search_term } /
456
+ tbl << row
457
+ items += 1
458
+ end
424
459
425
460
if recursive && ffstat && ffstat . directory?
426
461
if client . fs . file . is_glob? ( path )
@@ -430,7 +465,7 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
430
465
child_path = path + ::File ::SEPARATOR + fname
431
466
end
432
467
begin
433
- list_path ( child_path , columns , sort , order , short , recursive , depth + 1 )
468
+ list_path ( child_path , columns , sort , order , short , recursive , depth + 1 , search_term )
434
469
rescue RequestError
435
470
end
436
471
end
@@ -448,39 +483,48 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
448
483
# Lists files
449
484
#
450
485
def cmd_ls ( *args )
451
-
452
- # Check sort column
453
- sort = args . include? ( '-S' ) ? 'Size' : 'Name'
454
- sort = args . include? ( '-t' ) ? 'Last modified' : sort
455
- args . delete ( '-S' )
456
- args . delete ( '-t' )
457
-
458
- # Check whether to include the short name option
459
- short = args . include? ( '-x' )
460
- args . delete ( '-x' )
461
-
462
- # Check sort order
463
- order = args . include? ( '-r' ) ? :reverse : :forward
464
- args . delete ( '-r' )
465
-
466
- # Check for recursive mode
467
- recursive = !args . delete ( '-R' ) . nil?
468
-
469
- args . delete ( '-l' )
470
-
471
- # Check for cries of help
472
- if args . length > 1 || args . any? { |a | a [ 0 ] == '-' }
473
- print_line ( 'Usage: ls [dir] [-x] [-S] [-t] [-r]' )
474
- print_line ( ' -x Show short file names' )
475
- print_line ( ' -S Sort by size' )
476
- print_line ( ' -t Sort by time modified' )
477
- print_line ( ' -r Reverse sort order' )
478
- print_line ( ' -l List in long format (default)' )
479
- print_line ( ' -R Recursively list subdirectories encountered.' )
480
- return true
481
- end
482
-
483
- path = args [ 0 ] || client . fs . dir . getwd
486
+ # Set defaults
487
+ path = client . fs . dir . getwd
488
+ search_term = nil
489
+ sort = 'Name'
490
+ short = nil
491
+ order = :forward
492
+ recursive = nil
493
+
494
+ # Parse the args
495
+ @@ls_opts . parse ( args ) { |opt , idx , val |
496
+ case opt
497
+ # Sort options
498
+ when '-s'
499
+ sort = 'Size'
500
+ when '-t'
501
+ sort = 'Last modified'
502
+ # Output options
503
+ when '-x'
504
+ short = true
505
+ when '-l'
506
+ short = nil
507
+ when '-r'
508
+ order = :reverse
509
+ when '-R'
510
+ recursive = true
511
+ # Search
512
+ when '-S'
513
+ search_term = val
514
+ if search_term . nil?
515
+ print_error ( "Enter a search term" )
516
+ return true
517
+ else
518
+ search_term = /#{ search_term } /nmi
519
+ end
520
+ # Help and path
521
+ when "-h"
522
+ cmd_ls_help
523
+ return 0
524
+ when nil
525
+ path = val
526
+ end
527
+ }
484
528
485
529
columns = [ 'Mode' , 'Size' , 'Type' , 'Last modified' , 'Name' ]
486
530
columns . insert ( 4 , 'Short Name' ) if short
@@ -499,7 +543,7 @@ def cmd_ls(*args)
499
543
500
544
stat = client . fs . file . stat ( stat_path )
501
545
if stat . directory?
502
- list_path ( path , columns , sort , order , short , recursive )
546
+ list_path ( path , columns , sort , order , short , recursive , 0 , search_term )
503
547
else
504
548
print_line ( "#{ stat . prettymode } #{ stat . size } #{ stat . ftype [ 0 , 3 ] } #{ stat . mtime } #{ path } " )
505
549
end
0 commit comments