Esempio n. 1
0
 def get_volattr(self,attr):
     cd = self.get_clusterdata(0)
     vl = DirEntry(data=cd[:DirEntry.SIZE])
     if attr == 'name':
         return vl.name().strip()
     else:
         return vl.__getattribute__(attr)
Esempio n. 2
0
 def copy_real(self,name,cluster,data,**kwargs):
     nclusters = int(len(data)/self.bs.cluster_size()+1)
     # actually make file
     chain = self.fat.create_chain(nclusters)
     kwargs['start'] = chain[0]
     kwargs['size']  = len(data)
     kwargs.setdefault('mdate',str2date(time.strftime("%d/%m/%Y")))
     kwargs.setdefault('mtime',str2time(time.strftime("%H:%M:%S")))
     de = DirEntry(**kwargs)
     de.encode_name(name)
     self.add_direntry(cluster,de)
     for i,c in enumerate(chain):
         self.clusterdata[c] = data[i*self.bs.cluster_size():
                                    (i+1)*self.bs.cluster_size()]
Esempio n. 3
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())
Esempio n. 4
0
 def set_volattr(self,attr,value):
     cd = self.get_clusterdata(0)
     vl = DirEntry(data=cd[:DirEntry.SIZE])
     if attr == 'name':
         vl.set_name(value)
     else:
         vl.__setattr__(attr,value)
     self.clusterdata[0] = vl.to_raw() + cd[DirEntry.SIZE:]
Esempio n. 5
0
    def get_clusterdata(self,index,throw=False):
        if index == 0:
            return self.clusterdata.get(
                0, DirEntry(shortname='\x20'*8, shortext='\x20'*3,
                            attr=DirEntry.ATTR_VOLLABEL).to_raw())
        elif index == 1:
            raise S3Exception("illegal cluster index")
        else:
            if not throw:
                return self.clusterdata.get(
                    index,'\xcb'*self.bs.cluster_size())

        return self.clusterdata[index]
Esempio n. 6
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)
Esempio n. 7
0
 def mkdir_real(self,name,cluster,**kwargs):
     # actually make directory
     newstart = self.fat.create_chain(1)[0]
     kwargs['attr']  = DirEntry.ATTR_DIR | kwargs.get('attr',0)
     kwargs['start'] = newstart
     kwargs.setdefault('mdate',str2date(time.strftime("%d/%m/%Y")))
     kwargs.setdefault('mtime',str2time(time.strftime("%H:%M:%S")))
     de = DirEntry(**kwargs)
     de.encode_name(name)
     self.add_direntry(cluster,de)
     de.set_name('.')
     self.clusterdata[newstart] = de.to_raw()
     de.set_name('..')
     de.start = cluster
     self.clusterdata[newstart] += de.to_raw()