]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/scripts/dvd-handler.cmd
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / scripts / dvd-handler.cmd
1 @ECHO off\r
2 REM \r
3 REM  Check the free space available on a writable DVD\r
4 REM  Should always exit with 0 status, otherwise it indicates a serious error.\r
5 REM  (wrong number of arguments, Python exception...)\r
6 REM \r
7 REM   called:  dvd-handler <dvd-device-name> operation args\r
8 REM \r
9 REM   operations used by Bacula:\r
10 REM \r
11 REM    free   (no arguments)\r
12 REM       Scan the device and report the available space. It returns:\r
13 REM       Prints on the first output line the free space available in \r
14 REM       bytes.\r
15 REM       If an error occurs, prints a negative number (-errno), followed,\r
16 REM       on the second line, by an error message.\r
17 REM \r
18 REM    write  op filename\r
19 REM        Write a part file to disk.\r
20 REM        This operation needs two additional arguments.\r
21 REM        The first (op) indicates to\r
22 REM           0 -- append\r
23 REM           1 -- first write to a blank disk\r
24 REM           2 -- blank or truncate a disk\r
25 REM \r
26 REM        The second is the filename to write\r
27 REM \r
28 REM   operations available but not used by Bacula:\r
29 REM \r
30 REM    test      Scan the device and report the information found.\r
31 REM              This operation needs no further arguments.\r
32 REM    prepare   Prepare a DVD+/-RW for being used by Bacula.\r
33 REM              Note: This is only useful if you already have some\r
34 REM              non-Bacula data on a medium, and you want to use\r
35 REM              it with Bacula. Don't run this on blank media, it\r
36 REM              is useless.\r
37 REM \r
38 REM  \r
39 REM  $Id: dvd-handler.in,v 1.11 2006/08/30 16:19:30 kerns Exp $\r
40 REM \r
41 \r
42 setlocal ENABLEDELAYEDEXPANSION\r
43 SET PATH=%PATH%;@bin_dir_cmd@\r
44 REM  Configurable values:\r
45 \r
46 SET dvdrwmediainfo=dvd+rw-mediainfo.exe\r
47 SET growcmd=growisofs.exe\r
48 SET dvdrwformat=dvd+rw-format.exe\r
49 SET dd=dd.exe\r
50 SET margin=10485760\r
51 \r
52 REM Comment the following line if you want the tray to be reloaded\r
53 REM when writing ends.\r
54 SET growcmd=%growcmd% -use-the-force-luke^^^^^=notray\r
55 \r
56 REM  end of configurable values\r
57 \r
58 IF "%1" == "" GOTO :usage\r
59 IF "%2" == "" GOTO :usage\r
60 \r
61 CALL :init %1\r
62 \r
63 IF "%2" == "free" (\r
64    CALL :free\r
65    ECHO !ERRORLEVEL!\r
66    ECHO No Error reported.\r
67 ) ELSE IF "%2" == "prepare" (\r
68    CALL :prepare\r
69    ECHO Medium prepared successfully.\r
70 ) ELSE IF "%2" == "test" (\r
71    IF %freespace_collected% EQU 0 CALL :collect_freespace\r
72    IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
73    ECHO Class disk, initialized with device %device%\r
74    ECHO  type = '!disktype!' mode='!diskmode!' status = '!diskstatus!'\r
75    ECHO  next_session = !next_session! capacity = !capacity!\r
76    ECHO  Hardware device is '!hardwaredevice!'\r
77    ECHO  growcmd = '!growcmd!'\r
78    ECHO  growparams = '!growparams!'\r
79    ECHO.\r
80    SET empty_disk=false\r
81    CALL :is_blank\r
82    IF !ERRORLEVEL! EQU 1 SET empty_disk=true\r
83    SET rewritable=false\r
84    CALL :is_RW\r
85    IF !ERRORLEVEL! EQU 1 SET rewritable=true\r
86    SET plus_RW_disk=false\r
87    CALL :is_plus_RW\r
88    IF !ERRORLEVEL! EQU 1 SET plus_RW_disk=true\r
89    SET minus_RW_disk=false\r
90    CALL :is_minus_RW\r
91    IF !ERRORLEVEL! EQU 1 SET minus_RW_disk=true\r
92    SET restricted_overwrite_disk=false\r
93    CALL :is_restricted_overwrite\r
94    IF !ERRORLEVEL! EQU 1 SET restricted_overwrite_disk=true\r
95    SET blank_disk=false\r
96    CALL :is_blank\r
97    IF !ERRORLEVEL! EQU 1 SET blank_disk=true\r
98    ECHO Empty disk: !empty_disk!  Blank Disk: !blank_disk!  ReWritable disk: !rewritable!\r
99    ECHO Plus RW: !plus_RW_disk!  Minus RW: !minus_RW_disk!  Restricted Overwrite: !restricted_overwrite_disk!\r
100    CALL :free\r
101    ECHO Free space: !ERRORLEVEL!\r
102 ) ELSE IF "%2" == "write" (\r
103    IF "%3" == "" GOTO :usage\r
104    IF "%4" == "" GOTO :usage\r
105    CALL :write %3 %4\r
106    ECHO Part file %4 successfully written to disk.\r
107 ) ELSE (\r
108    ECHO No operation - use test, free, prepare or write.\r
109    ECHO THIS MIGHT BE A CASE OF DEBUGGING BACULA OR AN ERROR!\r
110 )\r
111 EXIT /b 0\r
112 \r
113 REM ##########################################################################\r
114 REM\r
115 REM  The rest of this file is a set of subroutines that return DVD disk \r
116 REM  information.\r
117 REM\r
118 REM  Status information about the device and the disk loaded is collected \r
119 REM  only when asked for (for example dvd-freespace doesn't need to know the \r
120 REM  media type, and dvd-writepart doesn't not always need to know the free \r
121 REM  space).\r
122 REM\r
123 REM  The following subroutines are implemented:\r
124 REM  init       we need that...\r
125 REM  is_empty   Set ERRORLEVEL to TRUE if the disk is empty, blank... \r
126 REM              this needs more work, especially concerning non-RW media \r
127 REM              and blank vs. no filesystem considerations. Here, we \r
128 REM              should also look for other filesystems - probably we don't\r
129 REM              want to silently overwrite UDF or ext2 or anything not \r
130 REM              mentioned in fstab...\r
131 REM              (NB: I don't think it is a problem)\r
132 REM  is_RW       Set ERRORLEVEL to TRUE if the disk is RW (DVD-RW or DVD+RW)\r
133 REM  is_plus_RW  Set ERRORLEVEL to TRUE if the disk is DVD+RW\r
134 REM  is_minus_RW Set ERRORLEVEL to TRUE if the disk is DVD-RW\r
135 REM  is_blank    Set ERRORLEVEL to TRUE if the disk is blank\r
136 REM  free        Returns the available free space.\r
137 REM  write       Writes one part file to disk, either starting a new file\r
138 REM              system on disk, or appending to it.\r
139 REM              This method should also prepare a blank disk so that a\r
140 REM              certain part of the disk is used to allow detection of a\r
141 REM              used disk by all / more disk drives.\r
142 REM  blank       Blank the device\r
143 REM \r
144 REM ##########################################################################\r
145 \r
146 :init\r
147 SET device=%1\r
148 SET disktype=none\r
149 SET diskmode=none\r
150 SET diskstatus=none\r
151 SET hardwaredevice=none\r
152 SET pid=0\r
153 SET next_session=-1\r
154 SET capacity=-1\r
155 \r
156 SET freespace_collected=0\r
157 SET mediumtype_collected=0\r
158 \r
159 SET growcmd=%growcmd% -quiet -use-the-force-luke^^^=4gms\r
160 \r
161 SET growparams=-A "Bacula Data" -input-charset=default -iso-level 3 -pad\r
162 SET growparams=%growparams% -p "dvd-handler / growisofs" -sysid "BACULADATA" -R\r
163 GOTO :EOF\r
164 \r
165 :collect_freespace\r
166 SET next_session=0\r
167 SET capacity=0\r
168 FOR /f "delims== tokens=1*" %%i in ( '%growcmd% -F %device%' ) DO (\r
169    IF "%%i" == "next_session" ( \r
170       SET next_session=%%j\r
171    ) ELSE IF "%%i" == "capacity" (\r
172       SET capacity=%%j\r
173    ) ELSE IF "%%j" == "" (\r
174       SET result=!result! %%i\r
175    ) ELSE (\r
176       SET RESULT=!result! %%i=%%j\r
177    )\r
178 )\r
179 SET status=%ERRORLEVEL%\r
180 IF %STATUS% NEQ 0 (\r
181    SET /a STATUS=STATUS ^& 0x7F\r
182    IF !STATUS! EQU 112 (\r
183       REM Kludge to force dvd-handler to return a free space of 0\r
184       next_session = 1\r
185       capacity = 1\r
186       freespace_collected = 1\r
187       GOTO :EOF\r
188    ) ELSE (\r
189       ECHO growisofs returned with an error !STATUS!. Please check you are using a patched version of dvd+rw-tools.\r
190       EXIT !STATUS!\r
191    )\r
192 )\r
193 \r
194 IF %next_session% EQU 0 IF %capacity% EQU 0 (\r
195    ECHO Cannot get next_session and capacity from growisofs.\r
196    ECHO Returned: %result:|=^|%\r
197    EXIT 1\r
198 )\r
199 SET freespace_collected=1\r
200 GOTO :EOF\r
201 \r
202 :collect_mediumtype\r
203 SET hardwaredevice=\r
204 SET disktype=\r
205 SET diskmode=\r
206 SET diskstatus=\r
207 SET lasterror=\r
208 FOR /f "delims=: tokens=1,2 usebackq" %%i in ( `"%dvdrwmediainfo%" %device%` ) DO (\r
209    IF "%%i" == "INQUIRY" FOR /f "tokens=*" %%k in ( "%%j" ) DO SET hardwaredevice=%%k\r
210    IF "%%i" == " Mounted Media" FOR /f "tokens=1,2* delims=, " %%k in ( "%%j" ) DO (\r
211       SET disktype=%%l\r
212       SET diskmode=%%m\r
213    )\r
214    IF "%%i" == " Disc status" FOR /f "tokens=*" %%k in ( "%%j" ) DO SET diskstatus=%%k\r
215 )\r
216 \r
217 IF NOT DEFINED disktype (\r
218    ECHO Media type not found in %dvdrwmediainfo% output\r
219    EXIT 1\r
220 )\r
221 \r
222 IF "%disktype%" == "DVD-RW" IF NOT DEFINED diskmode (\r
223    ECHO Media mode not found for DVD-RW in %dvdrwmediainfo% output\r
224    EXIT 1\r
225 )\r
226 \r
227 IF NOT DEFINED diskstatus (\r
228    ECHO Disc status not found in %dvdrwmediainfo% output\r
229    EXIT 1\r
230 )\r
231 \r
232 SET mediumtype_collected=1\r
233 GOTO :EOF\r
234 \r
235 :is_empty\r
236 IF %freespace_collected% EQU 0 CALL :collect_freespace\r
237 IF %next_session% EQU 0 ( EXIT /b 1 ) ELSE EXIT /b 0\r
238 \r
239 :is_RW\r
240 IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
241 IF %disktype% == "DVD-RW" EXIT /b 1\r
242 IF %disktype% == "DVD+RW" EXIT /b 1\r
243 IF %disktype% == "DVD-RAM" EXIT /b 1\r
244 EXIT /b 0\r
245 \r
246 :is_plus_RW\r
247 IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
248 IF "%disktype%" == "DVD+RW" EXIT /b 1\r
249 EXIT /b 0\r
250 \r
251 :is_minus_RW\r
252 IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
253 IF "%disktype%" == "DVD-RW" EXIT /b 1\r
254 EXIT /b 0\r
255       \r
256 :is_restricted_overwrite\r
257 IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
258 IF "%diskmode%" == "Restricted Overwrite" EXIT /b 1\r
259 EXIT /b 0\r
260 \r
261 :is_blank\r
262 IF %mediumtype_collected% EQU 0 CALL :collect_mediumtype\r
263 IF "%diskstatus%" == "blank" EXIT /b 1\r
264 EXIT /b 0\r
265 \r
266 :free\r
267 IF %freespace_collected% EQU 0 CALL :collect_freespace\r
268 FOR /f %%i in ( 'expr64 "capacity - next_session - margin"' ) DO SET fr=%%i\r
269 expr64 /q "fr < 0" && ( EXIT /b 0 ) || EXIT /b %fr%\r
270 \r
271 REM %1 - newvol, %2 - partfile\r
272 :write\r
273 REM Blank DVD+RW when there is no data on it\r
274 CALL :is_plus_RW\r
275 SET tmpvar=%ERRORLEVEL%\r
276 CALL :is_blank\r
277 SET /a tmpvar=tmpvar + ERRORLEVEL\r
278 IF %1 EQU 1 IF %tmpvar% EQU 2 (\r
279    ECHO DVD+RW looks brand-new, blank it to fix some DVD-writers bugs.\r
280    CALL :blank\r
281    ECHO Done, now writing the part file.\r
282 )\r
283 CALL :is_minus_RW\r
284 IF %ERRORLEVEL% NEQ 0 IF %1 NEQ 0 (\r
285    CALL :is_restricted_overwrite\r
286    IF !ERRORLEVEL! EQU 0 (\r
287       ECHO DVD-RW is in %diskmode% mode, reformating it to Restricted Overwrite\r
288       CALL :reformat_minus_RW\r
289       ECHO Done, now writing the part file.\r
290    )\r
291 )\r
292 if %1 NEQ 0 (\r
293    REM Ignore any existing iso9660 filesystem - used for truncate\r
294    if %1 EQU 2 (\r
295       SET cmd_opts= -use-the-force-luke^^^=tty -Z\r
296    ) ELSE (\r
297       SET cmd_opts= -Z\r
298    )\r
299 ) ELSE (\r
300    SET cmd_opts= -M\r
301 )\r
302 ECHO Running %growcmd% %growparams% %cmd_opts% %device% %2\r
303 %growcmd% %growparams% %cmd_opts% %device% %2\r
304 IF %ERRORLEVEL% NEQ 0 (\r
305    ECHO Exited with status !ERRORLEVEL!\r
306    EXIT !ERRORLEVEL!\r
307 )\r
308 GOTO :EOF\r
309 \r
310 :prepare\r
311 CALL :is_RW\r
312 IF %ERRORLEVEL% EQU 0 (\r
313    ECHO I won't prepare a non-rewritable medium\r
314    EXIT /b 1\r
315 )\r
316 \r
317 REM Blank DVD+RW when there is no data on it\r
318 CALL :is_plus_RW\r
319 SET result=%ERRORLEVEL%\r
320 CALL :is_blank\r
321 SET /a result=result + ERRORLEVEL\r
322 IF %result% EQU 2 (\r
323    ECHO DVD+RW looks brand-new, blank it to fix some DVD-writers bugs.\r
324    CALL :blank\r
325    GOTO :EOF\r
326 )\r
327 \r
328 CALL :is_minus_RW\r
329 IF %ERRORLEVEL% EQU 1 (\r
330    CALL :is_restricted_overwrite\r
331    IF !ERRORLEVEL! EQU 0 (\r
332       ECHO DVD-RW is in %diskmode% mode, reformating it to Restricted Overwrite\r
333       CALL :reformat_minus_RW\r
334       GOTO :EOF\r
335    )\r
336 )\r
337 CALL :blank\r
338 GOTO :EOF\r
339 \r
340 :blank\r
341 ECHO Running %growcmd% -Z %device% =/dev/zero\r
342 %growcmd% -Z %device% =/dev/zero\r
343 IF %ERRORLEVEL% NEQ 0 (\r
344    ECHO Exited with status !ERRORLEVEL!\r
345    EXIT !ERRORLEVEL!\r
346 )\r
347 GOTO :EOF\r
348 \r
349 :reformat_minus_RW\r
350 ECHO Running %dvdrwformat% -force %device%\r
351 %dvdrwformat% -force %device%\r
352 IF %ERRORLEVEL% NEQ 0 (\r
353    ECHO Exited with status !ERRORLEVEL!\r
354    EXIT !ERRORLEVEL!\r
355 )\r
356 GOTO :EOF\r
357 \r
358 REM  class disk ends here.\r
359 \r
360 :usage\r
361 ECHO Wrong number of arguments.\r
362 ECHO.\r
363 ECHO Usage:\r
364 ECHO.\r
365 ECHO dvd-handler DVD-DRIVE test\r
366 ECHO dvd-handler DVD-DRIVE free\r
367 ECHO dvd-handler DVD-DRIVE write APPEND FILE\r
368 ECHO dvd-handler DVD-DRIVE blank\r
369 ECHO.\r
370 ECHO where DVD-DRIVE is the drive letter of the DVD burner like D:\r
371 ECHO.\r
372 ECHO Operations:\r
373 ECHO test      Scan the device and report the information found.\r
374 ECHO           This operation needs no further arguments.\r
375 ECHO free      Scan the device and report the available space.\r
376 ECHO write     Write a part file to disk.\r
377 ECHO           This operation needs two additional arguments.\r
378 ECHO           The first indicates to append (0), restart the\r
379 ECHO           disk (1) or restart existing disk (2). The second\r
380 ECHO           is the file to write.\r
381 ECHO prepare   Prepare a DVD+/-RW for being used by Bacula.\r
382 ECHO           Note: This is only useful if you already have some\r
383 ECHO           non-Bacula data on a medium, and you want to use\r
384 ECHO           it with Bacula. Don't run this on blank media, it\r
385 ECHO           is useless.\r
386 \r
387 EXIT /b 1\r