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 <daan.hoogl...@gmail.com> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/9ff38486 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/9ff38486 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/9ff38486 Branch: refs/heads/master Commit: 9ff38486a1e5c10f54b6f8f1c64fad0bccc6817e Parents: 6271663 Author: wilderrodrigues <wrodrig...@schubergphilis.com> Authored: Thu Jun 4 08:38:06 2015 +0200 Committer: Daan Hoogland <daan.hoogl...@gmail.com> Committed: Thu Jun 4 12:34:05 2015 +0200 ---------------------------------------------------------------------- .../kvm/storage/KVMStorageProcessor.java | 25 ++++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/9ff38486/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java ---------------------------------------------------------------------- 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 201659d..d785293 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<String, Object> params = new HashMap<String, Object>(); @@ -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());