Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dlerror() information #32

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pipeline {
stage('PHPStan') {
agent {
docker {
image 'ghcr.io/phpstan/phpstan:1.0.2'
image 'ghcr.io/phpstan/phpstan:1.8.1'
args '--mount type=volume,source=phpstan-cache,destination=/tmp/phpstan ' +
'--user root:root ' +
"--entrypoint='' "
Expand Down
8 changes: 0 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ parameters:
ignoreErrors:
- '/Call to an undefined method FFI::.*/'
- '/Access to an undefined property FFI\\CData::.*/'
-
message: '/Binary operation "-" between numeric-string and string results in an error./'
paths:
- test/ListObjectsTest.php
-
message: "#^Binary operation \"\\-\" between int and string results in an error\\.$#"
count: 1
path: test/StatObjectTest.php
-
message: "#^Property Storj\\\\Uplink\\\\Access\\:\\:\\$scope is never read, only written\\.$#"
count: 1
Expand Down
43 changes: 39 additions & 4 deletions src/Uplink.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,45 @@ public static function create(): self
$os = str_replace(' ', '_', strtolower(php_uname('s')));
$extension = strpos($os, 'windows') !== false ? 'dll' : 'so';

$ffi = FFI::cdef(
file_get_contents($root . '/build/uplink-php.h'),
"{$root}/build/libuplink-{$arch}-{$os}.{$extension}"
);
$libuplinkSo = "{$root}/build/libuplink-{$arch}-{$os}.{$extension}";
try {
$ffi = FFI::cdef(
file_get_contents($root . '/build/uplink-php.h'),
$libuplinkSo
);
} catch (\FFI\Exception $ffiException) {
// dlerror() may have a more detailed error.
// Example: no permission, wrong glibc version, unknown binary format etc.
// This check should really be in PHP core.
try {
$ffi2 = FFI::cdef("
void *dlopen(const char *filename, int flags);
char *dlerror();
int dlclose(void *handle);
");
$handle = $ffi2->dlopen($libuplinkSo, 1);
$scope = Scope::exit(function() use ($handle, $ffi2) {
if ($handle !== null) {
$ffi2->dlclose($handle);
}
});
$cError = $ffi2->dlerror();
} catch (\Throwable $innerException) {
// error when calling dlopen/dlerror, rethrow original error
throw $ffiException;
}

if ($cError == null) {
// dlerror had no info, rethrow original error
throw $ffiException;
}

$dlErrorMessage = FFI::string($cError);

throw new \FFI\Exception(
$ffiException->getMessage() . ": " . $dlErrorMessage
);
}

return new self($ffi);
}
Expand Down