Skip to content

Restore file from trashbin to a different place overwriting a file #35974

Open

Description

@phil-davis

Steps to reproduce

  Scenario Outline: restoring a file to an already existing path overrides the file
    Given using <dav-path> DAV path
    And user "user0" has been created with default attributes and skeleton files
    And user "user0" has uploaded file with content "file to delete" to "/textfile0.txt"
    And user "user0" has uploaded file with content "PARENT textfile0 content" to "/PARENT/textfile0.txt"
    And user "user0" has deleted file "/textfile0.txt"
    When user "user0" restores the file with original path "/textfile0.txt" to "/PARENT/textfile0.txt" using the trashbin API
    Then the HTTP status code should be "204"
    # Sometimes "/PARENT/textfile0.txt" is found in the trashbin. Should it? Or not?
    # That seems to be what happens when the restore-overwrite happens properly,
    # The original /PARENT/textfile0.txt seems to be "deleted" and so goes to the trashbin
    #And as "user0" the file with original path "/PARENT/textfile0.txt" should not exist in trash
    And as "user0" file "/PARENT/textfile0.txt" should exist
    # sometimes the restore from trashbin does overwrite the existing file, but sometimes it does not. That is also surprising.
    # the current observed behavior is that if the original /PARENT/textfile0.txt ended up in the trashbin,
    # then the new /PARENT/textfile0.txt has the "file to delete" content.
    # otherwise /PARENT/textfile0.txt has its old content
    And the content of file "/PARENT/textfile0.txt" for user "user0" if the file is also in the trashbin should be "file to delete" otherwise "PARENT textfile0 content"
    #And the content of file "/PARENT/textfile0.txt" for user "user0" should be "file to delete"
    Examples:
      | dav-path |
      | old      |
      | new      |

Expected behaviour

Maybe:

  1. the restored file should just overwrite the existing file, and the existing file is gone (or is saved as an old version if files_versions is enabled), or;
  2. The restored file has (2) on the end of its name, and the existing file stays, or;
  3. ???

Actual behaviour

Sometimes the restore from trashbin overwrites the existing file. When that happens, the existing file is found in the trashbin. I guess that the code-path deletes the existing file (triggering an event that causes it to be saved into the trashbin). And the restored file has the correctly-restored content.

But also sometimes the existing file is not found in the trashbin, and has not been overwritten (it still has the old content). But the API call to restore from trashbin still returns 204 success.

Server configuration

Local runs of the above scenario in a development environment on top of current master
And in drone CI it happens also.

