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