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)
| 1023 | return buf + self._create_header(info, GNU_FORMAT, encoding, errors) |
| 1024 | |
| 1025 | def create_pax_header(self, info, encoding): |
| 1026 | """Return the object as a ustar header block. If it cannot be |
| 1027 | represented this way, prepend a pax extended header sequence |
| 1028 | with supplement information. |
| 1029 | """ |
| 1030 | info["magic"] = POSIX_MAGIC |
| 1031 | pax_headers = self.pax_headers.copy() |
| 1032 | |
| 1033 | # Test string fields for values that exceed the field length or cannot |
| 1034 | # be represented in ASCII encoding. |
| 1035 | for name, hname, length in ( |
| 1036 | ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK), |
| 1037 | ("uname", "uname", 32), ("gname", "gname", 32)): |
| 1038 | |
| 1039 | if hname in pax_headers: |
| 1040 | # The pax header has priority. |
| 1041 | continue |
| 1042 | |
| 1043 | # Try to encode the string as ASCII. |
| 1044 | try: |
| 1045 | info[name].encode("ascii", "strict") |
| 1046 | except UnicodeEncodeError: |
| 1047 | pax_headers[hname] = info[name] |
| 1048 | continue |
| 1049 | |
| 1050 | if len(info[name]) > length: |
| 1051 | pax_headers[hname] = info[name] |
| 1052 | |
| 1053 | # Test number fields for values that exceed the field limit or values |
| 1054 | # that like to be stored as float. |
| 1055 | for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)): |
| 1056 | needs_pax = False |
| 1057 | |
| 1058 | val = info[name] |
| 1059 | val_is_float = isinstance(val, float) |
| 1060 | val_int = round(val) if val_is_float else val |
| 1061 | if not 0 <= val_int < 8 ** (digits - 1): |
| 1062 | # Avoid overflow. |
| 1063 | info[name] = 0 |
| 1064 | needs_pax = True |
| 1065 | elif val_is_float: |
| 1066 | # Put rounded value in ustar header, and full |
| 1067 | # precision value in pax header. |
| 1068 | info[name] = val_int |
| 1069 | needs_pax = True |
| 1070 | |
| 1071 | # The existing pax header has priority. |
| 1072 | if needs_pax and name not in pax_headers: |
| 1073 | pax_headers[name] = str(val) |
| 1074 | |
| 1075 | # Create a pax extended header if necessary. |
| 1076 | if pax_headers: |
| 1077 | buf = self._create_pax_generic_header(pax_headers, XHDTYPE, encoding) |
| 1078 | else: |
| 1079 | buf = b"" |
| 1080 | |
| 1081 | return buf + self._create_header(info, USTAR_FORMAT, "ascii", "replace") |
| 1082 |
no test coverage detected