"Fossies" - the Fresh Open Source Software Archive 
Member "xorriso-1.5.4/frontend/xorriso_broker.sh" (30 Jan 2021, 5347 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #!/bin/sh
2
3 # Copyright (C) 2015
4 # Thomas Schmitt <scdbackup@gmx.net>, libburnia-project.org
5 # Provided under BSD license: Use, modify, and distribute as you like.
6
7 # set -x
8
9 # ---------------------------- functions ---------------------------
10
11 usage() {
12 echo >&2
13 echo "usage: $0 "'\' >&2
14 echo " [-xorriso path] id_string [-dev] iso_adr [xorriso_arguments ...]" >&2
15 echo >&2
16 echo " This script looks for named pipe" >&2
17 echo ' /tmp/xorriso_stdin_pipe_${id_string}' >&2
18 echo " which is supposed to be connected to a xorriso process." >&2
19 echo " If not found, the stdin pipe and a stdout pipe get created" >&2
20 echo " and a xorriso dialog process gets started and connected." >&2
21 echo " Each character in id_string must match [-+:.,=@0-9A-Za-z]." >&2
22 echo " If iso_adr differs from the previous run with the same id_string," >&2
23 echo " then any changes on the previous ISO are committed as session" >&2
24 echo " before command -dev is performed to load the meta data of" >&2
25 echo " the newly addressed ISO." >&2
26 echo " After this is done, the optionally given xorriso_arguments" >&2
27 echo " are written into the stdin pipe from where xorriso will read" >&2
28 echo " them as commands and their parameters." >&2
29 echo >&2
30 }
31
32
33 # Make filenames safe for transport by wrapping them in quotes and
34 # escaping quotes in their text
35 xorriso_esc() {
36 echo -n "'"
37 echo -n "$1" | sed -e "s/'/'"'"'"'"'"'"'/g"
38 echo -n "'"
39 }
40
41
42 # Send one or more command lines to xorriso
43 xorriso_send_cmd() {
44 # $1 : the lines to send
45
46 # >>> is it possible to have a timeout on echo ?
47
48 if test -p "$cmd_pipe"
49 then
50 echo " $1" >"$cmd_pipe"
51 else
52 xorriso_is_running=0
53 return 1
54 fi
55 }
56
57
58 # Send command and wait for answer
59 xorriso_cmd_and_result() {
60 # $1: command line for xorriso
61 # $2: if not empty, grep expression for stdout
62 if test "$xorriso_is_running" = 0
63 then
64 return 1
65 fi
66 xorriso_send_cmd "$1" || return 1
67 if test -n "$2"
68 then
69 cat "$result_pipe"
70 else
71 grep "$2" <"$result_pipe"
72 fi
73 return 0
74 }
75
76
77 # ------------------------------- main -----------------------------
78
79 # Argument interpreter
80
81 if test "$#" -lt 2
82 then
83 usage "$0"
84 exit 1
85 fi
86
87 xorriso=xorriso
88 if test o"$1" = o"-xorriso"
89 then
90 xorriso="$2"
91 shift 2
92 fi
93 export xorriso_is_running=0
94
95 if test "$#" -lt 2
96 then
97 usage "$0"
98 exit 1
99 fi
100
101 id_string=$(echo "$1" | sed -e 's/[^-+:.,=@0-9A-Za-z]/_/g' )
102 shift 1
103
104 # Ignore second argument -dev
105 if test o"$1" = o"-dev"
106 then
107 shift 1
108 if test "$#" -lt 1
109 then
110 usage "$0"
111 exit 1
112 fi
113 fi
114 device="$1"
115 shift 1
116
117 # Perform the action
118
119 export cmd_pipe=/tmp/xorriso_stdin_pipe_$id_string
120 export result_pipe=/tmp/xorriso_stdout_pipe_$id_string
121
122 if test -p "$cmd_pipe"
123 then
124 xorriso_is_running=1
125 else
126 xorriso_is_running=0
127 fi
128 if test "$xorriso_is_running" = "0"
129 then
130 # xorriso is not started yet
131
132 # Check for xorriso version which knows command -named_pipe_loop
133 echo "Checking xorriso version ..." >&2
134 xorriso_version_req="1.3.2"
135 version=$("$xorriso" -version | grep '^xorriso version' |
136 sed -e 's/^xorriso version : //')
137 smallest=$( (echo "$xorriso_version_req" ; echo "$version" ) | \
138 sort | head -1)
139 if test "$smallest" = "$xorriso_version_req"
140 then
141 dummy=dummy
142 else
143 echo "$0 : FATAL : Need xorriso version >= $xorriso_version_req" >&2
144 echo "Found version: $version" >&2
145 exit 2
146 fi
147
148 if mknod "$cmd_pipe" p
149 then
150 echo "Created named pipe for xorriso commands: $cmd_pipe" >&2
151 else
152 echo "Failed to create named pipe for xorriso commands: $cmd_pipe" >&2
153 exit 3
154 fi
155 if mknod "$result_pipe" p
156 then
157 echo "Created named pipe for xorriso result channel: $result_pipe" >&2
158 else
159 echo \
160 "Failed to create named pipe for xorriso result channel: $result_pipe" >&2
161 if rm "$cmd_pipe"
162 then
163 echo "Removed named pipe for xorriso commands: $cmd_pipe" >&2
164 fi
165 exit 3
166 fi
167 echo "Starting xorriso process ..." >&2
168 "$xorriso" -abort_on NEVER -for_backup \
169 -named_pipe_loop cleanup:buffered "$cmd_pipe" "$result_pipe" "-" \
170 >&2 &
171 # (stdout is redirected to stderr, in order not to keep a pipe waiting for
172 # input from the still open stdout copy of the background process.
173 # -named_pipe_loop will disconnect xorriso result channel from stdout.)
174 xorriso_is_running=1
175 fi
176
177 # Inquire current xorriso -dev
178 xorriso_device=$(xorriso_cmd_and_result "-status -dev" "^-dev" | \
179 sed -e 's/^-dev //')
180 if echo " $device" | grep "^ '" >/dev/null
181 then
182 quoted="$device"
183 else
184 quoted=$(xorriso_esc "$device")
185 fi
186 if test "$xorriso_device" = "$quoted"
187 then
188 dummy=dummy
189 else
190 # Inquire the need for a -commit command
191 pending=$(xorriso_cmd_and_result "-changes_pending show_status" \
192 "^-changes_pending" \
193 | sed -e 's/^-changes_pending //')
194 if test "$pending" = "yes"
195 then
196 if xorriso_cmd_and_result "-commit"
197 then
198 dummy=dummy
199 else
200 exit 1
201 fi
202 fi
203 # Now change ISO filesystem
204 if xorriso_cmd_and_result "-dev $device"
205 then
206 xorriso_device="$device"
207 else
208 exit 1
209 fi
210 fi
211
212 test "$*" = "" && exit 0
213
214 if xorriso_cmd_and_result "$*"
215 then
216 dummy=dummy
217 else
218 exit 1
219 fi
220
221 exit 0
222