add free
[enigma2.git] / lib / python / Components / Harddisk.py
index a09073b..708bb62 100644 (file)
@@ -1,4 +1,4 @@
-
+import os
 
 def tryOpen(filename):
        try:
@@ -10,9 +10,15 @@ def tryOpen(filename):
 class Harddisk:
        def __init__(self, index):
                self.index = index
+
+               host = self.index / 4
+               bus = (self.index & 2)
+               target = (self.index & 1)
+
                #perhaps this is easier?
                self.prochdx = "/proc/ide/hd" + ("a","b","c","d","e","f","g","h")[index] + "/"
-               
+               self.devidex = "/dev/ide/host" + str(host) + "/bus" + str(bus) + "/target" + str(target) + "/lun0/"
+
        def capacity(self):
                procfile = tryOpen(self.prochdx + "capacity")
                
@@ -22,10 +28,12 @@ class Harddisk:
                line = procfile.readline()
                procfile.close()
                
-               if line == "":
+               try:
+                       cap = int(line)
+               except:
                        return -1
-
-               return int(line)
+               
+               return cap      
                                                
        def model(self):
                procfile = tryOpen(self.prochdx + "model")
@@ -39,6 +47,23 @@ class Harddisk:
                return line
 
        def free(self):
-               pass
+               procfile = tryOpen("/proc/mounts")
                
-       
\ No newline at end of file
+               if procfile == "":
+                       return -1
+
+               free = -1
+               while 1:
+                       line = procfile.readline()
+                       if line == "":
+                               break
+                       if line.startswith(self.devidex):
+                               parts = line.strip().split(" ")
+                               try:
+                                       stat = os.statvfs(parts[1])
+                               except OSError:
+                                       continue
+                               free = stat.f_bfree/1000 * stat.f_bsize/1000
+                               break
+               procfile.close()
+               return free