예제 #1
0
 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
예제 #2
0
 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
예제 #3
0
파일: rdbtool.py 프로젝트: jukeks/amitools
 def _open_rdisk(self):
     if self.rdisk == None:
         self.rdisk = RDisk(self.blkdev)
         if self.args.verbose:
             print("opening rdisk:", self.img)
         return self.rdisk.open()
     else:
         return True
예제 #4
0
 def init_rdisk(self, blkdev):
   opts = KeyValue.parse_key_value_strings(self.opts)
   # number of cylinders for RDB
   if opts.has_key('rdb_cyls'):
     rdb_cyls = int(opts['rdb_cyls'])
   else:
     rdb_cyls = 1
   rdisk = RDisk(blkdev)
   rdisk.create(blkdev.geo, rdb_cyls=rdb_cyls)
   return rdisk
예제 #5
0
파일: rdbtool.py 프로젝트: jukeks/amitools
 def init_rdisk(self, blkdev):
     opts = KeyValue.parse_key_value_strings(self.opts)
     # number of cylinders for RDB
     if 'rdb_cyls' in opts:
         rdb_cyls = int(opts['rdb_cyls'])
     else:
         rdb_cyls = 1
     rdisk = RDisk(blkdev)
     rdisk.create(blkdev.geo, rdb_cyls=rdb_cyls)
     return rdisk
예제 #6
0
 def _open_rdisk(self):
   if self.rdisk == None:
     self.rdisk = RDisk(self.blkdev)
     if self.args.verbose:
       print("opening rdisk:", self.img)
     return self.rdisk.open()
   else:
     return True
예제 #7
0
파일: rdbtool.py 프로젝트: jukeks/amitools
 def init_blkdev(self, file_name):
     # make sure image file exists
     if not os.path.exists(file_name):
         raise IOError("Image File not found: '%s'" % file_name)
     # parse opts
     opts = KeyValue.parse_key_value_strings(self.opts)
     # is a block size given in options? if yes then enforce it
     bs = 512
     opts_bs = self._get_opts_block_size(opts)
     if opts_bs:
         bs = opts_bs
     # setup initial raw block dev with default block size
     blkdev = RawBlockDevice(file_name, self.args.read_only, block_bytes=bs)
     blkdev.open()
     # if no bs was given in options then try to find out block size
     # from an existing rdb
     if not opts_bs:
         rd = RDisk(blkdev)
         peek_bs = rd.peek_block_size()
         # real block size differs: re-open dev with correct size
         if peek_bs and peek_bs != blkdev.block_bytes:
             blkdev.close()
             blkdev = RawBlockDevice(file_name,
                                     self.args.read_only,
                                     block_bytes=peek_bs)
             blkdev.open()
             bs = peek_bs
     # try to guess geometry
     file_size = blkdev.num_blocks * blkdev.block_bytes
     geo = DiskGeometry(block_bytes=bs)
     if not geo.detect(file_size, opts):
         raise IOError("Can't detect geometry of disk: '%s'" % file_name)
     # make sure block size is still the same
     if geo.block_bytes != bs:
         raise IOError("Invalid geo block size chosen: %d" %
                       geo.block_bytes)
     # keep geo
     blkdev.geo = geo
     return blkdev
