-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/session: add recursive session cleanup for dirname > 0 #21491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae2f866
add recursive session cleanup for dirname > 0
jorgsowa 49b45d7
test: move tests to distinct directory
jorgsowa 0548dbc
merge directory traversal loops
jorgsowa 5a65c96
Added all comments and improved the flow of function
jorgsowa a6facb5
chore: move time into the loop
jorgsowa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --TEST-- | ||
| session GC cleans expired sessions with save_path dirdepth=2 (two subdir levels) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include(__DIR__ . '/../skipif.inc'); ?> | ||
| --INI-- | ||
| session.gc_probability=0 | ||
| session.gc_maxlifetime=10 | ||
| --FILE-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_dirdepth2_test'; | ||
| @mkdir($base); | ||
| @mkdir("$base/a"); | ||
| @mkdir("$base/a/b"); | ||
|
|
||
| session_save_path("2;$base"); | ||
|
|
||
| $stale_id = 'abcdefghijklmnopqrstuvwx'; | ||
| $stale_file = "$base/a/b/sess_$stale_id"; | ||
| file_put_contents($stale_file, 'user|s:5:"alice";'); | ||
| touch($stale_file, time() - 100); | ||
|
|
||
| session_id('ab000000000000000000000000'); | ||
| session_start(); | ||
| $result = session_gc(); | ||
| session_destroy(); | ||
|
|
||
| echo "session_gc() return value: "; | ||
| var_dump($result); | ||
|
|
||
| echo "expired file removed: "; | ||
| var_dump(!file_exists($stale_file)); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_dirdepth2_test'; | ||
| @unlink("$base/a/b/sess_ab000000000000000000000000"); | ||
| @rmdir("$base/a/b"); | ||
| @rmdir("$base/a"); | ||
| @rmdir($base); | ||
| ?> | ||
| --EXPECT-- | ||
| session_gc() return value: int(1) | ||
| expired file removed: bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| --TEST-- | ||
| session GC correctly cleans expired sessions when save_path dirdepth > 0 | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include(__DIR__ . '/../skipif.inc'); ?> | ||
| --INI-- | ||
| session.gc_probability=0 | ||
| session.gc_maxlifetime=1 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $base = __DIR__ . '/gc_dirdepth_test'; | ||
| @mkdir($base); | ||
| @mkdir("$base/a"); | ||
|
|
||
| // ── Part 1: dirdepth=1 | ||
| session_save_path("1;$base"); | ||
|
|
||
| $stale_id = 'abcdefghijklmnopqrstuvwx'; | ||
| $stale_file = "$base/a/sess_$stale_id"; | ||
| file_put_contents($stale_file, 'user|s:5:"alice";'); | ||
| touch($stale_file, time() - 100); // 100 s old; gc_maxlifetime=1 → must be GC'd | ||
|
|
||
| session_id('a0000000000000000000000000'); | ||
| session_start(); | ||
| $result_depth = session_gc(); | ||
| session_destroy(); | ||
| $depth_file_gone = !file_exists($stale_file); | ||
|
|
||
| // ── Part 2: dirdepth=0 | ||
| session_save_path($base); | ||
|
|
||
| $flat_id = 'bbcdefghijklmnopqrstuvwx'; | ||
| $flat_file = "$base/sess_$flat_id"; | ||
| file_put_contents($flat_file, 'user|s:5:"alice";'); | ||
| touch($flat_file, time() - 100); | ||
|
|
||
| session_start(); | ||
| $result_flat = session_gc(); | ||
| session_destroy(); | ||
| $flat_file_gone = !file_exists($flat_file); | ||
|
|
||
| echo "dirdepth=1 — session_gc() return value: "; | ||
| var_dump($result_depth); | ||
|
|
||
| echo "dirdepth=1 — expired session file removed: "; | ||
| var_dump($depth_file_gone); | ||
|
|
||
| echo "dirdepth=0 — session_gc() return value: "; | ||
| var_dump($result_flat); | ||
|
|
||
| echo "dirdepth=0 — expired session file removed: "; | ||
| var_dump($flat_file_gone); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_dirdepth_test'; | ||
| @unlink("$base/a/sess_abcdefghijklmnopqrstuvwx"); | ||
| @unlink("$base/a/sess_a0000000000000000000000000"); | ||
| @rmdir("$base/a"); | ||
| @rmdir($base); | ||
| ?> | ||
| --EXPECT-- | ||
| dirdepth=1 — session_gc() return value: int(1) | ||
| dirdepth=1 — expired session file removed: bool(true) | ||
| dirdepth=0 — session_gc() return value: int(1) | ||
| dirdepth=0 — expired session file removed: bool(true) |
54 changes: 54 additions & 0 deletions
54
ext/session/tests/mod_files/gc_dirdepth_multi_subdir_count.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --TEST-- | ||
| session GC accumulates correct total count across multiple subdirs, including empty ones (dirdepth=1) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include(__DIR__ . '/../skipif.inc'); ?> | ||
| --INI-- | ||
| session.gc_probability=0 | ||
| session.gc_maxlifetime=10 | ||
| --FILE-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_multi_subdir_test'; | ||
| @mkdir($base); | ||
| @mkdir("$base/a"); | ||
| @mkdir("$base/b"); | ||
| @mkdir("$base/c"); | ||
| @mkdir("$base/d"); // empty subdir | ||
|
|
||
| session_save_path("1;$base"); | ||
|
|
||
| $files = [ | ||
| "$base/a/sess_aexpired0000000000000000", | ||
| "$base/b/sess_bexpired0000000000000000", | ||
| "$base/c/sess_cexpired0000000000000000", | ||
| ]; | ||
| foreach ($files as $f) { | ||
| file_put_contents($f, 'user|s:5:"alice";'); | ||
| touch($f, time() - 100); | ||
| } | ||
|
|
||
| session_id('a0000000000000000000000000'); | ||
| session_start(); | ||
| $result = session_gc(); | ||
| session_destroy(); | ||
|
|
||
| echo "session_gc() return value: "; | ||
| var_dump($result); | ||
|
|
||
| echo "all expired files removed: "; | ||
| var_dump(!file_exists($files[0]) && !file_exists($files[1]) && !file_exists($files[2])); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_multi_subdir_test'; | ||
| @unlink("$base/a/sess_a0000000000000000000000000"); | ||
| @rmdir("$base/a"); | ||
| @rmdir("$base/b"); | ||
| @rmdir("$base/c"); | ||
| @rmdir("$base/d"); | ||
| @rmdir($base); | ||
| ?> | ||
| --EXPECT-- | ||
| session_gc() return value: int(3) | ||
| all expired files removed: bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| --TEST-- | ||
| session GC deletes only expired sess_* files and leaves all other files untouched (dirdepth=1) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include(__DIR__ . '/../skipif.inc'); ?> | ||
| --INI-- | ||
| session.gc_probability=0 | ||
| session.gc_maxlifetime=10 | ||
| --FILE-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_selective_test'; | ||
| @mkdir($base); | ||
| @mkdir("$base/a"); | ||
|
|
||
| session_save_path("1;$base"); | ||
|
|
||
| $expired = "$base/a/sess_aexpired0000000000000000"; | ||
| $fresh = "$base/a/sess_afresh000000000000000000"; | ||
| $other = "$base/a/other_file"; | ||
|
|
||
| file_put_contents($expired, 'user|s:5:"alice";'); | ||
| touch($expired, time() - 100); // 100 s old > gc_maxlifetime=10 → deleted | ||
|
|
||
| file_put_contents($fresh, 'user|s:5:"alice";'); | ||
| touch($fresh, time() - 1); // 1 s old < gc_maxlifetime=10 → kept | ||
|
|
||
| file_put_contents($other, 'untouched'); | ||
| touch($other, time() - 100); // old but no sess_ prefix → kept | ||
|
|
||
| session_id('a0000000000000000000000000'); // first char 'a' → $base/a/ | ||
| session_start(); | ||
| $result = session_gc(); // int(1): exactly one deletion proves selectivity | ||
| session_destroy(); | ||
|
|
||
| echo "session_gc() return value: "; | ||
| var_dump($result); | ||
|
|
||
| echo "expired sess_ file removed: "; | ||
| var_dump(!file_exists($expired)); | ||
|
|
||
| echo "other file kept: "; | ||
| var_dump(file_exists($other)); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $base = __DIR__ . '/gc_selective_test'; | ||
| @unlink("$base/a/sess_afresh000000000000000000"); | ||
| @unlink("$base/a/sess_a0000000000000000000000000"); | ||
| @unlink("$base/a/other_file"); | ||
| @rmdir("$base/a"); | ||
| @rmdir($base); | ||
| ?> | ||
| --EXPECT-- | ||
| session_gc() return value: int(1) | ||
| expired sess_ file removed: bool(true) | ||
| other file kept: bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.