Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Safely write and update important files on the disk

License

NotificationsYou must be signed in to change notification settings

robojones/safe-write

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safe-write provides methods to manage critical files where it is important that they are either completelywritten 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.

GoDoc

package mainimport ("fmt""github.com/robojones/safe-write")funcmain() {data:= []byte("{\"data\":\"some important data\" }")safe.WriteFile("config.json",data)got,_:=safe.ReadFile("config.json")fmt.Printf("The data is: %s",string(got))safe.RemoveFile("config.json")}

How it works

TL;DR: When overwriting files, theWriteFile 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 theWriteFile method to create a file calledconfig.json. The write procedure is as follows.

  1. Write the contents of the file to a temporary file (e.g.config.json.2020-01-02T15-04-05.000000)
  2. Create a hard link fromconfig.json.1 to the temporary file
  3. Create a hard link fromconfig.json to the temporary file
  4. Remove the temporary file. Because we are using hard links, the contents of the file are still available using the file namesconfig.json andconfig.json.1

The reason why the intermediate linkconfig.json.1 is created becomes clear when we overwrite the contents of the file. Again, we are using theWriteFile method.

  1. Write the updated contents of the file to a new temporary file (e.g.config.json.2020-02-01T10-03-08.004001)
  2. Remove the previous hard linkconfig.json.1
  3. Create a new hard link fromconfig.json.1 to our new temporary file
  4. Remove the previous hard linkconfig.json
  5. Create a new hard link fromconfig.json to the new temporary file
  6. Remove the temporary file

The intermediate linkconfig.json.1 is created so there is always one of our linksconfig.json orconfig.json.1pointing 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 eitherconfig.json orconfig.json.1 safely written on the disk.

TheReadFile method of this module always checks for both links, so even if theconfig.json link is missing,there is a valid file available viaconfig.json.1.

About

Safely write and update important files on the disk

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp