github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/valkeystore/encryption/io.go (about) 1 package encryption 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 ) 8 9 func writeTemporaryKeyFile(file string, content []byte) (string, error) { 10 // Create the keystore directory with appropriate permissions 11 // in case it is not present yet. 12 const dirPerm = 0700 13 if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil { 14 return "", err 15 } 16 // Atomic write: create a temporary hidden file first 17 // then move it into place. TempFile assigns mode 0600. 18 f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp") 19 if err != nil { 20 return "", err 21 } 22 if _, err := f.Write(content); err != nil { 23 f.Close() 24 os.Remove(f.Name()) 25 return "", err 26 } 27 f.Close() 28 return f.Name(), nil 29 }