publicscripts/slocatedb-dump/slocatedb-dump.rb

91 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

#!/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