Class: Gomiko
- Inherits:
-
Object
- Object
- Gomiko
- Defined in:
- lib/gomiko.rb
Instance Attribute Summary collapse
-
#trashdir ⇒ Object
readonly
Returns the value of attribute trashdir.
Instance Method Summary collapse
-
#dir_list ⇒ Object
ls, dir_list return stocked directories.
- #empty(ids: [], mtime: 0, time: Time.now, verbose: true, io: $stdout) ⇒ Object
-
#info(id) ⇒ Object
Example of return data: [ 236K, 20170623-021233, [/home/ippei/tmp/a.txt, /home/ippei/tmp/b.txt ] ].
-
#initialize(dir: nil, verbose: true) ⇒ Gomiko
constructor
class NotFoundError < StandardError; end.
-
#throw(paths:, time: Time.new, verbose: true, io: $stdout) ⇒ Object
If paths includes exist and not exist files, throw all exist file and report not exist files.
- #undo(id, verbose: true, io: $stdout) ⇒ Object
-
#yaml_list ⇒ Object
ls, yaml_list return stocked yaml.
Constructor Details
#initialize(dir: nil, verbose: true) ⇒ Gomiko
class NotFoundError < StandardError; end
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gomiko.rb', line 19 def initialize(dir: nil, verbose: true) if dir @trashdir = dir else if ENV['UID'] == 0 # this is needed for 'su -m' @trashdir ='/root/.trash' else @trashdir = ENV['HOME'] + "/.trash" end end FileUtils.mkdir_p(@trashdir) end |
Instance Attribute Details
#trashdir ⇒ Object (readonly)
Returns the value of attribute trashdir.
15 16 17 |
# File 'lib/gomiko.rb', line 15 def trashdir @trashdir end |
Instance Method Details
#dir_list ⇒ Object
ls, dir_list return stocked directories.
183 184 185 186 187 188 189 |
# File 'lib/gomiko.rb', line 183 def dir_list Dir.glob("#{@trashdir}/*").select { |path| FileTest.directory? path } .map { |path| path.sub(/^#{@trashdir}\//, '').split[0] } . sort end |
#empty(ids: [], mtime: 0, time: Time.now, verbose: true, io: $stdout) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gomiko.rb', line 66 def empty(ids: [], mtime: 0, time: Time.now, verbose: true, io: $stdout) if ids.empty? tgts = (dir_list + yaml_list.map{|v| v.sub(".yaml", "")}).sort.uniq else tgts = ids.map {|v| path2id(v.sub(".yaml", ""))} end tgts = tgts.select { |v| begin str2datetime(v) - time < 86400 * mtime rescue Errno::ENOENT io.puts "Not found: #{v}" end } tgts.map! {|v| @trashdir + '/' + v} if tgts.empty? io.puts "No directory was emptied." else tgts.each do |path| ["", ".yaml"].each do |ext| new_path = path + ext FileUtils.rm_rf(new_path, :verbose => verbose) if File.exist? new_path end end end end |
#info(id) ⇒ Object
Example of return data: [
236K,
20170623-021233,
[/home/ippei/tmp/a.txt, /home/ippei/tmp/b.txt ]
]
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gomiko.rb', line 111 def info(id) id = path2id id cur_trash_dir = Pathname.new(@trashdir) + id results = [] results << `du --human-readable --max-depth=0 #{cur_trash_dir}`.split(' ')[0] results << id trash_paths = Dir.glob("#{cur_trash_dir}/**/*", File::FNM_DOTMATCH).sort trash_paths = trash_paths.select { |t_path| ! /\/\.$/.match t_path } # '/.', で終わるのを除外 candidates = [] # fo rm target results_long = [] additions = [] #flag_conflict = false flag_include_file = false trash_paths.each do |trash_path| orig_path = trash_path.sub(/^#{cur_trash_dir}/, '') trash_type = ftype_str(trash_path) flag_include_file = true unless File.directory? trash_path if FileTest.exist? orig_path orig_type = ftype_str(orig_path) if File.ftype(trash_path) != File.ftype(orig_path) #flag_conflict = true additions << 'conflict' candidates << trash_path end else candidates << trash_path orig_type = ' ' end results_long << [ trash_type, orig_type, trash_path.sub(/^#{cur_trash_dir}/, '') ] end unless flag_include_file additions << 'only directory' candidates << trash_paths[-1] end results << YAML.load_file(cur_trash_dir.to_s + ".yaml")['paths'][0] ## if no candidate, last file is adopted. if trash_paths.empty? #results << '(empty)' results << [] else additions << 'conflict' if candidates.empty? #candidates = candidates.map {|path| # tmp = path.sub(/^#{cur_trash_dir}/, '') # tmp += '/' if FileTest.directory? path # tmp #} #results << candidates[0] ## output '...' when multiple. candidates = candidates.select{|pa| ! pa.include? candidates[0]} results[-1] += ' ...' unless candidates.empty? #results[2] += ' (conflict)' if flag_conflict results[2] += ' (' + additions.join(',') + ')' unless additions.empty? results << results_long end results end |
#throw(paths:, time: Time.new, verbose: true, io: $stdout) ⇒ Object
If paths includes exist and not exist files, throw all exist file and report not exist files.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gomiko.rb', line 34 def throw(paths: , time: Time.new, verbose: true, io: $stdout) paths = paths.select do |path| flag = FileTest.symlink?(path) || FileTest.exist?(path) # for deadlink unless flag if verbose io.puts "gomiko rm: cannot remove '#{path}': No such file or directory" end end flag end return if paths.empty? trash_subdir = mkdir_time(time) File.open(trash_subdir + ".yaml", "w") do |yaml_io| YAML.dump( { 'pwd' => ENV["PWD"], 'paths' => paths }, yaml_io) end paths.each do |path| dst = trash_subdir + File.(path) dst_dir = File.dirname dst FileUtils.mkdir_p(dst_dir) if path == '.' io.puts "gomiko rm: failed to remove '.': Invalid argument" if verbose next else FileUtils.mv(path, dst_dir + '/') io.puts "mv #{path} #{dst_dir}" if verbose File.utime(time, time, trash_subdir) end end end |
#undo(id, verbose: true, io: $stdout) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/gomiko.rb', line 92 def undo(id, verbose: true, io: $stdout) id = path2id id fullpath = Pathname.new(@trashdir) + id Dir.glob("#{fullpath}/*").sort.each do |path| graft(fullpath, '', dst_root: '/', verbose: verbose) end if Dir.glob("#{fullpath}/**/*").find {|path| FileTest.file? path} io.puts "Unable to complete undo: #{fullpath}" else FileUtils.rm_rf fullpath # risky? end end |
#yaml_list ⇒ Object
ls, yaml_list return stocked yaml.
175 176 177 178 179 |
# File 'lib/gomiko.rb', line 175 def yaml_list Dir.glob("#{@trashdir}/*.yaml").map { |path| path.sub(/^#{@trashdir}\//, '').split[0] } . sort end |