Skip to content

Commit 5acdfe6

Browse files
committed
Add FileGlobals too
1 parent 48980a1 commit 5acdfe6

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

allowed_bindings.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ bind! {
234234
executor_globals_offset,
235235
core_globals_offset,
236236
sapi_globals_offset,
237+
php_file_globals,
238+
file_globals,
237239
TRACK_VARS_POST,
238240
TRACK_VARS_GET,
239241
TRACK_VARS_COOKIE,

src/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "php.h"
1818

1919
#include "ext/standard/info.h"
20+
#include "ext/standard/file.h"
2021
#include "zend_exceptions.h"
2122
#include "zend_inheritance.h"
2223
#include "zend_interfaces.h"

src/zend/globals.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::ffi::{
1212
ext_php_rs_sapi_globals, php_core_globals, sapi_globals_struct, sapi_header_struct,
1313
sapi_headers_struct, sapi_request_info, zend_is_auto_global, TRACK_VARS_COOKIE, TRACK_VARS_ENV,
1414
TRACK_VARS_FILES, TRACK_VARS_GET, TRACK_VARS_POST, TRACK_VARS_REQUEST, TRACK_VARS_SERVER,
15+
php_file_globals, file_globals
1516
};
1617

1718
use crate::types::{ZendHashTable, ZendObject, ZendStr};
@@ -358,13 +359,53 @@ impl SapiRequestInfo {
358359
}
359360
}
360361

362+
/// Stores global variables used in the SAPI.
363+
pub type FileGlobals = php_file_globals;
364+
365+
impl FileGlobals {
366+
/// Returns a reference to the PHP process globals.
367+
///
368+
/// The process globals are guarded by a RwLock. There can be multiple
369+
/// immutable references at one time but only ever one mutable reference.
370+
/// Attempting to retrieve the globals while already holding the global
371+
/// guard will lead to a deadlock. Dropping the globals guard will release
372+
/// the lock.
373+
pub fn get() -> GlobalReadGuard<Self> {
374+
// SAFETY: PHP executor globals are statically declared therefore should never
375+
// return an invalid pointer.
376+
let globals = unsafe { &file_globals };
377+
let guard = FILE_GLOBALS_LOCK.read();
378+
GlobalReadGuard { globals, guard }
379+
}
380+
381+
/// Returns a mutable reference to the PHP executor globals.
382+
///
383+
/// The executor globals are guarded by a RwLock. There can be multiple
384+
/// immutable references at one time but only ever one mutable reference.
385+
/// Attempting to retrieve the globals while already holding the global
386+
/// guard will lead to a deadlock. Dropping the globals guard will release
387+
/// the lock.
388+
pub fn get_mut() -> GlobalWriteGuard<Self> {
389+
// SAFETY: PHP executor globals are statically declared therefore should never
390+
// return an invalid pointer.
391+
let globals = unsafe { &mut file_globals };
392+
let guard = SAPI_GLOBALS_LOCK.write();
393+
GlobalWriteGuard { globals, guard }
394+
}
395+
396+
pub fn stream_wrappers(&self) -> Option<&'static ZendHashTable> {
397+
unsafe { self.stream_wrappers.as_ref() }
398+
}
399+
}
400+
361401
/// Executor globals rwlock.
362402
///
363403
/// PHP provides no indication if the executor globals are being accessed so
364404
/// this is only effective on the Rust side.
365405
static GLOBALS_LOCK: RwLock<()> = const_rwlock(());
366406
static PROCESS_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
367407
static SAPI_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
408+
static FILE_GLOBALS_LOCK: RwLock<()> = const_rwlock(());
368409

369410
/// Wrapper guard that contains a reference to a given type `T`. Dropping a
370411
/// guard releases the lock on the relevant rwlock.

src/zend/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub use function::FunctionEntry;
2020
pub use globals::ExecutorGlobals;
2121
pub use globals::ProcessGlobals;
2222
pub use globals::SapiGlobals;
23+
pub use globals::FileGlobals;
2324
pub use handlers::ZendObjectHandlers;
2425
pub use linked_list::ZendLinkedList;
2526
pub use module::ModuleEntry;

0 commit comments

Comments
 (0)