publicscripts/mlocatedb-dump/mlocatedb-dump.rb

139 lines
2.3 KiB
Ruby
Raw Normal View History

2009-01-22 14:36:49 +00:00
#!/usr/bin/env ruby
# $Id$
# $URL$
# Parser voor mlocate.db bestanden. Toont een filelisting van alle bestanden
# uit de database.
# Zie ook: http://linux.die.net/man/5/mlocate.db
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
2009-01-22 14:36:49 +00:00
def arr2long(arr)
return (arr[0]<<24) + (arr[1]<<16) + (arr[2]<<8) + arr[3]
end
def arr2string(arr)
string = ""
(0...arr.length).step(2){|i|
string += ((arr[i]<<8) + (arr[i+1])).chr
}
return string
end
parse_options
2009-01-22 14:36:49 +00:00
fh = File.new(@options["-i"], "r")
2009-01-22 14:36:49 +00:00
magic_number=fh.read(8)
if magic_number == "\000mlocate"
# puts "mlocate database found"
else
puts "Not an mlocate database"
exit
end
configuration_block_size=arr2long(fh.read(4))
#puts "config block #{configuration_block_size}"
file_format_version=fh.read(1).to_i
#puts "FF vers #{file_format_version}"
require_visibility=fh.read(1).to_i
#puts "require visibility #{require_visibility}"
# get rid of padding
fh.read(2)
#
dbroot=''
while (b=fh.read(1)) != "\000"
dbroot += b
end
#puts "DB root: #{dbroot}"
# skip configuration, we don't care much
bla = fh.read(configuration_block_size)
#p bla
# skip dir header. Blijkbaar is die toch echt 2 keer 8 bytes
fh.read(16)
curdir=''
while (b=fh.read(1)) != "\000"
curdir += b
end
puts curdir
if curdir == '/'
curdir = ''
end
while true
type = fh.read(1)
case type
when "\000" then
file=''
while (b=fh.read(1)) != "\000"
file += b
end
puts "#{curdir}/#{file}"
when "\001" then
subdir=''
while (b=fh.read(1)) != "\000"
subdir += b
end
# puts "#{curdir}/#{subdir}/"
when "\002" then
# dan komt er vast een nieuwe
fh.read(16)
curdir=''
while (b=fh.read(1)) != "\000"
if b.nil?
# we're done
exit
end
2009-01-22 14:36:49 +00:00
curdir += b
end
puts "#{curdir}/"
end
end