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