Skip to content

Commit 9c1aa9b

Browse files
CopilotswissspidyCopilot
authored
Harden SQLite install cache copy operations with file_exists guards (#317)
* Initial plan * Improve SQLite db copying with file_exists guards Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Update src/Context/FeatureContext.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/Context/FeatureContext.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/Context/FeatureContext.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent fd13365 commit 9c1aa9b

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/Context/FeatureContext.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,38 @@ public function install_wp( $subdir = '', $version = '' ): void {
16141614

16151615
$install_cache_path = self::$install_cache_dir . '/install_' . md5( implode( ':', $install_args ) . ':subdir=' . $subdir );
16161616

1617-
if ( file_exists( $install_cache_path ) ) {
1617+
$install_cache_is_valid = is_dir( $install_cache_path )
1618+
&& ( 'sqlite' !== self::$db_type || file_exists( "{$install_cache_path}.sqlite" ) );
1619+
1620+
if ( ! $install_cache_is_valid && file_exists( $install_cache_path ) ) {
1621+
if ( is_dir( $install_cache_path ) ) {
1622+
$iterator = new \RecursiveIteratorIterator(
1623+
new \RecursiveDirectoryIterator( $install_cache_path, \FilesystemIterator::SKIP_DOTS ),
1624+
\RecursiveIteratorIterator::CHILD_FIRST
1625+
);
1626+
foreach ( $iterator as $fileinfo ) {
1627+
if ( $fileinfo->isDir() ) {
1628+
rmdir( $fileinfo->getPathname() );
1629+
} else {
1630+
unlink( $fileinfo->getPathname() );
1631+
}
1632+
}
1633+
rmdir( $install_cache_path );
1634+
} else {
1635+
unlink( $install_cache_path );
1636+
}
1637+
1638+
$sqlite_cache = "{$install_cache_path}.sqlite";
1639+
if ( file_exists( $sqlite_cache ) && is_file( $sqlite_cache ) ) {
1640+
unlink( $sqlite_cache );
1641+
}
1642+
1643+
$sql_cache = "{$install_cache_path}.sql";
1644+
if ( file_exists( $sql_cache ) && is_file( $sql_cache ) ) {
1645+
unlink( $sql_cache );
1646+
}
1647+
}
1648+
if ( $install_cache_is_valid ) {
16181649
self::copy_dir( $install_cache_path, $run_dir );
16191650

16201651
// This is the sqlite equivalent of restoring a database dump in MySQL
@@ -1627,7 +1658,9 @@ public function install_wp( $subdir = '', $version = '' ): void {
16271658
} else {
16281659
$this->proc( 'wp core install', $install_args, $subdir )->run_check();
16291660

1630-
mkdir( $install_cache_path );
1661+
if ( ! is_dir( $install_cache_path ) ) {
1662+
mkdir( $install_cache_path );
1663+
}
16311664

16321665
self::dir_diff_copy( $run_dir, self::$cache_dir, $install_cache_path );
16331666

@@ -1646,7 +1679,13 @@ public function install_wp( $subdir = '', $version = '' ): void {
16461679

16471680
if ( 'sqlite' === self::$db_type ) {
16481681
// This is the sqlite equivalent of creating a database dump in MySQL
1649-
copy( "$run_dir/wp-content/database/.ht.sqlite", "{$install_cache_path}.sqlite" );
1682+
$sqlite_source = "$run_dir/wp-content/database/.ht.sqlite";
1683+
if ( file_exists( $sqlite_source ) ) {
1684+
copy( $sqlite_source, "{$install_cache_path}.sqlite" );
1685+
} elseif ( file_exists( "{$install_cache_path}.sqlite" ) ) {
1686+
// Ensure we don't keep a stale cached SQLite DB if the source wasn't produced
1687+
unlink( "{$install_cache_path}.sqlite" );
1688+
}
16501689
}
16511690
}
16521691
}

0 commit comments

Comments
 (0)