1+ from __future__ import annotations
2+
13import stat
24import struct
35import tarfile
@@ -38,7 +40,7 @@ class CpioInfo(tarfile.TarInfo):
3840 """
3941
4042 @classmethod
41- def fromtarfile (cls , tarfile : tarfile .TarFile ) -> tarfile . TarInfo :
43+ def fromtarfile (cls , tarfile : tarfile .TarFile ) -> CpioInfo :
4244 if tarfile .format not in (
4345 FORMAT_CPIO_BIN ,
4446 FORMAT_CPIO_ODC ,
@@ -64,7 +66,7 @@ def fromtarfile(cls, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
6466 return obj ._proc_member (tarfile )
6567
6668 @classmethod
67- def frombuf (cls , buf : bytes , format : int , encoding : str , errors : str ) -> tarfile . TarInfo :
69+ def frombuf (cls , buf : bytes , format : int , encoding : str , errors : str ) -> CpioInfo :
6870 if format in (FORMAT_CPIO_BIN , FORMAT_CPIO_ODC , FORMAT_CPIO_HPBIN , FORMAT_CPIO_HPODC ):
6971 obj = cls ._old_frombuf (buf , format )
7072 elif format in (FORMAT_CPIO_NEWC , FORMAT_CPIO_CRC ):
@@ -78,7 +80,7 @@ def frombuf(cls, buf: bytes, format: int, encoding: str, errors: str) -> tarfile
7880 return obj
7981
8082 @classmethod
81- def _old_frombuf (cls , buf : bytes , format : int ):
83+ def _old_frombuf (cls , buf : bytes , format : int ) -> CpioInfo :
8284 if format in (FORMAT_CPIO_BIN , FORMAT_CPIO_HPBIN ):
8385 values = list (struct .unpack ("<13H" , buf ))
8486 if values [0 ] == _swap16 (CPIO_MAGIC_OLD ):
@@ -131,7 +133,7 @@ def _old_frombuf(cls, buf: bytes, format: int):
131133 return obj
132134
133135 @classmethod
134- def _new_frombuf (cls , buf : bytes , format : int ):
136+ def _new_frombuf (cls , buf : bytes , format : int ) -> CpioInfo :
135137 values = struct .unpack ("<6s8s8s8s8s8s8s8s8s8s8s8s8s8s" , buf )
136138 values = [int (values [0 ], 8 )] + [int (v , 16 ) for v in values [1 :]]
137139 if values [0 ] not in (CPIO_MAGIC_NEW , CPIO_MAGIC_CRC ):
@@ -157,7 +159,7 @@ def _new_frombuf(cls, buf: bytes, format: int):
157159
158160 return obj
159161
160- def _proc_member (self , tarfile : tarfile .TarFile ) -> tarfile . TarInfo :
162+ def _proc_member (self , tarfile : tarfile .TarFile ) -> CpioInfo | None :
161163 self .name = tarfile .fileobj .read (self .namesize - 1 ).decode (tarfile .encoding , tarfile .errors )
162164 if self .name == "TRAILER!!!" :
163165 # The last entry in a cpio file has the special name ``TRAILER!!!``, indicating the end of the archive
@@ -177,10 +179,11 @@ def _proc_member(self, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
177179 def _round_word (self , offset : int ) -> int :
178180 if self .format in (FORMAT_CPIO_BIN , FORMAT_CPIO_HPBIN ):
179181 return (offset + 1 ) & ~ 0x01
180- elif self .format in (FORMAT_CPIO_NEWC , FORMAT_CPIO_CRC ):
182+
183+ if self .format in (FORMAT_CPIO_NEWC , FORMAT_CPIO_CRC ):
181184 return (offset + 3 ) & ~ 0x03
182- else :
183- return offset
185+
186+ return offset
184187
185188 def issocket (self ) -> bool :
186189 """Return True if it is a socket."""
@@ -211,13 +214,13 @@ def _swap16(value: int) -> int:
211214 return ((value & 0xFF ) << 8 ) | (value >> 8 )
212215
213216
214- def CpioFile (* args , ** kwargs ):
217+ def CpioFile (* args , ** kwargs ) -> tarfile . TarFile :
215218 """Utility wrapper around ``tarfile.TarFile`` to easily open cpio archives."""
216219 kwargs .setdefault ("format" , FORMAT_CPIO_UNKNOWN )
217220 return tarfile .TarFile (* args , ** kwargs , tarinfo = CpioInfo )
218221
219222
220- def open (* args , ** kwargs ):
223+ def open (* args , ** kwargs ) -> tarfile . TarFile :
221224 """Utility wrapper around ``tarfile.open`` to easily open cpio archives."""
222225 kwargs .setdefault ("format" , FORMAT_CPIO_UNKNOWN )
223226 return tarfile .open (* args , ** kwargs , tarinfo = CpioInfo )
0 commit comments