Change in pysim[master]: pySim-shell: fix and improve file system exporter

laforge gerrit-no-reply at lists.osmocom.org
Fri Apr 2 14:44:55 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23568 )

Change subject: pySim-shell: fix and improve file system exporter
......................................................................

pySim-shell: fix and improve file system exporter

The file system exporter function export() selects the exported EF from
inside a try block. It also selects the parent DF again when leaving the
try block. If an exception ocurrs during select this is fine, but if it
happens during read it leaves the EF selected which makes messes up the
recursive filesystem walk.

There are also minor inconsistancies with the exception strings and the
path displayed in the execption strings

Related: OS#4963
Change-Id: Ie9b1712b37e5b39e9016497185510a2a45c4ca6c
---
M pySim-shell.py
1 file changed, 20 insertions(+), 10 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/pySim-shell.py b/pySim-shell.py
index 2ac0b26..3c56f51 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -277,28 +277,33 @@
 		self.walk()
 
 	def export(self, filename, context):
+		""" Select and export a single file """
 		context['COUNT'] += 1
-		path_list = self._cmd.rs.selected_file.fully_qualified_path(True)
-		path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)
+		df = self._cmd.rs.selected_file
+
+		if not isinstance(df, CardDF):
+			raise RuntimeError("currently selected file %s is not a DF or ADF" % str(df))
+
+		df_path_list = df.fully_qualified_path(True)
+		df_path_list_fid = df.fully_qualified_path(False)
 
 		self._cmd.poutput("#" * 80)
-		file_str = '/'.join(path_list) + "/" + str(filename) + " " * 80
+		file_str = '/'.join(df_path_list) + "/" + str(filename) + " " * 80
 		self._cmd.poutput("# " + file_str[0:77] + "#")
 		self._cmd.poutput("#" * 80)
 
-		self._cmd.poutput("# directory: %s (%s)" % ('/'.join(path_list), '/'.join(path_list_fid)))
+		self._cmd.poutput("# directory: %s (%s)" % ('/'.join(df_path_list), '/'.join(df_path_list_fid)))
 		try:
 			fcp_dec = self._cmd.rs.select(filename, self._cmd)
-			path_list = self._cmd.rs.selected_file.fully_qualified_path(True)
-			path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)
-			self._cmd.poutput("# file: %s (%s)" % (path_list[-1], path_list_fid[-1]))
+			self._cmd.poutput("# file: %s (%s)" % (self._cmd.rs.selected_file.name, self._cmd.rs.selected_file.fid))
 
 			fd = fcp_dec['file_descriptor']
 			structure = fd['structure']
 			self._cmd.poutput("# structure: %s" % str(structure))
 
-			for f in path_list:
+			for f in df_path_list:
 				self._cmd.poutput("select " + str(f))
+			self._cmd.poutput("select " + self._cmd.rs.selected_file.name)
 
 			if structure == 'transparent':
 				result = self._cmd.rs.read_binary()
@@ -308,13 +313,18 @@
 				for r in range(1, num_of_rec + 1):
 					result = self._cmd.rs.read_record(r)
 					self._cmd.poutput("update_record %d %s" % (r, str(result[0])))
-			fcp_dec = self._cmd.rs.select("..", self._cmd)
 		except Exception as e:
-			bad_file_str = '/'.join(path_list) + "/" + str(filename) + ", " + str(e)
+			bad_file_str = '/'.join(df_path_list) + "/" + str(filename) + ", " + str(e)
 			self._cmd.poutput("# bad file: %s" % bad_file_str)
 			context['ERR'] += 1
 			context['BAD'].append(bad_file_str)
 
+		# When reading the file is done, make sure the parent file is
+		# selected again. This will be the usual case, however we need
+		# to check before since we must not select the same DF twice
+		if df != self._cmd.rs.selected_file:
+			self._cmd.rs.select(df.fid or df.aid, self._cmd)
+
 		self._cmd.poutput("#")
 
 	export_parser = argparse.ArgumentParser()

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23568
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie9b1712b37e5b39e9016497185510a2a45c4ca6c
Gerrit-Change-Number: 23568
Gerrit-PatchSet: 4
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210402/324fe099/attachment.htm>


More information about the gerrit-log mailing list