Skip to content

Keep local items whose stat fails instead of deleting them. - #11359

Merged
mgallien merged 4 commits into
masterfrom
bugfix/statfail
Nov 25, 2026
Merged

mgallien merged 4 commits into
masterfrom
bugfix/statfail

Conversation

@camilasan

@camilasan camilasan commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Resolves

A failed stat on an existing local file was dropped from discovery and then propagated as a remote delete, so the server trashed the file.

Errors found in the logs (Windows):
"WindowsError: -7fe0fffb: An invalid name request was made. The name requested cannot be retrieved at this time."

"WindowsError: -7ff8fe7a: The operation is only supported on files under a cloud sync root."

Summary

The item is kept and flagged with is_stat_error so discovery ignores it for that sync.

To test this (Windows)

I was only able to test this by quitting the client and then running:
icacls "E:\Nextcloud\TestFolder\test.txt" /deny "<username>:(S)"

Start the client again and then the ignore instruction will be in the logs:

2026-11-18 16:46:18:018 [ info nextcloud.sync.propagator desktop\src\libsync\owncloudpropagator.h:214 ]: Starting CSyncEnums::CSYNC_INSTRUCTION_IGNORE propagation of "TestFolder/test.txt" by OCC::PropagateIgnoreJob(0x21280df8a30)
2026-11-18 16:46:18:018 [ warning nextcloud.sync.propagator desktop\src\libsync\owncloudpropagator.cpp:946 ]: Could not complete propagation of "TestFolder/test.txt" by OCC::OwncloudPropagator(0x212ff95f700) with status OCC::SyncFileItem::FileIgnored and error: "Could not read local file. It will be checked again on a later sync."

Checklist

AI (if applicable)

@camilasan

Copy link
Copy Markdown
Member Author

/backport to stable-35.0

@camilasan
camilasan marked this pull request as ready for review November 18, 2026 15:51

@i2h3 i2h3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I cannot test on Windows and I am not familiar with the classic sync engine in depth. So take these agent findings with a grain of salt for your consideration. I intentionally did not review with a request for changes.

  1. Unreadable files are still deleted on the server on some file systems (src/libsync/discoveryphase.cpp:L381-383, src/csync/vio/csync_vio_local_unix.cpp:L84-109)

    • What this is: On Linux and macOS, csync_vio_local_readdir() first takes the file type from the directory listing (d_type). Then it calls lstat(), which asks the OS for size, time and file ID, and DiscoverySingleLocalDirectoryJob::run() drops every entry whose type is still ItemTypeSkip.
    • Problem: Some file systems leave d_type as DT_UNKNOWN, for example XFS made with ftype=0 and some network mounts, and readdir(3) says apps must handle that. There a failed lstat() leaves the type at ItemTypeSkip, so the entry is dropped before anyone reads is_stat_error, and the server copy is deleted just like before this PR.
    • Fix: Keep entries with a stat error: if (dirent->type == ItemTypeSkip && !dirent->is_stat_error) continue;. The new branch in processFileAnalyzeLocalInfo() then ignores them.
    • If skipped: The bug this PR fixes stays open for everyone whose sync folder is on such a file system.
  2. A folder deleted on the server still takes the unreadable file with it (src/libsync/discovery.cpp:L1388-1394)

    • What this is: Discovery is the step that compares local files, server files and the sync database and decides what to do. When it ignores a child it sets _childIgnored = true, and processSubJobs() (L2410) then turns a REMOVE of the parent folder into NONE.
    • Problem: The new branch does not set this flag. Example: someone deletes Project/ on the server while Project/draft.docx cannot be read here, so Project/ stays REMOVE, the propagator (the part that carries out the decisions) skips ignored items inside a removed folder (owncloudpropagator.cpp:L573), and PropagateLocalRemove deletes the whole folder with draft.docx in it.
    • Fix: Add _childIgnored = true; to the branch, like handleExcluded() (L516) and processBlacklisted() (L2171) do. In the Unix test, remove sub on the server before the second sync and check with ItemCompletedSpy that sub does not complete as REMOVE.
    • If skipped: Edits in that file that were not uploaded yet are lost with the folder. Stopping that kind of loss is the point of this PR.
  3. "It will be checked again on a later sync" is not true for most syncs (src/libsync/discovery.cpp:L1389-1391)

    • What this is: When the file watcher works, most syncs only scan the local paths on the LocalDiscoveryTracker list (paths that changed or failed last time) and read everything else from the database. A full local scan runs once an hour by default (ConfigFile::fullLocalDiscoveryInterval()).
    • Problem: LocalDiscoveryTracker::slotItemCompleted() counts FileIgnored as a success and drops the path from that list. So the next sync reads the file from the database, and an edit made while stat failed waits for the hourly scan or for the next write to that file.
    • Fix: Set SyncFileItem::SoftError instead of FileIgnored, like the "open in editor" case at L1357. A soft error puts the path back on the list, and blacklistUpdate() does not blacklist it because it has no HTTP code.
    • If skipped: With the short-lived Windows errors from the PR description, an edit can stay only on this computer for up to an hour.
  4. The Windows test most likely passes without the fix (test/testlocaldiscovery.cpp:L106-114)

    • What this is: The test denies "read attributes" (RA) on a.txt with icacls, so that csync_vio_local_stat() should fail.
    • Problem: The PR description says only /deny (S) reproduced the bug. I think the reason is that NTFS still grants read attributes on a file to anyone who may list its folder, and CreateFileW with access 0 only asks for that plus SYNCHRONIZE, so the open works, stat succeeds and the new code never runs.
    • Fix: Deny (S) as in the manual test. Revert the two csync_vio_local_*.cpp lines once and confirm the test fails.
    • If skipped: Windows, where the bug was reported, has no test that fails when the fix breaks.

