weizhouapache commented on code in PR #10896: URL: https://github.com/apache/cloudstack/pull/10896#discussion_r2097723392
########## services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/formatinspector/Qcow2Inspector.java: ########## @@ -108,6 +109,32 @@ public static Map<String, byte[]> unravelQcow2Header(InputStream qcow2InputStrea return result; } + + /** + * Validates if the file has a minimum version. + * @param filePath Path of the file to be validated. + * @param minVersion the minimum version that it should contain. + * @throws RuntimeException If a IOException is thrown. + * @return true if file version is >= minVersion, false otherwise. + */ + public static boolean validateQcow2Version(String filePath, int minVersion) { + try (InputStream inputStream = new FileInputStream(filePath)) { + for (Qcow2HeaderField qcow2Header : Qcow2HeaderField.values()) { + if (qcow2Header != Qcow2HeaderField.VERSION) { + skipHeader(inputStream, qcow2Header, filePath); + continue; + } + + byte[] headerValue = readHeader(inputStream, qcow2Header, filePath); + int version = new BigInteger(headerValue).intValue(); + return version >= minVersion; + } + } catch (IOException ex) { + throw new RuntimeException(String.format("Unable to validate file [%s] due to: ", filePath), ex); Review Comment: use `CloudRuntimeException` instead ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org