[elbe-devel] [PATCH 3/9] elbepack: filesystem: drop cat_file()

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Jul 29 10:46:30 CEST 2024


The function cat_file() has various issues:

* Its name is wrong
* It silently swallows errors
* It leaks the file handle on errors

Replace cat_file() with a fixed helper function in the only calling
module.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/efilesystem.py | 14 +++++++++-----
 elbepack/filesystem.py  | 10 ----------
 2 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index ccc47737f936..7fb7b7ccc933 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -96,6 +96,11 @@ def dpkg_architecture():
     ).rstrip('\n')
 
 
+def _readlines(rfs, file):
+    with rfs.open(file) as f:
+        return f.readlines()
+
+
 def extract_target(src, xml, dst, cache):
 
     # create filelists describing the content of the target rfs
@@ -115,12 +120,11 @@ def extract_target(src, xml, dst, cache):
 
         file_list = []
         for line in pkglist:
-            file_list += src.cat_file(f'var/lib/dpkg/info/{line}.list')
-            file_list += src.cat_file(f'var/lib/dpkg/info/{line}.conffiles')
+            file_list += _readlines(src, f'var/lib/dpkg/info/{line}.list')
+            file_list += _readlines(src, f'var/lib/dpkg/info/{line}.conffiles')
 
-            file_list += src.cat_file(f'var/lib/dpkg/info/{line}:{arch}.list')
-            file_list += src.cat_file(
-                f'var/lib/dpkg/info/{line}:{arch}.conffiles')
+            file_list += _readlines(src, f'var/lib/dpkg/info/{line}:{arch}.list')
+            file_list += _readlines(src, f'var/lib/dpkg/info/{line}:{arch}.conffiles')
 
         file_list = sorted(set(file_list),
                            key=lambda k: k[4:] if k.startswith('/usr') else k)
diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
index d13755e0921c..0b58714f2d99 100644
--- a/elbepack/filesystem.py
+++ b/elbepack/filesystem.py
@@ -275,16 +275,6 @@ class Filesystem:
     def utime(self, path, times=None):
         os.utime(self.fname(path), times)
 
-    def cat_file(self, inf):
-        content = []
-        try:
-            f = self.open(inf)
-            content = f.readlines()
-            f.close()
-        except IOError:
-            pass
-        return content
-
     def remove(self, path, noerr=False):
         """
         >>> this.remove("remove") # doctest: +ELLIPSIS

-- 
2.45.2



More information about the elbe-devel mailing list