1 from os import system, listdir, statvfs, popen, makedirs
3 from Tools.Directories import SCOPE_HDD, resolveFilename
4 from Tools.CList import CList
6 from SystemInfo import SystemInfo
10 procFile = open(filename)
16 return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
19 def __init__(self, index):
22 host = (self.index & 2) >> 1
24 target = (self.index & 1)
26 self.prochdx = num2prochdx(index)
27 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
36 ret = "External (CF) - "
46 procfile = tryOpen(self.prochdx + "capacity")
51 line = procfile.readline()
59 cap = cap / 1000 * 512 / 1000
61 return "%d.%03d GB" % (cap/1024, cap%1024)
64 procfile = tryOpen(self.prochdx + "model")
69 line = procfile.readline()
75 procfile = tryOpen("/proc/mounts")
82 line = procfile.readline()
85 if line.startswith(self.devidex):
86 parts = line.strip().split(" ")
88 stat = statvfs(parts[1])
91 free = stat.f_bfree/1000 * stat.f_bsize/1000
96 def numPartitions(self):
98 idedir = listdir(self.devidex)
102 for filename in idedir:
103 if filename.startswith("disc"):
105 if filename.startswith("part"):
110 cmd = "/bin/umount " + self.devidex + "part*"
114 def createPartition(self):
115 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
116 sfdisk = popen(cmd, "w")
117 sfdisk.write("0,\n;\n;\n;\ny\n")
122 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
127 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
131 def createMovieFolder(self):
133 makedirs(resolveFilename(SCOPE_HDD))
138 errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Unmount failed")]
140 def initialize(self):
143 if self.createPartition() != 0:
149 if self.mount() != 0:
152 #only create a movie folder on the internal hdd
153 if not self.index & 2 and self.createMovieFolder() != 0:
159 mediafile = tryOpen(num2prochdx(num) + "media")
164 line = mediafile.readline()
167 if line.startswith("disk"):
173 def __init__(self, mountpoint, description = "", force_mounted = False):
174 self.mountpoint = mountpoint
175 self.description = description
176 self.force_mounted = force_mounted
179 return statvfs(self.mountpoint)
184 return s.f_bavail * s.f_bsize
191 return s.f_blocks * s.f_bsize
196 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
197 # TODO: can os.path.ismount be used?
198 if self.force_mounted:
200 procfile = tryOpen("/proc/mounts")
201 for n in procfile.readlines():
202 if n.split(' ')[1] == self.mountpoint:
206 class HarddiskManager:
211 self.partitions = [ ]
213 self.on_partition_list_change = CList()
215 for hddNum in range(8):
217 hdd = Harddisk(hddNum)
220 SystemInfo["Harddisc"] = len(self.hdd) > 0
222 # currently, this is just an enumeration of what's possible,
223 # this probably has to be changed to support automount stuff.
224 # still, if stuff is mounted into the correct mountpoints by
225 # external tools, everything is fine (until somebody inserts
226 # a second usb stick.)
228 ("/media/hdd", _("Harddisk")),
229 ("/media/card", _("Card")),
230 ("/media/cf", _("Compact Flash")),
231 ("/media/mmc1", _("MMC Card")),
232 ("/media/net", _("Network Mount")),
233 ("/media/ram", _("Ram Disk")),
234 ("/media/usb", _("USB Stick")),
235 ("/", _("Internal Flash"))
239 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
241 def getAutofsMountpoint(self, device):
242 return "/autofs/%s/" % (device)
244 def addHotplugPartition(self, device, description):
245 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
246 self.partitions.append(p)
247 self.on_partition_list_change("add", p)
249 def removeHotplugPartition(self, device):
250 mountpoint = self.getAutofsMountpoint(device)
251 for x in self.partitions[:]:
252 if x.mountpoint == mountpoint:
253 self.partitions.remove(x)
254 self.on_partition_list_change("remove", x)
262 hdd = hd.model() + " ("
268 list.append((hdd, hd))
272 def getMountedPartitions(self):
273 return [x for x in self.partitions if x.mounted()]
275 harddiskmanager = HarddiskManager()