add free
[enigma2.git] / lib / python / Components / Harddisk.py
1 import os
2
3 def tryOpen(filename):
4         try:
5                 procFile = open(filename)
6         except IOError:
7                 return ""
8         return procFile
9
10 class Harddisk:
11         def __init__(self, index):
12                 self.index = index
13
14                 host = self.index / 4
15                 bus = (self.index & 2)
16                 target = (self.index & 1)
17
18                 #perhaps this is easier?
19                 self.prochdx = "/proc/ide/hd" + ("a","b","c","d","e","f","g","h")[index] + "/"
20                 self.devidex = "/dev/ide/host" + str(host) + "/bus" + str(bus) + "/target" + str(target) + "/lun0/"
21
22         def capacity(self):
23                 procfile = tryOpen(self.prochdx + "capacity")
24                 
25                 if procfile == "":
26                         return -1
27
28                 line = procfile.readline()
29                 procfile.close()
30                 
31                 try:
32                         cap = int(line)
33                 except:
34                         return -1
35                 
36                 return cap      
37                                                 
38         def model(self):
39                 procfile = tryOpen(self.prochdx + "model")
40                 
41                 if procfile == "":
42                         return ""
43
44                 line = procfile.readline()
45                 procfile.close()
46
47                 return line
48
49         def free(self):
50                 procfile = tryOpen("/proc/mounts")
51                 
52                 if procfile == "":
53                         return -1
54
55                 free = -1
56                 while 1:
57                         line = procfile.readline()
58                         if line == "":
59                                 break
60                         if line.startswith(self.devidex):
61                                 parts = line.strip().split(" ")
62                                 try:
63                                         stat = os.statvfs(parts[1])
64                                 except OSError:
65                                         continue
66                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
67                                 break
68                 procfile.close()
69                 return free