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