def init_blkdev(self, image_file): opts = KeyValue.parse_key_value_strings(self.opts[1:]) f = BlkDevFactory() blkdev = f.open(image_file, options=opts, read_only=False, none_if_missing=True) if not blkdev: return f.create(image_file, options=opts, force=self.args.force)
def create(self, path): self.cleanUp() self.blkdev = BlkDevFactory().create(path) self.volume = ADFSVolume(self.blkdev) name = os.path.basename(path) self.volume.create(make_fsstr(name), dos_type=None)
def create_out_blkdev(self, image_file, force=True, options=None): if self.in_blkdev == None: return None # clone geo from input if options == None: options = self.in_blkdev.get_chs_dict() f = BlkDevFactory() self.out_blkdev = f.create(image_file, force=force, options=options) return self.out_blkdev
def pack_create_blkdev(self, in_path, image_file, force=True, options=None): # try to read options from blkdev file if options == None or len(options) == 0: blkdev_path = in_path + ".blkdev" if os.path.exists(blkdev_path): f = open(blkdev_path, "rb") options = {} for line in f: KeyValue.parse_key_value_string(line, options) f.close() f = BlkDevFactory() return f.create(image_file, force=force, options=options)
def pack_create_blkdev(self, in_path, image_file, force=True, options=None): factory = BlkDevFactory() blkdev = None if not force: # try to open an existing image or return None blkdev = factory.open(image_file, none_if_missing=True) if not blkdev: # try to read options from blkdev file if options == None or len(options) == 0: blkdev_path = in_path + ".blkdev" if os.path.exists(blkdev_path): f = open(blkdev_path, "rb") options = {} for line in f: KeyValue.parse_key_value_string(line, options) f.close() # create a new blkdev blkdev = factory.create(image_file, force=force, options=options) return blkdev
class ADFSScanner: def __init__(self): self.factory = BlkDevFactory() def can_handle(self, scan_file): base_name = scan_file.get_basename().lower() for ext in self.factory.valid_extensions: if base_name.endswith(ext): return True return False def handle(self, scan_file, scanner): if scan_file.is_seekable(): sf = scan_file else: sf = scanner.promote_scan_file(scan_file, seekable=True) # create blkdev blkdev = self.factory.open(sf.get_local_path(), fobj=sf.get_fobj()) # create volume volume = ADFSVolume(blkdev) volume.open() # scan volume node = volume.get_root_dir() ok = self._scan_node(sf, scanner, node) # done volume.close() blkdev.close() return ok def _scan_node(self, scan_file, scanner, node): if node.is_dir(): # recurse into dir entries = node.get_entries() for e in entries: ok = self._scan_node(scan_file, scanner, e) if not ok: return False return True elif node.is_file(): # read file in ram fobj data = node.get_file_data() node.flush() size = len(data) path = node.get_node_path_name().get_unicode() fobj = io.StringIO(data) sf = scan_file.create_sub_path(path, fobj, size, True, False) ok = scanner.scan_obj(sf) sf.close() return True
class ADFSScanner: def __init__(self): self.factory = BlkDevFactory() def can_handle(self, scan_file): base_name = scan_file.get_basename().lower() for ext in self.factory.valid_extensions: if base_name.endswith(ext): return True return False def handle(self, scan_file, scanner): if scan_file.is_seekable(): sf = scan_file else: sf = scanner.promote_scan_file(scan_file, seekable=True) # create blkdev blkdev = self.factory.open(sf.get_local_path(), fobj=sf.get_fobj()) # create volume volume = ADFSVolume(blkdev) volume.open() # scan volume node = volume.get_root_dir() ok = self._scan_node(sf, scanner, node) # done volume.close() blkdev.close() return ok def _scan_node(self, scan_file, scanner, node): if node.is_dir(): # recurse into dir entries = node.get_entries() for e in entries: ok = self._scan_node(scan_file, scanner, e) if not ok: return False return True elif node.is_file(): # read file in ram fobj data = node.get_file_data() node.flush() size = len(data) path = node.get_node_path_name().get_unicode() fobj = StringIO.StringIO(data) sf = scan_file.create_sub_path(path, fobj, size, True, False) ok = scanner.scan_obj(sf) sf.close() return True
def __init__(self): self.factory = BlkDevFactory()
print("%20s %s " % (msg, path), end="\r"), sys.stdout.flush() def log_path(path, msg): print("%20s %s " % (msg, path)) def print_block(percent): print("%3.1f%%" % (percent / 10.0), end="\r"), sys.stdout.flush() # ----- scanner ----- factory = BlkDevFactory() def scan(path, args): if not os.path.exists(path): log_path(path, "DOES NOT EXIST") return 1 if os.path.isdir(path): return scan_dir(path, args) elif os.path.isfile(path): return scan_file(path, args) def scan_dir(path, args): for name in sorted(os.listdir(path)): epath = os.path.join(path, name)
def open(self, path): self.cleanUp() self.blkdev = BlkDevFactory().open(path) self.volume = ADFSVolume(self.blkdev) self.volume.open()
def create_in_blkdev(self): f = BlkDevFactory() self.in_blkdev = f.open(self.in_image_file, read_only=True, options=self.in_options) return self.in_blkdev
def init_blkdev(self, image_file): opts = KeyValue.parse_key_value_strings(self.opts) f = BlkDevFactory() return f.open(image_file, options=opts, read_only=self.args.read_only)
class ADF(): def __init__(self, app): self.app = app self.volume = None self.blkdev = None self.node = None self.path = None def absolutePath(self, name): return (self.path + '/' if self.path != '/' else '') + name def create(self, path): self.cleanUp() self.blkdev = BlkDevFactory().create(path) self.volume = ADFSVolume(self.blkdev) name = os.path.basename(path) self.volume.create(make_fsstr(name), dos_type=None) def open(self, path): self.cleanUp() self.blkdev = BlkDevFactory().open(path) self.volume = ADFSVolume(self.blkdev) self.volume.open() def navigate(self, path='/'): try: self.node = self.volume.get_path_name(make_fsstr(path)) except: return self.path = path self.entries = [{ 'name': entry.get_file_name().get_name().__str__(), 'type': 'dir' if entry.is_dir() else 'file' } for entry in self.node.get_entries_sorted_by_name()] self.app.updatePath(self.path) self.app.updateBrowser(self.entries) def navigateDown(self, dir): if self.path == '/': self.navigate(dir) else: self.navigate(self.path + '/' + dir) def parent(self): path = self.path.split('/')[:-1] if len(path) > 1: self.navigate(path.join('/')) elif len(path) == 1: self.navigate(path[0]) else: self.navigate() def volumeName(self): return self.volume.get_volume_name().__str__() def volumeInfo(self): return self.volume.get_info().__str__() def extract(self, name, output): path = self.absolutePath(name) node = self.volume.get_path_name(make_fsstr(path)) if node.is_file(): data = node.get_file_data() fh = open(output, 'wb') fh.write(data) fh.close() elif node.is_dir(): img = Imager(meta_mode=Imager.META_MODE_NONE) img.unpack_dir(node, output) def insert(self, input): name = os.path.basename(input) if os.path.isfile(input): fh = open(input, 'rb') data = fh.read() fh.close() self.volume.write_file(data, make_fsstr(self.path), make_fsstr(name)) elif os.path.isdir(input): parent, name = self.volume.get_create_path_name( make_fsstr(self.path), make_fsstr(name)) node = parent.create_dir(name) img = Imager(meta_mode=Imager.META_MODE_NONE) img.pack_dir(input, node) self.navigate(self.path) def makeDir(self, name): path = self.absolutePath(name) self.volume.create_dir(make_fsstr(path)) self.navigate(self.path) def delete(self, name): path = self.absolutePath(name) self.volume.delete(make_fsstr(path), all=True) self.navigate(self.path) def relabel(self, name): self.volume.relabel(make_fsstr(name)) def cleanUp(self): if self.volume: self.volume.close() if self.blkdev: self.blkdev.close()
def init_blkdev(self, image_file): opts = KeyValue.parse_key_value_strings(self.opts[1:]) f = BlkDevFactory() return f.create(image_file, options=opts, force=self.args.force)
def main(): a = sys.argv n = len(a) if n < 3: print "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" print """Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """ return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == 'detect': if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print "Invalid size!" else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "size: ", size print "opts: ", opts size = d.detect(size, opts) if size != None: print "geo: ", d else: print "FAILED" # setup a new disk geometry from options elif cmd == 'setup': d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print "opts: ", opts size = d.setup(opts) if size != None: print "setup: ", size, ByteSize.to_byte_size_str(size) print "geo: ", d else: print "FAILED" # open a blkdev and detect geometry elif cmd == 'open': opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ", opts f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print "blkdev: ", blkdev.__class__.__name__ print "geo: ", blkdev.get_geometry() blkdev.close() else: print "FAILED" # create a new blkdev with setup geometry elif cmd == 'create': opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ", opts f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print "blkdev: ", blkdev.__class__.__name__ print "geo: ", blkdev.get_geometry() blkdev.close() else: print "FAILED" return 0
def main(): a = sys.argv n = len(a) if n < 3: print "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" print """Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """ return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == 'detect': if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print "Invalid size!" else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "size: ",size print "opts: ",opts size = d.detect(size, opts) if size != None: print "geo: ",d else: print "FAILED" # setup a new disk geometry from options elif cmd == 'setup' : d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print "opts: ",opts size = d.setup(opts) if size != None: print "setup: ",size,ByteSize.to_byte_size_str(size) print "geo: ",d else: print "FAILED" # open a blkdev and detect geometry elif cmd == 'open': opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ",opts f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print "blkdev: ",blkdev.__class__.__name__ print "geo: ",blkdev.get_geometry() blkdev.close() else: print "FAILED" # create a new blkdev with setup geometry elif cmd == 'create': opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ",opts f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print "blkdev: ",blkdev.__class__.__name__ print "geo: ",blkdev.get_geometry() blkdev.close() else: print "FAILED" return 0
def main(args=None): if not args: a = sys.argv else: a = args n = len(a) if n < 3: print( "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" ) print("""Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """) return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == "detect": if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print("Invalid size!") else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print("size: ", size) print("opts: ", opts) size = d.detect(size, opts) if size != None: print("geo: ", d) else: print("FAILED") # setup a new disk geometry from options elif cmd == "setup": d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print("opts: ", opts) size = d.setup(opts) if size != None: print("setup: ", size, ByteSize.to_byte_size_str(size)) print("geo: ", d) else: print("FAILED") # open a blkdev and detect geometry elif cmd == "open": opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print("opts: ", opts) f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print("blkdev: ", blkdev.__class__.__name__) print("geo: ", blkdev.get_geometry()) blkdev.close() else: print("FAILED") # create a new blkdev with setup geometry elif cmd == "create": opts = KeyValue.parse_key_value_strings(a[3:]) print("opts: ", opts) f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print("blkdev: ", blkdev.__class__.__name__) print("geo: ", blkdev.get_geometry()) blkdev.close() else: print("FAILED") return 0