]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/dvd-freespace.in
- Add Date, Job, level to updates to .bsr file in
[bacula/bacula] / bacula / scripts / dvd-freespace.in
1 #!@PYTHON@
2 #
3 # Check the free space available on a writable DVD
4 # Should always exit with 0 status, otherwise it indicates a serious error.
5 # (wrong number of arguments, Python exception...)
6 #
7 #  called:  dvd-freespace <dvd-device-name> <part-number>
8 #
9 #
10 # returns:
11 # Prints on the first output line the free space available in bytes.
12 # If an error occurs, prints a negative number (-errno), followed,
13 # on the second line, by an error message.
14
15 # $Id$
16
17 # Configurable values:
18 mkisofs="@MKISOFS@"
19 growisofs="@GROWISOFS@"
20 dvdrwmediainfo="@DVDRWMEDIAINFO@"
21
22 margin=10485760 # 10 mb security margin
23
24 growisofsparams=""
25
26 # Uncomment the following line if you have a Linux kernel >=2.6.8, and
27 # if you want to allow a session to start behind the 4gb boundary.
28 #growisofsparams = growisofsparams + " -use-the-force-luke=4gms"
29
30 # end of configurable values
31
32 import popen2
33 import os
34 import errno
35 import tempfile
36 import sys
37
38 if len(sys.argv) != 3:
39    print "Wrong number of argument."
40    sys.exit(1)
41
42 device=sys.argv[1]
43 part_num=int(sys.argv[2])
44
45 os.environ["MKISOFS"] = mkisofs
46
47 def gettotalsize():
48    cmd=dvdrwmediainfo + " " + device
49    processi = popen2.Popen4(cmd)
50    status = processi.wait()
51    if not os.WIFEXITED(status):
52       print -errno.EPIPE
53       print dvdrwmediainfo + " process did not exit correctly."
54       sys.exit(0)
55    if os.WEXITSTATUS(status) != 0:
56       print -errno.EPIPE
57       print 'Cannot get media info.'
58       sys.exit(0)
59    while 1:
60       result = processi.fromchild.readline()
61       if result.find("Legacy lead-out at:") > -1:
62          index = result.find("=")
63          if index > -1:
64             res = result[index+1:-1]
65             if not res.isdigit():
66                print -errno.EPIPE
67                print "Invalid format in media info lead-out line from " + dvdrwmediainfo
68                sys.exit(0)
69             return int(res)
70          else:
71             print -errno.EPIPE
72             print "Invalid format in media info lead-out line from " + dvdrwmediainfo
73             sys.exit(0)
74       if not result:
75          print -errno.EPIPE
76          print "Cannot get media lead-out index from " + dvdrwmediainfo
77          sys.exit(0)
78
79 tmpfile = tempfile.NamedTemporaryFile()
80 if part_num == 0:
81    flag = "-Z"
82 else:
83    flag = "-M"
84 cmd=growisofs + " " + growisofsparams + " -use-the-force-luke=tty -dry-run -quiet " + flag + " " + device + " -R " + tmpfile.name
85 process = popen2.Popen4(cmd)
86 status = process.wait()
87 if not os.WIFEXITED(status):
88    print -errno.EPIPE
89    print growisofs + " process did not not exit correctly."
90    sys.exit(0)
91
92 exitstat = os.WEXITSTATUS(status) & ~0x80
93 if exitstat == errno.ENOSPC:
94    print "0"
95    print os.strerror(exitstat)
96    sys.exit(0)
97 if exitstat != 0:
98    print -exitstat
99    print os.strerror(exitstat)
100    sys.exit(0)
101 while 1:
102    result = process.fromchild.readline()
103    if not result:
104       print -errno.EPIPE
105       print "Cannot find the seek argument in the output from " + growisofs
106       sys.exit(0)
107    index = result.find("seek=")
108    if index > -1:
109       res = result[index+5:-2]
110       if not res.isdigit():
111          print -errno.EPIPE
112          print "Wrong seek argument in the output from " + growisofs
113          sys.exit(0)
114       size = int(res)*32*1024
115       size = gettotalsize()-(size+margin)
116       if size < 0:
117          size = 0
118       print size
119       print "No error occured"
120       sys.exit(0)