commit:     5f256ab11e5510f98d7951ac9770948e00c36ed5
Author:     Sheng Yu <syu.os <AT> protonmail <DOT> com>
AuthorDate: Wed Nov  2 19:05:29 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Nov  2 22:58:15 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=5f256ab1

Unconditionally update first level of dir name in binpkgs

Enabling / disabling binpkg-multi-instance while doing metadata move /
update may cause binary package structure mismatch.

This will unconditional replace the first level name (cpv) in the
package.

Closes: https://bugs.gentoo.org/877271
Signed-off-by: Sheng Yu <syu.os <AT> protonmail.com>
Closes: https://github.com/gentoo/portage/pull/929
Signed-off-by: Sam James <sam <AT> gentoo.org>

 lib/portage/gpkg.py | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/lib/portage/gpkg.py b/lib/portage/gpkg.py
index 5a8c16186..3d2aa4223 100644
--- a/lib/portage/gpkg.py
+++ b/lib/portage/gpkg.py
@@ -1048,6 +1048,14 @@ class gpkg:
             self._add_signature(checksum_info, image_tarinfo, container)
 
         self._add_manifest(container)
+
+        # Check if all directories are the same in the container
+        prefix = os.path.commonpath(container.getnames())
+        if not prefix:
+            raise InvalidBinaryPackageFormat(
+                f"gpkg file structure mismatch in {self.gpkg_file}"
+            )
+
         container.close()
 
     def decompress(self, decompress_dir):
@@ -1132,9 +1140,9 @@ class gpkg:
                         os.path.join(self.prefix, file_name_old)
                     )
                     new_data_tarinfo = copy(old_data_tarinfo)
-                    new_data_tarinfo.name = new_data_tarinfo.name.replace(
-                        old_basename, new_basename, 1
-                    )
+                    new_file_path = list(os.path.split(new_data_tarinfo.name))
+                    new_file_path[0] = new_basename
+                    new_data_tarinfo.name = os.path.join(*new_file_path)
                     container.addfile(
                         new_data_tarinfo, 
container_old.extractfile(old_data_tarinfo)
                     )
@@ -1142,6 +1150,13 @@ class gpkg:
 
             self._add_manifest(container)
 
+            # Check if all directories are the same in the container
+            prefix = os.path.commonpath(container.getnames())
+            if not prefix:
+                raise InvalidBinaryPackageFormat(
+                    f"gpkg file structure mismatch in {self.gpkg_file}"
+                )
+
         shutil.move(tmp_gpkg_file_name, self.gpkg_file)
 
     def update_signature(self, keep_current_signature=False):
@@ -1222,6 +1237,13 @@ class gpkg:
 
             self._add_manifest(container)
 
+            # Check if all directories are the same in the container
+            prefix = os.path.commonpath(container.getnames())
+            if not prefix:
+                raise InvalidBinaryPackageFormat(
+                    f"gpkg file structure mismatch in {self.gpkg_file}"
+                )
+
         shutil.move(tmp_gpkg_file_name, self.gpkg_file)
 
     def _add_metadata(self, container, metadata, compression_cmd):
@@ -1438,6 +1460,14 @@ class gpkg:
             self._add_signature(checksum_info, image_tarinfo, container)
 
         self._add_manifest(container)
+
+        # Check if all directories are the same in the container
+        prefix = os.path.commonpath(container.getnames())
+        if not prefix:
+            raise InvalidBinaryPackageFormat(
+                f"gpkg file structure mismatch in {self.gpkg_file}"
+            )
+
         container.close()
 
     def _record_checksum(self, checksum_info, tarinfo):

Reply via email to