예제 #8
0
 def init_blkdev(self, file_name):
   # make sure image file exists
   if not os.path.exists(file_name):
     raise IOError("Image File not found: '%s'" % file_name)
   # parse opts
   opts = KeyValue.parse_key_value_strings(self.opts)
   # is a block size given in options? if yes then enforce it
   bs = 512
   opts_bs = self._get_opts_block_size(opts)
   if opts_bs:
     bs = opts_bs
   # setup initial raw block dev with default block size
   blkdev = RawBlockDevice(file_name, self.args.read_only, block_bytes=bs)
   blkdev.open()
   # if no bs was given in options then try to find out block size
   # from an existing rdb
   if not opts_bs:
     rd = RDisk(blkdev)
     peek_bs = rd.peek_block_size()
     # real block size differs: re-open dev with correct size
     if peek_bs and peek_bs != blkdev.block_bytes:
       blkdev.close()
       blkdev = RawBlockDevice(file_name, self.args.read_only,
                               block_bytes=peek_bs)
       blkdev.open()
       bs = peek_bs
   # try to guess geometry
   file_size = blkdev.num_blocks * blkdev.block_bytes
   geo = DiskGeometry(block_bytes=bs)
   if not geo.detect(file_size, opts):
     raise IOError("Can't detect geometry of disk: '%s'" % file_name)
   # make sure block size is still the same
   if geo.block_bytes != bs:
     raise IOError("Invalid geo block size chosen: %d" % geo.block_bytes)
   # keep geo
   blkdev.geo = geo
   return blkdev
예제 #9
0
    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
예제 #10
0
class FSCommandQueue(CommandQueue):
  def __init__(self, args, cmd_list, sep, cmd_map):
    CommandQueue.__init__(self, cmd_list, sep, cmd_map)
    self.args = args
    self.blkdev = None
    self.rdisk = None

  def run(self):
    self.img = self.args.image_file
    try:
      # main command loop
      exit_code = CommandQueue.run(self)
    except FSError as e:
      cmd = "'%s'" % " ".join(self.cmd_line)
      print(cmd, "FSError:", str(e))
      exit_code = 3
    except IOError as e:
      cmd = "'%s'" % " ".join(self.cmd_line)
      print(cmd, "IOError:", str(e))
      exit_code = 4
    finally:
      # close rdisk
      if self.rdisk != None:
        self.rdisk.close()
        if self.args.verbose:
          print("closing rdisk:", self.img)
      # close blkdev
      if self.blkdev != None:
        self.blkdev.close()
        if self.args.verbose:
          print("closing image:", self.img)
    return exit_code

  def create_cmd(self, cclass, name, opts):
    return cclass(self.args, opts)

  def _open_rdisk(self):
    if self.rdisk == None:
      self.rdisk = RDisk(self.blkdev)
      if self.args.verbose:
        print("opening rdisk:", self.img)
      return self.rdisk.open()
    else:
      return True

  def run_first(self, cmd_line, cmd):
    self.cmd_line = cmd_line

    # check if first command is an init command
    if not cmd.has_init_blkdev():
      # auto add 'open' command
      pre_cmd = OpenCommand(self.args, [])
      if self.args.verbose:
        print("auto open command:", self.cmd_line)
      exit_code = pre_cmd.run(self.blkdev, self.rdisk)
      if self.args.verbose:
        print("auto open exit_code:", exit_code)
      if exit_code != 0:
        return exit_code
      self.blkdev = pre_cmd.blkdev
      # setup rdisk (if necessary)
      if cmd.need_rdisk():
        if not self._open_rdisk():
          raise IOError("No RDB Disk?")

    # run first command
    if self.args.verbose:
      print("command:", self.cmd_line)
    if cmd.edit and self.args.read_only:
      raise IOError("Edit commands not allowed in read-only mode")

    # check code of command after __init__ parsing
    if cmd.exit_code != 0:
      return cmd.exit_code

    # perform command
    exit_code = cmd.run(self.blkdev, self.rdisk)
    if cmd.blkdev != None:
      self.blkdev = cmd.blkdev
    if cmd.rdisk != None:
      self.rdisk = cmd.rdisk

    # final exit code
    if self.args.verbose:
      print("exit_code:", exit_code)
    return exit_code

  def run_next(self, cmd_line, cmd):
    self.cmd_line = cmd_line
    if self.args.verbose:
      print("command:", self.cmd_line)
    # verify command
    if cmd.edit and self.args.read_only:
      raise IOError("Edit commands not allowed in read-only mode")
    # make sure rdisk is set up
    if self.rdisk == None and cmd.need_rdisk():
      if not self._open_rdisk():
        raise IOError("No RDB Disk?")
    # run command
    exit_code = cmd.run(self.blkdev, self.rdisk)
    if cmd.blkdev != None:
      self.blkdev = cmd.blkdev
    if cmd.rdisk != None:
      self.rdisk = cmd.rdisk
    if self.args.verbose:
      print("exit_code:", exit_code)
    return exit_code
