]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/dvd-freespace.in
- Add check for df path for dvd_freespace
[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>
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 df="@DF@"
19 dvdrwmediainfo="@DVDRWMEDIAINFO@"
20
21 margin=10485760 # 10 mb security margin
22
23
24 # end of configurable values
25
26 import popen2
27 import os
28 import errno
29 import sys
30
31 if len(sys.argv) < 2:
32    print "Wrong number of arguments."
33    sys.exit(1)
34
35 device=sys.argv[1]
36
37 def gettotalsize():
38    cmd=dvdrwmediainfo + " " + device
39    processi = popen2.Popen4(cmd)
40    status = processi.wait()
41    if not os.WIFEXITED(status):
42       print -errno.EPIPE
43       print dvdrwmediainfo + " process did not exit correctly."
44       sys.exit(0)
45    if os.WEXITSTATUS(status) != 0:
46       print -errno.EPIPE
47       print "Cannot get media info from " + dvdrwmediainfo
48       sys.exit(0)
49    while 1:
50       result = processi.fromchild.readline()
51       if result.find("Track Size:") > -1:
52          index = result.find("*2KB")
53          if index > 0: 
54             return long(result[12:index]) * 2048
55          else:
56             print -errno.EPIPE
57             print "Invalid format in media info lead-out line from " + dvdrwmediainfo
58             sys.exit(0)
59       if result.find("Legacy lead-out at:") > -1:
60          index = result.find("=")
61          if index > -1:
62             res = result[index+1:-1]
63             if not res.isdigit():
64                print -errno.EPIPE
65                print "Invalid format in media info lead-out line from " + dvdrwmediainfo
66                sys.exit(0)
67             return long(res)
68          else:
69             print -errno.EPIPE
70             print "Invalid format in media info lead-out line from " + dvdrwmediainfo
71             sys.exit(0)
72       if not result:
73          print -errno.EPIPE
74          print "Cannot get media lead-out index from " + dvdrwmediainfo
75          sys.exit(0)
76
77 cmd=df + " " + device
78 process = popen2.Popen4(cmd)
79 status = process.wait()
80 if not os.WIFEXITED(status):
81    print -errno.EPIPE
82    print df + " process did not not exit correctly."
83    sys.exit(0)
84
85 exitstat = os.WEXITSTATUS(status) & ~0x80
86 if exitstat == errno.ENOSPC:
87    print "0"
88    print os.strerror(exitstat)
89    sys.exit(0)
90 if exitstat != 0:
91    print -exitstat
92    print os.strerror(exitstat)
93    sys.exit(0)
94 size = 0
95 while 1:
96    result = process.fromchild.readline()
97    if not result:
98       print -errno.EPIPE
99       print "Cannot get output from " + df
100       sys.exit(0)
101    index = result.find(device)
102    if index > -1: 
103       while result[index] != ' ':
104          index += 1
105       while result[index] == ' ':
106          index += 1
107       eindex = index;
108       while result[eindex] != ' ':
109          eindex += 1
110       res = result[index:eindex] 
111       if not res.isdigit():
112          print -errno.EPIPE;
113          print "Could not find size in df output"
114          sys.exit(0)
115       size = long(res)*1024
116       break
117
118 size = gettotalsize()-(size+margin)
119 if size < 0:
120    size = 0
121 print size
122 print "No error occurred"
123 sys.exit(0)