Skip to content

fix(cache): preserve root directory and handle empty folder on filesystem flush - #14059

Merged
HarshMN2345 merged 2 commits into
appwrite:fix-cache-filesystem-flushfrom
Spandan-Mishra:fix-14007-cache-filesystem-flush-empty-and-rmdir-bug
Oct 15, 2026
Merged

HarshMN2345 merged 2 commits into
appwrite:fix-cache-filesystem-flushfrom
Spandan-Mishra:fix-14007-cache-filesystem-flush-empty-and-rmdir-bug

Conversation

@Spandan-Mishra

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes three issues in Utopia\Cache\Adapter\Filesystem::flush():

  1. Empty Directory Crash: When flush() was called on an empty cache directory, glob($path . '*', GLOB_MARK) returned []. The error guard if (! $files) evaluated to true, mistakenly treating an empty directory as a glob failure and throwing Exception: Error happened during glob. This is fixed by strictly checking if ($files === false).
  2. Root Directory Deletion: deleteDirectory($path) unconditionally concluded with rmdir($path). When called from flush(), $path is $this->path (the root cache directory), causing flush() to delete the root folder from disk rather than clearing its contents. This is resolved by adding an optional $isRoot = false parameter and calling $this->deleteDirectory($this->path, true), returning true for root while recursively removing emptied subdirectories with rmdir().
  3. Subsequent ping() and Repeated flush() Failures: Preserving the root directory ensures $cache->ping() remains true after flush(), and subsequent flush() calls operate idempotently instead of throwing Exception: <path> must be a directory.

Test Plan

  1. Automated Unit Tests:
    Ran the updated unit test suite covering empty flush, post-flush ping, idempotency, and recursive subdirectory clearing:

    docker compose exec appwrite test packages/cache/tests/FilesystemTest.php

    Output:

    PHPUnit 12.5.35 by Sebastian Bergmann and contributors.
    
    Runtime:       PHP 8.5.10
    Configuration: /usr/src/code/phpunit.xml
    
    .............                                                     13 / 13 (100%)
    
    Time: 00:03.043, Memory: 26.00 MB
    
    OK (13 tests, 44 assertions)
    
  2. Sibling Adapter Pool Test:

    docker compose exec appwrite test packages/cache/tests/PoolTest.php
    # OK (10 tests, 32 assertions)
  3. Static Analysis & Monorepo Validation:

    docker compose exec appwrite vendor/bin/phpstan analyse -l 4 packages/cache/src/Adapter/Filesystem.php packages/cache/tests/FilesystemTest.php
    # [OK] No errors
    
    docker compose exec appwrite bin/monorepo validate
    # all packages valid
    

Related PRs and Issues

Fixes #14007

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@ghost

ghost commented Oct 1, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The deletion-failure bug is fixed, but the new regression test needs a guard for root environments that cannot drop privileges.

Updates filesystem cache flushing to accept empty directories and preserve the cache root while removing its contents. Deletion failures now propagate through recursive calls instead of being reported as success. Regression tests cover root preservation, repeated flushing, health checks, nested entries, and deletion failure.

Latest changes: The newest commits propagate unlink and recursive deletion failures and add a permission-based regression test.

Verdict New comments Fixed Still open
💬 Commented 1 1 0

Note

@Spandan-Mishra does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

Finding Where
🟡 Skip the permission test when root cannot drop privileges packages/cache/tests/FilesystemTest.php:124
Fix with agent prompt
### Issue 1
packages/cache/tests/FilesystemTest.php:124-130
**Skip the permission test when root cannot drop privileges**

When running as root without the POSIX extension, or when `posix_seteuid()` fails, chmod does not prevent deletion and this assertion fails even with the correct implementation. Please skip the test when an unprivileged execution context cannot be established; the package does not require ext-posix.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 2
File Change
packages/cache/src/Adapter/Filesystem.php Preserves the root, accepts empty glob results, and returns false when entry deletion fails.
packages/cache/tests/FilesystemTest.php Adds regression coverage for successful, repeated, and permission-denied flushes.
✅ Fixed since the last review · 1
  • Preserve failure reporting when cache entries cannot be deleted · packages/cache/src/Adapter/Filesystem.php:179

Reviewed the commits since dea2cd2 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

Track deletion status across entries during directory traversal and
return false when files or subdirectories cannot be deleted.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

Comment on lines +124 to +130
if ($isRoot && function_exists('posix_seteuid')) {
$origEuid = posix_geteuid();
$switched = @posix_seteuid(1000);
}

try {
$this->assertFalse($cache->flush());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip the permission test when root cannot drop privileges

When running as root without the POSIX extension, or when posix_seteuid() fails, chmod does not prevent deletion and this assertion fails even with the correct implementation. Please skip the test when an unprivileged execution context cannot be established; the package does not require ext-posix.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/tests/FilesystemTest.php
Line: 124-130

Comment:
**Skip the permission test when root cannot drop privileges**

When running as root without the POSIX extension, or when `posix_seteuid()` fails, chmod does not prevent deletion and this assertion fails even with the correct implementation. Please skip the test when an unprivileged execution context cannot be established; the package does not require ext-posix.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@Spandan-Mishra Spandan-Mishra changed the title fix(cache): preserve root directory and handle empty folder on filesy… fix(cache): preserve root directory and handle empty folder on filesystem Oct 1, 2026
@Spandan-Mishra Spandan-Mishra changed the title fix(cache): preserve root directory and handle empty folder on filesystem fix(cache): preserve root directory and handle empty folder on filesystem flush Oct 1, 2026
@HarshMN2345
HarshMN2345 changed the base branch from main to fix-cache-filesystem-flush October 15, 2026 07:21
@HarshMN2345
HarshMN2345 merged commit 45641ad into appwrite:fix-cache-filesystem-flush Oct 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling flush() on a cache backed by the Filesystem adapter leads runtime failures

2 participants