|
2
by Scott Moser
iso-extract: replace bash with sh |
1 |
#!/bin/sh
|
|
1
by Scott Moser
add iso-extract |
2 |
# vi: ts=4 noexpandtab
|
3 |
#
|
|
4 |
# Copyright (C) 2010 Canonical Ltd.
|
|
5 |
#
|
|
6 |
# Authors: Scott Moser <smoser@canonical.com>
|
|
7 |
#
|
|
8 |
# This program is free software: you can redistribute it and/or modify
|
|
9 |
# it under the terms of the GNU General Public License as published by
|
|
10 |
# the Free Software Foundation, version 3 of the License.
|
|
11 |
#
|
|
12 |
# This program is distributed in the hope that it will be useful,
|
|
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
# GNU General Public License for more details.
|
|
16 |
#
|
|
17 |
# You should have received a copy of the GNU General Public License
|
|
18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 |
||
20 |
TEMP_D="" |
|
21 |
||
22 |
error() { echo "$@" 1>&2; } |
|
23 |
fail() { [ $# -eq 0 ] || error "$@"; exit 1; } |
|
24 |
Usage() { |
|
25 |
cat <<EOF
|
|
26 |
Usage: ${0##*/} iso-file dir
|
|
27 |
copy contents of iso9660 "iso" file to dir
|
|
28 |
options:
|
|
29 |
-l | --lower write lowercase filenames (only valid if not rockridge iso)
|
|
30 |
-v | --verbose increase verbosity
|
|
31 |
--no-rock do not use rockridge extensions
|
|
32 |
EOF
|
|
33 |
}
|
|
34 |
bad_Usage() { Usage 1>&2; fail "$@"; } |
|
35 |
cleanup() { |
|
36 |
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}" |
|
37 |
}
|
|
38 |
debug() { |
|
39 |
local level=${1} |
|
40 |
[ "${level}" -gt "${VERBOSITY}" ] && return |
|
41 |
shift; |
|
42 |
error "${@}"
|
|
43 |
}
|
|
44 |
||
45 |
short_opts="hlv" |
|
46 |
long_opts="help,lower,no-rock,verbose" |
|
47 |
getopt_out=$(getopt --name "${0##*/}" \ |
|
48 |
--options "${short_opts}" --long "${long_opts}" -- "$@") && |
|
49 |
eval set -- "${getopt_out}" || |
|
50 |
bad_Usage |
|
51 |
||
52 |
iso="" |
|
53 |
outd="" |
|
54 |
lower=0 |
|
55 |
use_rockridge=1 |
|
56 |
VERBOSITY=0 |
|
57 |
while [ $# -ne 0 ]; do |
|
58 |
cur=${1}; next=${2}; |
|
59 |
case "$cur" in |
|
60 |
-h|--help) Usage; exit 0;; |
|
61 |
-v|--verbose) VERBOSITY=$(($VERBOSITY+1));; |
|
62 |
-l|--lower) lower=1;; |
|
63 |
--no-rockridge) use_rockridge=0;; |
|
64 |
--) shift; break;; |
|
65 |
esac
|
|
66 |
shift; |
|
67 |
done
|
|
68 |
||
69 |
[ $# -eq 2 ] || bad_Usage "must supply iso and output dir" |
|
70 |
iso=${1} |
|
71 |
outd=${2} |
|
72 |
||
73 |
||
74 |
[ -f "${iso}" ] || fail "${iso} is not a file" |
|
75 |
||
76 |
{ [ -d "${outd}" ] || mkdir "${outd}"; } || |
|
77 |
fail "failed to create ${outd}";
|
|
78 |
||
79 |
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/.${0##*/}.XXXXXX") || |
|
80 |
fail "failed to make tempd"
|
|
81 |
trap cleanup EXIT
|
|
82 |
||
83 |
out=$(which isoinfo >/dev/null 2>&1) || |
|
84 |
fail "isoinfo not found in path"
|
|
85 |
||
86 |
lslr_out="${TEMP_D}/ls-lr.txt" |
|
87 |
rockridge=0 |
|
88 |
if [ "${use_rockridge}" -eq 1 ]; then |
|
89 |
isoinfo -R -l -i "${iso}" > "${lslr_out}" && rockridge=1 |
|
90 |
fi
|
|
91 |
if [ ! -s "${lslr_out}" ]; then |
|
92 |
isoinfo -l -i "${iso}" > "${lslr_out}" || |
|
93 |
fail "failed to get directory listing of ${iso}"
|
|
94 |
if [ "${lower}" -ne 0 ]; then |
|
95 |
tr "[A-Z]" "[a-z]" < "${lslr_out}" > "${lslr_out}.tmp" && |
|
96 |
mv "${lslr_out}.tmp" "${lslr_out}" || |
|
97 |
fail "failed to translate to lower case"
|
|
98 |
fi
|
|
99 |
fi
|
|
100 |
||
101 |
curpath="" |
|
102 |
while read line; do |
|
103 |
set -- ${line} |
|
104 |
case "${line}" in |
|
105 |
[Dd]irectory\ *) |
|
106 |
# Directory Listing of <dir>
|
|
107 |
curpath=${4%/} # it always has a trailing slash |
|
108 |
{ [ -d "${outd}/${curpath}" ] || mkdir -p "${outd}/${curpath}"; } || |
|
109 |
fail "failed to make dir ${outd}/${curpath}"
|
|
110 |
;; |
|
111 |
*)
|
|
112 |
fname=${12} |
|
113 |
size=${5} |
|
114 |
if [ "${rockridge}" -eq 1 ]; then |
|
115 |
# determine its a file by not a 'd' at the beginning
|
|
116 |
[ "${1#d}" = "${1}" -a -n "${fname}" ] || continue |
|
117 |
curf="${curpath}/${fname}" |
|
|
3
by Scott Moser
fix bug where rock ridge extracted files were empty. |
118 |
isoinfo -R -i "${iso}" -x "${curf}" > "${outd}/${curf}" || |
119 |
fail "failed to extract ${curf}"
|
|
|
1
by Scott Moser
add iso-extract |
120 |
else
|
121 |
# determine that its a file by filename ends in ;1
|
|
122 |
[ "${fname}" = "${fname%;1}" ] && continue |
|
123 |
curf="${curpath}/${fname%;1}" |
|
|
3
by Scott Moser
fix bug where rock ridge extracted files were empty. |
124 |
isoinfo -i "${iso}" -x "${curf}" > "${outd}/${curf}" || |
125 |
fail "failed to extract ${curf}"
|
|
|
1
by Scott Moser
add iso-extract |
126 |
fi
|
127 |
debug 1 "${curf}" |
|
|
3
by Scott Moser
fix bug where rock ridge extracted files were empty. |
128 |
isoX "${iso}" "${curf}" > "${outd}/${curf}" |
|
1
by Scott Moser
add iso-extract |
129 |
esac
|
130 |
done < "${lslr_out}" |
|
131 |
||
132 |
echo "extracted contents of ${iso} to ${outd}" |
|
133 |
exit 0
|