Skip to content

Commit

Permalink
Fix move step
Browse files Browse the repository at this point in the history
The iterator gets gets messed up when modifying the directory. Thus we copy() instead of rename() and remove the whole folders/files afterwards in a separate iteration.
See #509 and #158 for details.

Signed-off-by: CaCO3 <caco@ruinelli.ch>
  • Loading branch information
CaCO3 committed Oct 29, 2023
1 parent f2ec935 commit 223671d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
24 changes: 20 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,20 +997,36 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($path, $this->baseDir . '/../' . $fileName);
$state = copy($path, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
'Could not copy %s to %s',
$path,
$this->baseDir . '/../' . $fileName
)
);
}
}
if ($fileInfo->isDir()) {
$this->recursiveDelete($path);
}

// Cleanup (delete $path)
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);

// Exclude the exclusions
if (isset($folderStructure[0])) {
if (array_search($folderStructure[0], $excludedElements) !== false) {
continue;
}
} else {
if (array_search($fileName, $excludedElements) !== false) {
continue;
}
}

$this->recursiveDelete($path);
}
}

Expand Down
24 changes: 20 additions & 4 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,20 +958,36 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
throw new \Exception('Could not mkdir ' . $this->baseDir . '/../' . dirname($fileName));
}
}
$state = rename($path, $this->baseDir . '/../' . $fileName);
$state = copy($path, $this->baseDir . '/../' . $fileName);
if ($state === false) {
throw new \Exception(
sprintf(
'Could not rename %s to %s',
'Could not copy %s to %s',
$path,
$this->baseDir . '/../' . $fileName
)
);
}
}
if ($fileInfo->isDir()) {
$this->recursiveDelete($path);
}

// Cleanup (delete $path)
foreach ($this->getRecursiveDirectoryIterator($dataLocation) as $path => $fileInfo) {
$fileName = explode($dataLocation, $path)[1];
$folderStructure = explode('/', $fileName, -1);

// Exclude the exclusions
if (isset($folderStructure[0])) {
if (array_search($folderStructure[0], $excludedElements) !== false) {
continue;
}
} else {
if (array_search($fileName, $excludedElements) !== false) {
continue;
}
}

$this->recursiveDelete($path);
}
}

Expand Down

0 comments on commit 223671d

Please sign in to comment.