Skip to content

In place file updates #3897

Open

Description

@tylla

I would like to request a feature to enable the special handling of hard links, namely overwrite them in-place.
I am aware that currently Syncthing doesn't care about hard links, it treats them as regular files and when updating such files it writes to a temp file, deletes the old file and renames the temp file. As a consequence every file that has multiple hard links gets detached from the original content and other links.
But sometimes there is a need to use content with multiple names (in separate directories), and this over-weighs the possible risk of file corruption that could rise from direct updating the content of such files.
I would suggest an option (e.g: inPlaceOverwriteHardLinks or something alike) that can be enabled on any share, so that files with link count greater than 1 are overwritten in place.

Activity

  1. AudriusButkevicius commented on Jan 12, 2017

    @AudriusButkevicius
    Member

    This makes very little sense when you take all the operating systems we support.

  2. calmh commented on Jan 13, 2017

    @calmh
    Member

    Yes. I'm not philosophically against doing this, but I suspect it would be complicated and fraught with corner cases given all the filesystems etc that don't support hard links. Complicated enough that I'm going to close this as it's extremely unlikely to be implemented.

  3. AudriusButkevicius commented on Jan 13, 2017

    @AudriusButkevicius
    Member

    I mean in place updates would solve this, but that already has a ticket somewhere.

  4. tylla commented on Jan 13, 2017

    @tylla
    Author

    @calmh I don't really see the corner cases. I mean the proposed logic would be:

    • if there is no option enabled for the given share than do the usual way.
    • if there is the option enabled, than check for multiple hard links
      • if check unsuccessful or single hard link found than do the usual way.
      • if the check indicates multiple hard links, than in-place update

    If an operating system doesn't support hard links than the error handling would guide the flow of events into the right path.
    The biggest part of this would be the implementation of in-place-update as this would be sticking out from the existing functionality and inflict some problems, but everyone using this feature would know that there is a trade-off between secure file update and multiple hard link usage.

    @AudriusButkevicius could you point out the "in place update" ticket, I couldn't find anything like that. As a fallback that could do for me.

    I would like to clarify the background of the request: there is a big directory of images, where one directory contains the images with original file names (IMG_XXXXX) and some other directories that contain sorted files with the same data but with more manageable names (like full date/time in the file name). As there are a few thousand images and directories nearing one hundred and constantly growing there isn't any other way to solve the storage of these files. Symlinks are out of question (as the main point is to keep the original if it gets deleted from the sorted directory) and bind mount doesn't fit the case.

  5. Ferroin commented on Jan 13, 2017

    @Ferroin

    @tylla The other problem though is that even if the OS supports hard-links, the FS might not. The classic example is FAT{16,32}, which does not support hard-links but is supported on every platform Syncthing runs on. There are a handful of other odd cases as well (classic HFS doesn't support hard-links either), and how the OS responds to trying to create a hard-link on a FS that doesn't support it isn't exactly well defined.

  6. AudriusButkevicius commented on Jan 13, 2017

    @AudriusButkevicius
    Member

    I can't find the ticket so opening this one instead.

  7. changed the title [-]Feature request: preserve hard links[/-] [+]Feature request: in-place updates[/+] on Jan 13, 2017
  8. tylla commented on Jan 13, 2017

    @tylla
    Author

    @Ferroin I don't think there would be a problem if any OS/FS layer will not support the handling of hard links. In that case the feature will simply not work. But anyway if one user would like to use the "handle files with multiple hard links specially" feature then s/he will have to intentionally enable this, and I think we can assume that s/he will know which FS is used and whether that and the OS supports hard links.
    Not to mention there isn't any place in the feature where Syncthing would/should create hard links at all, the whole point is to detect when there is hard linked content and treat it specially.
    @AudriusButkevicius thanks for reopening/renaming. Meanwhile I have found the wiki article about in-place-update, maybe this is what you remembered.

  9. AudriusButkevicius commented on Jan 14, 2017

    @AudriusButkevicius
    Member

    If you feel like writing go, I think we could just have an option flag that instead of this:
    https://github.com/syncthing/syncthing/blob/master/lib/model/rwfolder.go#L1464

    Copies the content from file to file and then removes the temp file.

  10. calmh commented on Jan 15, 2017

    @calmh
    Member
  11. spectras commented on Jan 18, 2017

    @spectras
    • if there is the option enabled, than check for multiple hard links
      • if check unsuccessful or single hard link found than do the usual way.
      • if the check indicates multiple hard links, than in-place update

    This approach has a race condition built-in. It's totally possible that the second name for the file gets created after the check, but before the temp file is complete. What then? Calmh's link has a much better approach. Still has a race condition, but on a much shorted time span.

    Yet the inherent danger of corrupting the files anytime makes in-place updates useless for most purposes.

  12. added
    enhancement
    on Feb 7, 2017
  13. added this to the milestone on Feb 7, 2017
  14. 18 remaining items

  15. spectras commented on Sep 3, 2021

    @spectras

    I would just like to add that the way of dealing with files that syncthing uses is standard on Unix systems. Pretty much all modern applications work that way (writing to a new file then atomically renaming it over the old one).

    It is the only way to prevent losing user data in case of a crash or a power outage: assuming proper filesystem settings, at any point in time one can recover either the old file or the new one (or both). On the other hand, modifying file in place can leave you with an empty file or partial file and your data irremediably lost.

    That's okay if you have another copy to recover. But for many syncthing users, syncthing is the tool that maintains such copies. Dependability is key: if it suddenly started to lose files, most of its value would go away.

  16. tylla commented on Sep 3, 2021

    @tylla
    Author

    @spectras I think there is no debate about the defaultness (and usefulness) of write-new-file-and-rename-over-old method.
    The whole point here is about a possible feature covering those rare but painful to handle cases where one has to use hard-links (for whatever reasons) and sync them between multiple machines.
    It would be the typical thing that comes with strings attached, so off by default, but if needed then one could use it while knowing the possible risks associated.

  17. calmh commented on Sep 3, 2021

    @calmh
    Member

    The feature request does not require arguing for, it's clear. It requires implementation. There's an embryo of a design early in this thread. It just lacks someone willing to tackle it.

  18. haoadoreorange commented on Sep 12, 2021

    @haoadoreorange

    Maybe changing the name of the issuet to something like, Detached hard link due to in place file updates ?

  19. AudriusButkevicius commented on Sep 12, 2021

    @AudriusButkevicius
    Member

    I disagree. I think you are describing only one of the symptoms.

  20. haoadoreorange commented on Nov 17, 2021

    @haoadoreorange

    just a quick note on this, finally for me making the scripts deal with symlink to the files was a more trivial task than proposing in place write for syncthing

  21. EternityForest commented on Oct 5, 2023

    @EternityForest

    One very useful application for this would be databases. If every node could have its own log file where it stored operations, you could implement a CRDT like cr-sqlite, in a way that allows sync.

    You could also manage text based log files very effectively, and it would be useful for syncing video files in real time as they are recorded.

    This wouldn't actually require full in place updates, just in place file append operations, with fallback to normal sync, and with the understanding that only one device is going to be appending to the file.

    For this limited use case, perhaps an options dotfile could go right in the same folder with the log files, containing a list of file patterns to be treated specially.

    Applications could then create that file automatically, if they were SyncThing-aware, or you could manually add it.

  22. pizdjuk commented on Nov 24, 2023

    @pizdjuk

    Right now I ran into the issue that there is no simple option to have some folder only to share it with a devices, where there is not much space.

    For example, there is some big folder to share. And in that folder there are only some files that I want to see on my android device. I would simply create a subfolder and populate it with hardlinks to files I want. But that if I change some file from the android the hardlink is unlinked. Clear, I could set the folder as "read only", but anyway... would be a useful feature.

  23. added theissue type on May 30, 2025
  24. dgudim commented on Nov 8, 2026

    @dgudim

    I would like to see this implemented too. My reason is not with hardlinks though. I recently hit a bug where I was syncing a large folder to my phone which led to syncthing being killed by android. That's fine on it's own, but it died in the middle of the temp - delete - rename chain and I lost the some of the files. After restart syncthing saw missing files and propagated them as deletions to other machines.

    I think that while this is rare, it's still a data loss possibility, especially in folders with no versioning.

    Why not do an in-place rename over the file where it's possible to do atomically?

  25. calmh commented on Nov 8, 2026

    @calmh
    Member

    Why not do an in-place rename over the file where it's possible to do atomically?

    Syncthing does exactly that, always. You've probably run into some other funky corner case of user space filesystem shims in Android or something.

  26. marbens-arch commented on Nov 8, 2026

    @marbens-arch
    Member

    Why not do an in-place rename over the file where it's possible to do atomically?

    Syncthing does exactly that, always. You've probably run into some other funky corner case of user space filesystem shims in Android or something.

    @calmh If lib/model/folder_sendrecv.go:1736 doesn't delete the file from disk before the temp rename, what does it do?

    if !curFile.IsDirectory() && !curFile.IsSymlink() && file.InConflictWith(curFile) {
    // The new file has been changed in conflict with the existing one. We
    // should file it away as a conflict instead of just removing or
    // archiving.
    // Directories and symlinks aren't checked for conflicts.
    err = f.inWritableDir(func(name string) error {
    return f.moveForConflict(name, file.ModifiedBy.String(), scanChan)
    }, curFile.Name)
    } else {
    err = f.deleteItemOnDisk(curFile, scanChan)
    }

  27. calmh commented on Nov 8, 2026

    @calmh
    Member

    Ah, I see what you mean now. Yeah that's not optimal. This would have been less of an issue in 1.x as the delete +failed sync is a conflict that would always be resolved in favour of the non-delete, but that's not the case any more. This has nothing to do with in place file edits though, so if you could file a separate issue on the problem that'd be awesome.

  28. dgudim commented on Nov 8, 2026

    @dgudim

    Here, done, I hope this is good enough. Thanks for quick feedback!

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

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions