Browse code

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>

Vivek Goyal authored on 2014/11/05 23:25:02
Showing 1 changed files
... ...
@@ -200,11 +200,8 @@ func (devices *DeviceSet) removeMetadata(info *DevInfo) error {
200 200
 	return nil
201 201
 }
202 202
 
203
-func (devices *DeviceSet) saveMetadata(info *DevInfo) error {
204
-	jsonData, err := json.Marshal(info)
205
-	if err != nil {
206
-		return fmt.Errorf("Error encoding metadata to json: %s", err)
207
-	}
203
+// Given json data and file path, write it to disk
204
+func (devices *DeviceSet) writeMetaFile(jsonData []byte, filePath string) error {
208 205
 	tmpFile, err := ioutil.TempFile(devices.metadataDir(), ".tmp")
209 206
 	if err != nil {
210 207
 		return fmt.Errorf("Error creating metadata file: %s", err)
... ...
@@ -223,10 +220,23 @@ func (devices *DeviceSet) saveMetadata(info *DevInfo) error {
223 223
 	if err := tmpFile.Close(); err != nil {
224 224
 		return fmt.Errorf("Error closing metadata file %s: %s", tmpFile.Name(), err)
225 225
 	}
226
-	if err := os.Rename(tmpFile.Name(), devices.metadataFile(info)); err != nil {
226
+	if err := os.Rename(tmpFile.Name(), filePath); err != nil {
227 227
 		return fmt.Errorf("Error committing metadata file %s: %s", tmpFile.Name(), err)
228 228
 	}
229 229
 
230
+	return nil
231
+}
232
+
233
+func (devices *DeviceSet) saveMetadata(info *DevInfo) error {
234
+	jsonData, err := json.Marshal(info)
235
+	if err != nil {
236
+		return fmt.Errorf("Error encoding metadata to json: %s", err)
237
+	}
238
+	err = devices.writeMetaFile(jsonData, devices.metadataFile(info))
239
+	if err != nil {
240
+		return err
241
+	}
242
+
230 243
 	if devices.NewTransactionId != devices.TransactionId {
231 244
 		if err = setTransactionId(devices.getPoolDevName(), devices.TransactionId, devices.NewTransactionId); err != nil {
232 245
 			return fmt.Errorf("Error setting devmapper transition ID: %s", err)