Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
pkg/vmextension/status: use io.TempFile for roubstness
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetb committed Jul 26, 2016
1 parent 2d26e1e commit e1087d6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/vmextension/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -60,20 +59,23 @@ func (r StatusReport) marshal() ([]byte, error) {
// sequence number. The operation consists of writing to a temporary file in the
// same folder and moving it to the final destination for atomicity.
func (r StatusReport) Save(statusFolder string, seqNum int) error {
suffix := fmt.Sprintf(".tmp.%d", rand.New(rand.NewSource(time.Now().UnixNano())).Int63())
fn := fmt.Sprintf("%d.status", seqNum)
path := filepath.Join(statusFolder, fn)
tmpPath := filepath.Join(statusFolder, fn+suffix)
tmpFile, err := ioutil.TempFile(statusFolder, fn)
if err != nil {
return fmt.Errorf("status: failed to create temporary file: %v", err)
}
tmpFile.Close()

b, err := r.marshal()
if err != nil {
return fmt.Errorf("status: failed to marshal into json: %v", err)
}
if err := ioutil.WriteFile(tmpPath, b, 0644); err != nil {
return fmt.Errorf("status: failed to path=%s error=%v", tmpPath, err)
if err := ioutil.WriteFile(tmpFile.Name(), b, 0644); err != nil {
return fmt.Errorf("status: failed to path=%s error=%v", tmpFile.Name(), err)
}

if err := os.Rename(tmpPath, path); err != nil {
if err := os.Rename(tmpFile.Name(), path); err != nil {
return fmt.Errorf("status: failed to move to path=%s error=%v", path, err)
}
return nil
Expand Down

0 comments on commit e1087d6

Please sign in to comment.