Skip to content

Commit

Permalink
Workaround undocumented amphp/file v2 quirk
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 15, 2021
1 parent 5969ebe commit 45b72b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jobs:
strategy:
matrix:
php:
- '8.1'
- '8.0'
- '7.4'
- '7.3'
Expand Down
3 changes: 2 additions & 1 deletion src/danog/MadelineProto/MTProtoTools/FilesLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public function downloadToStream($messageMedia, $stream, $cb = null, int $offset
yield $stream->seek($offset);
}
}
return yield $stream->write($payload);
yield $stream->write($payload);
return \strlen($payload);
};
return yield from $this->downloadToCallable($messageMedia, $callable, $cb, $seekable, $offset, $end);
}
Expand Down
21 changes: 10 additions & 11 deletions src/danog/MadelineProto/SessionPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use function Amp\File\exists;
use function Amp\File\openFile;
use function Amp\File\put;
use function Amp\File\rename as renameAsync;
use function Amp\File\stat;

Expand Down Expand Up @@ -97,19 +98,17 @@ public function serialize(object $object, string $path): \Generator
try {
Logger::log("Got exclusive lock of $path.lock...");

$object = \serialize($object);
$object = Serialization::PHP_HEADER
.\chr(Serialization::VERSION)
.\chr(PHP_MAJOR_VERSION)
.\chr(PHP_MINOR_VERSION)
.\serialize($object);

$file = yield openFile("$path.temp.php", 'bw+');
$l = yield $file->write(Serialization::PHP_HEADER);
$l += yield $file->write(\chr(Serialization::VERSION));
$l += yield $file->write(\chr(PHP_MAJOR_VERSION));
$l += yield $file->write(\chr(PHP_MINOR_VERSION));
$l += yield $file->write($object);
yield $file->close();
yield put(
"$path.temp.php",
$object
);

if ($l !== ($need = \strlen(Serialization::PHP_HEADER)+3+\strlen($object))) {
throw new Exception("Did not write all the data (need $need, wrote $l)");
}
yield renameAsync("$path.temp.php", $path);
} finally {
$unlock();
Expand Down

0 comments on commit 45b72b8

Please sign in to comment.