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