ook een versie voor slocate en mooi opties afhandelen

This commit is contained in:
Ward Wouts 2009-01-24 09:08:07 +00:00
parent 7b0f469d24
commit ef13833b4e
2 changed files with 133 additions and 3 deletions

View file

@ -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 <<XXX
Usage: slocatedb-dump.rb [-i <inputfile>] [-l] [-h]
-i <inputfile> use <inputfile> 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}/"

View file

@ -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 <<XXX
Usage: slocatedb-dump.rb [-i <inputfile>] [-l] [-h]
-i <inputfile> use <inputfile> 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