From: Rudolf Polzer Date: 2002-12-08T18:05:56+09:00 Subject: Re: Thank God for backups Scripsit ille aut illa Gavin Sinclair : > From: "Rudolf Polzer" [...] > > ..bashrc: > > | alias rm=recycle > > Nice. CAn we have that in Ruby, please? :) My first attempt (it uses a configuration file in ~/.recycle which looks like the following: mountpoints = /etc/fstab additional characters = ������� pathsep = : trash of /tmp = /mnt/ramdisk/.recycled where the mountpoints file must look like a fstab, additional characters are not escaped in pathnames and pathsep is used instead of / to escape path names (if using /, they are escaped to %2F)). #!/usr/bin/ruby # simple recycler # # this is how you could initialize your system for using this (as root, # given that root and rpolzer are the only users who may use this): # # for USER in root rpolzer; do # while read X MOUNT X; do # X="$MOUNT/.recycled/$USER" # mkdir -p "$X" # chown "$USER" "$X" # done '/proc/mounts', 'additional characters' => '', 'pathsep' => '/' } $mountpoints = {} begin IO.foreach(CONFIG_FILE) do |line| line.chomp!() line.sub!(/\s*#.*/, '') line =~ /^\s*(.*?)\s*=\s*(.*)$/ and $config[$1] = $2 end rescue end begin IO.foreach($config['mountpoints']) do |line| line.chomp!() line.sub!(/\s*#.*/, '') line.gsub!(/\s+/, ' ') line.sub!(/^ | $/, '') ents = line.split(/ /) dir = ents[1] # fstab syntax dev = File::stat(dir).dev() $mountpoints[dev] = dir end rescue end class String def pathify!() gsub!(%r{//+}, '/') sub!(%r{/$}, '') return self end end def rm(filename) # find the mountpoint begin mountpoint = $mountpoints[File::lstat(filename).dev()] || '/' rescue puts "recycle: couldn't stat #{filename.inspect()}" return end recycledir = $config['trash of ' + mountpoint] || mountpoint + '/.recycled/' + ENV['USER'] recycledir.pathify!() to_filename = File.expand_path(filename) to_filename.pathify!() to_filename.gsub!(%r{[^ -~]|%|#{$config['pathsep']}}) do |ch| if $config['additional characters'].index(ch) ch else '%%%02X' % [ch[0]] # you know that? It's uri_escape format! This way you can undelete # using Larry Walls's rename tool: # sh$ cd /.recycled/rpolzer # sh$ rename 'use URI::Escape; uri_unescape $_' directory:filename end end to_filename.gsub!(%r{/}, $config['pathsep']) to_filename = recycledir + '/' + to_filename puts "recycle: #{filename.inspect()} => #{to_filename.inspect()}" File.rename(filename, to_filename) end ignore_opts = true ARGV.each() do |file| if ignore_opts and file =~ /^--$/ ignore_opts = false next elsif ignore_opts and file =~ /^-/ next else rm(file) end end