Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Timestamps.php

function Timestamps::updateTimestamps

Updates each file's unix timestamps in the PHAR

The PHAR signature can then be produced in a reproducible manner.

Parameters

int|\DateTimeInterface|string $timestamp Date string or DateTime or unix timestamp to use:

File

vendor/seld/phar-utils/src/Timestamps.php, line 33

Class

Timestamps

Namespace

Seld\PharUtils

Code

public function updateTimestamps($timestamp = null) {
    if ($timestamp instanceof \DateTime || $timestamp instanceof \DateTimeInterface) {
        $timestamp = $timestamp->getTimestamp();
    }
    elseif (is_string($timestamp)) {
        $timestamp = strtotime($timestamp);
    }
    elseif (!is_int($timestamp)) {
        $timestamp = strtotime('1984-12-24T00:00:00Z');
    }
    // detect manifest offset / end of stub
    if (!preg_match('{__HALT_COMPILER\\(\\);(?: +\\?>)?\\r?\\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) {
        throw new \RuntimeException('Could not detect the stub\'s end in the phar');
    }
    // set starting position and skip past manifest length
    $pos = $match[0][1] + strlen($match[0][0]);
    $stubEnd = $pos + $this->readUint($pos, 4);
    $pos += 4;
    $numFiles = $this->readUint($pos, 4);
    $pos += 4;
    // skip API version (YOLO)
    $pos += 2;
    // skip PHAR flags
    $pos += 4;
    $aliasLength = $this->readUint($pos, 4);
    $pos += 4 + $aliasLength;
    $metadataLength = $this->readUint($pos, 4);
    $pos += 4 + $metadataLength;
    while ($pos < $stubEnd) {
        $filenameLength = $this->readUint($pos, 4);
        $pos += 4 + $filenameLength;
        // skip filesize
        $pos += 4;
        // update timestamp to a fixed value
        $timeStampBytes = pack('L', $timestamp);
        $this->contents[$pos + 0] = $timeStampBytes[0];
        $this->contents[$pos + 1] = $timeStampBytes[1];
        $this->contents[$pos + 2] = $timeStampBytes[2];
        $this->contents[$pos + 3] = $timeStampBytes[3];
        // skip timestamp, compressed file size, crc32 checksum and file flags
        $pos += 4 * 4;
        $metadataLength = $this->readUint($pos, 4);
        $pos += 4 + $metadataLength;
        $numFiles--;
    }
    if ($numFiles !== 0) {
        throw new \LogicException('All files were not processed, something must have gone wrong');
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal