missing commit
[enigma2.git] / lib / python / Components / Harddisk.py
1 from os import system, listdir, statvfs, popen
2
3 from Tools.Directories import SCOPE_HDD, resolveFilename
4
5 def tryOpen(filename):
6         try:
7                 procFile = open(filename)
8         except IOError:
9                 return ""
10         return procFile
11
12 def num2prochdx(num):
13         return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
14
15 class Harddisk:
16         def __init__(self, index):
17                 self.index = index
18
19                 host = self.index / 4
20                 bus = (self.index & 2)
21                 target = (self.index & 1)
22
23                 self.prochdx = num2prochdx(index)
24                 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
25
26         def getIndex(self):
27                 return self.index
28
29         def bus(self):
30                 ret = ""
31
32                 if self.index & 2:
33                         ret = "External (CF) - "
34                 else:
35                         ret = "Internal - "
36                 
37                 if self.index & 1:
38                         return ret + "Slave"
39                 else:
40                         return ret + "Master"
41
42         def capacity(self):
43                 procfile = tryOpen(self.prochdx + "capacity")
44                 
45                 if procfile == "":
46                         return ""
47
48                 line = procfile.readline()
49                 procfile.close()
50                 
51                 try:
52                         cap = int(line)
53                 except:
54                         return ""
55                 
56                 cap = cap / 1000 * 512 / 1000
57                 
58                 return "%d.%03d GB" % (cap/1024, cap%1024)
59                                                                 
60         def model(self):
61                 procfile = tryOpen(self.prochdx + "model")
62
63                 if procfile == "":
64                         return ""
65
66                 line = procfile.readline()
67                 procfile.close()
68
69                 return line.strip()
70
71         def free(self):
72                 procfile = tryOpen("/proc/mounts")
73                 
74                 if procfile == "":
75                         return -1
76
77                 free = -1
78                 while 1:
79                         line = procfile.readline()
80                         if line == "":
81                                 break
82                         if line.startswith(self.devidex):
83                                 parts = line.strip().split(" ")
84                                 try:
85                                         stat = statvfs(parts[1])
86                                 except OSError:
87                                         continue
88                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
89                                 break
90                 procfile.close()
91                 return free             
92
93         def numPartitions(self):
94                 try:
95                         idedir = listdir(self.devidex)
96                 except OSError:
97                         return -1
98                 numPart = -1
99                 for filename in idedir:
100                         if filename.startswith("disc"):
101                                 numPart += 1
102                         if filename.startswith("part"):
103                                 numPart += 1
104                 return numPart
105
106         def unmount(self):
107                 cmd = "/bin/umount " + self.devidex + "part*"
108                 res = system(cmd)
109                 return (res >> 8)
110
111         def createPartition(self):
112                 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
113                 sfdisk = popen(cmd, "w")
114                 sfdisk.write("0,\n;\n;\n;\ny\n")
115                 sfdisk.close()
116                 return 0
117
118         def mkfs(self):
119                 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
120                 res = system(cmd)
121                 return (res >> 8)
122
123         def mount(self):
124                 cmd = "/bin/mount -t ext3 " + self.devidex + "part1 /hdd"
125                 res = system(cmd)
126                 return (res >> 8)
127
128         def createMovieFolder(self):
129                 res = system("mkdir " + resolveFilename(SCOPE_HDD))
130                 return (res >> 8)
131                 
132         errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Unmount failed")]
133
134         def initialize(self):
135                 self.unmount()
136
137                 if self.createPartition() != 0:
138                         return -1
139
140                 if self.mkfs() != 0:
141                         return -2
142
143                 if self.mount() != 0:
144                         return -3
145
146                 if self.createMovieFolder() != 0:
147                         return -4
148                 
149                 return 0
150                 
151 def existHDD(num):
152         mediafile = tryOpen(num2prochdx(num) + "media")
153
154         if mediafile == "":
155                 return -1
156
157         line = mediafile.readline()
158         mediafile.close()
159         
160         if line.startswith("disk"):
161                 return 1
162         
163         return -1
164
165 class Partition:
166         def __init__(self, mountpoint, description = ""):
167                 self.mountpoint = mountpoint
168                 self.description = description
169
170         def stat(self):
171                 return statvfs(self.mountpoint)
172
173         def free(self):
174                 try:
175                         s = self.stat()
176                         return s.f_bavail * s.f_bsize
177                 except OSError:
178                         return None
179         
180         def total(self):
181                 try:
182                         s = self.stat()
183                         return s.f_blocks * s.f_bsize
184                 except OSError:
185                         return None
186
187         def mounted(self):
188                 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
189                 # TODO: can os.path.ismount be used?
190                 procfile = tryOpen("/proc/mounts")
191                 for n in procfile.readlines():
192                         if n.split(' ')[1] == self.mountpoint:
193                                 return True
194                 return False
195
196 class HarddiskManager:
197         def __init__(self):
198                 hddNum = 0
199                 self.hdd = [ ]
200                 
201                 self.partitions = [ ]
202                 
203                 for hddNum in range(8):
204                         if existHDD(hddNum):
205                                 hdd = Harddisk(hddNum)
206                                 self.hdd.append(hdd)
207
208                 # currently, this is just an enumeration of what's possible,
209                 # this probably has to be changed to support automount stuff.
210                 # still, if stuff is mounted into the correct mountpoints by
211                 # external tools, everything is fine (until somebody inserts 
212                 # a second usb stick.)
213                 p = [
214                                         ("/media/hdd", _("Harddisk")), 
215                                         ("/media/card", _("Card")), 
216                                         ("/media/cf", _("Compact Flash")),
217                                         ("/media/mmc1", _("MMC Card")),
218                                         ("/media/net", _("Network Mount")),
219                                         ("/media/ram", _("Ram Disk")),
220                                         ("/media/usb", _("USB Stick")),
221                                         ("/", _("Internal Flash"))
222                                 ]
223                 
224                 for x in p:
225                         self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
226
227         def HDDCount(self):
228                 return len(self.hdd)
229
230         def HDDList(self):
231                 list = [ ]
232                 for hd in self.hdd:
233                         hdd = hd.model() + " (" 
234                         hdd += hd.bus()
235                         cap = hd.capacity()     
236                         if cap != "":
237                                 hdd += ", " + cap
238                         hdd += ")"
239                         list.append((hdd, hd))
240
241                 return list
242
243         def getMountedPartitions(self):
244                 return [x for x in self.partitions if x.mounted()]
245
246 harddiskmanager = HarddiskManager()