]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/dvd-freespace.in
- DVD writing/reading seems to be mostly working.
[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 sys
36
37 if len(sys.argv) != 3:
38    print "Wrong number of argument."
39    sys.exit(1)
40
41 device=sys.argv[1]
42 part_num=int(sys.argv[2])
43
44 os.environ["MKISOFS"] = mkisofs
45
46 def gettotalsize():
47    cmd=dvdrwmediainfo + " " + device
48    processi = popen2.Popen4(cmd)
49    status = processi.wait()
50    if not os.WIFEXITED(status):
51       print -errno.EPIPE
52       print dvdrwmediainfo + " process did not exit correctly."
53       sys.exit(0)
54    if os.WEXITSTATUS(status) != 0:
55       print -errno.EPIPE
56       print "Cannot get media info from " + dvdrwmediainfo
57       sys.exit(0)
58    while 1:
59       result = processi.fromchild.readline()
60       if result.find("Track Size:") > -1:
61          index = result.find("*2KB")
62          if index > 0: 
63             return long(result[12:index]) * 2048
64          else:
65             print -errno.EPIPE
66             print "Invalid format in media info lead-out line from " + dvdrwmediainfo
67             sys.exit(0)
68       if result.find("Legacy lead-out at:") > -1:
69          index = result.find("=")
70          if index > -1:
71             res = result[index+1:-1]
72             if not res.isdigit():
73                print -errno.EPIPE
74                print "Invalid format in media info lead-out line from " + dvdrwmediainfo
75                sys.exit(0)
76             return long(res)
77          else:
78             print -errno.EPIPE
79             print "Invalid format in media info lead-out line from " + dvdrwmediainfo
80             sys.exit(0)
81       if not result:
82          print -errno.EPIPE
83          print "Cannot get media lead-out index from " + dvdrwmediainfo
84          sys.exit(0)
85
86 if part_num == 0:
87    flag = "-Z"
88 else:
89    flag = "-M"
90 # the growisofs at the end is a dummy
91 cmd=growisofs + " " + growisofsparams + " -use-the-force-luke=tty -dry-run -quiet " + flag + " " + device + " -R " + growisofs
92 process = popen2.Popen4(cmd)
93 status = process.wait()
94 if not os.WIFEXITED(status):
95    print -errno.EPIPE
96    print growisofs + " process did not not exit correctly."
97    sys.exit(0)
98
99 exitstat = os.WEXITSTATUS(status) & ~0x80
100 if exitstat == errno.ENOSPC:
101    print "0"
102    print os.strerror(exitstat)
103    sys.exit(0)
104 if exitstat != 0:
105    print -exitstat
106    print os.strerror(exitstat)
107    sys.exit(0)
108 while 1:
109    result = process.fromchild.readline()
110    if not result:
111       print -errno.EPIPE
112       print "Cannot find the seek argument in the output from " + growisofs
113       sys.exit(0)
114    index = result.find("seek=")
115    if index > -1:
116       res = result[index+5:-2]
117       if not res.isdigit():
118          print -errno.EPIPE
119          print "Wrong seek argument in the output from " + growisofs
120          sys.exit(0)
121       size = long(res)*32*1024
122       size = gettotalsize()-(size+margin)
123       if size < 0:
124          size = 0
125       print size
126       print "No error occurred"
127       sys.exit(0)