Fix/finish modularization
[mapsector.git] / mapsector
1 #!/bin/sh
2
3 ###########################################################################
4
5 mappers=""
6 scanners=""
7 version="unknown"
8
9 register_mapper()
10 {
11     mappers="${mappers}${mappers:+ }$1"
12 }
13
14 register_scanner()
15 {
16     scanners="${scanners}${scanners:+ }$1"
17 }
18
19 load()
20 {
21     for lib in `ls "$1"/[0-9][0-9]_*.sh | sort`; do
22         source $lib
23     done
24 }
25
26 # Map device/sector through blockdevice mappings (e.g. lvm)
27
28 map()
29 {
30     local found
31     found=1
32     while [ -n "$found" ]; do
33         found=""
34         for name in $mappers; do
35             if detect_$name; then
36                 found=1
37                 if do_$name; then
38                     return
39                 fi
40                 break
41             fi
42         done
43     done
44 }
45
46 scan()
47 {
48     for name in $scanners; do
49         if detect_$name; then
50             if do_$name; then
51                 return
52             fi
53         fi
54     done
55 }
56
57 ###########################################################################
58
59 unset LANG
60
61 #### Find library directory and load library files
62
63 name="`basename "$0"`"
64 X="`dirname "$0"`"
65 if [ "`basename "$X"`" == "bin" ]; then # `"
66     libdir="`dirname "$X"`/share/$name" #`"
67 fi
68 if [ -z "$libdir" -o ! -d "$libdir" ]; then
69     libdir="$X/lib"
70 fi
71
72 if [ ! -d "$libdir" ]; then
73     echo "! Library directory not found" 1>&2
74     exit 1
75 fi
76
77 load "$libdir"
78
79 #### Parse command line arguments
80
81 X=`getopt -o "hV" --long "help,noscan,version" -n "$name" -- "$@"`
82 if [ $? != 0 ]; then exit 1; fi
83 eval set -- "$X"
84
85 noscan=""
86 help=""
87
88 while true; do
89     case "$1" in
90         -h|--help) help="0"; shift ;;
91         --noscan) noscan="0"; shift ;;
92         -V|--version) echo "$name $version"; exit 0 ;;
93         --) shift; break ;;
94         *) echo "! internal error"; exit 1 ;;
95     esac
96 done
97
98 if [ -n "$help" -o -z "$2" ]; then
99     cat <<EOF
100 Usage: $name [-h|--help] [--noscan] <device> <sector>"
101
102 mapsector -- Map sector numbers to file name(s)
103
104 Given a device and a sector number, mapsector will try to find the
105 file name(s) mapping to this sector. It will try to gather as much
106 information about the given sector as possible.
107
108 mapsector currently has support for the following mapping schemes:
109
110 EOF
111     for mapper in $mappers; do
112         describe_$mapper
113     done
114     cat <<EOF
115
116 mapsector currently supports the following filesystems
117
118 EOF
119     for scanner in $scanners; do
120         describe_$scanner
121     done
122 cat <<EOF
123
124 mapsector will try it's best to find an associated file name but
125 depending on the filesystem state and the type of sector (e.g. if
126 the sector is part of a filesystem metadata block) this may not be
127 possible.
128
129 For mapsector to work, the filesystem must be currently active
130 (e.g. LVM must be running, crypted devices must have been set up). The
131 filesystem must not necessarily be mounted (though if mounted,
132 mapsector will give you the mountpoint).
133
134 if '--noscan' is given, the possibly lengthy (!!) filesystem scan for
135 filenames is skipped.
136
137 -- mapsector Version $version
138 EOF
139     exit "${help:-1}"
140 fi
141
142 device="$1"
143 sector="$2"
144
145 echo "device $device"
146 echo "sector $sector"
147
148 map
149 if [ -z "$noscan" ]; then
150     scan
151 fi