1#! /usr/bin/env tclkit
2
3# Show byte offset where MK data starts in a file and the view structure
4# Nov 2002, jcw@equi4.com
5
6proc fail {msg} { puts stderr " $msg"; exit 1 }
7
8if {[llength $argv] < 1} {
9  fail "usage: $argv0 file ..."
10}
11
12foreach filename $argv {
13  puts $filename
14
15  if {![file exists $filename]} {
16    fail "file does not exist"
17  }
18  if {![file isfile $filename]} {
19    fail "this is not a regular file (perhaps mounted as VFS?)"
20  }
21  set end [file size $filename]
22  if {$end < 27} {
23    fail "file too small, cannot be a datafile"
24  }
25
26  set fd [open $filename]
27  fconfigure $fd -translation binary
28  seek $fd -16 end
29  binary scan [read $fd 16] IIII a b c d
30
31 #puts [format %x-%d-%x-%d $a $b $c $d]
32
33  if {($c >> 24) != -128} {
34    fail "this is not a Metakit datafile"
35  }
36
37  # avoid negative sign / overflow issues
38  if {[format %x [expr {$a & 0xffffffff}]] eq "80000000"} {
39    set start [expr {$end - 16 - $b}]
40  } else {
41    # if the file is in commit-progress state, we need to do more
42    fail "this code needs to be finished..."
43  }
44
45  seek $fd $start
46  switch -- [read $fd 2] {
47    JL { set endian little }
48    LJ { set endian big }
49    default { fail "failed to locate file header" }
50  }
51  close $fd
52
53  puts "  Metakit data starts at offset $start and is stored as $endian-endian"
54
55  if {[catch {
56    package require vlerq
57
58    set db [vlerq open $filename]
59
60    # MK files always have exactly one row with only top-level views
61    if {[vlerq get $db #] == 1 && [regexp {^[ V]+$} [vlerq types $db]]} {
62      foreach x [vlerq names $db] {
63	set v [vlerq get $db 0 $x]
64	puts "[format %7d [vlerq size $v]]x $x\[[vlerq structdesc $v]\]"
65      }
66    } else {
67      puts "  Structure:"
68      puts "    [vlerq structdesc $db]"
69      puts "  (this file cannot be used with Metakit 2.4.x)"
70    }
71  }]} {
72    mk::file open db $filename -readonly
73    puts "  Views:"
74    foreach x [mk::file views db] {
75      puts "[format %7d [mk::view size db.$x]]x\
76			[list $x [mk::view layout db.$x]]"
77    }
78    mk::file close db
79  }
80}
81