@@ -42,7 +42,7 @@ class Updater {
4242 * @throws \Exception
4343 */
4444 public function __construct (
45- private string $ baseDir
45+ string $ baseDir,
4646 ) {
4747 $ this ->nextcloudDir = realpath (dirname ($ baseDir ));
4848
@@ -307,22 +307,28 @@ private function getRecursiveDirectoryIterator(string $folder, array $excludedPa
307307 if ($ handle === false ) {
308308 throw new \Exception ('Could not open ' .$ folder );
309309 }
310+
311+ /* Store first level children in an array to avoid trouble if changes happen while iterating */
312+ $ children = [];
310313 while ($ name = readdir ($ handle )) {
311314 if (in_array ($ name , ['. ' , '.. ' ])) {
312315 continue ;
313316 }
314317 if (isset ($ exclusions [$ name ])) {
315318 continue ;
316319 }
320+ $ children [] = $ name ;
321+ }
322+
323+ closedir ($ handle );
324+
325+ foreach ($ children as $ name ) {
317326 $ path = $ folder .'/ ' .$ name ;
318327 if (is_dir ($ path )) {
319328 yield from $ this ->getRecursiveDirectoryIterator ($ path , []);
320329 }
321330 yield $ path => new \SplFileInfo ($ path );
322331 }
323-
324-
325- closedir ($ handle );
326332 }
327333
328334 /**
@@ -381,7 +387,7 @@ public function setMaintenanceMode(bool $state): void {
381387 if ($ dir = getenv ('NEXTCLOUD_CONFIG_DIR ' )) {
382388 $ configFileName = rtrim ($ dir , '/ ' ) . '/config.php ' ;
383389 } else {
384- $ configFileName = $ this ->baseDir . '/.. /config/config.php ' ;
390+ $ configFileName = $ this ->nextcloudDir . '/config/config.php ' ;
385391 }
386392 $ this ->silentLog ('[info] configFileName ' . $ configFileName );
387393
@@ -426,25 +432,26 @@ public function createBackup(): void {
426432 throw new \Exception ('Could not create backup folder location ' );
427433 }
428434
429- foreach ($ this ->getRecursiveDirectoryIterator ($ this ->nextcloudDir , $ excludedElements ) as $ path => $ fileInfo ) {
430- $ fileName = explode ($ this ->nextcloudDir , $ path )[1 ];
435+ foreach ($ this ->getRecursiveDirectoryIterator ($ this ->nextcloudDir , $ excludedElements ) as $ absolutePath => $ fileInfo ) {
436+ $ relativePath = explode ($ this ->nextcloudDir , $ absolutePath )[1 ];
437+ $ relativeDirectory = dirname ($ relativePath );
431438
432439 // Create folder if it doesn't exist
433- if (!file_exists ($ backupFolderLocation . '/ ' . dirname ( $ fileName ) )) {
434- $ state = mkdir ($ backupFolderLocation . '/ ' . dirname ( $ fileName ) , 0750 , true );
440+ if (!file_exists ($ backupFolderLocation . '/ ' . $ relativeDirectory )) {
441+ $ state = mkdir ($ backupFolderLocation . '/ ' . $ relativeDirectory , 0750 , true );
435442 if ($ state === false ) {
436- throw new \Exception ('Could not create folder: ' .$ backupFolderLocation .'/ ' .dirname ( $ fileName ) );
443+ throw new \Exception ('Could not create folder: ' .$ backupFolderLocation .'/ ' .$ relativeDirectory );
437444 }
438445 }
439446
440447 // If it is a file copy it
441448 if ($ fileInfo ->isFile ()) {
442- $ state = copy ($ fileInfo ->getRealPath (), $ backupFolderLocation . $ fileName );
449+ $ state = copy ($ fileInfo ->getRealPath (), $ backupFolderLocation . $ relativePath );
443450 if ($ state === false ) {
444451 $ message = sprintf (
445452 'Could not copy "%s" to "%s" ' ,
446453 $ fileInfo ->getRealPath (),
447- $ backupFolderLocation . $ fileName
454+ $ backupFolderLocation . $ relativePath
448455 );
449456
450457 if (is_readable ($ fileInfo ->getRealPath ()) === false ) {
@@ -455,11 +462,11 @@ public function createBackup(): void {
455462 );
456463 }
457464
458- if (is_writable ($ backupFolderLocation . $ fileName ) === false ) {
465+ if (is_writable ($ backupFolderLocation . $ relativePath ) === false ) {
459466 $ message = sprintf (
460467 '%s. Destination %s is not writable ' ,
461468 $ message ,
462- $ backupFolderLocation . $ fileName
469+ $ backupFolderLocation . $ relativePath
463470 );
464471 }
465472
@@ -746,7 +753,7 @@ public function extractDownload(): void {
746753
747754 // Ensure that the downloaded version is not lower
748755 $ downloadedVersion = $ this ->getVersionByVersionFile (dirname ($ downloadedFilePath ) . '/nextcloud/version.php ' );
749- $ currentVersion = $ this ->getVersionByVersionFile ($ this ->baseDir . '/.. /version.php ' );
756+ $ currentVersion = $ this ->getVersionByVersionFile ($ this ->nextcloudDir . '/version.php ' );
750757 if (version_compare ($ downloadedVersion , $ currentVersion , '< ' )) {
751758 throw new \Exception ('Downloaded version is lower than installed version ' );
752759 }
@@ -774,14 +781,14 @@ public function replaceEntryPoints(): void {
774781 $ content = "<?php \nhttp_response_code(503); \ndie('Update in process.'); " ;
775782 foreach ($ filesToReplace as $ file ) {
776783 $ this ->silentLog ('[info] replace ' . $ file );
777- $ parentDir = dirname ($ this ->baseDir . '/.. / ' . $ file );
784+ $ parentDir = dirname ($ this ->nextcloudDir . '/ ' . $ file );
778785 if (!file_exists ($ parentDir )) {
779786 $ r = mkdir ($ parentDir );
780787 if ($ r !== true ) {
781788 throw new \Exception ('Can \'t create parent directory for entry point: ' . $ file );
782789 }
783790 }
784- $ state = file_put_contents ($ this ->baseDir . '/.. / ' . $ file , $ content );
791+ $ state = file_put_contents ($ this ->nextcloudDir . '/ ' . $ file , $ content );
785792 if ($ state === false ) {
786793 throw new \Exception ('Can \'t replace entry point: ' .$ file );
787794 }
@@ -828,7 +835,7 @@ private function recursiveDelete(string $folder): void {
828835 public function deleteOldFiles (): void {
829836 $ this ->silentLog ('[info] deleteOldFiles() ' );
830837
831- $ shippedAppsFile = $ this ->baseDir . '/.. /core/shipped.json ' ;
838+ $ shippedAppsFile = $ this ->nextcloudDir . '/core/shipped.json ' ;
832839 $ shippedAppsFileContent = file_get_contents ($ shippedAppsFile );
833840 if ($ shippedAppsFileContent === false ) {
834841 throw new \Exception ('core/shipped.json is not available ' );
@@ -854,10 +861,10 @@ public function deleteOldFiles(): void {
854861 $ shippedApps = array_merge ($ shippedApps , $ newShippedApps );
855862 /** @var string $app */
856863 foreach ($ shippedApps as $ app ) {
857- $ this ->recursiveDelete ($ this ->baseDir . '/.. /apps/ ' . $ app );
864+ $ this ->recursiveDelete ($ this ->nextcloudDir . '/apps/ ' . $ app );
858865 }
859866
860- $ configSampleFile = $ this ->baseDir . '/.. /config/config.sample.php ' ;
867+ $ configSampleFile = $ this ->nextcloudDir . '/config/config.sample.php ' ;
861868 if (file_exists ($ configSampleFile )) {
862869 $ this ->silentLog ('[info] config sample exists ' );
863870
@@ -868,7 +875,7 @@ public function deleteOldFiles(): void {
868875 }
869876 }
870877
871- $ themesReadme = $ this ->baseDir . '/.. /themes/README ' ;
878+ $ themesReadme = $ this ->nextcloudDir . '/themes/README ' ;
872879 if (file_exists ($ themesReadme )) {
873880 $ this ->silentLog ('[info] themes README exists ' );
874881
@@ -878,7 +885,7 @@ public function deleteOldFiles(): void {
878885 throw new \Exception ('Could not delete themes README ' );
879886 }
880887 }
881- $ this ->recursiveDelete ($ this ->baseDir . '/.. /themes/example/ ' );
888+ $ this ->recursiveDelete ($ this ->nextcloudDir . '/themes/example/ ' );
882889
883890 // Delete the rest
884891 $ excludedElements = [
@@ -923,19 +930,19 @@ private function moveWithExclusions(string $dataLocation, array $excludedElement
923930 $ fileName = explode ($ dataLocation , $ path )[1 ];
924931
925932 if ($ fileInfo ->isFile ()) {
926- if (!file_exists ($ this ->baseDir . '/.. / ' . dirname ($ fileName ))) {
927- $ state = mkdir ($ this ->baseDir . '/.. / ' . dirname ($ fileName ), 0755 , true );
933+ if (!file_exists ($ this ->nextcloudDir . '/ ' . dirname ($ fileName ))) {
934+ $ state = mkdir ($ this ->nextcloudDir . '/ ' . dirname ($ fileName ), 0755 , true );
928935 if ($ state === false ) {
929- throw new \Exception ('Could not mkdir ' . $ this ->baseDir . '/.. / ' . dirname ($ fileName ));
936+ throw new \Exception ('Could not mkdir ' . $ this ->nextcloudDir . '/ ' . dirname ($ fileName ));
930937 }
931938 }
932- $ state = rename ($ path , $ this ->baseDir . '/.. / ' . $ fileName );
939+ $ state = rename ($ path , $ this ->nextcloudDir . '/ ' . $ fileName );
933940 if ($ state === false ) {
934941 throw new \Exception (
935942 sprintf (
936943 'Could not rename %s to %s ' ,
937944 $ path ,
938- $ this ->baseDir . '/.. / ' . $ fileName
945+ $ this ->nextcloudDir . '/ ' . $ fileName
939946 )
940947 );
941948 }
0 commit comments