add updateversion.sh script
[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 run()
29 {
30     local found
31     foundm=1
32     while [ -n "$found" ]; do
33         found=""
34         for name in "$@"; 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 ###########################################################################
47
48 unset LANG
49
50 #### Find library directory and load library files
51
52 name="`basename "$0"`"
53 X="`dirname "$0"`"
54 if [ "`basename "$X"`" == "bin" ]; then # `"
55     libdir="`dirname "$X"`/lib/$name" #`"
56 fi
57 if [ -z "$libdir" -o ! -d "$libdir" ]; then
58     libdir="$X/lib"
59 fi
60
61 if [ ! -d "$libdir" ]; then
62     echo "! Library directory not found" 1>&2
63     exit 1
64 fi
65
66 load "$libdir"
67
68 #### Parse command line arguments
69
70 X=`getopt -o "hV" --long "help,noscan,version" -n "$name" -- "$@"`
71 if [ $? != 0 ]; then exit 1; fi
72 eval set -- "$X"
73
74 noscan=""
75 help=""
76
77 while true; do
78     case "$1" in
79         -h|--help) help="0"; shift ;;
80         --noscan) noscan="0"; shift ;;
81         -V|--version) echo "$name $version"; exit 0 ;;
82         --) shift; break ;;
83         *) echo "! internal error"; exit 1 ;;
84     esac
85 done
86
87 if [ -n "$help" -o -z "$2" ]; then
88     cat <<EOF
89 Usage: $name [-h|--help] [--noscan] <device> <sector>"
90
91 mapsector -- Map sector numbers to file name(s)
92
93 Given a device and a sector number, mapsector will try to find the
94 file name(s) mapping to this sector. It will try to gather as much
95 information about the given sector as possible.
96
97 mapsector currently has support for the following mapping schemes:
98
99 EOF
100     for mapper in $mappers; do
101         describe_$mapper
102     done
103     cat <<EOF
104
105 mapsector currently supports the following filesystems
106
107 EOF
108     for scanner in $scanners; do
109         describe_$scanner
110     done
111 cat <<EOF
112
113 mapsector will try it's best to find an associated file name but
114 depending on the filesystem state and the type of sector (e.g. if
115 the sector is part of a filesystem metadata block) this may not be
116 possible.
117
118 For mapsector to work, the filesystem must be currently active
119 (e.g. LVM must be running, crypted devices must have been set up). The
120 filesystem must not necessarily be mounted (though if mounted,
121 mapsector will give you the mountpoint).
122
123 if '--noscan' is given, the possibly lengthy (!!) filesystem scan for
124 filenames is skipped.
125
126 -- mapsector Version $version
127 EOF
128     exit "${help:-1}"
129 fi
130
131 device="$1"
132 sector="$2"
133
134 echo "device $device"
135 echo "sector $sector"
136
137 run $mappers
138 if [ -z "$noscan" ]; then
139     run $scanners
140 fi