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 <vgoyal@redhat.com>
This commit is contained in:
parent
a61c4dc959
commit
67fbd34d83
1 changed files with 16 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue