laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/pysim/+/43974?usp=email )


Change subject: esim.saip: create a template DF missing from a file's parent 
path
......................................................................

esim.saip: create a template DF missing from a file's parent path

FsProfileElement.add_file() walks a file template's ppath and expects
every DF on it to be in the tree already. A PE can list a child EF
before the DF it sits in, or leave the DF out: an EF.PBR of
FilesTelecomV2 (ppath [0x5F3A]) under a DF.TELECOM that has no
DF.PHONEBOOK yet raises KeyError: 24378.

Create the missing DF from the same template before adding the child.
When the PE's own entry for that DF is decoded later, it takes over the
node created for it, so the children already attached stay in place.
The "already exists" error now names file.pe_name, not the undefined
file.pename.

The folder osmocom-pysim-saip-implicit-template-ppath of
hampushellsberg-dev/PySIM-upstream-patches on GitHub has the analysis
and a repro.

Change-Id: Id52621d41ed63f684fb3b71a982a1f18038d2b87
---
M pySim/esim/saip/__init__.py
1 file changed, 29 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/74/43974/1

diff --git a/pySim/esim/saip/__init__.py b/pySim/esim/saip/__init__.py
index 01302df..f0abfab 100644
--- a/pySim/esim/saip/__init__.py
+++ b/pySim/esim/saip/__init__.py
@@ -673,7 +673,15 @@
         logger.debug("%s.add_file: %s" % (self.__class__.__name__, repr(file)))
         template = 
templates.ProfileTemplateRegistry.get_by_oid(self.templateID)
         if file.pe_name in self.files:
-            raise KeyError('Cannot add file: %s already exists' % file.pename)
+            existing = self.files[file.pe_name]
+            if getattr(existing, '_implicit_template_path', False) and 
existing.node:
+                file.node = existing.node
+                file.node.file = file
+                self.files[file.pe_name] = file
+                if self.pe_sequence and isinstance(file.node, FsNodeDF):
+                    self.pe_sequence.cur_df = file.node
+                return
+            raise KeyError('Cannot add file: %s already exists' % file.pe_name)
         self.files[file.pe_name] = file
         if self.pe_sequence:
             if file.pe_name == 'mf': #file.fid == 0x3f00:
@@ -688,7 +696,26 @@
                     pe_df = self.files[template.base_df().pe_name].node
                     if file.template:
                         for d in file.template.ppath: # TODO: revert list?
-                            pe_df = pe_df[d]
+                            try:
+                                pe_df = pe_df[d]
+                                continue
+                            except KeyError:
+                                path_template = None
+                                for candidate in template.files:
+                                    if candidate.fid == d and 
candidate.file_type in ['MF', 'DF', 'ADF']:
+                                        path_template = candidate
+                                        break
+                                if path_template is None:
+                                    raise
+                                path_file = 
self.files.get(path_template.pe_name)
+                                if path_file is None:
+                                    path_file = File(path_template.pe_name, 
None, path_template)
+                                    path_file._implicit_template_path = True
+                                    self.files[path_template.pe_name] = 
path_file
+                                if path_file.node:
+                                    pe_df = path_file.node
+                                else:
+                                    pe_df = pe_df.add_file(path_file)
                     self.pe_sequence.cur_df = pe_df
                 elif template.parent:
                     # this is a template that belongs into the [A]DF of 
another template

--
To view, visit https://gerrit.osmocom.org/c/pysim/+/43974?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Id52621d41ed63f684fb3b71a982a1f18038d2b87
Gerrit-Change-Number: 43974
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <[email protected]>
Gerrit-CC: hampushellsberg <[email protected]>

Reply via email to