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

Directory separator conflicts with PharData file path #168

Open
KurlenkovEvgeniy opened this issue May 7, 2024 · 4 comments
Open

Directory separator conflicts with PharData file path #168

KurlenkovEvgeniy opened this issue May 7, 2024 · 4 comments
Labels

Comments

@KurlenkovEvgeniy
Copy link

PharData has FilesystemIterator::UNIX_PATHS flag as default in constructor (https://www.php.net/manual/en/phardata.construct.php, https://www.php.net/manual/en/class.filesystemiterator.php#filesystemiterator.constants.unix-paths).
However, ‎DIRECTORY_SEPARATOR is used in

Str::afterLast($file->getPath(), DIRECTORY_SEPARATOR),
which fails to retrieve valid $relativePath on Windows.

@KurlenkovEvgeniy
Copy link
Author

And maybe it is possible to extract file exact to $this->getDatabasePath()?

$archive->extractTo($storage->path('/'), $relativePath, true);

It leaves extracted junk files.

@stevebauman
Copy link
Owner

Hi @KurlenkovEvgeniy,

What do you suggest? Should we create new PharData with unix-paths disabled for Windows systems?

@stevebauman stevebauman added the bug label May 7, 2024
@KurlenkovEvgeniy
Copy link
Author

Since the FilesystemIterator::UNIX_PATHS flag used in PharData, changing DIRECTORY_SEPARATOR to '/' in

Str::afterLast($file->getPath(), DIRECTORY_SEPARATOR),

should be enough.

@KurlenkovEvgeniy
Copy link
Author

KurlenkovEvgeniy commented May 7, 2024

What can you say about junk files?
There is a directory in downloading .tar.gz file. The structure is like this - maxmind.tar.gz/GeoLite2-Country_20240503/GeoLite2-Country.mmdb
After extracting, directory
%root directory from config('location.maxmind.local.path')%/GeoLite2-Country_20240503/GeoLite2-Country.mmdb
is not being deleted.
Looks like we can't extract files within directories without latter themselves.
I can suggest to clear those temp directories.
From line

$relativePath = implode('/', [

$tmpDirectory = Str::afterLast($file->getPath(), '/'); // add var and unix-separator
if ($tmpDirectory == $tar) { // check if in the future the .mmdb file will be in the root of archive
    $relativePath = $file->getFilename();
} else {
    $relativePath = implode('/', [
        $tmpDirectory,
        $file->getFilename(),
    ]);
}

$archive->extractTo($storage->path('/'), $relativePath, true);

$dataBasePath = $this->getDatabasePath();
file_put_contents(
    $dataBasePath,
    fopen($storage->path($relativePath), 'r')
);

if ($tmpDirectory == $tar && !Str::endsWith($dataBasePath, DIRECTORY_SEPARATOR.$relativePath)) { // in case of db file name from config equals db file name from maxmind
    $storage->delete($relativePath);
} else {
    $storage->deleteDirectory($tmpDirectory); // delete dir and it's files
}
$storage->delete($tar);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants