Class: Archivededup::Db
- Inherits:
-
Object
- Object
- Archivededup::Db
- Defined in:
- lib/archivededup/db.rb
Instance Method Summary collapse
- #add_directory(dir, &blk) ⇒ Object
- #add_file(d) ⇒ Object
- #close ⇒ Object
- #each_dup ⇒ Object
- #each_hash ⇒ Object
-
#initialize(dbfile) ⇒ Db
constructor
A new instance of Db.
Constructor Details
#initialize(dbfile) ⇒ Db
Returns a new instance of Db.
9 10 11 |
# File 'lib/archivededup/db.rb', line 9 def initialize(dbfile) @db = DBM.new(dbfile, 0755, DBM::WRCREAT) end |
Instance Method Details
#add_directory(dir, &blk) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/archivededup/db.rb', line 35 def add_directory(dir, &blk) Dir.new(dir).each do |dent| d = File.join(dir, dent) next if dent == '.' next if dent == '..' next if dent.start_with? '.' if File.directory? d add_directory(d, &blk) else yield d if block_given? end end end |
#add_file(d) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/archivededup/db.rb', line 51 def add_file(d) # puts "Got #{d}" s = File.stat(d) # noimplemented - s.birthtime s.mtime s.ctime # unused - s.atime # puts d, s hash = Digest::MD5.new outbuf = '' File.open(d) do |io| while io.read(4096, outbuf) do hash.update outbuf end end unless @db.has_key? "name - #{d}" hash = hash.hexdigest @db["name - #{d}"] = YAML::dump({ dir: d, hash: hash, }) if @db.has_key?('hash - '+hash) o = YAML::load(@db['hash - '+hash]) buf1 = '' buf2 = '' File.open(d) do |io1| File.open(o['files'][0]) do |io2| v1 = '' v2 = '' while v1 && v2 && v1 == v2 do v1 = io1.read(4096, buf1) v2 = io2.read(4096, buf2) end if v1 == v2 o['files'] << d puts "Dups found: #{o}" @db['hash - '+hash] = YAML::dump(o) end end end else puts "Storing #{d}." @db['hash - '+hash] = YAML::dump({ 'files' => [ d ], 'hash' => hash, }) end else puts "Had #{d}." end end |
#close ⇒ Object
109 110 111 |
# File 'lib/archivededup/db.rb', line 109 def close() @db.close() end |
#each_dup ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/archivededup/db.rb', line 23 def each_dup @db.select do |k, v| k.start_with? 'hash - ' end.map do |k, v| [k, YAML::load(v)] end.select do |k, r| r['files'].length > 1 end.each do |k, r| yield k, r end end |
#each_hash ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/archivededup/db.rb', line 13 def each_hash @db.select do |k, v| k.start_with? 'hash - ' end.map do |k, v| [k, YAML::load(v)] end.each do |k, r| yield k, r end end |