Activity

  1. phil-davis commented on Aug 2, 2019

    @phil-davis
    ContributorAuthor

    One guess is that there is some timing of events/callbacks happening when the server is processing a restore from trashbin request, and that restore involves deleting (or overwriting) the target file, and so save-to-trashbin callbacks also get triggered...

    @micbar @DeepDiver1975 I tried to make sure that this is not an artifact of the test runner.

    The Webdav Trashbin API will end up in 10.3, so maybe this issue should be looked into, as there might be some underlying behaviour found that can be adjusted/fixed.

  2. phil-davis commented on Aug 5, 2019

    @phil-davis
    ContributorAuthor

    @micbar I added this to the current sprint backlog. Please decide if it is something that should be looked into for 10.3

  3. VicDeo commented on Aug 19, 2019

    @VicDeo
    Contributor

    As far as I remember, the restore from trash was designed to work exactly like this.
    In case files_versions is disabled the user will loose existing file with the same name.

    To change the behavior here one just needs to delete the code that keeps user data safe.

  4. PVince81 commented on Aug 19, 2019

    @PVince81
    Contributor

    is this using the new API or old API ?

    did OC behave the same using the old API / old OC version ?

  5. VicDeo commented on Aug 19, 2019

    @VicDeo
    Contributor

    it was already proposed as an enhancement a while ago and declined.
    See #3131 (comment)

  6. PVince81 commented on Aug 19, 2019

    @PVince81
    Contributor

    Ok, so something to clarify on the requirements side @pmaier1

    We could decide to keep the behavior as is for now and document it.
    There's also the question how often it happens that an admin disabled files_versions.

  7. phil-davis commented on Aug 19, 2019

    @phil-davis
    ContributorAuthor

    Note: from run to run of the above scenario I get inconsistent results. I made the test scenario in PR #35948 be "flexible" to allow the 2 observed behaviours. Whatever the desired behaviour, at least it should be consistent. If someone can find how this scenario can always return HTTP 204 but sometimes the file content is restored, and sometimes not, then that would be very helpful.

    (events for trashbin and/or versions going off in not-predictable orders?)

  8. PVince81 commented on Aug 19, 2019

    @PVince81
    Contributor

    when I hear "sometimes" I usually hear "race conditions".
    but don't see how those could happen in a linear test scenario

  9. phil-davis commented on Aug 20, 2019

    @phil-davis
    ContributorAuthor

    when I hear "sometimes" I usually hear "race conditions".
    but don't see how those could happen in a linear test scenario

    same here - but I get close to 50% split of each result???

  10. PVince81 commented on Aug 20, 2019

    @PVince81
    Contributor

    @phil-davis this reminds me now about another time related component: the file name used when trashing a file contains the current timestamp. If tests run faster there could be timestamp overwrites.

  11. self-assigned this
    on Aug 20, 2019
  12. phil-davis commented on Aug 20, 2019

    @phil-davis
    ContributorAuthor

    I assigned myself to investigate test timing.

  13. removed their assignment
    on Jun 19, 2021
  14. phil-davis commented on Jun 19, 2021

    @phil-davis
    ContributorAuthor

    As far as I can tell, there is not a test timing issue.

    https://drone.owncloud.com/owncloud/core/30747/4/13
    I put extra sleep(5) before the checks in Then steps and still the same result - about half the time the updated content gets restored, and half the time not. So it does not seem to be a test timing issue.

    See #38867 (comment)

  15. phil-davis commented on Jun 21, 2021

    @phil-davis
    ContributorAuthor

    PR #38869 cleans up the "good" scenario so that it only checks for the really-required behavior. That will help for testing the OCIS implementation. And it still gets 50% failures in oC10 core, so it can be used as a test for fixing the problem in oC10.

  16. phil-davis commented on Sep 16, 2021

    @phil-davis
    ContributorAuthor

    PR #38867 demonstrates that the issue is still happening.

  17. DeepDiver1975 commented on Jun 21, 2026

    @DeepDiver1975
    Contributor

    Classification: confirmed-bug (potential data loss)

    🤖 Automated issue analysis by Claude Code review agent.

    I reproduced the root cause on current master (7da5538). Restoring a trashbin item to an explicitly specified target path can silently overwrite an existing file there, and whether the overwritten file is itself saved to the trashbin depends on hook/event ordering — which explains the ~50% inconsistency @phil-davis observed.

    Located cause

    apps/files_trashbin/lib/Trashbin.php::restore($filename, $targetLocation = null):

    • The collision-safe naming via getUniqueFilename() (which appends (restored) / (restored N) when the destination already exists) is only reached when $targetLocation === null — i.e. the "restore to original location" fallback:
    if ($targetLocation === null) {
        ...
        // we need a extension in case a file/dir with the same name already exists
        $uniqueFilename = self::getUniqueFilename($location, $nameOfFileWithoutTimestamp, $view);
        $targetLocation = $location . '/' . $uniqueFilename;
    }
    $source = Filesystem::normalizePath('files_trashbin/files/' . $filename);
    $target = Filesystem::normalizePath('files/' . $targetLocation);
    ...
    $restoreResult = $view->rename($source, $target);   // no existence check on $target
    • When an explicit $targetLocation IS passed, the code skips getUniqueFilename() entirely and goes straight to $view->rename($source, $target) with no file_exists($target) guard.

    View::rename() (lib/private/Files/View.php:805) does not refuse an existing target — it computes $exists = $this->file_exists($path2) purely for hook signalling, then calls $storage->rename() / moveFromStorage(), which overwrites the destination.

    The WebDAV restore-to-different-place path hits exactly this

    Directory::moveInto() → AbstractTrashBinNode::restore($targetLocation) → TrashBinManager::restore(...) → Trashbin::restore($path, $targetLocation) with a non-null $targetLocation. So the WebDAV Trashbin API restore-to-a-different-path always takes the unguarded rename() branch — matching the failing scenario in the issue.

    Why "sometimes"

    The pre-existing target is only preserved if a delete-hook moves it into the trashbin before the rename overwrites it. That hook/event ordering is not deterministic relative to the overwrite, hence content sometimes survives (old content kept / found in trash) and sometimes is lost — consistent with @PVince81's "race condition" intuition and @phil-davis's sleep(5) test showing it is not a test-timing artifact.

    Note on intent

    Per @VicDeo, restore was designed to overwrite (relying on files_versions for safety), and the related enhancement was previously declined (#3131). So the product decision on the desired behavior (overwrite vs. unique-name vs. reject) still stands with the maintainers. But independent of that decision, two things are defects:

    1. Inconsistency — the same call returns 204 yet non-deterministically overwrites or doesn't. At minimum the behavior must be deterministic.
    2. Data loss when files_versions is disabled — the overwrite is unrecoverable, with no caller opt-in/confirmation.

    A safe direction: have restore() apply the same getUniqueFilename() collision handling (or an explicit overwrite flag) on the explicit-$targetLocation branch, so an explicit restore target never silently clobbers an existing file.

    This remains reproducible on master; PR #38867 ([tests-only], now closed) demonstrated it and the cleaned-up scenario from #38869 (merged) can serve as the regression test for a fix.

  18. DeepDiver1975 commented on Jun 21, 2026

    @DeepDiver1975
    Contributor

    🤖 Follow-up: opened a fix — #41645. It adds a data-loss guard so restoring to an explicit target that already exists now fails (returns false) instead of silently overwriting the existing file. The normal restore-to-original path is unaffected (it already picks a unique name). Includes a regression test.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions