diff --git a/mlocatedb-dump/mlocatedb-dump.rb b/mlocatedb-dump/mlocatedb-dump.rb index 9aa42a2..0eceabc 100755 --- a/mlocatedb-dump/mlocatedb-dump.rb +++ b/mlocatedb-dump/mlocatedb-dump.rb @@ -8,8 +8,43 @@ # Zie ook: http://linux.die.net/man/5/mlocate.db -# Geen argumenten. Probeert gewoon een bestand met de naam 'mlocate.db' -# in de huidige dir te openen +require "getoptlong" + +def parse_options + @options = {} + begin + opts = GetoptLong.new( + [ "-i", GetoptLong::REQUIRED_ARGUMENT ], + [ "-h", "--help", GetoptLong::NO_ARGUMENT ] + ) + opts.quiet=true + opts.each do |opt, arg| + @options[opt] = arg + end + if @options["-h"] + usage + end + rescue GetoptLong::InvalidOption + print "#{$!}\n" + usage + end + # default values + if @options["-i"].nil? + @options["-i"] = "slocate.db" + end + return @options +end + +def usage + print <] [-l] [-h] + +-i use for input (default: slocate.db) +-h this help message +XXX + exit +end + def arr2long(arr) return (arr[0]<<24) + (arr[1]<<16) + (arr[2]<<8) + arr[3] @@ -23,8 +58,9 @@ def arr2string(arr) return string end +parse_options -fh = File.new("mlocate.db", "r") +fh = File.new(@options["-i"], "r") magic_number=fh.read(8) if magic_number == "\000mlocate" @@ -91,6 +127,10 @@ while true curdir='' while (b=fh.read(1)) != "\000" + if b.nil? + # we're done + exit + end curdir += b end puts "#{curdir}/" diff --git a/slocatedb-dump/slocatedb-dump.rb b/slocatedb-dump/slocatedb-dump.rb new file mode 100755 index 0000000..8a59ae5 --- /dev/null +++ b/slocatedb-dump/slocatedb-dump.rb @@ -0,0 +1,90 @@ +#!/usr/bin/env ruby + +# $Id$ +# $URL$ + +# Parser voor slocate.db bestanden. Toont een filelisting van alle bestanden +# uit de database. + +require "getoptlong" + +def parse_options + @options = {} + begin + opts = GetoptLong.new( + [ "-i", GetoptLong::REQUIRED_ARGUMENT ], + [ "-h", "--help", GetoptLong::NO_ARGUMENT ], + [ "-l", GetoptLong::NO_ARGUMENT ] + ) + opts.quiet=true + opts.each do |opt, arg| + @options[opt] = arg + end + if @options["-h"] + usage + end + rescue GetoptLong::InvalidOption + print "#{$!}\n" + usage + end + # default values + if @options["-l"].nil? + @options["-l"] = false + end + if @options["-i"].nil? + @options["-i"] = "slocate.db" + end + return @options +end + +def usage + print <] [-l] [-h] + +-i use for input (default: slocate.db) +-l parse as traditional locate database format (default slocate.db style) +-h this help message +XXX + exit +end + + +def signedbyte(byte) + if byte >= 128 + ((byte^0xff)+1)*-1 + else + byte + end +end + +parse_options + +fh = File.new(@options["-i"], "r") + +# dit byte schijnt heel het verschil te zijn met traditionele locate databases +# dus bij een traditionele even 4 regels niet gebruiken +if ! @options["-l"] + secure=fh.read(1) + if secure == '1' + puts "Uses security features..." + end +end + +curdepth=0 +curpaths="" +while true + char = fh.read(1) + if char.nil? + # we're done + exit + end + depth = signedbyte(char[0]) + curdepth+=depth + curpaths=curpaths[0...curdepth] + newname='' + while(b=fh.read(1)) != "\000" + newname += b + end + curpaths+=newname + puts curpaths +end