Skip to content

Commit 49f60ca

Browse files
committed
Fix a coverity issue about unchecked returns and make the code flow a
litle bit more easy to follow.
1 parent 67876b2 commit 49f60ca

8 files changed

Lines changed: 28 additions & 66 deletions

File tree

core/src/com/cloud/storage/template/IsoProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
6161
}
6262

6363
@Override
64-
public Long getVirtualSize(File file) {
64+
public long getVirtualSize(File file) {
6565
return file.length();
6666
}
6767

core/src/com/cloud/storage/template/OVAProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
8686
}
8787

8888
@Override
89-
public Long getVirtualSize(File file) {
89+
public long getVirtualSize(File file) {
9090
try {
9191
long size = getTemplateVirtualSize(file.getParent(), file.getName());
9292
return size;

core/src/com/cloud/storage/template/Processor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.cloud.storage.template;
2121

2222
import java.io.File;
23+
import java.io.IOException;
2324

2425
import com.cloud.exception.InternalErrorException;
2526
import com.cloud.storage.Storage.ImageFormat;
@@ -51,6 +52,6 @@ public static class FormatInfo {
5152
public boolean isCorrupted;
5253
}
5354

54-
Long getVirtualSize(File file);
55+
long getVirtualSize(File file) throws IOException;
5556

5657
}

core/src/com/cloud/storage/template/QCOW2Processor.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
@Local(value = Processor.class)
3838
public class QCOW2Processor extends AdapterBase implements Processor {
3939
private static final Logger s_logger = Logger.getLogger(QCOW2Processor.class);
40-
StorageLayer _storage;
40+
private static final int VIRTUALSIZE_HEADER_LOCATION = 24;
41+
42+
private StorageLayer _storage;
4143

4244
@Override
4345
public FormatInfo process(String templatePath, ImageFormat format, String templateName) {
@@ -60,52 +62,30 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
6062
File qcow2File = _storage.getFile(qcow2Path);
6163

6264
info.size = _storage.getSize(qcow2Path);
63-
FileInputStream strm = null;
64-
byte[] b = new byte[8];
65+
6566
try {
66-
strm = new FileInputStream(qcow2File);
67-
strm.skip(24);
68-
strm.read(b);
69-
} catch (Exception e) {
70-
s_logger.warn("Unable to read qcow2 file " + qcow2Path, e);
67+
info.virtualSize = getVirtualSize(qcow2File);
68+
} catch (IOException e) {
69+
s_logger.error("Unable to get virtual size from " + qcow2File.getName());
7170
return null;
72-
} finally {
73-
if (strm != null) {
74-
try {
75-
strm.close();
76-
} catch (IOException e) {
77-
}
78-
}
7971
}
8072

81-
long templateSize = NumbersUtil.bytesToLong(b);
82-
info.virtualSize = templateSize;
83-
8473
return info;
8574
}
8675

8776
@Override
88-
public Long getVirtualSize(File file) {
89-
FileInputStream strm = null;
77+
public long getVirtualSize(File file) throws IOException {
9078
byte[] b = new byte[8];
91-
try {
92-
strm = new FileInputStream(file);
93-
strm.skip(24);
94-
strm.read(b);
95-
} catch (Exception e) {
96-
s_logger.warn("Unable to read qcow2 file " + file, e);
97-
return null;
98-
} finally {
99-
if (strm != null) {
100-
try {
101-
strm.close();
102-
} catch (IOException e) {
103-
}
79+
try (FileInputStream strm = new FileInputStream(file)) {
80+
if (strm.skip(VIRTUALSIZE_HEADER_LOCATION) != VIRTUALSIZE_HEADER_LOCATION) {
81+
throw new IOException("Unable to skip to the virtual size header");
82+
}
83+
if (strm.read(b) != 8) {
84+
throw new IOException("Unable to properly read the size");
10485
}
10586
}
10687

107-
long templateSize = NumbersUtil.bytesToLong(b);
108-
return templateSize;
88+
return NumbersUtil.bytesToLong(b);
10989
}
11090

11191
@Override

core/src/com/cloud/storage/template/RawImageProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
6969
}
7070

7171
@Override
72-
public Long getVirtualSize(File file) {
72+
public long getVirtualSize(File file) {
7373
return file.length();
7474
}
7575

core/src/com/cloud/storage/template/VhdProcessor.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.File;
2323
import java.io.FileInputStream;
2424
import java.io.IOException;
25-
import java.util.Arrays;
2625
import java.util.Map;
2726

2827
import javax.ejb.Local;
@@ -105,7 +104,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
105104
}
106105

107106
@Override
108-
public Long getVirtualSize(File file) {
107+
public long getVirtualSize(File file) {
109108
FileInputStream strm = null;
110109
byte[] currentSize = new byte[8];
111110
byte[] creatorApp = new byte[4];
@@ -142,21 +141,4 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
142141
return true;
143142
}
144143

145-
private void imageSignatureCheck(byte[] creatorApp) throws InternalErrorException {
146-
boolean findKnownCreator = false;
147-
for (int i = 0; i < citrixCreatorApp.length; i++) {
148-
if (Arrays.equals(creatorApp, citrixCreatorApp[i])) {
149-
findKnownCreator = true;
150-
break;
151-
}
152-
}
153-
if (!findKnownCreator) {
154-
/*Only support VHD image created by citrix xenserver, and xenconverter*/
155-
String readableCreator = "";
156-
for (int j = 0; j < creatorApp.length; j++) {
157-
readableCreator += (char)creatorApp[j];
158-
}
159-
throw new InternalErrorException("Image creator is:" + readableCreator + ", is not supported");
160-
}
161-
}
162144
}

core/src/com/cloud/storage/template/VmdkProcessor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
import java.io.BufferedReader;
2323
import java.io.File;
24-
import java.io.FileReader;
2524
import java.io.FileNotFoundException;
25+
import java.io.FileReader;
2626
import java.io.IOException;
2727
import java.util.Map;
28-
import java.util.regex.Pattern;
2928
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
3030

3131
import javax.ejb.Local;
3232
import javax.naming.ConfigurationException;
@@ -72,7 +72,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
7272
}
7373

7474
@Override
75-
public Long getVirtualSize(File file) {
75+
public long getVirtualSize(File file) {
7676
try {
7777
long size = getTemplateVirtualSize(file.getParent(), file.getName());
7878
return size;
@@ -86,8 +86,6 @@ public long getTemplateVirtualSize(String templatePath, String templateName) thr
8686
long virtualSize = 0;
8787
String templateFileFullPath = templatePath.endsWith(File.separator) ? templatePath : templatePath + File.separator;
8888
templateFileFullPath += templateName.endsWith(ImageFormat.VMDK.getFileExtension()) ? templateName : templateName + "." + ImageFormat.VMDK.getFileExtension();
89-
String vmdkHeader = "";
90-
9189
try {
9290
FileReader fileReader = new FileReader(templateFileFullPath);
9391
BufferedReader bufferedReader = new BufferedReader(fileReader);

services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ private ImageFormat getTemplateFormat(String filePath) {
798798

799799
}
800800

801-
protected Long getVirtualSize(File file, ImageFormat format) {
801+
protected long getVirtualSize(File file, ImageFormat format) {
802802
Processor processor = null;
803803
try {
804804
if (format == null) {
@@ -822,9 +822,10 @@ protected Long getVirtualSize(File file, ImageFormat format) {
822822
processor.configure("template processor", new HashMap<String, Object>());
823823
return processor.getVirtualSize(file);
824824
} catch (Exception e) {
825-
s_logger.debug("Failed to get virtual size:", e);
825+
s_logger.warn("Failed to get virtual size, returning file size instead:", e);
826+
return file.length();
826827
}
827-
return file.length();
828+
828829
}
829830

830831
protected Answer copyFromNfsToS3(CopyCommand cmd) {

0 commit comments

Comments
 (0)