# dupedb.tcl v0.1.1 for everything tcl # # ATTENTION: this version is a very early alpha version!!! # # Copyright (C) 2004 CyBex, #tcl + #cybex @ QuakeNet # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # testet v0.1.1 with eggdrop 1.6.15 and tclsh 8.3 # # requires: # * tcl-lib features (crc32) # when u dont have it, download @ http://www.tcl.tk/software/tcllib/ and remove comment from source # crc32: http://cvs.sourceforge.net/viewcvs.py/tcllib/tcllib/modules/crc/crc32.tcl version 1.x # source scripts/crc32.tcl # both package are faster when you install the Trf package, # download and info @ http://www.oche.de/~akupries/soft/trf/index.html # # # project starts: 04.Mai.2004 # last edit: 04.Mai.2004 package req crc32 1.0 namespace eval ::dupedb { variable stderr stderr variable stderr stdout if ![info exi dupever] {variable dupever 0.1.1} # only load one times at startup of the script if ![info exi keepinram] {variable keepinram 1} # switch only on when you know what you do, like build an eggdrop base and save every hour if ![info exi delayedsave] {variable delayedsave 0} if ![file exists dupe.db] { close [open dupe.db w] } if {[info command putcmdlog]!="putcmdlog"} {proc putcmdlog text {variable stderr; puts $stderr $text}} if ![info exi dupedb] {set dupedb [list]} if ![info exi lastdb] {set lastdb [list]} if ![info exi lastdupe] {set lastdupe [list]} proc check {buffer file} { set buffer [split $buffer] #putcmdlog "check" variable dupedb variable keepinram set r_crc [catch {::crc::crc32 $buffer} crc] if $r_crc {putcmdlog "ATTENTION from Gallery, crc32 dont work, result: #$r_crc $crc"; return} set len [string length $buffer] if {$len<3} {return 2} if !$keepinram load set name [file tail $file] set dir [file dirname $file] set poslen [lsearch -glob $dupedb $len] if {$poslen!=-1} { set g_len [lindex $dupedb [expr $poslen+1]] set poscrc [lsearch -glob $g_len $crc] if {$poscrc!=-1} { set g_crc [lindex $g_len [expr $poscrc+1]] set posname [lsearch -glob $g_crc $name] if {$posname!=-1} { set g_name [lindex $_crc [expr $posname+1]] set posdir [lsearch -glob $g_name $dir] if {$posdir!=1} { putcmdlog "oldfile"; return 3 # file with the same name and dir exist } { putcmdlog "oldfile newpath"; return 4 # file with same name and new dir } } { putcmdlog "newname $g_crc"; return 5 # file with new name exist } } { putcmdlog "newfile crc"; return -2 } } { putcmdlog "newfile len"; return -1 } # file not exist } proc add {buffer file} { set buffer [split $buffer] #putcmdlog "add" variable dupedb variable keepinram variable delayedsave variable lastdupe set r_crc [catch {::crc::crc32 $buffer} crc] if $r_crc {putcmdlog "\n\nATTENTION from Gallery, crc32 dont work, result: #$r_crc $crc\n"; return} set len [string length $buffer] if {$len<3} {puts -nonewline "\<"; return 0} if {!$keepinram} load set exist 0 set name [file tail $file] set dir [file dirname $file] set poslen [lsearch -glob $dupedb $len] if {$poslen!=-1} { #putcmdlog "len found" set g_len [lindex $dupedb [expr $poslen+1]] set poscrc [lsearch -glob $g_len $crc] if {$poscrc!=-1} { #putcmdlog "crc found" set g_crc [lindex $g_len [expr $poscrc+1]] #putcmdlog "g_crc $g_crc" set posname [lsearch -glob $g_crc $name] if {$posname!=-1} { #putcmdlog "name found" set g_name [lindex $g_crc [expr $posname+1]] set posdir [lsearch -glob $g_name $dir] if {$posdir!=-1} { #putcmdlog "dir found" puts -nonewline "." set lastdupe $g_name set exist 1 } { #putcmdlog "\noldname $g_name" lappend g_name $dir #putcmdlog "newname $g_name" #puts "oldcrc $g_crc // [expr $posname+1]" set g_crc [lreplace $g_crc [expr $posname+1] [expr $posname+1] $g_name] #puts "newcrc $g_crc" set g_len [lreplace $g_len [expr $poscrc+1] [expr $poscrc+1] $g_crc] set dupedb [lreplace $dupedb [expr $poslen+1] [expr $poslen+1] $g_len] puts -nonewline "P" set lastdupe $g_name set exist 4 } } { if {[string match *.thumb.* $name] || [string match *.sized.* $name] || [string match *.bak $name] || [string match *.dat $name]} { puts -nonewline "F" #putcmdlog "filter\nname:$name\nold:$g_crc" set exist 3 } { puts -nonewline "D" #putcmdlog "newname\nname:$name\nold:$g_crc" set exist 2 } set lastdupe [list $name [list $dir]] #putcmdlog "\nold g_crc: $g_crc" lappend g_crc $name [list $dir] #putcmdlog "new g_crc: $g_crc" set g_len [lreplace $g_len [expr $poscrc+1] [expr $poscrc+1] $g_crc] set dupedb [lreplace $dupedb [expr $poslen+1] [expr $poslen+1] $g_len] # file with new name exist } } { #putcmdlog "newfile crc" puts -nonewline "2" lappend g_len $crc [list $name [list $dir]] set dupedb [lreplace $dupedb [expr $poslen+1] [expr $poslen+1] $g_len] set exist -2 } } { #putcmdlog "newfile len" puts -nonewline "1" lappend dupedb $len [list $crc [list $name [list $dir]]] set exist -1 } #if {$exist!=0} {putcmdlog "dupedb $dupedb"} if {$exist!=0} { if {!$delayedsave || !$keepinram} save } if !$keepinram clean flush stdout return $exist # file not exist } proc clean args { variable dupedb variable lastdb if {$dupedb==[list]} {return 0} #puts -nonewline "clean" unset dupedb lastdb set dupedb [list]; set lastdb [list] return 1 } proc load args { variable dupedb variable lastdb if ![file exists dupe.db] {return 0} set f [open dupe.db r]; set dupedb [gets $f]; close $f #puts -nonewline "load"; #putcmdlog "load dupedb" set lastdb $dupedb return 1 } proc save args { variable dupedb variable lastdb variable keepinram if {$dupedb==[list]} {return 0} if {$dupedb==$lastdb} {return 0} #puts -nonewline "save" #putcmdlog "dupedb $dupedb" #putcmdlog "lastdb $lastdb" file rename -force dupe.db dupedb.bak set f [open dupe.db w]; puts $f $dupedb; close $f if !$keepinram clean { set lastdb $dupedb } #putcmdlog "save dupedb" return 1 } if $keepinram load } package prov dupedb $::dupedb::dupever putcmdlog "DupeDB $::dupedb::dupever loaded" return $::dupedb::dupever # dupedb format