MCPcopy Index your code
hub / github.com/RustPython/RustPython / create_pax_header

Method create_pax_header

Lib/tarfile.py:1078–1134  ·  view source on GitHub ↗

Return the object as a ustar header block. If it cannot be represented this way, prepend a pax extended header sequence with supplement information.

(self, info, encoding)

Source from the content-addressed store, hash-verified

1076 return buf + self._create_header(info, GNU_FORMAT, encoding, errors)
1077
1078 def create_pax_header(self, info, encoding):
1079 """Return the object as a ustar header block. If it cannot be
1080 represented this way, prepend a pax extended header sequence
1081 with supplement information.
1082 """
1083 info["magic"] = POSIX_MAGIC
1084 pax_headers = self.pax_headers.copy()
1085
1086 # Test string fields for values that exceed the field length or cannot
1087 # be represented in ASCII encoding.
1088 for name, hname, length in (
1089 ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK),
1090 ("uname", "uname", 32), ("gname", "gname", 32)):
1091
1092 if hname in pax_headers:
1093 # The pax header has priority.
1094 continue
1095
1096 # Try to encode the string as ASCII.
1097 try:
1098 info[name].encode("ascii", "strict")
1099 except UnicodeEncodeError:
1100 pax_headers[hname] = info[name]
1101 continue
1102
1103 if len(info[name]) > length:
1104 pax_headers[hname] = info[name]
1105
1106 # Test number fields for values that exceed the field limit or values
1107 # that like to be stored as float.
1108 for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)):
1109 needs_pax = False
1110
1111 val = info[name]
1112 val_is_float = isinstance(val, float)
1113 val_int = round(val) if val_is_float else val
1114 if not 0 <= val_int < 8 ** (digits - 1):
1115 # Avoid overflow.
1116 info[name] = 0
1117 needs_pax = True
1118 elif val_is_float:
1119 # Put rounded value in ustar header, and full
1120 # precision value in pax header.
1121 info[name] = val_int
1122 needs_pax = True
1123
1124 # The existing pax header has priority.
1125 if needs_pax and name not in pax_headers:
1126 pax_headers[name] = str(val)
1127
1128 # Create a pax extended header if necessary.
1129 if pax_headers:
1130 buf = self._create_pax_generic_header(pax_headers, XHDTYPE, encoding)
1131 else:
1132 buf = b""
1133
1134 return buf + self._create_header(info, USTAR_FORMAT, "ascii", "replace")
1135

Callers 2

tobufMethod · 0.95

Calls 8

_create_headerMethod · 0.95
lenFunction · 0.85
isinstanceFunction · 0.85
roundFunction · 0.85
strFunction · 0.85
copyMethod · 0.45
encodeMethod · 0.45

Tested by 1