Module: Benry::UnixCommand

Defined in:
lib/benry/unixcommand.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

CHMOD_MODES =
{
  ## perm => [user, group, other, all]
  'r' => [ 0400,  0040,  0004,  0444],
  'w' => [ 0200,  0020,  0002,  0222],
  'x' => [ 0100,  0010,  0001,  0111],
  's' => [04000, 02000,     0, 06000],
  't' => [    0,     0,     0, 01000],
}.freeze

Class Method Summary collapse

Class Method Details

.__build_echoback_str(args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/benry/unixcommand.rb', line 153

def __build_echoback_str(args)
  #; [!4dcra] if arg is one array, quotes or escapes arguments.
  #; [!ueoov] if arg is multiple string, quotes or escapes arguments.
  #; [!hnp41] if arg is one string, not quote nor escape argument.
  echoback_str = (
    if    args[0].is_a?(Array) ; args[0].collect {|x| __qq(x) }.join(" ")
    elsif args.length == 1     ; args[0]
    else                       ; args.collect {|x| __qq(x) }.join(" ")
    end
  )
end

.__capture(cmd, args, kws, ignore_error) ⇒ Object

:nodoc:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/benry/unixcommand.rb', line 232

def __capture(cmd, args, kws, ignore_error)   # :nodoc:
  #; [!5p4dw] calls 'Open3.capture2()'.
  #; [!jgn71] calls 'Open3.capture2e()'.
  #; [!n91rh] calls 'Open3.capture3()'.
  #; [!2s1by] error when command failed.
  #; [!qr3ka] error when command failed.
  #; [!thnyv] error when command failed.
  #; [!357e1] ignore errors even if command failed.
  #; [!o0b7c] ignore errors even if command failed.
  #; [!rwfiu] ignore errors even if command failed.
  require 'open3' unless defined?(::Open3)
  echoback(args.join(" ")) if __echoback?()
  arr = ::Open3.__send__(cmd, *args, **kws)
  ignore_error || arr[-1].exitstatus == 0  or
    raise "Command failed with status (#{arr[-1].exitstatus}): #{args.join(' ')}"
  return arr if ignore_error
  arr.pop()
  return arr.length == 1 ? arr[0] : arr
end

.__chmod(cmd, args, _debug = false) ⇒ Object

:nodoc:



919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# File 'lib/benry/unixcommand.rb', line 919

def __chmod(cmd, args, _debug=false)    # :nodoc:
  #; [!pmmvj] echobacks command and arguments.
  optchars = __prepare(cmd, args, "R", nil)
  recursive = optchars.include?("R")
  #; [!94hl9] error when mode not specified.
  mode_s = args.shift()  or
    __err "#{cmd}: argument required."
  #; [!c8zhu] mode can be integer or octal string.
  mode_i = nil; mask = op = nil
  case mode_s
  when Integer
    mode_i = mode_s
    #; [!j3nqp] error when integer mode is invalid.
    (0..0777).include?(mode_i)  or
      __err "#{cmd}: #{mode_i}: Invalid file mode."
  when /\A[0-7][0-7][0-7][0-7]?\z/
    mode_i = mode_s.to_i(8)    # octal -> decimal
  #; [!ox3le] converts 'u+r' style mode into mask.
  when /\A([ugoa])([-+])([rwxst])\z/
    who = $1; op = $2; perm = $3
    i = "ugoa".index(who)  or raise "** assertion failed: who=#{who.inspect}"
    mask = CHMOD_MODES[perm][i]
  #; [!axqed] error when mode is invalid.
  else
    __err "#{cmd}: #{mode_s}: Invalid file mode."
  end
  return mode_i, mask if _debug
  #; [!ru371] expands file pattern.
  #; [!ou3ih] error when file not exist.
  #; [!8sd4b] error when file pattern not matched to anything.
  filenames = __glob_filenames(cmd, args, false) do |arg, filenames|
    __err "#{cmd}: #{arg}: No such file or directory."
  end
  #; [!q1psx] changes file mode.
  #; [!4en6n] skips symbolic links.
  #; [!4e7ve] changes mode recursively if '-R' option specified.
  __each_file(filenames, recursive) do |type, fpath|
    next if type == :sym
    if mode_i
      mode = mode_i
    else
      mode = File.stat(fpath).mode
      mode = case op
             when '+' ; mode | mask
             when '-' ; mode & ~mask
             end
    end
    File.chmod(mode, fpath)
  end
end

.__chown(cmd, args, _debug = false) ⇒ Object

:nodoc:



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/benry/unixcommand.rb', line 1006

def __chown(cmd, args, _debug=false)    # :nodoc:
  #; [!5jqqv] echobacks command and arguments.
  optchars = __prepare(cmd, args, "R", nil)
  recursive = optchars.include?("R")
  #; [!hkxgu] error when owner not specified.
  owner = args.shift()  or
    __err "#{cmd}: argument required."
  #; [!0a35v] accepts integer as user id.
  owner = owner.to_s if owner.is_a?(Integer)
  #; [!b5qud] accepts 'user:group' argument.
  #; [!18gf0] accepts 'user' argument.
  #; [!mw5tg] accepts ':group' argument.
  case owner
  when /\A(\w+):?\z/     ; user = $1 ; group = nil
  when /\A(\w+):(\w+)\z/ ; user = $1 ; group = $2
  when /\A:(\w+)\z/      ; user = nil; group = $1
  else
    __err "#{cmd}: #{owner}: invalid owner."
  end
  #; [!jyecc] converts user name into user id.
  #; [!kt7mp] error when invalid user name specified.
  begin
    user_id = user ? __chown_uid(user) : nil
  rescue ArgumentError
    __err "#{cmd}: #{user}: unknown user name."
  end
  #; [!f7ye0] converts group name into group id.
  #; [!szlsb] error when invalid group name specified.
  begin
    group_id = group ? __chown_gid(group) : nil
  rescue ArgumentError
    __err "#{cmd}: #{group}: unknown group name."
  end
  return user_id, group_id if _debug
  #; [!138eh] expands file pattern.
  #; [!tvpey] error when file not exist.
  #; [!ovkk8] error when file pattern not matched to anything.
  filenames = __glob_filenames(cmd, args, false) do |arg, filenames|
    __err "#{cmd}: #{arg}: No such file or directory."
  end
  #
  #; [!7tf3k] changes file mode.
  #; [!m6mrg] skips symbolic links.
  #; [!b07ff] changes file mode recursively if '-R' option specified.
  __each_file(filenames, recursive) do |type, fpath|
    next if type == :sym
    File.chown(user_id, group_id, fpath)
  end
end

.__chown_gid(group) ⇒ Object

:nodoc:



1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/benry/unixcommand.rb', line 1065

def __chown_gid(group)    # :nodoc:
  require 'etc' unless defined?(::Etc)
  case group
  when nil       ; return nil
  when /\A\d+\z/ ; return group.to_i
  else           ; return (x = Etc.getgrnam(group)) ? x.gid : nil  # ArgumentError
  end
end

.__chown_uid(user) ⇒ Object

:nodoc:



1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/benry/unixcommand.rb', line 1056

def __chown_uid(user)    # :nodoc:
  require 'etc' unless defined?(::Etc)
  case user
  when nil       ; return nil
  when /\A\d+\z/ ; return user.to_i
  else           ; return (x = Etc.getpwnam(user)) ? x.uid : nil  # ArgumentError
  end
end

.__cp(cmd, args, to: nil, overwrite: nil) ⇒ Object

:nodoc:



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/benry/unixcommand.rb', line 407

def __cp(cmd, args, to: nil, overwrite: nil)  # :nodoc:
  #; [!mtuec] echoback copy command and arguments.
  optchars = __prepare(cmd, args, "prfl", to)
  recursive = optchars.include?("r")
  preserve  = optchars.include?("p")
  ignore    = optchars.include?("f")
  hardlink  = optchars.include?("l")
  #; [!u98f8] when `to:` keyword arg not specified...
  if ! to
    #; [!u39p0] error when number of arguments is not 2.
    #; [!fux6x] error when source pattern matched to multiple files.
    #; [!y74ux] error when destination pattern matched to multiple files.
    src, dst = __filecheck1(cmd, args)
    #
    if File.file?(src)
      #; [!qfidz] error when destination is a directory.
      ! File.directory?(dst)  or
        __err "#{cmd}: #{dst}: cannot copy into directory (requires `to: '#{dst}'` keyword option)."
      #; [!073so] (cp) error when destination already exists to avoid overwriting it.
      #; [!cpr7l] (cp!) overwrites existing destination file.
      ! File.exist?(dst) || overwrite  or
        __err "#{cmd}: #{dst}: file already exists (to overwrite it, call `#{cmd}!` instead of `#{cmd}`)."
    elsif File.directory?(src)
      #; [!0tw8r] error when source is a directory but '-r' not specified.
      recursive  or
        __err "#{cmd}: #{src}: is a directory (requires `:-r` option)."
      #; [!lf6qi] error when target already exists.
      ! File.exist?(dst)  or
        __err "#{cmd}: #{dst}: already exists."
    elsif File.exist?(src)
      #; [!4xxpe] error when source is a special file.
      __err "#{cmd}: #{src}: cannot copy special file."
    else
      #; [!urh40] do nothing if source file not found and '-f' option specified.
      return if ignore
      #; [!lr2bj] error when source file not found and '-f' option not specified.
      __err "#{cmd}: #{src}: not found."
    end
    #; [!lac46] keeps file mtime if '-p' option specified.
    #; [!d49vw] not keep file mtime if '-p' option not specified.
    #; [!kqgdl] copy a directory recursively if '-r' option specified.
    #; [!ko4he] copy a file into new file if '-r' option not specifieid.
    #; [!ubthp] creates hard link instead of copy if '-l' option specified.
    #; [!yu51t] error when copying supecial files such as character device.
    #FileUtils.cp_r src, dst, preserve: preserve, verbose: false if recursive
    #FileUtils.cp src, dst, preserve: preserve, verbose: false unless recursive
    __cp_file(cmd, src, dst, preserve, hardlink)
  #; [!z8xce] when `to:` keyword arg specified...
  else
    #; [!ms2sv] error when destination directory not exist.
    #; [!q9da3] error when destination pattern matched to multiple filenames.
    #; [!lg3uz] error when destination is not a directory.
    dir = __glob_onedir(cmd, to)
    #; [!slavo] error when file not exist but '-f' option not specified.
    filenames = __glob_filenames(cmd, args, ignore)
    #; [!1ceaf] (cp) error when target file or directory already exists.
    #; [!melhx] (cp!) overwrites existing files.
    __filecheck2(cmd, filenames, dir, overwrite)
    #; [!bi897] error when copying directory but '-r' option not specified.
    if ! recursive
      filenames.each do |fname|
        ! File.directory?(fname)  or
          __err "#{cmd}: #{fname}: cannot copy directory (add '-r' option to copy it)."
      end
    end
    #; [!k8gyx] keeps file timestamp (mtime) if '-p' option specified.
    #; [!zoun9] not keep file timestamp (mtime) if '-p' option not specified.
    #; [!654d2] copy files recursively if '-r' option specified.
    #; [!i5g8r] copy files non-recursively if '-r' option not specified.
    #; [!p7ah8] creates hard link instead of copy if '-l' option specified.
    #; [!e90ii] error when copying supecial files such as character device.
    #FileUtils.cp_r filenames, dir, preserve: preserve, verbose: false if recursive
    #FileUtils.cp   filenames, dir, preserve: preserve, verbose: false unless recursive
    filenames.each do |fname|
      newfile = File.join(dir, File.basename(fname))
      __cp_file(cmd, fname, newfile, preserve, hardlink)
    end
  end
end

.__cp_file(cmd, srcpath, dstpath, preserve, hardlink, bufsize = 4096) ⇒ Object

:nodoc:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/benry/unixcommand.rb', line 487

def __cp_file(cmd, srcpath, dstpath, preserve, hardlink, bufsize=4096)   # :nodoc:
  ftype = File.ftype(srcpath)
  case ftype
  when 'link'
    File.symlink(File.readlink(srcpath), dstpath)
  when 'file'
    if hardlink
      File.link(srcpath, dstpath)
    else
      File.open(srcpath, 'rb') do |sf|
        File.open(dstpath, 'wb') do |df; bytes|
          df.write(bytes) while (bytes = sf.read(bufsize))
        end
      end
      __cp_meta(srcpath, dstpath) if preserve
    end
  when 'directory'
    Dir.mkdir(dstpath)
    Dir.open(srcpath) do |d|
      d.each do |x|
        next if x == '.' || x == '..'
        __cp_file(cmd, File.join(srcpath, x), File.join(dstpath, x), preserve, hardlink, bufsize)
      end
    end
    __cp_meta(srcpath, dstpath) if preserve
  else # characterSpecial, blockSpecial, fifo, socket, unknown
    __err "#{cmd}: #{srcpath}: cannot copy #{ftype} file."
  end
end

.__cp_meta(src, dst) ⇒ Object

:nodoc:



517
518
519
520
521
522
# File 'lib/benry/unixcommand.rb', line 517

def __cp_meta(src, dst)    # :nodoc:
  stat = File.stat(src)
  File.chmod(stat.mode, dst)
  File.chown(stat.uid, stat.gid, dst)
  File.utime(stat.atime, stat.mtime, dst)
end

.__each_file(filenames, recursive, &b) ⇒ Object

:nodoc:



970
971
972
973
974
# File 'lib/benry/unixcommand.rb', line 970

def __each_file(filenames, recursive, &b)    # :nodoc:
  filenames.each do |fname|
    __each_path(fname, recursive, &b)
  end
end

.__each_path(fpath, recursive, &b) ⇒ Object

:nodoc:



976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/benry/unixcommand.rb', line 976

def __each_path(fpath, recursive, &b)    # :nodoc:
  if File.symlink?(fpath)
    yield :sym, fpath
  elsif File.directory?(fpath) && recursive
    Dir.open(fpath) do |d|
      d.each do |x|
        next if x == '.' || x == '..'
        __each_path(File.join(fpath, x), recursive, &b)
      end
    end
    yield :dir, fpath
  else
    yield :file, fpath
  end
end

.__echo(cmd, args) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/benry/unixcommand.rb', line 82

def __echo(cmd, args)
  #; [!mzbdj] echoback command arguments.
  optchars = __prepare(cmd, args, "n", nil)
  not_nl   = optchars.include?('n')
  #; [!cjggd] prints arguments.
  #; [!vhpw3] not print newline at end if '-n' option specified.
  print args.join(" ")
  puts "" unless not_nl
end

.__echoback?Boolean

alias fu_output_message echoback private :fu_output_message

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/benry/unixcommand.rb', line 47

def __echoback?()
  #; [!ik00u] returns value of `@__BENRY_ECHOBACK` or `$BENRY_ECHOBACK`.
  #; [!1hp69] instance var `@__BENRY_ECHOBACK` is prior than `$BENRY_ECHOBACK`.
  return @__BENRY_ECHOBACK != nil ? @__BENRY_ECHOBACK : $BENRY_ECHOBACK
end

.__err(msg) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
# File 'lib/benry/unixcommand.rb', line 25

def __err(msg)
  raise ArgumentError.new(msg)
end

.__filecheck1(cmd, args) ⇒ Object

:nodoc:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/benry/unixcommand.rb', line 339

def __filecheck1(cmd, args)  # :nodoc:
  n = args.length
  if    n < 2 ; __err "#{cmd}: requires two arguments."
  elsif n > 2 ; __err "#{cmd}: too much arguments."
  end
  #
  arr = Dir.glob(args[0]); n = arr.length
  if    n < 1 ; src = args[0]
  elsif n > 1 ; __err "#{cmd}: #{args[0]}: unexpectedly matched to multiple files (#{arr.sort.join(', ')})."
  else        ; src = arr[0]
  end
  #
  arr = Dir.glob(args[1]); n = arr.length
  if    n < 1 ; dst = args[1]
  elsif n > 1 ; __err "#{cmd}: #{args[1]}: unexpectedly matched to multiple files (#{arr.sort.join(', ')})."
  else        ; dst = arr[0]
  end
  #
  return src, dst
end

.__filecheck2(cmd, filenames, dir, overwrite) ⇒ Object

:nodoc:



371
372
373
374
375
376
377
378
379
# File 'lib/benry/unixcommand.rb', line 371

def __filecheck2(cmd, filenames, dir, overwrite)   # :nodoc:
  if ! overwrite
    filenames.each do |fname|
      newfile = File.join(dir, File.basename(fname))
      ! File.exist?(newfile)  or
        __err "#{cmd}: #{newfile}: file or directory already exists (to overwrite it, call '#{cmd}!' instead of '#{cmd}')."
    end
  end
end

.__glob_filenames(cmd, args, ignore) ⇒ Object

:nodoc:



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/benry/unixcommand.rb', line 381

def __glob_filenames(cmd, args, ignore)   # :nodoc:
  filenames = []
  block_p = block_given?()
  args.each do |arg|
    arr = Dir.glob(arg)
    if ! arr.empty?
      filenames.concat(arr)
    elsif block_p
      yield arg, filenames
    else
      ignore  or
        __err "#{cmd}: #{arg}: file or directory not found (add '-f' option to ignore missing files)."
    end
  end
  return filenames
end

.__glob_onedir(cmd, to) ⇒ Object

:nodoc:



360
361
362
363
364
365
366
367
368
369
# File 'lib/benry/unixcommand.rb', line 360

def __glob_onedir(cmd, to)    # :nodoc:
  arr = Dir.glob(to); n = arr.length
  if    n < 1 ; __err "#{cmd}: #{to}: directory not found."
  elsif n > 1 ; __err "#{cmd}: #{to}: unexpectedly matched to multiple filenames (#{arr.sort.join(', ')})."
  end
  dir = arr[0]
  File.directory?(dir)  or
    __err "#{cmd}: #{dir}: Not a directory."
  return dir
end

.__ln(cmd, args, to: nil, overwrite: nil) ⇒ Object

:nodoc:



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/benry/unixcommand.rb', line 735

def __ln(cmd, args, to: nil, overwrite: nil)    # :nodoc:
  #; [!ycp6e] echobacks command and arguments.
  #; [!umk6m] keyword arg `to: xx` is echobacked as `-t xx`.
  optchars = __prepare(cmd, args, "s", to) do |optchars, args_, to_|
    buf = [cmd]
    buf << "-t #{to_}" if to_
    buf << "-#{optchars}n"         # `-n` means "don't follow symbolic link"
    echoback(buf.concat(args).join(" ")) if __echoback?()
  end
  symbolic = optchars.include?("s")
  #; [!qtbp4] when `to:` keyword argument not specified...
  if !to
    #; [!n1zpi] error when number of arguments is not 2.
    #; [!2rxqo] error when source pattern matched to multiple files.
    #; [!ysxdq] error when destination pattern matched to multiple files.
    src, dst = __filecheck1(cmd, args)
    #
    if ! symbolic
      #; [!4ry8j] (hard link) error when source file not exists.
      File.exist?(src)  or
        __err "#{cmd}: #{src}: No such file or directory."
      #; [!tf29w] (hard link) error when source is a directory.
      ! File.directory?(src)  or
        __err "#{cmd}: #{src}: Is a directory."
    end
    #; [!zmijh] error when destination is a directory without `to:` keyword argument.
    if File.directory?(dst)
      __err "#{cmd}: #{dst}: cannot create link under directory without `to:` keyword option."
    end
    #; [!nzci0] (ln) error when destination already exists.
    if ! overwrite
      ! File.exist?(dst)  or
        __err "#{cmd}: #{dst}: File exists (to overwrite it, call `#{cmd}!` instead of `#{cmd}`)."
    #; [!dkqgq] (ln!) overwrites existing destination file.
    else
      File.unlink(dst) if File.symlink?(dst) || File.file?(dst)
    end
    #; [!oxjqv] create symbolic link if '-s' option specified.
    #; [!awig1] (symlink) can create symbolic link to non-existing file.
    #; [!5kl3w] (symlink) can create symbolic link to directory.
    if symbolic
      File.unlink(dst) if overwrite && File.symlink?(dst)
      File.symlink(src, dst)
    #; [!sb29p] create hard link if '-s' option not specified.
    else
      File.link(src, dst) unless symbolic
    end
  #; [!5x2wr] when `to:` keyword argument specified...
  else
    #; [!5gfxk] error when destination directory not exist.
    #; [!euu5d] error when destination pattern matched to multiple filenames.
    #; [!42nb7] error when destination is not a directory.
    dir = __glob_onedir(cmd, to)
    #; [!x7wh5] (symlink) can create symlink to unexisting file.
    #; [!ml1vm] (hard link) error when source file not exist.
    filenames = __glob_filenames(cmd, args, false) do |arg, filenames|
      if symbolic
        filenames << arg
      else
        __err "#{cmd}: #{arg}: No such file or directory."
      end
    end
    #; [!mwukw] (ln) error when target file or directory already exists.
    #; [!c3vwn] (ln!) error when target file is a directory.
    #__filecheck2(cmd, filenames, dir, overwrite)
    filenames.each do |fname|
      newfile = File.join(dir, fname)
      if File.symlink?(newfile)
        overwrite  or
          __err "#{cmd}: #{newfile}: symbolic link already exists (to overwrite it, call `#{cmd}!` instead of `#{cmd}`)."
      elsif File.file?(newfile)
        overwrite  or
          __err "#{cmd}: #{newfile}: File exists (to overwrite it, call `#{cmd}!` instead of `#{cmd}`)."
      elsif File.directory?(newfile)
        __err "#{cmd}: #{newfile}: directory already exists."
      end
    end
    #
    filenames.each do |fname|
      newfile = File.join(dir, File.basename(fname))
      #; [!bfcki] (ln!) overwrites existing symbolic links.
      #; [!ipy2c] (ln!) overwrites existing files.
      if File.symlink?(newfile) || File.file?(newfile)
        File.unlink(newfile) if overwrite
      end
      #; [!c8hpp] (hard link) create hard link under directory if '-s' option not specified.
      #; [!9tv9g] (symlik) create symbolic link under directory if '-s' option specified.
      if symbolic
        File.symlink(fname, newfile)
      else
        File.link(fname, newfile)
      end
    end
  end
end

.__mkdir(cmd, args) ⇒ Object

:nodoc:



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/benry/unixcommand.rb', line 626

def __mkdir(cmd, args)    # :nodoc:
  optchars = __prepare(cmd, args, "pm", nil)
  mkpath = optchars.include?("p")
  mode   = optchars.include?("m") ? args.shift() : nil
  #; [!wd7rm] error when mode is invalid.
  case mode
  when nil              ; # pass
  when Integer          ; # pass
  when /\A\d+\z/        ; mode = mode.to_i(8)
  when /\A\w+[-+]\w+\z/ ; __err "#{cmd}: #{mode}: '-m' option doesn't support this style mode (use '0755' tyle instead)."
  else                  ; __err "#{cmd}: #{mode}: invalid mode."
  end
  #; [!xusor] raises error when argument not specified.
  ! args.empty?  or
    __err "#{cmd}: argument required."
  #
  filenames = []
  args.each do |arg|
    arr = Dir.glob(arg)
    if arr.empty?
      #; [!xx7mv] error when parent directory not exist but '-p' option not specified.
      if ! File.directory?(File.dirname(arg))
        mkpath  or
          __err "#{cmd}: #{arg}: parent directory not exists (add '-p' to create it)."
      end
      filenames << arg
    #; [!51pmg] error when directory already exists but '-p' option not specified.
    #; [!pydy1] ignores existing directories if '-p' option specified.
    elsif File.directory?(arr[0])
      mkpath  or
        __err "#{cmd}: #{arr[0]}: directory already exists."
    #; [!om8a6] error when file already exists.
    else
      __err "#{cmd}: #{arr[0]}: file exists."
    end
  end
  #; [!jc8hm] '-m' option specifies mode of new directories.
  if mkpath
    #; [!0zeu3] create intermediate path if '-p' option specified.
    #FileUtils.mkdir_p args, mode: mode, verbose: false
    pr = proc do |fname|
      parent = File.dirname(fname)
      parent != fname  or
        raise "** assertion failed: fname=#{fname.inspect}, parent=#{parent.inspect}"
      pr.call(parent) unless File.directory?(parent)
      Dir.mkdir(fname)
      File.chmod(mode, fname) if mode
    end
    filenames.each {|fname| pr.call(fname) }
  else
    #; [!l0pr8] create directories if '-p' option not specified.
    #FileUtils.mkdir   args, mode: mode, verbose: false
    filenames.each {|fname|
      Dir.mkdir(fname)
      File.chmod(mode, fname) if mode
    }
  end
end

.__mkpath(dirpath, pathcache = {}) ⇒ Object



1125
1126
1127
1128
1129
1130
1131
1132
# File 'lib/benry/unixcommand.rb', line 1125

def __mkpath(dirpath, pathcache={})
  if ! pathcache.include?(dirpath)
    parent = File.dirname(dirpath)
    __mkpath(parent, pathcache) unless parent == dirpath
    Dir.mkdir(dirpath) unless File.exist?(dirpath)
    pathcache[dirpath] = true
  end
end

.__mv(cmd, args, to: nil, overwrite: nil) ⇒ Object

:nodoc:



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/benry/unixcommand.rb', line 533

def __mv(cmd, args, to: nil, overwrite: nil)  # :nodoc:
  #; [!ajm59] echoback command and arguments.
  optchars = __prepare(cmd, args, "f", to)
  ignore = optchars.include?("f")
  #; [!g732t] when `to:` keyword argument not specified...
  if !to
    #; [!0f106] error when number of arguments is not 2.
    #; [!xsti2] error when source pattern matched to multiple files.
    #; [!4wam3] error when destination pattern matched to multiple files.
    src, dst = __filecheck1(cmd, args)
    #
    if !File.exist?(src)
      #; [!397kn] do nothing when file or directory not found but '-f' option specified.
      return if ignore
      #; [!1z89i] error when source file or directory not found.
      __err "#{cmd}: #{src}: not found."
    end
    #
    if File.exist?(dst)
      #; [!ude1j] cannot move file into existing directory.
      if File.file?(src) && File.directory?(dst)
        __err "#{cmd}: cannot move file '#{src}' into directory '#{dst}' without 'to:' keyword option."
      end
      #; [!2aws0] cannt rename directory into existing file or directory.
      if File.directory?(src)
        __err "#{cmd}: cannot rename directory '#{src}' to existing file or directory."
      end
      #; [!3fbpu] (mv) error when destination file already exists.
      #; [!zpojx] (mv!) overwrites existing files.
      overwrite  or
        __err "#{cmd}: #{dst}: already exists (to overwrite it, call `#{cmd}!` instead of `#{cmd}`)."
    end
    #; [!9eqt3] rename file or directory.
    #FileUtils.mv src, dst, verbose: false
    File.rename(src, dst)
  #; [!iu87y] when `to:` keyword argument specified...
  else
    #; [!wf6pc] error when destination directory not exist.
    #; [!8v4dn] error when destination pattern matched to multiple filenames.
    #; [!ppr6n] error when destination is not a directory.
    dir = __glob_onedir(cmd, to)
    #; [!bjqwi] error when file not exist but '-f' option not specified.
    filenames = __glob_filenames(cmd, args, ignore)
    #; [!k21ns] (mv) error when target file or directory already exists.
    #; [!vcaf5] (mv!) overwrites existing files.
    __filecheck2(cmd, filenames, dir, overwrite)
    #; [!ri2ia] move files into existing directory.
    #FileUtils.mv filenames, dir, verbose: false
    filenames.each do |fname|
      newfile = File.join(dir, File.basename(fname))
      File.rename(fname, newfile)
    end
  end
end

.__popen(cmd, args, kws, &b) ⇒ Object

:nodoc:



216
217
218
219
220
221
222
223
# File 'lib/benry/unixcommand.rb', line 216

def __popen(cmd, args, kws, &b)   # :nodoc:
  #; [!8que2] calls 'Open3.popen2()'.
  #; [!s6g1r] calls 'Open3.popen2e()'.
  #; [!evlx7] calls 'Open3.popen3()'.
  require 'open3' unless defined?(::Open3)
  echoback(args.join(" ")) if __echoback?()
  return ::Open3.__send__(cmd, *args, **kws, &b)
end

.__prepare(cmd, args, short_opts, to = nil) ⇒ Object

:nodoc:



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/benry/unixcommand.rb', line 309

def __prepare(cmd, args, short_opts, to=nil)   # :nodoc:
  optchars = ""
  errmsg = nil
  while args[0].is_a?(Symbol)
    optstr = args.shift().to_s.sub(/^-/, '')
    optstr.each_char do |c|
      if short_opts.include?(c)
        optchars << c
      else
        errmsg ||= "#{cmd}: -#{c}: unknown option."
      end
    end
  end
  #
  if block_given?()
    yield optchars, args, to
  elsif __echoback?()
    buf = [cmd]
    buf << "-#{optchars}" unless optchars.empty?
    buf.concat(args)
    buf << to if to
    echoback(buf.join(" "))
  else
    nil
  end
  #
  __err errmsg if errmsg
  return optchars
end

.__qq(str) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/benry/unixcommand.rb', line 165

def __qq(str)
  if str =~ /\s/
    return "\"#{str.gsub(/"/, '\\"')}\""
  else
    return str.gsub(/(['"\\])/, '\\\\\1')
  end
end

.__rm(cmd, args) ⇒ Object

:nodoc:



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/benry/unixcommand.rb', line 593

def __rm(cmd, args)    # :nodoc:
  #; [!bikrs] echoback command and arguments.
  optchars = __prepare(cmd, args, "rf", nil)
  recursive = optchars.include?("r")
  ignore    = optchars.include?("f")
  #; [!va1j0] error when file not exist but '-f' option not specified.
  #; [!t6vhx] ignores missing files if '-f' option specified.
  filenames = __glob_filenames(cmd, args, ignore)
  #; [!o92yi] cannot remove directory unless '-r' option specified.
  if ! recursive
    filenames.each do |fname|
      ! File.directory?(fname)  or
        __err "#{cmd}: #{fname}: cannot remove directory (add '-r' option to remove it)."
    end
  end
  #; [!srx8w] remove directories recursively if '-r' option specified.
  #; [!mdgjc] remove files if '-r' option not specified.
  #FileUtils.rm_r filenames, verbose: false, secure: true if recursive
  #FileUtils.rm   filenames, verbose: false unless recursive
  __each_file(filenames, recursive) do |type, fpath|
    case type
    when :sym  ; File.unlink(fpath)
    when :dir  ; Dir.rmdir(fpath)
    when :file ; File.unlink(fpath)
    end
  end
end

.__rmdir(cmd, args) ⇒ Object

:nodoc:



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/benry/unixcommand.rb', line 690

def __rmdir(cmd, args)   # :nodoc:
  optchars = __prepare(cmd, args, "", nil)
  _ = optchars           # avoid waring of `ruby -wc`
  #; [!bqhdd] error when argument not specified.
  ! args.empty?  or
    __err "#{cmd}: argument required."
  #; [!o1k3g] error when directory not exist.
  dirnames = __glob_filenames(cmd, args, false) do |arg, filenames|
    __err "#{cmd}: #{arg}: No such file or directory."
  end
  #
  dirnames.each do |dname|
    #; [!ch5rq] error when directory is a symbolic link.
    if File.symlink?(dname)
      __err "#{cmd}: #{dname}: Not a directory."
    #; [!igfti] error when directory is not empty.
    elsif File.directory?(dname)
      found = Dir.open(dname) {|d|
        d.any? {|x| x != '.' && x != '..' }
      }
      ! found  or
        __err "#{cmd}: #{dname}: Directory not empty."
    #; [!qnnqy] error when argument is not a directory.
    elsif File.exist?(dname)
      __err "#{cmd}: #{dname}: Not a directory."
    else
      raise "** assertion failed: dname=#{dname.inspect}"
    end
  end
  #; [!jgmw7] remove empty directories.
  #FileUtils.rmdir dirnames, verbose: false
  dirnames.each do |dname|
    Dir.rmdir(dname)
  end
end

.__ruby(cmd, args, ignore_error, &b) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/benry/unixcommand.rb', line 197

def __ruby(cmd, args, ignore_error, &b)
  #; [!98qro] echoback command and args.
  #; [!u5f5l] run ruby command.
  #; [!2jano] returns process status object if ruby command succeeded.
  #; [!69clt] (ruby) error when ruby command failed.
  #; [!z1f03] (ruby!) ignores error even when ruby command failed.
  ruby = RbConfig.ruby
  if args.length == 1
    __sys(cmd, ["#{ruby} #{args[0]}"], ignore_error, &b)
  else
    __sys(cmd, [ruby]+args, ignore_error, &b)
  end
end

.__store(cmd, args, overwrite, to:) ⇒ Object



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/benry/unixcommand.rb', line 1083

def __store(cmd, args, overwrite, to:)
  #; [!9wr1o] error when `to:` keyword argument not specified.
  ! to.nil?  or
    __err "#{cmd}: 'to:' keyword argument required."
  #; [!n43u2] echoback command and arguments.
  optchars = __prepare(cmd, args, "pfl", to)
  preserve = optchars.include?("p")
  ignore   = optchars.include?("f")
  hardlink = optchars.include?("l")
  #; [!588e5] error when destination directory not exist.
  #; [!lm43y] error when destination pattern matched to multiple filenames.
  #; [!u5zoy] error when destination is not a directory.
  dir = __glob_onedir(cmd, to)
  #; [!g1duw] error when absolute path specified.
  args.each do |arg|
    #! File.absolute_path?(arg)  or   # Ruby >=  2.7
    File.absolute_path(arg) != arg  or
      __err "#{cmd}: #{arg}: absolute path not expected (only relative path expected)."
  end
  #; [!je1i2] error when file not exist but '-f' option not specified.
  filenames = __glob_filenames(cmd, args, ignore)
  #; [!5619q] (store) error when target file or directory already exists.
  #; [!cw08t] (store!) overwrites existing files.
  if ! overwrite
    filenames.each do |fpath|
      newpath = File.join(dir, fpath)
      ! File.exist?(newpath)  or
        __err "#{cmd}: #{newpath}: destination file or directory already exists."
    end
  end
  #; [!4y4zy] copy files with keeping filepath.
  #; [!f0n0y] copy timestamps if '-p' option specified.
  #; [!w8oq6] creates hard links if '-l' option specified.
  #; [!7n869] error when copying supecial files such as character device.
  pathcache = {}
  filenames.each do |fpath|
    newpath = File.join(dir, fpath)
    __mkpath(File.dirname(newpath), pathcache)
    __cp_file(cmd, fpath, newpath, preserve, hardlink, bufsize=4096)
  end
end

.__sys(cmd, args, ignore_error, &b) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/benry/unixcommand.rb', line 101

def __sys(cmd, args, ignore_error, &b)
  optchars = __prepare(cmd, args, "q", nil) { nil }
  quiet_p  = optchars.include?("q")
  #; [!fb1ji] error if both array and string are specified at the same time.
  if args[0].is_a?(Array)
    args.length == 1  or
      __err "#{cmd}: Invalid argument (if arg is specified as an array, other args should not be specified)."
  end
  #; [!rqe7a] echoback command and arguments when `:p` not specified.
  #; [!ptipz] not echoback command and arguments when `:p` specified.
  #; [!4u9lj] arguments in echoback string should be quoted or escaped.
  echoback_str = __build_echoback_str(args)
  echoback(echoback_str) if ! quiet_p && __echoback?()
  #; [!dccme] accepts one string, one array, or multiple strings.
  #; [!r9ne3] shell is not invoked if arg is one array or multiple string.
  #; [!w6ol7] globbing is enabled when arg is multiple string.
  #; [!ifgkd] globbing is disabled when arg is one array.
  if args[0].is_a?(Array)
    result = __system(*args[0], shell: false)  # shell: no, glob: no
  elsif args.length == 1
    result = __system(args[0])                 # shell: yes (if necessary)
  else
    args2 = glob_if_possible(*args)            # glob: yes
    result = __system(*args2, shell: false)    # shell: no
  end
  #; [!agntr] returns process status if command succeeded.
  #; [!clfig] yields block if command failed.
  #; [!deu3e] not yield block if command succeeded.
  #; [!chko8] block argument is process status.
  #; [!0yy6r] (sys) not raise error if block result is truthy
  #; [!xsspi] (sys) raises error if command failed.
  #; [!tbfii] (sys!) returns process status if command failed.
  stat = $?
  return stat if result
  if block_given?()
    result = yield stat
    return stat if result
  end
  return stat if ignore_error
  raise "Command failed with status (#{$?.exitstatus}): #{echoback_str}"
end

.__system(*args, shell: true) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/benry/unixcommand.rb', line 143

def __system(*args, shell: true)
  #; [!9xarc] invokes command without shell when `shell:` is falty.
  #; [!0z33p] invokes command with shell (if necessary) when `shell:` is truthy.
  if shell
    return system(*args)    # with shell (if necessary)
  else
    return system([args[0], args[0]], *args[1..-1])   # without shell
  end
end

.__touch(cmd, *args) ⇒ Object

:nodoc:



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/benry/unixcommand.rb', line 860

def __touch(cmd, *args)    # :nodoc:
  #; [!ifxob] echobacks command and arguments.
  optchars = __prepare(cmd, args, "amrc", nil)
  access_time = optchars.include?("a")
  modify_time = optchars.include?("m")
  not_create  = optchars.include?("c")
  ref_file    = optchars.include?("r") ? args.shift() : nil
  #; [!c7e51] error when reference file not exist.
  ref_file.nil? || File.exist?(ref_file)  or
    __err "#{cmd}: #{ref_file}: not exist."
  #; [!pggnv] changes both access time and modification time in default.
  if access_time == false && modify_time == false
    access_time = true
    modify_time = true
  end
  #; [!o9h74] expands file name pattern.
  filenames = []
  args.each do |arg|
    arr = Dir.glob(arg)
    if arr.empty?
      filenames << arg
    else
      filenames.concat(arr)
    end
  end
  #; [!9ahsu] changes timestamp of files to current datetime.
  now = Time.now
  filenames.each do |fname|
    atime = mtime = now
    #; [!wo080] if reference file specified, use it's timestamp.
    if ref_file
      atime = File.atime(ref_file)
      mtime = File.mtime(ref_file)
    end
    #; [!726rq] creates empty file if file not found and '-c' option not specified.
    #; [!cfc40] skips non-existing files if '-c' option specified.
    if ! File.exist?(fname)
      next if not_create
      File.open(fname, 'w') {|f| f.write("") }
    end
    #; [!s50bp] changes only access timestamp if '-a' option specified.
    #; [!k7zap] changes only modification timestamp if '-m' option specified.
    #; [!b5c1n] changes both access and modification timestamps in default.
    if false
    elsif access_time && modify_time
      File.utime(atime, mtime, fname)
    elsif access_time
      File.utime(atime, File.mtime(fname), fname)
    elsif modify_time
      File.utime(File.atime(fname), mtime, fname)
    end
  end
end

.__unzip(cmd, args, overwrite) ⇒ Object



1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/benry/unixcommand.rb', line 1224

def __unzip(cmd, args, overwrite)
  #; [!eqx48] requires 'zip' gem automatically.
  require 'zip' unless defined?(::Zip)
  #; [!ednxk] echoback command and arguments.
  optchars = __prepare(cmd, args, "d", nil)
  outdir   = optchars.include?('d') ? args.shift() : nil
  #; [!1lul7] error if zip file not specified.
  zip_filename = args.shift()  or
    __err "#{cmd}: zip filename required."
  #; [!0yyg8] target directory should not exist, or be empty.
  if outdir
    if ! File.exist?(outdir)
      # pass
    elsif File.directory?(outdir)
      #; [!1ls2h] error if target directory not empty.
      found = Dir.open(outdir) {|dir|
        dir.find {|x| x != '.' && x != '..' }
      }
      ! found  or
        __err "#{cmd}: #{outdir}: directory not empty."
    else
      #; [!lb6r5] error if target directory is not a directory.
      __err "#{cmd}: #{outdir}: not a directory."
    end
  end
  #; [!o1ot5] expands glob pattern.
  #; [!92bh4] error if glob pattern matched to multiple filenames.
  #; [!esnke] error if zip file not found.
  arr = Dir.glob(zip_filename); n = arr.length
  if    n < 1 ; __err "#{cmd}: #{zip_filename}: zip file not found."
  elsif n > 1 ; __err "#{cmd}: #{zip_filename}: matched to multiple filenames (#{arr.sort.join(' ')})."
  else        ; zip_filename = arr[0]
  end
  #
  filenames = args
  filenames = nil if filenames.empty?
  #; [!dzk7c] creates target directory if not exists.
  __mkpath(outdir, {}) if outdir && ! File.exist?(outdir)
  #
  orig = ::Zip.on_exists_proc
  begin
    #; [!06nyv] (unzip!) overwrites existing files.
    ::Zip.on_exists_proc = overwrite
    extglob = File::FNM_EXTGLOB
    #; [!ekllx] (unzip) error when file already exists.
    ::Zip::File.open(zip_filename) do |zf|
      zf.each do |x|
        next if filenames && ! filenames.find {|pat| File.fnmatch?(pat, x.name, extglob) }
        #; [!zg60i] error if file has absolute path.
        outdir || File.absolute_path(x.name) != x.name  or
          __err "#{cmd}: #{x.name}: cannot extract absolute path."
        #
        next if x.directory?
        fpath = outdir ? File.join(outdir, x.name) : x.name
        overwrite || ! File.exist?(fpath)  or
          __err "#{cmd}: #{fpath}: file already exists (to overwrite it, call `#{cmd}!` command instead of `#{cmd}` command)."
      end
    end
    #; [!0tedi] extract zip file.
    ::Zip::File.open(zip_filename) do |zf|
      zf.each do |x|
        #; [!ikq5w] if filenames are specified, extracts files matched to them.
        next if filenames && ! filenames.find {|pat| File.fnmatch?(pat, x.name, extglob) }
        #; [!dy4r4] if '-d' option specified, extracts files under target directory.
        if outdir
          x.extract(File.join(outdir, x.name))
        #; [!5u645] if '-d' option not specified, extracts files under current directory.
        else
          x.extract()
        end
      end
    end
  ensure
    #; [!sjf80] (unzip!) `Zip.on_exists_proc` should be recovered.
    ::Zip.on_exists_proc = orig
  end
end

.__zip(cmd, args, overwrite) ⇒ Object



1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
# File 'lib/benry/unixcommand.rb', line 1143

def __zip(cmd, args, overwrite)
  #; [!zzvuk] requires 'zip' gem automatically.
  require 'zip' unless defined?(::Zip)
  #; [!zk1qt] echoback command and arguments.
  optchars = __prepare(cmd, args, "r0123456789", nil)
  recursive = optchars.include?('r')
  complevel = (optchars =~ /(\d)/ ? $1.to_i : nil)
  #; [!lrnj7] zip filename required.
  zip_filename = args.shift()  or
    __err "#{cmd}: zip filename required."
  #; [!khbiq] zip filename can be glob pattern.
  #; [!umbal] error when zip file glob pattern matched to mutilple filenames.
  arr = Dir.glob(zip_filename); n = arr.length
  if    n < 1 ; nil
  elsif n > 1 ; __err "#{cmd}: #{zip_filename}: matched to multiple filenames (#{arr.sort.join(', ')})."
  else        ; zip_filename = arr[0]
  end
  #; [!oqzna] (zip) raises error if zip file already exists.
  ! File.exist?(zip_filename) || overwrite  or
    __err "#{cmd}: #{zip_filename}: already exists (to overwrite it, call `#{cmd}!` command instead of `#{cmd}` command)."
  #; [!uu8uz] expands glob pattern.
  #; [!nahxa] error if file not exist.
  filenames = __glob_filenames(cmd, args, false) do |arg, _|
    __err "#{cmd}: #{arg}: file or directory not found."
  end
  #; [!qsp7c] cannot specify absolute path.
  filenames.each do |fname|
    if File.absolute_path(fname) == fname   # Ruby >= 2.7: File.absolute_path?()
      __err "#{cmd}: #{fname}: not support absolute path."
    end
  end
  #; [!e995z] (zip!) removes zip file if exists.
  File.unlink(zip_filename) if File.exist?(zip_filename)
  #; [!3sxmg] supports complession level (0~9).
  orig = Zip.default_compression
  Zip.default_compression = complevel if complevel
  #; [!p8alf] creates zip file.
  begin
    zipf = ::Zip::File.open(zip_filename, create: true) do |zf|  # `compression_level: n` doesn't work. why?
      filenames.each do |fname|
        __zip_add(cmd, zf, fname, recursive)
      end
      zf
    end
  ensure
    #; [!h7yxl] restores value of `Zip.default_compression`.
    Zip.default_compression = orig if complevel
  end
  #; [!fvvn8] returns zip file object.
  return zipf
end

.__zip_add(cmd, zf, fpath, recursive) ⇒ Object



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/benry/unixcommand.rb', line 1195

def __zip_add(cmd, zf, fpath, recursive)
  ftype = File.ftype(fpath)
  case ftype
  when 'link'; zf.add(fpath, fpath)
  when 'file'; zf.add(fpath, fpath)
  when 'directory'
    zf.add(fpath, fpath)
    #; [!bgdg7] adds files recursively into zip file if '-r' option specified.
    Dir.open(fpath) do |dir|
      dir.each do |x|
        next if x == '.' || x == '..'
        __zip_add(cmd, zf, File.join(fpath, x), recursive)
      end
    end if recursive
  else
    #; [!jgt96] error when special file specified.
    __err "#{cmd}: #{fpath}: #{ftype} file not supported."
  end
end

.atomic_symlink!(src, dst) ⇒ Object



832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/benry/unixcommand.rb', line 832

def atomic_symlink!(src, dst)
  cmd = 'atomic_symlink!'
  #; [!gzp4a] creates temporal symlink and rename it when symlink already exists.
  #; [!lhomw] creates temporal symlink and rename it when symlink not exist.
  if File.symlink?(dst) || ! File.exist?(dst)
    tmp = "#{dst}.#{rand().to_s[2..5]}"
    echoback("ln -s #{src} #{tmp} && mv -Tf #{tmp} #{dst}") if __echoback?()
    File.symlink(src, tmp)
    File.rename(tmp, dst)
  #; [!h75kp] error when destination is normal file or directory.
  else
    __err "#{cmd}: #{dst}: not a symbolic link."
  end
end

.capture2(*args, **kws) ⇒ Object



225
# File 'lib/benry/unixcommand.rb', line 225

def capture2(  *args, **kws); __capture(:capture2 , args, kws, false); end

.capture2!(*args, **kws) ⇒ Object



228
# File 'lib/benry/unixcommand.rb', line 228

def capture2!( *args, **kws); __capture(:capture2 , args, kws, true ); end

.capture2e(*args, **kws) ⇒ Object



226
# File 'lib/benry/unixcommand.rb', line 226

def capture2e( *args, **kws); __capture(:capture2e, args, kws, false); end

.capture2e!(*args, **kws) ⇒ Object



229
# File 'lib/benry/unixcommand.rb', line 229

def capture2e!(*args, **kws); __capture(:capture2e, args, kws, true ); end

.capture3(*args, **kws) ⇒ Object



227
# File 'lib/benry/unixcommand.rb', line 227

def capture3(  *args, **kws); __capture(:capture3 , args, kws, false); end

.capture3!(*args, **kws) ⇒ Object



230
# File 'lib/benry/unixcommand.rb', line 230

def capture3!( *args, **kws); __capture(:capture3 , args, kws, true ); end

.cd(arg, &b) ⇒ Object Also known as: chdir



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/benry/unixcommand.rb', line 253

def cd(arg, &b)
  cmd = 'cd'
  #; [!gnmdg] expands file pattern.
  #; [!v7bn7] error when pattern not matched to any file.
  #; [!08wuv] error when pattern matched to multiple files.
  #; [!hs7u8] error when argument is not a directory name.
  dir = __glob_onedir(cmd, arg)
  #; [!cg5ns] changes current directory.
  here = Dir.pwd
  echoback("cd #{dir}") if __echoback?()
  Dir.chdir(dir)
  #; [!uit6q] if block given, then back to current dir.
  if block_given?()
    @__depth ||= 0
    @__depth += 1
    begin
      yield
    ensure
      @__depth -= 1
      echoback("cd -") if __echoback?()
      Dir.chdir(here)
    end
  end
  #; [!cg298] returns path before changing directory.
  return here
end

.chmod(*args) ⇒ Object



915
916
917
# File 'lib/benry/unixcommand.rb', line 915

def chmod(*args)
  __chmod("chmod", args)
end

.chown(*args) ⇒ Object



1002
1003
1004
# File 'lib/benry/unixcommand.rb', line 1002

def chown(*args)
  __chown("chown", args)
end

.cp(*args, to: nil) ⇒ Object



399
400
401
# File 'lib/benry/unixcommand.rb', line 399

def cp(*args, to: nil)
  __cp('cp', args, to: to, overwrite: false)
end

.cp!(*args, to: nil) ⇒ Object



403
404
405
# File 'lib/benry/unixcommand.rb', line 403

def cp!(*args, to: nil)
  __cp('cp!', args, to: to, overwrite: true)
end

.echo(*args) ⇒ Object



78
79
80
# File 'lib/benry/unixcommand.rb', line 78

def echo(*args)
  __echo('echo', args)
end

.echoback(cmd) ⇒ Object



40
41
42
43
# File 'lib/benry/unixcommand.rb', line 40

def echoback(cmd)
  #; [!x7atu] prints argument string into $stdout with prompt.
  puts "#{prompt!(@__depth ||= 0)}#{cmd}"
end

.echoback_off(&block) ⇒ Object



60
61
62
63
# File 'lib/benry/unixcommand.rb', line 60

def echoback_off(&block)
  #; [!prkfg] disables echoback temporarily.
  echoback_switch(false, &block)
end

.echoback_on(&block) ⇒ Object



55
56
57
58
# File 'lib/benry/unixcommand.rb', line 55

def echoback_on(&block)
  #; [!9x2lh] enables echoback temporarily.
  echoback_switch(true, &block)
end

.echoback_switch(val, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/benry/unixcommand.rb', line 65

def echoback_switch(val, &block)
  #; [!aw9b2] switches on/off of echoback temporarily.
  defined = instance_variable_defined?(:@__BENRY_ECHOBACK)
  prev = @__BENRY_ECHOBACK
  @__BENRY_ECHOBACK = val
  yield
  nil
ensure
  defined ? (@__BENRY_ECHOBACK = prev) \
          : remove_instance_variable(:@__BENRY_ECHOBACK)
end

.glob_if_possible(*strs) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/benry/unixcommand.rb', line 173

def glob_if_possible(*strs)
  #; [!xvr32] expands file pattern matching.
  #; [!z38re] if pattern not matched to any files, just returns pattern as is.
  arr = []
  strs.each do |s|
    globbed = Dir.glob(s)
    if globbed.empty?
      arr << s
    else
      arr.concat(globbed)
    end
  end
  return arr
end

.ln(*args, to: nil) ⇒ Object



727
728
729
# File 'lib/benry/unixcommand.rb', line 727

def ln(*args, to: nil)
  __ln('ln', args, to: to, overwrite: false)
end

.ln!(*args, to: nil) ⇒ Object



731
732
733
# File 'lib/benry/unixcommand.rb', line 731

def ln!(*args, to: nil)
  __ln('ln!', args, to: to, overwrite: true)
end

.mkdir(*args) ⇒ Object



622
623
624
# File 'lib/benry/unixcommand.rb', line 622

def mkdir(*args)
  __mkdir('mkdir', args)
end

.mv(*args, to: nil) ⇒ Object



525
526
527
# File 'lib/benry/unixcommand.rb', line 525

def mv(*args, to: nil)
  __mv('mv', args, to: to, overwrite: false)
end

.mv!(*args, to: nil) ⇒ Object



529
530
531
# File 'lib/benry/unixcommand.rb', line 529

def mv!(*args, to: nil)
  __mv('mv!', args, to: to, overwrite: true)
end

.popen2(*args, **kws, &b) ⇒ Object

:nodoc:



212
# File 'lib/benry/unixcommand.rb', line 212

def popen2( *args, **kws, &b); __popen(:popen2 , args, kws, &b); end

.popen2e(*args, **kws, &b) ⇒ Object

:nodoc:



213
# File 'lib/benry/unixcommand.rb', line 213

def popen2e(*args, **kws, &b); __popen(:popen2e, args, kws, &b); end

.popen3(*args, **kws, &b) ⇒ Object

:nodoc:



214
# File 'lib/benry/unixcommand.rb', line 214

def popen3( *args, **kws, &b); __popen(:popen3 , args, kws, &b); end

.promptObject



30
31
32
33
# File 'lib/benry/unixcommand.rb', line 30

def prompt()
  #; [!uilyk] returns prompt string.
  return "$ "
end

.prompt!(depth) ⇒ Object



35
36
37
38
# File 'lib/benry/unixcommand.rb', line 35

def prompt!(depth)
  #; [!q992e] adds indentation after prompt.
  return prompt() + ' ' * depth
end

.pushd(arg, &b) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/benry/unixcommand.rb', line 281

def pushd(arg, &b)
  cmd = 'pushd'
  #; [!xl6lg] raises error when block not given.
  block_given?()  or
    __err "pushd: requires block argument."
  #; [!nvkha] expands file pattern.
  #; [!q3itn] error when pattern not matched to any file.
  #; [!hveaj] error when pattern matched to multiple files.
  #; [!y6cq9] error when argument is not a directory name.
  dir = __glob_onedir(cmd, arg)
  #; [!7ksfd] replaces home path with '~'.
  here = Dir.pwd
  home = File.expand_path("~")
  here2 = here.start_with?(home) ? here.sub(home, "~") : here
  #; [!rxtd0] changes directory and yields block.
  echoback("pushd #{dir}") if __echoback?()
  @__depth ||= 0
  @__depth += 1
  Dir.chdir(dir)
  yield
  @__depth -= 1
  #; [!9jszw] back to origin directory after yielding block.
  echoback("popd    # back to #{here2}") if __echoback?()
  Dir.chdir(here)
  here
end

.pwdObject



848
849
850
851
852
853
# File 'lib/benry/unixcommand.rb', line 848

def pwd()
  #; [!aelx6] echoback command and arguments.
  echoback("pwd") if __echoback?()
  #; [!kh3l2] prints current directory path.
  puts Dir.pwd
end

.rm(*args) ⇒ Object



589
590
591
# File 'lib/benry/unixcommand.rb', line 589

def rm(*args)
  __rm('rm', args)
end

.rmdir(*args) ⇒ Object



686
687
688
# File 'lib/benry/unixcommand.rb', line 686

def rmdir(*args)
  __rmdir('rmdir', args)
end

.ruby(*args, &b) ⇒ Object



189
190
191
# File 'lib/benry/unixcommand.rb', line 189

def ruby(*args, &b)
  __ruby('ruby', args, false, &b)
end

.ruby!(*args, &b) ⇒ Object



193
194
195
# File 'lib/benry/unixcommand.rb', line 193

def ruby!(*args, &b)
  __ruby('ruby!', args, true, &b)
end

.store(*args, to:) ⇒ Object



1075
1076
1077
# File 'lib/benry/unixcommand.rb', line 1075

def store(*args, to:)
  __store('store', args, false, to: to)
end

.store!(*args, to:) ⇒ Object



1079
1080
1081
# File 'lib/benry/unixcommand.rb', line 1079

def store!(*args, to:)
  __store('store!', args, true, to: to)
end

.sys(*args, &b) ⇒ Object



93
94
95
# File 'lib/benry/unixcommand.rb', line 93

def sys(*args, &b)
  __sys('sys', args, false, &b)
end

.sys!(*args, &b) ⇒ Object



97
98
99
# File 'lib/benry/unixcommand.rb', line 97

def sys!(*args, &b)
  __sys('sys!', args, true, &b)
end

.time(format = nil, &b) ⇒ Object



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
# File 'lib/benry/unixcommand.rb', line 1303

def time(format=nil, &b)
  #; [!ddl3a] measures elapsed time of block and reports into stderr.
  pt1 = Process.times()
  t1  = Time.new
  yield
  t2  = Time.new
  pt2 = Process.times()
  user = pt2.cutime - pt1.cutime
  sys  = pt2.cstime - pt1.cstime
  real = t2 - t1
  format ||= "        %.3fs real       %.3fs user       %.3fs sys"
  $stderr.puts ""
  $stderr.puts format % [real, user, sys]
end

.touch(*args) ⇒ Object



856
857
858
# File 'lib/benry/unixcommand.rb', line 856

def touch(*args)
  __touch('touch', *args)
end

.unzip(*args) ⇒ Object



1216
1217
1218
# File 'lib/benry/unixcommand.rb', line 1216

def unzip(*args)
  __unzip('unzip', args, false)
end

.unzip!(*args) ⇒ Object



1220
1221
1222
# File 'lib/benry/unixcommand.rb', line 1220

def unzip!(*args)
  __unzip('unzip!', args, true)
end

.zip(*args) ⇒ Object



1135
1136
1137
# File 'lib/benry/unixcommand.rb', line 1135

def zip(*args)
  __zip('zip', args, false)
end

.zip!(*args) ⇒ Object



1139
1140
1141
# File 'lib/benry/unixcommand.rb', line 1139

def zip!(*args)
  __zip('zip', args, true)
end