Comment thread src/csync/vio/csync_vio_local_unix.cpp Resolved
Comment thread src/libsync/discoveryphase.cpp Resolved
@camilasan
camilasan force-pushed the bugfix/statfail branch 4 times, most recently from 20465bc to e53675c Compare November 19, 2026 12:55
Comment thread src/csync/vio/csync_vio_local_unix.cpp Resolved

@i2h3 i2h3 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is your call which to consider.

Must change

  1. Subfolders of an unreadable folder are deleted on the server (src/libsync/discovery.cpp:L1388-1395)

    • What this is: For an item whose stat failed, the new branch calls finalize(), which walks into folders. ItemTypeSkipError has isDirectory == false, so the walk uses ParentDontExist, which means "this folder no longer exists on this computer".
    • Problem: Inside that walk every synced child looks deleted here and gets REMOVE toward the server. Files survive because the folder's ignore job fails and cancels them, but a subfolder REMOVE goes to the root's folder deletion list (owncloudpropagator.cpp:L650-654), which still runs after a SoftError (L1717-1750).
    • Fix: Replace finalize(); with Q_EMIT _discoveryData->itemDiscovered(item);, like the VFS suffix ignore at L652-660, so nothing below the unreadable item is touched this sync. Test it in the Unix test: mkdir("sub/inner/deep"), add sub/inner/deep/c.txt, sync, set sub to ReadOwner, sync, and check that sub/inner/deep/c.txt is still on the server before the recovery sync, because the recovery sync uploads it again and hides the loss.
    • If skipped: Every folder one level below an unreadable folder goes to the server trash and is removed from all other devices.
  2. An unreadable placeholder still deletes its file on the server with suffix VFS (src/csync/vio/csync_vio_local_unix.cpp:L107-110, src/libsync/discovery.cpp:L152-155)

    • What this is: Suffix VFS keeps virtual files as name.nextcloud stubs and is used outside Windows when OpenVFS is not available. process() moves the local entry foo.nextcloud onto foo so it matches the database and server entries, but only when isVirtualFile is true, which VfsSuffix::statTypeVirtualFile() sets from the name.
    • Problem: The new early return skips that call when lstat() fails, so foo has no local entry, gets REMOVE toward the server, and the server copy of foo is deleted. On master the call still ran after a failed stat, so this is new.
    • Fix: Also move error entries that carry the suffix: if (!e.isVirtualFile && !(e.type == ItemTypeSkipError && hasVirtualFileSuffix(e.name))) continue;. The new branch then ignores foo. Add a case to test/testsyncvirtualfiles.cpp: a dehydrated placeholder in a folder set to ReadOwner, sync, and check that the server file still exists.
    • If skipped: Suffix VFS users lose a server file whenever its placeholder can't be read, which is the bug this PR sets out to fix.
  3. The Windows test comment contradicts the test (test/testlocaldiscovery.cpp:L125)

    • What this is: The comment says a zero-access open "skips the DACL check", which is the check against the file's permission list.
    • Problem: If that check were skipped, denying S would make no difference either, so the comment gives the wrong reason.
    • Fix: // Denying SYNCHRONIZE makes the open in csync_vio_local_stat() fail. Denying read attributes does not.
    • If skipped: The next person who changes this test starts from a wrong idea.
  4. Part of a test check can never be true (test/testlocaldiscovery.cpp:L107)

    • What this is: QVERIFY(!subItem || subItem->_instruction != CSYNC_INSTRUCTION_REMOVE);
    • Problem: ItemCompletedSpy::findItem() returns a new empty item when nothing matches and never returns null (test/syncenginetestutils.cpp:L1590), so !subItem is always false.
    • Fix: QVERIFY(completeSpy.findItem("sub")->_instruction != CSYNC_INSTRUCTION_REMOVE);
    • If skipped: Readers think a missing item is a special case here.

The four points from my last review are fixed.

Report items whose stat fails as ItemTypeSkipError and pass them up to discovery.
The parent is marked child ignored and the item is retried via SoftError.

Assisted-by: ClaudeCode:claude-sonnet-5-5
Signed-off-by: Camila Ayres <hello@camilasan.com>
Signed-off-by: Camila Ayres <hello@camilasan.com>
Emit the unreadable item directly so subfolders are not
removed on the server.
Move suffixed error entries onto the unsuffixed name so suffix
VFS placeholders keep their server file.

