From 67fbd34d8379a1b8232aea5d126a389f64bdc59a Mon Sep 17 00:00:00 2001 From: Vivek Goyal Date: Wed, 5 Nov 2014 09:25:02 -0500 Subject: [PATCH] devmapper: Move file write and rename functionality in a separate function Currently we save device metadata and have a helper function saveMetadata() which converts data in json format as well as saves it to file. For converting data in json format, one needs to know what is being saved. Break this function down in two functions. One function only has file write capability and takes in argument about byte array of json data. Now this function does not have to know what data is being saved. It only knows about a stream of json data is being saved to a file. This allows me to reuse this function to save a different type of metadata. In this case I am planning to save NextDeviceId so that docker can use this device Id upon next restart. Otherwise docker starts from 0 which is suboptimal. Signed-off-by: Vivek Goyal --- daemon/graphdriver/devmapper/deviceset.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/daemon/graphdriver/devmapper/deviceset.go b/daemon/graphdriver/devmapper/deviceset.go index fdfc089a82..7c767fff97 100644 --- a/daemon/graphdriver/devmapper/deviceset.go +++ b/daemon/graphdriver/devmapper/deviceset.go @@ -200,11 +200,8 @@ func (devices *DeviceSet) removeMetadata(info *DevInfo) error { return nil } -func (devices *DeviceSet) saveMetadata(info *DevInfo) error { - jsonData, err := json.Marshal(info) - if err != nil { - return fmt.Errorf("Error encoding metadata to json: %s", err) - } +// Given json data and file path, write it to disk +func (devices *DeviceSet) writeMetaFile(jsonData []byte, filePath string) error { tmpFile, err := ioutil.TempFile(devices.metadataDir(), ".tmp") if err != nil { return fmt.Errorf("Error creating metadata file: %s", err) @@ -223,10 +220,23 @@ func (devices *DeviceSet) saveMetadata(info *DevInfo) error { if err := tmpFile.Close(); err != nil { return fmt.Errorf("Error closing metadata file %s: %s", tmpFile.Name(), err) } - if err := os.Rename(tmpFile.Name(), devices.metadataFile(info)); err != nil { + if err := os.Rename(tmpFile.Name(), filePath); err != nil { return fmt.Errorf("Error committing metadata file %s: %s", tmpFile.Name(), err) } + return nil +} + +func (devices *DeviceSet) saveMetadata(info *DevInfo) error { + jsonData, err := json.Marshal(info) + if err != nil { + return fmt.Errorf("Error encoding metadata to json: %s", err) + } + err = devices.writeMetaFile(jsonData, devices.metadataFile(info)) + if err != nil { + return err + } + if devices.NewTransactionId != devices.TransactionId { if err = setTransactionId(devices.getPoolDevName(), devices.TransactionId, devices.NewTransactionId); err != nil { return fmt.Errorf("Error setting devmapper transition ID: %s", err)