Skip to content

Commit

Permalink
Merge pull request #46 from cod3beat/write-changes-when-trunc-size-is…
Browse files Browse the repository at this point in the history
…-smaller-than-original

BUG FIX: Write changes properly to LzwStreamWrapper::stream_truncate
  • Loading branch information
wapmorgan authored Jun 21, 2024
2 parents 5f02ad0 + e0538fc commit 30debf4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/LzwStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public function stream_close()
if (file_exists($this->tmp2)) unlink($this->tmp2);
} else {
$this->data = null;
$this->dataSize = 0;
}
}

Expand Down Expand Up @@ -339,6 +340,7 @@ public function stream_write($data)
$postfix = substr($this->data, ($this->pointer + $count));
$this->data = $prefix.$data.$postfix;
$this->pointer += $count;
$this->dataSize = strlen($this->data);

return $count;
}
Expand Down Expand Up @@ -396,13 +398,17 @@ public function stream_truncate($new_size)
} elseif ($new_size < $actual_data_size) {
if ($this->tmp === null) {
$this->data = substr($this->data, 0, $new_size);
$this->dataSize = strlen($this->data);
$this->writtenBytes = $new_size;
} else {
$fp = fopen($this->tmp, 'w'.(strpos($this->mode, 'b') !== 0
? 'b' : null));
ftruncate($fp, $new_size);
fclose($fp);
}
}

return true;
}

/**
Expand Down

0 comments on commit 30debf4

Please sign in to comment.