Assisted-by: ClaudeCode:claude-sonnet-5-5
Signed-off-by: Camila Ayres <hello@camilasan.com>

@i2h3 i2h3 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. A new local file can be deleted for good when the server deletes its folder (src/libsync/discovery.cpp:L1389, L2411-2414, L2715-2720)

    • What this is: When the server deletes sub, a local file in it that was never uploaded gets REMOVE with "move to trash" from checkNewDeleteConflict(). That wish only works inside a folder delete, because outside Windows cfapi a single PropagateLocalRemove ignores it (propagatorjobs.cpp:L171-174).
    • Problem: If sub also holds an unreadable a.txt, the new _childIgnored = true turns the folder delete into NONE, so new.txt is removed on its own with FileSystem::remove() and never reaches the trash. Locked and excluded siblings (L516) already cause this on master, and this PR adds one more trigger, which I asked for in my first review.
    • Fix: Open a ticket instead of growing this PR. When processSubJobs() turns a folder REMOVE into NONE, it should also turn the checkNewDeleteConflict() items inside that folder into IGNORE.
    • If skipped: A file that exists nowhere else is deleted without a trash copy. It needs an unreadable file, a new file and a server-side folder delete at the same time, so it is rare.
  2. The "parent removal must not become a local removal" promise holds only one level deep (test/testlocaldiscovery.cpp:L100, src/libsync/discovery.cpp:L2130-2141, src/libsync/owncloudpropagator.cpp:L573)

    • What this is: The test checks that deleting sub on the server keeps sub/a.txt when a.txt can't be read. That works because _childIgnored turns the REMOVE of sub into NONE.
    • Problem: If the server deletes P and the unreadable file is P/sub/a.txt, sub runs in a separate deleted-folder job that never reports _childIgnored back to P, so P is still removed recursively and the propagator (the part that carries out the decisions) just skips the ignored child. The same happens in a partial scan, where a sync only re-reads changed folders and a.txt comes from the database without being read.
    • Fix: Track it in the same ticket. One idea: in OwncloudPropagator::start(), when an IGNORE item with an error status lies inside removedDirectory, drop that folder's delete job instead of skipping the item.
    • If skipped: On Linux and macOS the delete fails on every sync because the folder can't be searched. On Windows the unreadable file is probably deleted with its folder, which I could not check.
  3. The suffix VFS test passes without the fix (test/testsyncvirtualfiles.cpp:L132, L140)

    • What this is: testUnreadablePlaceholderKeepsServerFile is meant to prove that an unreadable A/a1.nextcloud keeps A/a1 on the server.
    • Problem: setLocalDiscoveryOptions(LocalDiscoveryStyle::DatabaseAndFilesystem) with no paths turns off local scanning for every folder, and it is the same call the "skip local discovery" row uses at L164. So A is never read, lstat() never fails and the test also passes on master, while findItem("A/a1") misses a REMOVE anyway because that is reported as A/a1.nextcloud.
    • Fix: Delete L132. Replace L140 with QCOMPARE(completeSpy.findItem("A/a1")->_instruction, CSYNC_INSTRUCTION_IGNORE);, which only passes when the re-keying moved the item.
    • If skipped: The suffix VFS fix from this round has no test that fails when it breaks.
  4. An unreadable leftover stub is reported under the readable file's name (src/libsync/discovery.cpp:L168-172)

    • What this is: With suffix VFS the re-keying loop now also takes unreadable foo.nextcloud entries. When a readable foo exists as well, the nameOverride branch processes the stub under the name foo.
    • Problem: The "Could not read local file" error then names foo, which is the readable file. The soft error can also wipe the error blacklist entry (the retry back-off record) of foo.
    • Fix: Keep the override for real placeholders only: } else if (!hasOtherData && e.isVirtualFile) {. The stub then keeps its own name, as in the previous round.
    • If skipped: Users see an error for the wrong file, and a failing upload of foo loses its back-off. Both names must exist locally for this, so it is rare.

Verdict: fix 3 and 4 first. 1 and 2 can go to a ticket.

@camilasan

Copy link
Copy Markdown
Member Author

Verdict: fix 3 and 4 first. 1 and 2 can go to a ticket.

✅

Make the suffix VFS regression test scan the folder locally so it
fails without the fix.

Assisted-by: ClaudeCode:claude-sonnet-5-5
Signed-off-by: Camila Ayres <hello@camilasan.com>

@i2h3 i2h3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I did another review round and it yielded no more findings except the already brought up missing commit signature. 🙂

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 New Code Smells (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@mgallien
mgallien merged commit cad7cdb into master Nov 25, 2026
21 of 27 checks passed
@mgallien
mgallien deleted the bugfix/statfail branch November 25, 2026 11:16
@mgallien

Copy link
Copy Markdown
Contributor

/backport to stable-34.0 please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants