Exemple #1
0
 def read_dir(self,cluster=0,no_dotdirs=False):
     dirs, files = [], []
     data = cStringIO.StringIO(self.get_clusterdata(cluster,throw=True))
     de = DirEntry(data=data)
     counter = 0
     while not de.is_empty():
         if not de.is_erased():
             if de.has_attr(DirEntry.ATTR_VOLLABEL): pass
             elif de.has_attr(DirEntry.ATTR_DIR):    dirs.append(de)
             else:                                   files.append(de)
         counter += 1
         if counter*32 == self.bs.cluster_size():
             cluster = self.fat.next_cluster(cluster)
             if cluster == Fat12.TERM: break
             counter = 0
             data = cStringIO.StringIO(
                 self.get_clusterdata(cluster,throw=True))
         try:    de = DirEntry(data=data)
         except: break
     if no_dotdirs: dirs = [ d for d in dirs if not d.is_dotdir() ]
     return (dirs,files)
Exemple #2
0
    def read(self,f,compatibilityMode=False,maxsize=-1):
        self.reset()

        if compatibilityMode:
            warn_defaults = [
                'bytespersector', 'nfats', 'nrootentries', 'nsectors',
                'sectorsperfat', 'sectorspertrack', 'nsectorsext',
            ]
        else:
            warn_defaults = []
            
        # read boot sector
        self.bs.from_raw(f,warn_defaults=warn_defaults)
        if maxsize > 0 and maxsize != self.bs.disk_size():
            s = "max size (%d bytes) not matching" \
                " size from boot sector (%d bytes)" % \
                (maxsize,self.bs.disk_size())
            if compatibilityMode:
                print("WARNING in %s:\n %s" % (filename,s))
            else:
                raise S3Exception(s)

        # read FAT
        f.seek(self.bs.fat_offset())
        self.fat.from_raw(f.read(self.bs.fat_size()))

        # read root directory
        f.seek(self.bs.root_offset())
        rootdir = ''
        for i in range(self.bs.nrootentries):
            buf = f.read(DirEntry.SIZE)
            if buf[0] == DirEntry.TYPE_EMPTY: break
            rootdir += buf
        if not len(rootdir):
            raise S3Exception(
                "error reading root directory")
        vl = DirEntry(data=rootdir[:DirEntry.SIZE])
        if not vl.has_attr(DirEntry.ATTR_VOLLABEL):
            raise S3Exception(
                "volume information is not first entry in root directory")
        self.clusterdata[0] = rootdir
        
        # read rest of disk
        for cluster in range(2,self.ndata_clusters()+2):
            if self.fat.get(cluster) != 0:
                f.seek(self.cluster_to_offset(cluster))
                self.clusterdata[cluster] = f.read(self.bs.cluster_size())