Fix a coverity issue about unchecked returns and make the code flow a

litle bit more easy to follow.
This commit is contained in:
Hugo Trippaers 2014-07-28 10:53:04 +02:00
parent 67876b215e
commit 49f60ca744
8 changed files with 30 additions and 68 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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

View File

@ -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();
}

View File

@ -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");
}
}
}

View File

@ -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);

View File

@ -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<String, Object>());
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) {