def open(self, img_file, read_only=False, options=None, fobj=None): """open an existing image file""" # file base check if fobj is None: # make sure image file exists if not os.path.exists(img_file): raise IOError("image file not found") # is readable? if not os.access(img_file, os.R_OK): raise IOError("can't read from image file") # is writeable? -> no: enforce read_only if not os.access(img_file, os.W_OK): read_only = True # check size st = os.stat(img_file) mode = st.st_mode if stat.S_ISBLK(mode) or stat.S_ISCHR(mode): size = BlkDevTools.getblkdevsize(img_file) else: size = os.path.getsize(img_file) if size == 0: raise IOError("image file is empty") # fobj else: fobj.seek(0,2) size = fobj.tell() fobj.seek(0,0) # detect type t = self.detect_type(img_file, fobj, options) if t == None: raise IOError("can't detect type of image file") # create blkdev if t == self.TYPE_ADF: blkdev = ADFBlockDevice(img_file, read_only, fobj=fobj) blkdev.open() elif t == self.TYPE_HDF: # detect geometry geo = DiskGeometry() if not geo.detect(size, options): raise IOError("can't detect geometry of HDF image file") blkdev = HDFBlockDevice(img_file, read_only, fobj=fobj) blkdev.open(geo) else: rawdev = RawBlockDevice(img_file, read_only, fobj=fobj) rawdev.open() # create rdisk instance rdisk = RDisk(rawdev) if not rdisk.open(): raise IOError("can't open rdisk of image file") # determine partition p = "0" if options != None and options.has_key('part'): p = str(options['part']) part = rdisk.find_partition_by_string(p) if part == None: raise IOError("can't find partition in image file") blkdev = part.create_blkdev(True) # auto_close rdisk blkdev.open() return blkdev
def open(self, img_file, read_only=False, options=None, fobj=None): """open an existing image file""" # file base check if fobj is None: # make sure image file exists if not os.path.exists(img_file): raise IOError("image file not found") # is readable? if not os.access(img_file, os.R_OK): raise IOError("can't read from image file") # is writeable? -> no: enforce read_only if not os.access(img_file, os.W_OK): read_only = True # check size st = os.stat(img_file) mode = st.st_mode if stat.S_ISBLK(mode) or stat.S_ISCHR(mode): size = BlkDevTools.getblkdevsize(img_file) else: size = os.path.getsize(img_file) if size == 0: raise IOError("image file is empty") # fobj else: fobj.seek(0, 2) size = fobj.tell() fobj.seek(0, 0) # detect type t = self.detect_type(img_file, fobj, options) if t == None: raise IOError("can't detect type of image file") # create blkdev if t == self.TYPE_ADF: blkdev = ADFBlockDevice(img_file, read_only, fobj=fobj) blkdev.open() elif t == self.TYPE_HDF: # detect geometry geo = DiskGeometry() if not geo.detect(size, options): raise IOError("can't detect geometry of HDF image file") blkdev = HDFBlockDevice(img_file, read_only, fobj=fobj) blkdev.open(geo) else: rawdev = RawBlockDevice(img_file, read_only, fobj=fobj) rawdev.open() # create rdisk instance rdisk = RDisk(rawdev) if not rdisk.open(): raise IOError("can't open rdisk of image file") # determine partition p = "0" if options != None and options.has_key('part'): p = str(options['part']) part = rdisk.find_partition_by_string(p) if part == None: raise IOError("can't find partition in image file") blkdev = part.create_blkdev(True) # auto_close rdisk blkdev.open() return blkdev
def open(self, img_file, read_only=False, options=None, fobj=None, none_if_missing=False): """open an existing image file""" # file base check if not fobj: # make sure image file exists if not os.path.exists(img_file): if none_if_missing: return None raise IOError("image file not found") # is readable? if not os.access(img_file, os.R_OK): raise IOError("can't read from image file") # is writeable? -> no: enforce read_only if not os.access(img_file, os.W_OK): read_only = True # detect type t = self.detect_type(img_file, fobj, options) if t is None: raise IOError("can't detect type of image file") # is gzipped? if t & self.GZIP_MASK: # only supported for read access for now if not read_only: raise IOError("can't write gzip'ed image files!") # automatically wrap a fobj to unzip fobj = gzip.GzipFile(img_file, "rb", fileobj=fobj) # remove gzip flag from type t = t & self.TYPE_MASK # retrieve size if fobj: # get size from fobj fobj.seek(0, 2) size = fobj.tell() fobj.seek(0, 0) else: # get size from file/blk dev/char dev st = os.stat(img_file) mode = st.st_mode if stat.S_ISBLK(mode) or stat.S_ISCHR(mode): size = BlkDevTools.getblkdevsize(img_file) else: size = os.path.getsize(img_file) if size == 0: raise IOError("image file is empty") # get block size bs = self._get_block_size(options) # now create blkdev if t in (self.TYPE_ADF, self.TYPE_ADF_HD): # check sizes if size in ADFBlockDevice.DD_IMG_SIZES: hd = False elif size in ADFBlockDevice.HD_IMG_SIZES: hd = True else: raise IOError("invalid ADF images size: %d" % size) blkdev = ADFBlockDevice(img_file, read_only, fobj=fobj, hd=hd) blkdev.open() elif t == self.TYPE_HDF: # detect geometry geo = DiskGeometry(block_bytes=bs) if not geo.detect(size, options): raise IOError("can't detect geometry of HDF image file") blkdev = HDFBlockDevice(img_file, read_only, fobj=fobj, block_size=bs) blkdev.open(geo) else: rawdev = RawBlockDevice(img_file, read_only, fobj=fobj, block_bytes=bs) rawdev.open() # check block size stored in rdb rdisk = RDisk(rawdev) rdb_bs = rdisk.peek_block_size() if rdb_bs != bs: # adjust block size and re-open rawdev.close() bs = rdb_bs rawdev = RawBlockDevice(img_file, read_only, fobj=fobj, block_bytes=bs) rawdev.open() rdisk = RDisk(rawdev) if not rdisk.open(): raise IOError("can't open rdisk of image file") # determine partition p = "0" if options != None and "part" in options: p = str(options["part"]) part = rdisk.find_partition_by_string(p) if part == None: raise IOError("can't find partition in image file") blkdev = part.create_blkdev(True) # auto_close rdisk blkdev.open() return blkdev