diff --git a/core/src/com/cloud/storage/template/IsoProcessor.java b/core/src/com/cloud/storage/template/IsoProcessor.java index f70643083c1..4d0a331f547 100644 --- a/core/src/com/cloud/storage/template/IsoProcessor.java +++ b/core/src/com/cloud/storage/template/IsoProcessor.java @@ -61,7 +61,7 @@ public class IsoProcessor extends AdapterBase implements Processor { } @Override - public Long getVirtualSize(File file) { + public long getVirtualSize(File file) { return file.length(); } diff --git a/core/src/com/cloud/storage/template/OVAProcessor.java b/core/src/com/cloud/storage/template/OVAProcessor.java index d87ca18040b..78825ced8ac 100644 --- a/core/src/com/cloud/storage/template/OVAProcessor.java +++ b/core/src/com/cloud/storage/template/OVAProcessor.java @@ -86,7 +86,7 @@ public class OVAProcessor extends AdapterBase implements Processor { } @Override - public Long getVirtualSize(File file) { + public long getVirtualSize(File file) { try { long size = getTemplateVirtualSize(file.getParent(), file.getName()); return size; diff --git a/core/src/com/cloud/storage/template/Processor.java b/core/src/com/cloud/storage/template/Processor.java index 7d6f6ce1a42..ba57563e1b6 100644 --- a/core/src/com/cloud/storage/template/Processor.java +++ b/core/src/com/cloud/storage/template/Processor.java @@ -20,6 +20,7 @@ package com.cloud.storage.template; import java.io.File; +import java.io.IOException; import com.cloud.exception.InternalErrorException; import com.cloud.storage.Storage.ImageFormat; @@ -51,6 +52,6 @@ public interface Processor extends Adapter { public boolean isCorrupted; } - Long getVirtualSize(File file); + long getVirtualSize(File file) throws IOException; } diff --git a/core/src/com/cloud/storage/template/QCOW2Processor.java b/core/src/com/cloud/storage/template/QCOW2Processor.java index c387a9be465..2c66415bf96 100644 --- a/core/src/com/cloud/storage/template/QCOW2Processor.java +++ b/core/src/com/cloud/storage/template/QCOW2Processor.java @@ -37,7 +37,9 @@ import com.cloud.utils.component.AdapterBase; @Local(value = Processor.class) public class QCOW2Processor extends AdapterBase implements Processor { private static final Logger s_logger = Logger.getLogger(QCOW2Processor.class); - StorageLayer _storage; + private static final int VIRTUALSIZE_HEADER_LOCATION = 24; + + private StorageLayer _storage; @Override public FormatInfo process(String templatePath, ImageFormat format, String templateName) { @@ -60,52 +62,30 @@ public class QCOW2Processor extends AdapterBase implements Processor { File qcow2File = _storage.getFile(qcow2Path); info.size = _storage.getSize(qcow2Path); - FileInputStream strm = null; - byte[] b = new byte[8]; - try { - strm = new FileInputStream(qcow2File); - strm.skip(24); - strm.read(b); - } catch (Exception e) { - s_logger.warn("Unable to read qcow2 file " + qcow2Path, e); - return null; - } finally { - if (strm != null) { - try { - strm.close(); - } catch (IOException e) { - } - } - } - long templateSize = NumbersUtil.bytesToLong(b); - info.virtualSize = templateSize; + try { + info.virtualSize = getVirtualSize(qcow2File); + } catch (IOException e) { + s_logger.error("Unable to get virtual size from " + qcow2File.getName()); + return null; + } return info; } @Override - public Long getVirtualSize(File file) { - FileInputStream strm = null; + public long getVirtualSize(File file) throws IOException { byte[] b = new byte[8]; - try { - strm = new FileInputStream(file); - strm.skip(24); - strm.read(b); - } catch (Exception e) { - s_logger.warn("Unable to read qcow2 file " + file, e); - return null; - } finally { - if (strm != null) { - try { - strm.close(); - } catch (IOException e) { - } + try (FileInputStream strm = new FileInputStream(file)) { + if (strm.skip(VIRTUALSIZE_HEADER_LOCATION) != VIRTUALSIZE_HEADER_LOCATION) { + throw new IOException("Unable to skip to the virtual size header"); + } + if (strm.read(b) != 8) { + throw new IOException("Unable to properly read the size"); } } - long templateSize = NumbersUtil.bytesToLong(b); - return templateSize; + return NumbersUtil.bytesToLong(b); } @Override diff --git a/core/src/com/cloud/storage/template/RawImageProcessor.java b/core/src/com/cloud/storage/template/RawImageProcessor.java index 030c4059768..820ef1986f5 100644 --- a/core/src/com/cloud/storage/template/RawImageProcessor.java +++ b/core/src/com/cloud/storage/template/RawImageProcessor.java @@ -69,7 +69,7 @@ public class RawImageProcessor extends AdapterBase implements Processor { } @Override - public Long getVirtualSize(File file) { + public long getVirtualSize(File file) { return file.length(); } diff --git a/core/src/com/cloud/storage/template/VhdProcessor.java b/core/src/com/cloud/storage/template/VhdProcessor.java index ac0fa7eb1f3..aff2942ea39 100644 --- a/core/src/com/cloud/storage/template/VhdProcessor.java +++ b/core/src/com/cloud/storage/template/VhdProcessor.java @@ -22,7 +22,6 @@ package com.cloud.storage.template; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.Arrays; import java.util.Map; import javax.ejb.Local; @@ -105,7 +104,7 @@ public class VhdProcessor extends AdapterBase implements Processor { } @Override - public Long getVirtualSize(File file) { + public long getVirtualSize(File file) { FileInputStream strm = null; byte[] currentSize = new byte[8]; byte[] creatorApp = new byte[4]; @@ -142,21 +141,4 @@ public class VhdProcessor extends AdapterBase implements Processor { return true; } - private void imageSignatureCheck(byte[] creatorApp) throws InternalErrorException { - boolean findKnownCreator = false; - for (int i = 0; i < citrixCreatorApp.length; i++) { - if (Arrays.equals(creatorApp, citrixCreatorApp[i])) { - findKnownCreator = true; - break; - } - } - if (!findKnownCreator) { - /*Only support VHD image created by citrix xenserver, and xenconverter*/ - String readableCreator = ""; - for (int j = 0; j < creatorApp.length; j++) { - readableCreator += (char)creatorApp[j]; - } - throw new InternalErrorException("Image creator is:" + readableCreator + ", is not supported"); - } - } } diff --git a/core/src/com/cloud/storage/template/VmdkProcessor.java b/core/src/com/cloud/storage/template/VmdkProcessor.java index ee4f8194615..6903b74a674 100644 --- a/core/src/com/cloud/storage/template/VmdkProcessor.java +++ b/core/src/com/cloud/storage/template/VmdkProcessor.java @@ -21,12 +21,12 @@ package com.cloud.storage.template; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; import java.util.Map; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.ejb.Local; import javax.naming.ConfigurationException; @@ -72,7 +72,7 @@ public class VmdkProcessor extends AdapterBase implements Processor { } @Override - public Long getVirtualSize(File file) { + public long getVirtualSize(File file) { try { long size = getTemplateVirtualSize(file.getParent(), file.getName()); return size; @@ -86,8 +86,6 @@ public class VmdkProcessor extends AdapterBase implements Processor { long virtualSize = 0; String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator; templateFileFullPath += templateName.endsWith(ImageFormat.VMDK.getFileExtension()) ? templateName : templateName + "." + ImageFormat.VMDK.getFileExtension(); - String vmdkHeader = ""; - try { FileReader fileReader = new FileReader(templateFileFullPath); BufferedReader bufferedReader = new BufferedReader(fileReader); diff --git a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java index b5368044e06..0e625f3c93c 100755 --- a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java +++ b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java @@ -798,7 +798,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } - protected Long getVirtualSize(File file, ImageFormat format) { + protected long getVirtualSize(File file, ImageFormat format) { Processor processor = null; try { if (format == null) { @@ -822,9 +822,10 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S processor.configure("template processor", new HashMap()); return processor.getVirtualSize(file); } catch (Exception e) { - s_logger.debug("Failed to get virtual size:", e); + s_logger.warn("Failed to get virtual size, returning file size instead:", e); + return file.length(); } - return file.length(); + } protected Answer copyFromNfsToS3(CopyCommand cmd) {