github.com/robojones/safe-write (go): Package safe provides methods to manage critical files where it is important that they are either completely written to the disk or not at all. If the process is unexpectedly interrupted while updating the contents of a file, the original version of the file remains untouched.  TL;DR: When **overwriting** files, the WriteFile method uses hard links and temporary files to ensure that there is always a consistent version of your file on the disk.  Lets assume we are using the WriteFile method to create a file called config.json. The write procedure is as follows.  The reason why the intermediate link config.json.1 is created becomes clear when we overwrite the contents of the file. Again, we are using the WriteFile method.  The intermediate link config.json.1 is created so there is always one of our links config.json or config.json.1 pointing to a complete version of our config file. Even if our process is interrupted by a server crash, at any point of the overwrite process, there is always either config.json or config.json.1 safely written on the disk.  The ReadFile method of this module always checks for both links, so even if the config.json link is missing, there is a valid file available via config.json.1
Keywords: config-files|filesystem|golang