@@ -1843,7 +1843,8 @@ public function download_wp( $subdir = '', $version = '' ): void {
18431843
18441844 $ dest_dir = rtrim ( $ this ->variables ['RUN_DIR ' ], '/ \\' ) . ( $ subdir ? DIRECTORY_SEPARATOR . $ subdir : '' );
18451845
1846- if ( $ subdir ) {
1846+ // The scenario may have created files in the subdirectory already.
1847+ if ( $ subdir && ! is_dir ( $ dest_dir ) ) {
18471848 mkdir ( $ dest_dir , 0777 , true /*recursive*/ );
18481849 }
18491850
@@ -1937,6 +1938,11 @@ public function install_wp( $subdir = '', $version = '' ): void {
19371938 $ this ->create_db ();
19381939 }
19391940 $ this ->create_run_dir ();
1941+
1942+ // Files the scenario created before installing belong to that scenario only, and must not
1943+ // end up in the installation cache that later scenarios are set up from.
1944+ $ scenario_files = self ::list_dir ( '' !== $ subdir ? ( $ this ->variables ['RUN_DIR ' ] . "/ $ subdir " ) : $ this ->variables ['RUN_DIR ' ] );
1945+
19401946 $ this ->download_wp ( $ subdir , $ version );
19411947 $ this ->create_config ( $ subdir , $ config_extra_php );
19421948
@@ -1969,6 +1975,7 @@ public function install_wp( $subdir = '', $version = '' ): void {
19691975 }
19701976
19711977 self ::dir_diff_copy ( $ run_dir , self ::$ cache_dir , $ install_cache_path );
1978+ self ::remove_from_dir ( $ install_cache_path , $ scenario_files );
19721979
19731980 if ( 'sqlite ' !== self ::$ db_type ) {
19741981 $ mysqldump_binary = Utils \get_sql_dump_command ();
@@ -2239,6 +2246,61 @@ private static function log_run_times_after_scenario( $scope ): void {
22392246 }
22402247 }
22412248
2249+ /**
2250+ * List the files and directories within a directory, relative to it.
2251+ *
2252+ * Symbolic links are listed, but not followed.
2253+ *
2254+ * @param string $dir
2255+ * @return array<string, bool> Whether each entry is a symbolic link, keyed by relative path, directories after
2256+ * their contents. Empty if the directory does not exist.
2257+ */
2258+ private static function list_dir ( $ dir ): array {
2259+ $ dir = rtrim ( $ dir , '/ \\' );
2260+ if ( ! is_dir ( $ dir ) ) {
2261+ return [];
2262+ }
2263+
2264+ $ paths = [];
2265+ $ iterator = new \RecursiveIteratorIterator (
2266+ new \RecursiveDirectoryIterator ( $ dir , \FilesystemIterator::SKIP_DOTS ),
2267+ \RecursiveIteratorIterator::CHILD_FIRST
2268+ );
2269+
2270+ /**
2271+ * @var \SplFileInfo $item
2272+ */
2273+ foreach ( $ iterator as $ item ) {
2274+ $ paths [ substr ( $ item ->getPathname (), strlen ( $ dir ) + 1 ) ] = $ item ->isLink ();
2275+ }
2276+
2277+ return $ paths ;
2278+ }
2279+
2280+ /**
2281+ * Remove the given files from a directory, as well as the given directories once they are empty.
2282+ *
2283+ * A copy of what a symbolic link pointed to is removed with all its contents.
2284+ *
2285+ * @param string $dir
2286+ * @param array<string, bool> $paths Whether each entry was a symbolic link, keyed by relative path, directories
2287+ * after their contents.
2288+ */
2289+ private static function remove_from_dir ( $ dir , array $ paths ): void {
2290+ foreach ( $ paths as $ path => $ was_link ) {
2291+ $ full_path = "{$ dir }/ {$ path }" ;
2292+
2293+ if ( $ was_link && is_dir ( $ full_path ) && ! is_link ( $ full_path ) ) {
2294+ self ::remove_dir ( $ full_path );
2295+ } elseif ( is_dir ( $ full_path ) && ! is_link ( $ full_path ) ) {
2296+ // Only succeeds for directories that are empty by now.
2297+ @rmdir ( $ full_path );
2298+ } elseif ( file_exists ( $ full_path ) || is_link ( $ full_path ) ) {
2299+ unlink ( $ full_path );
2300+ }
2301+ }
2302+ }
2303+
22422304 /**
22432305 * Copy files in updated directory that are not in source directory to copy directory. ("Incremental backup".)
22442306 * Note: does not deal with changed files (ie does not compare file contents for changes), for speed reasons.
0 commit comments