From 9ff38486a1e5c10f54b6f8f1c64fad0bccc6817e Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Thu, 4 Jun 2015 08:38:06 +0200 Subject: [PATCH] Coverity issue 1116677 - Avoiding catching only Exception. Makes the code too britle. - Catching the QemuImgException and throwing it to be caught further in the code - Surrounding the output stream with try/catch and throwing it to be further handled in the code. Closing the output stream quietly. Signed-off-by: Daan Hoogland --- .../kvm/storage/KVMStorageProcessor.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java index 201659d3e87..d785293a720 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java @@ -59,6 +59,7 @@ import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; import org.apache.cloudstack.utils.qemu.QemuImgException; import org.apache.cloudstack.utils.qemu.QemuImgFile; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.libvirt.Connect; import org.libvirt.Domain; @@ -525,8 +526,10 @@ public class KVMStorageProcessor implements StorageProcessor { try { q.convert(srcFile, destFile); } catch (final QemuImgException e) { - s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + - e.getMessage()); + final String message = "Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + + e.getMessage(); + + throw new QemuImgException(message); } final File templateProp = new File(tmpltPath + "/template.properties"); @@ -541,9 +544,14 @@ public class KVMStorageProcessor implements StorageProcessor { templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator"); final FileOutputStream templFo = new FileOutputStream(templateProp); - templFo.write(templateContent.getBytes()); - templFo.flush(); - templFo.close(); + try { + templFo.write(templateContent.getBytes()); + templFo.flush(); + } catch (final IOException e) { + throw e; + } finally { + IOUtils.closeQuietly(templFo); + } } final Map params = new HashMap(); @@ -566,6 +574,13 @@ public class KVMStorageProcessor implements StorageProcessor { newTemplate.setFormat(ImageFormat.QCOW2); newTemplate.setName(templateName); return new CopyCmdAnswer(newTemplate); + + } catch (final QemuImgException e) { + s_logger.error(e.getMessage()); + return new CopyCmdAnswer(e.toString()); + } catch (final IOException e) { + s_logger.debug("Failed to createTemplateFromVolume: ", e); + return new CopyCmdAnswer(e.toString()); } catch (final Exception e) { s_logger.debug("Failed to createTemplateFromVolume: ", e); return new CopyCmdAnswer(e.toString());