coverity 1116693: properties template file creation unlikely but if repetitive, serious.

Signed-off-by: Daan Hoogland <daan@onecht.net>
This commit is contained in:
Daan Hoogland 2015-07-07 22:55:09 +02:00
parent 2c7e19faff
commit bb2c02d144

View File

@ -495,66 +495,66 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
String destFileFullPath = destFile.getAbsolutePath() + File.separator + fileName; String destFileFullPath = destFile.getAbsolutePath() + File.separator + fileName;
s_logger.debug("copy snapshot " + srcFile.getAbsolutePath() + " to template " + destFileFullPath); s_logger.debug("copy snapshot " + srcFile.getAbsolutePath() + " to template " + destFileFullPath);
Script.runSimpleBashScript("cp " + srcFile.getAbsolutePath() + " " + destFileFullPath); Script.runSimpleBashScript("cp " + srcFile.getAbsolutePath() + " " + destFileFullPath);
String metaFileName = destFile.getAbsolutePath() + File.separator + "template.properties";
File metaFile = new File(metaFileName);
try { try {
// generate template.properties file
String metaFileName = destFile.getAbsolutePath() + File.separator + "template.properties";
_storage.create(destFile.getAbsolutePath(), "template.properties"); _storage.create(destFile.getAbsolutePath(), "template.properties");
File metaFile = new File(metaFileName); try ( // generate template.properties file
FileWriter writer = new FileWriter(metaFile); FileWriter writer = new FileWriter(metaFile);
BufferedWriter bufferWriter = new BufferedWriter(writer); BufferedWriter bufferWriter = new BufferedWriter(writer);
// KVM didn't change template unique name, just used the template name passed from orchestration layer, so no need ) {
// to send template name back. // KVM didn't change template unique name, just used the template name passed from orchestration layer, so no need
bufferWriter.write("uniquename=" + destData.getName()); // to send template name back.
bufferWriter.write("\n"); bufferWriter.write("uniquename=" + destData.getName());
bufferWriter.write("filename=" + fileName); bufferWriter.write("\n");
bufferWriter.write("\n"); bufferWriter.write("filename=" + fileName);
long size = _storage.getSize(destFileFullPath); bufferWriter.write("\n");
bufferWriter.write("size=" + size); long size = _storage.getSize(destFileFullPath);
bufferWriter.close(); bufferWriter.write("size=" + size);
writer.close();
/** /**
* Snapshots might be in either QCOW2 or RAW image format * Snapshots might be in either QCOW2 or RAW image format
* *
* For example RBD snapshots are in RAW format * For example RBD snapshots are in RAW format
*/ */
Processor processor = null; Processor processor = null;
if (srcFormat == ImageFormat.QCOW2) { if (srcFormat == ImageFormat.QCOW2) {
processor = new QCOW2Processor(); processor = new QCOW2Processor();
} else if (srcFormat == ImageFormat.RAW) { } else if (srcFormat == ImageFormat.RAW) {
processor = new RawImageProcessor(); processor = new RawImageProcessor();
} else { } else {
throw new ConfigurationException("Unknown image format " + srcFormat.toString()); throw new ConfigurationException("Unknown image format " + srcFormat.toString());
}
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("template processor", params);
String destPath = destFile.getAbsolutePath();
FormatInfo info = processor.process(destPath, null, templateName);
TemplateLocation loc = new TemplateLocation(_storage, destPath);
loc.create(1, true, destData.getName());
loc.addFormat(info);
loc.save();
TemplateProp prop = loc.getTemplateInfo();
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destData.getPath() + File.separator + fileName);
newTemplate.setFormat(srcFormat);
newTemplate.setSize(prop.getSize());
newTemplate.setPhysicalSize(prop.getPhysicalSize());
return new CopyCmdAnswer(newTemplate);
} catch (ConfigurationException e) {
s_logger.debug("Failed to create template:" + e.toString());
return new CopyCmdAnswer(e.toString());
} catch (InternalErrorException e) {
s_logger.debug("Failed to create template:" + e.toString());
return new CopyCmdAnswer(e.toString());
} }
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("template processor", params);
String destPath = destFile.getAbsolutePath();
FormatInfo info = processor.process(destPath, null, templateName);
TemplateLocation loc = new TemplateLocation(_storage, destPath);
loc.create(1, true, destData.getName());
loc.addFormat(info);
loc.save();
TemplateProp prop = loc.getTemplateInfo();
TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destData.getPath() + File.separator + fileName);
newTemplate.setFormat(srcFormat);
newTemplate.setSize(prop.getSize());
newTemplate.setPhysicalSize(prop.getPhysicalSize());
return new CopyCmdAnswer(newTemplate);
} catch (ConfigurationException e) {
s_logger.debug("Failed to create template:" + e.toString());
return new CopyCmdAnswer(e.toString());
} catch (IOException e) { } catch (IOException e) {
s_logger.debug("Failed to create template:" + e.toString()); s_logger.debug("Failed to create template:" + e.toString());
return new CopyCmdAnswer(e.toString()); return new CopyCmdAnswer(e.toString());
} catch (InternalErrorException e) {
s_logger.debug("Failed to create template:" + e.toString());
return new CopyCmdAnswer(e.toString());
} }
} }