1 from os import system, listdir, statvfs, popen, makedirs
3 from Tools.Directories import SCOPE_HDD, resolveFilename
4 from Tools.CList import CList
8 procFile = open(filename)
14 return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
17 def __init__(self, index):
20 host = (self.index & 2) >> 1
22 target = (self.index & 1)
24 self.prochdx = num2prochdx(index)
25 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
34 ret = "External (CF) - "
44 procfile = tryOpen(self.prochdx + "capacity")
49 line = procfile.readline()
57 cap = cap / 1000 * 512 / 1000
59 return "%d.%03d GB" % (cap/1024, cap%1024)
62 procfile = tryOpen(self.prochdx + "model")
67 line = procfile.readline()
73 procfile = tryOpen("/proc/mounts")
80 line = procfile.readline()
83 if line.startswith(self.devidex):
84 parts = line.strip().split(" ")
86 stat = statvfs(parts[1])
89 free = stat.f_bfree/1000 * stat.f_bsize/1000
94 def numPartitions(self):
96 idedir = listdir(self.devidex)
100 for filename in idedir:
101 if filename.startswith("disc"):
103 if filename.startswith("part"):
108 cmd = "/bin/umount " + self.devidex + "part*"
112 def createPartition(self):
113 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
114 sfdisk = popen(cmd, "w")
115 sfdisk.write("0,\n;\n;\n;\ny\n")
120 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
125 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
129 def createMovieFolder(self):
131 makedirs(resolveFilename(SCOPE_HDD))
136 errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Unmount failed")]
138 def initialize(self):
141 if self.createPartition() != 0:
147 if self.mount() != 0:
150 #only create a movie folder on the internal hdd
151 if not self.index & 2 and self.createMovieFolder() != 0:
157 mediafile = tryOpen(num2prochdx(num) + "media")
162 line = mediafile.readline()
165 if line.startswith("disk"):
171 def __init__(self, mountpoint, description = "", force_mounted = False):
172 self.mountpoint = mountpoint
173 self.description = description
174 self.force_mounted = force_mounted
177 return statvfs(self.mountpoint)
182 return s.f_bavail * s.f_bsize
189 return s.f_blocks * s.f_bsize
194 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
195 # TODO: can os.path.ismount be used?
196 if self.force_mounted:
198 procfile = tryOpen("/proc/mounts")
199 for n in procfile.readlines():
200 if n.split(' ')[1] == self.mountpoint:
204 class HarddiskManager:
209 self.partitions = [ ]
211 self.on_partition_list_change = CList()
213 for hddNum in range(8):
215 hdd = Harddisk(hddNum)
218 # currently, this is just an enumeration of what's possible,
219 # this probably has to be changed to support automount stuff.
220 # still, if stuff is mounted into the correct mountpoints by
221 # external tools, everything is fine (until somebody inserts
222 # a second usb stick.)
224 ("/media/hdd", _("Harddisk")),
225 ("/media/card", _("Card")),
226 ("/media/cf", _("Compact Flash")),
227 ("/media/mmc1", _("MMC Card")),
228 ("/media/net", _("Network Mount")),
229 ("/media/ram", _("Ram Disk")),
230 ("/media/usb", _("USB Stick")),
231 ("/", _("Internal Flash"))
235 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
237 def getAutofsMountpoint(self, device):
238 return "/autofs/%s/" % (device)
240 def addHotplugPartition(self, device, description):
241 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
242 self.partitions.append(p)
243 self.on_partition_list_change("add", p)
245 def removeHotplugPartition(self, device):
246 mountpoint = self.getAutofsMountpoint(device)
247 for x in self.partitions[:]:
248 if x.mountpoint == mountpoint:
249 self.partitions.remove(x)
250 self.on_partition_list_change("remove", x)
258 hdd = hd.model() + " ("
264 list.append((hdd, hd))
268 def getMountedPartitions(self):
269 return [x for x in self.partitions if x.mounted()]
271 harddiskmanager = HarddiskManager()