예제 #11
0
파일: rdbtool.py 프로젝트: jukeks/amitools
class FSCommandQueue(CommandQueue):
    def __init__(self, args, cmd_list, sep, cmd_map):
        CommandQueue.__init__(self, cmd_list, sep, cmd_map)
        self.args = args
        self.blkdev = None
        self.rdisk = None

    def run(self):
        self.img = self.args.image_file
        try:
            # main command loop
            exit_code = CommandQueue.run(self)
        except FSError as e:
            cmd = "'%s'" % " ".join(self.cmd_line)
            print(cmd, "FSError:", str(e))
            exit_code = 3
        except IOError as e:
            cmd = "'%s'" % " ".join(self.cmd_line)
            print(cmd, "IOError:", str(e))
            exit_code = 4
        finally:
            # close rdisk
            if self.rdisk != None:
                self.rdisk.close()
                if self.args.verbose:
                    print("closing rdisk:", self.img)
            # close blkdev
            if self.blkdev != None:
                self.blkdev.close()
                if self.args.verbose:
                    print("closing image:", self.img)
        return exit_code

    def create_cmd(self, cclass, name, opts):
        return cclass(self.args, opts)

    def _open_rdisk(self):
        if self.rdisk == None:
            self.rdisk = RDisk(self.blkdev)
            if self.args.verbose:
                print("opening rdisk:", self.img)
            return self.rdisk.open()
        else:
            return True

    def run_first(self, cmd_line, cmd):
        self.cmd_line = cmd_line

        # check if first command is an init command
        if not cmd.has_init_blkdev():
            # auto add 'open' command
            pre_cmd = OpenCommand(self.args, [])
            if self.args.verbose:
                print("auto open command:", self.cmd_line)
            exit_code = pre_cmd.run(self.blkdev, self.rdisk)
            if self.args.verbose:
                print("auto open exit_code:", exit_code)
            if exit_code != 0:
                return exit_code
            self.blkdev = pre_cmd.blkdev
            # setup rdisk (if necessary)
            if cmd.need_rdisk():
                if not self._open_rdisk():
                    raise IOError("No RDB Disk?")

        # run first command
        if self.args.verbose:
            print("command:", self.cmd_line)
        if cmd.edit and self.args.read_only:
            raise IOError("Edit commands not allowed in read-only mode")

        # check code of command after __init__ parsing
        if cmd.exit_code != 0:
            return cmd.exit_code

        # perform command
        exit_code = cmd.run(self.blkdev, self.rdisk)
        if cmd.blkdev != None:
            self.blkdev = cmd.blkdev
        if cmd.rdisk != None:
            self.rdisk = cmd.rdisk

        # final exit code
        if self.args.verbose:
            print("exit_code:", exit_code)
        return exit_code

    def run_next(self, cmd_line, cmd):
        self.cmd_line = cmd_line
        if self.args.verbose:
            print("command:", self.cmd_line)
        # verify command
        if cmd.edit and self.args.read_only:
            raise IOError("Edit commands not allowed in read-only mode")
        # make sure rdisk is set up
        if self.rdisk == None and cmd.need_rdisk():
            if not self._open_rdisk():
                raise IOError("No RDB Disk?")
        # run command
        exit_code = cmd.run(self.blkdev, self.rdisk)
        if cmd.blkdev != None:
            self.blkdev = cmd.blkdev
        if cmd.rdisk != None:
            self.rdisk = cmd.rdisk
        if self.args.verbose:
            print("exit_code:", exit_code)
        return exit_code