Zip_Normalizer

Last updatedView as Markdown

Strips macOS metadata from a release artifact and makes its modes readable.


  • Full name: \Linchpin_SatisPress\Helper\Zip_Normalizer

Constants

ConstantVisibilityTypeValue
APPLEDOUBLE_MAGICprivatestring"\x00\x05\x16\x07"
DS_STORE_MAGICprivatestring"\x00\x00\x00\x01Bud1"
DIR_MODEprivateint0755
FILE_MODEprivateint0644
EXECUTABLE_MODEprivateint0755
DIR_REQUIREDprivateint0555
FILE_REQUIREDprivateint0444

Methods

normalize

Normalise an artifact in place.

public static normalize(string $source): bool

Safe to call on anything: an archive that is already well formed, or one this cannot read, is left exactly as it was found.

  • This method is static. Parameters:
ParameterTypeDescription
$sourcestringAbsolute path to the zip to normalise.

Return Value:

True when the archive was rewritten.


inspect

Report what normalising an archive would change, without changing it.

public static inspect(string $source): array<string,mixed>
  • This method is static. Parameters:
ParameterTypeDescription
$sourcestringAbsolute path to the zip to inspect.

Return Value:

Entry, junk and re-mode counts plus top-level roots; empty when the archive cannot be read.


read_entries

Describe every entry in the archive.

private static read_entries(\ZipArchive $zip): array<int,array<string,mixed>>
  • This method is static. Parameters:
ParameterTypeDescription
$zip\ZipArchiveArchive to read.

is_junk

Is this entry macOS bookkeeping rather than part of the package?

private static is_junk(\ZipArchive $zip, int $index, string $name, bool $is_dir): bool

The name alone is not enough to decide. ._name is only conventionally an AppleDouble resource fork, and nothing stops a package from shipping a file or directory that happens to be named that way — dropping one silently would corrupt the package we are trying to repair. So a name is treated as a candidate and the file's own header is what convicts it: AppleDouble opens with the magic number 0x00051607, and a Finder .DS_Store opens with 0x00000001 followed by "Bud1". Anything that does not match is real content and is kept.

__MACOSX/ needs no such check. It is a name Finder reserves for the sidecar tree it writes beside an archive's real contents, it is the reason an archive ends up double-rooted, and SatisPress's own HiddenDirectoryValidator already treats its presence as fatal.

  • This method is static. Parameters:
ParameterTypeDescription
$zip\ZipArchiveArchive being read.
$indexintEntry index.
$namestringEntry name.
$is_dirboolWhether the entry is a directory.

opens_with

Does an entry's content begin with this signature?

private static opens_with(\ZipArchive $zip, int $index, string $magic): bool

Only the signature is read, not the whole entry.

  • This method is static. Parameters:
ParameterTypeDescription
$zip\ZipArchiveArchive being read.
$indexintEntry index.
$magicstringSignature to match.

normalised_attributes

Work out the external attributes an entry should carry instead.

private static normalised_attributes(bool $is_dir, int $opsys, int $attr): int|null
  • This method is static. Parameters:
ParameterTypeDescription
$is_dirboolWhether the entry is a directory.
$opsysintOperating system the entry's attributes came from.
$attrintExternal attributes as stored.

Return Value:

Replacement attributes, or null to leave the entry alone.


rewrite

Write the kept entries out and put the result back at the source path.

private static rewrite(\ZipArchive $zip, array<int,array<string,mixed>> $entries, string $source): bool

Entry bodies are buffered by ext-zip until close, which is why this only ever runs for an archive that genuinely needs repair.

  • This method is static. Parameters:
ParameterTypeDescription
$zip\ZipArchiveArchive being normalised.
$entriesarray<int,array<string,mixed>>Entries to carry over.
$sourcestringPath to overwrite.

add_entry

Copy one entry across, carrying its attributes.

private static add_entry(\ZipArchive $zip, \ZipArchive $normalised, array<string,mixed> $entry): bool
  • This method is static. Parameters:
ParameterTypeDescription
$zip\ZipArchiveArchive being normalised.
$normalised\ZipArchiveArchive being written.
$entryarray<string,mixed>Entry description.

temporary_file

Reserve a scratch path alongside the artifact.

private static temporary_file(string $source): string
  • This method is static. Parameters:
ParameterTypeDescription
$sourcestringArtifact being normalised.

Return Value:

Empty string when no temporary file could be created.


Have a question about this page?

Linchpin assistant

Was this helpful?