|
18 | 18 | package org.apache.cloudstack.storage.formatinspector; |
19 | 19 |
|
20 | 20 | import com.cloud.utils.NumbersUtil; |
| 21 | +import com.cloud.utils.exception.CloudRuntimeException; |
21 | 22 | import org.apache.commons.lang3.ArrayUtils; |
22 | 23 | import org.apache.logging.log4j.LogManager; |
23 | 24 | import org.apache.logging.log4j.Logger; |
24 | 25 |
|
25 | 26 | import java.io.FileInputStream; |
26 | 27 | import java.io.IOException; |
27 | 28 | import java.io.InputStream; |
| 29 | +import java.math.BigInteger; |
28 | 30 | import java.util.Arrays; |
29 | 31 | import java.util.HashMap; |
30 | 32 | import java.util.Map; |
@@ -108,6 +110,32 @@ public static Map<String, byte[]> unravelQcow2Header(InputStream qcow2InputStrea |
108 | 110 | return result; |
109 | 111 | } |
110 | 112 |
|
| 113 | + |
| 114 | + /** |
| 115 | + * Validates if the file has a minimum version. |
| 116 | + * @param filePath Path of the file to be validated. |
| 117 | + * @param minVersion the minimum version that it should contain. |
| 118 | + * @throws RuntimeException If a IOException is thrown. |
| 119 | + * @return true if file version is >= minVersion, false otherwise. |
| 120 | + */ |
| 121 | + public static boolean validateQcow2Version(String filePath, int minVersion) { |
| 122 | + try (InputStream inputStream = new FileInputStream(filePath)) { |
| 123 | + for (Qcow2HeaderField qcow2Header : Qcow2HeaderField.values()) { |
| 124 | + if (qcow2Header != Qcow2HeaderField.VERSION) { |
| 125 | + skipHeader(inputStream, qcow2Header, filePath); |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + byte[] headerValue = readHeader(inputStream, qcow2Header, filePath); |
| 130 | + int version = new BigInteger(headerValue).intValue(); |
| 131 | + return version >= minVersion; |
| 132 | + } |
| 133 | + } catch (IOException ex) { |
| 134 | + throw new CloudRuntimeException(String.format("Unable to validate file [%s] due to: ", filePath), ex); |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
111 | 139 | /** |
112 | 140 | * Skips the field's length in the InputStream. |
113 | 141 | * @param qcow2InputStream InputStream of the QCOW2 being unraveled. |
|
0 commit comments