Skip to content

Commit 18eff74

Browse files
committed
Add SQLite storage lock
1 parent abf0dac commit 18eff74

2 files changed

Lines changed: 428 additions & 0 deletions

File tree

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
/**
4+
* Manages locking for the WordPress SQLite database storage.
5+
*
6+
* Locking uses a dedicated empty SQLite database and a maintenance file:
7+
* 1. An exclusive lock is acquired on the locking database at ".ht.sqlite.lock".
8+
* 2. A maintenance marker file is created at ".ht.sqlite.maintenance":
9+
* - When requests see the file, they use the locking database.
10+
* - Its absence provides a fast path for normal traffic.
11+
* 3. When the lock is released, the maintenance file is automatically removed.
12+
*
13+
* Database locking uses a PDO SQLite connection:
14+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
15+
*
16+
* Filesystem warnings are suppressed to avoid exposing database paths:
17+
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
18+
*/
19+
class WP_SQLite_Storage {
20+
/**
21+
* Filename of the SQLite locking database.
22+
*/
23+
private const LOCK_FILENAME = '.ht.sqlite.lock';
24+
25+
/**
26+
* Filename of the marker that makes requests check the storage lock.
27+
*/
28+
private const MAINTENANCE_FILENAME = '.ht.sqlite.maintenance';
29+
30+
/**
31+
* Managed database root directory.
32+
*
33+
* @var string
34+
*/
35+
private $database_root;
36+
37+
/**
38+
* Absolute path of the SQLite locking database.
39+
*
40+
* @var string
41+
*/
42+
private $lock_path;
43+
44+
/**
45+
* Absolute path of the storage maintenance marker.
46+
*
47+
* @var string
48+
*/
49+
private $maintenance_path;
50+
51+
/**
52+
* Connection holding the exclusive transaction on the SQLite locking database.
53+
*
54+
* @var PDO|null
55+
*/
56+
private $storage_lock_connection;
57+
58+
/**
59+
* Time to wait for the SQLite storage lock, in milliseconds.
60+
*
61+
* @var int
62+
*/
63+
private $database_lock_timeout = 10000;
64+
65+
/**
66+
* Create a SQLite storage lock.
67+
*
68+
* @param string|null $database_root Managed database root. Defaults to FQDBDIR.
69+
*/
70+
public function __construct( ?string $database_root = null ) {
71+
$this->database_root = trailingslashit( $database_root ?? FQDBDIR );
72+
$this->lock_path = $this->database_root . self::LOCK_FILENAME;
73+
$this->maintenance_path = $this->database_root . self::MAINTENANCE_FILENAME;
74+
}
75+
76+
/**
77+
* Lock the database storage for maintenance.
78+
*
79+
* The locking mechanism does the following:
80+
* - Waits for a lock held by another process, up to the database lock timeout.
81+
* - Marks maintenance as active so new requests wait.
82+
*
83+
* The lock is released when unlock() is called or the process ends.
84+
*
85+
* @throws RuntimeException When the lock cannot be acquired.
86+
*/
87+
public function lock(): void {
88+
// Reuse a lock already held by this instance.
89+
if ( null !== $this->storage_lock_connection ) {
90+
return;
91+
}
92+
93+
// Serialize maintenance through the dedicated locking database.
94+
try {
95+
$this->ensure_database( $this->lock_path );
96+
$connection = new PDO( 'sqlite:' . $this->lock_path );
97+
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
98+
$connection->exec( 'PRAGMA busy_timeout = ' . $this->database_lock_timeout );
99+
$connection->exec( 'BEGIN EXCLUSIVE' );
100+
$this->storage_lock_connection = $connection;
101+
} catch ( Throwable $exception ) {
102+
throw new RuntimeException( 'Failed to acquire the SQLite storage lock.', 0, $exception );
103+
}
104+
105+
try {
106+
// Ensure the maintenance marker exists so new requests use the locking database.
107+
if ( ! @is_file( $this->maintenance_path ) ) {
108+
if ( false === @file_put_contents( $this->maintenance_path, '' ) ) {
109+
throw new RuntimeException( 'Failed to create the SQLite storage maintenance marker.' );
110+
}
111+
@chmod( $this->maintenance_path, 0600 );
112+
}
113+
} catch ( Throwable $exception ) {
114+
$this->unlock();
115+
throw $exception;
116+
}
117+
}
118+
119+
/**
120+
* Unlock the storage.
121+
*/
122+
public function unlock(): void {
123+
if ( null !== $this->storage_lock_connection ) {
124+
// Remove the marker before releasing the lock that protects it.
125+
@unlink( $this->maintenance_path );
126+
$this->storage_lock_connection = null;
127+
}
128+
}
129+
130+
/**
131+
* Ensure that a database and its protected directory exist.
132+
*
133+
* @param string $database_path Absolute database path.
134+
*/
135+
private function ensure_database( string $database_path ): void {
136+
$this->ensure_protected_directory( dirname( $database_path ) );
137+
138+
if ( ! @is_file( $database_path ) ) {
139+
// Create an empty database file with restricted permissions.
140+
$database_handle = @fopen( $database_path, 'c' );
141+
if ( false === $database_handle ) {
142+
throw new RuntimeException( 'Failed to create the SQLite database file.' );
143+
}
144+
fclose( $database_handle );
145+
@chmod( $database_path, 0600 );
146+
}
147+
}
148+
149+
/**
150+
* Ensure that a database directory exists and deny direct access.
151+
*
152+
* @param string $directory Absolute directory path.
153+
*/
154+
private function ensure_protected_directory( string $directory ): void {
155+
if ( ! @is_dir( $directory ) ) {
156+
// Create the path one directory at a time to avoid changing the process-wide umask.
157+
$missing_directories = array();
158+
for ( $path = untrailingslashit( $directory ); ! @is_dir( $path ); $path = dirname( $path ) ) {
159+
$missing_directories[] = $path;
160+
if ( dirname( $path ) === $path ) {
161+
break;
162+
}
163+
}
164+
165+
foreach ( array_reverse( $missing_directories ) as $path ) {
166+
if ( ! @mkdir( $path, 0700 ) && ! @is_dir( $path ) ) {
167+
throw new RuntimeException( 'Failed to create the SQLite database directory.' );
168+
}
169+
@chmod( $path, 0700 );
170+
}
171+
}
172+
173+
$this->ensure_file( trailingslashit( $directory ) . '.htaccess', 'DENY FROM ALL' );
174+
$this->ensure_file( trailingslashit( $directory ) . 'index.php', '<?php // Silence is golden.' );
175+
}
176+
177+
/**
178+
* Create a protected file when it does not already exist.
179+
*
180+
* @param string $path Absolute file path.
181+
* @param string $contents File contents.
182+
*/
183+
private function ensure_file( string $path, string $contents ): void {
184+
if ( @is_file( $path ) ) {
185+
return;
186+
}
187+
if ( false === @file_put_contents( $path, $contents ) ) {
188+
throw new RuntimeException( 'Failed to create SQLite database protection file.' );
189+
}
190+
@chmod( $path, 0600 );
191+
}
192+
}

0 commit comments

Comments
 (0)