Further implement the info method Also, use a HashMap<String, String> for storing key value pairs
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/ad9a20e0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/ad9a20e0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/ad9a20e0 Branch: refs/heads/qemu-img Commit: ad9a20e0e23b87ce553ab4366a7b82aa1f6f2271 Parents: 4d39ad4 Author: Wido den Hollander <[email protected]> Authored: Mon Feb 18 14:34:23 2013 +0100 Committer: Wido den Hollander <[email protected]> Committed: Mon Feb 18 14:34:23 2013 +0100 ---------------------------------------------------------------------- .../org/apache/cloudstack/utils/qemu/QemuImg.java | 38 +++++++++++---- 1 files changed, 29 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ad9a20e0/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java ---------------------------------------------------------------------- diff --git a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java index ae865a5..dfaf275 100644 --- a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java +++ b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java @@ -95,7 +95,7 @@ public class QemuImg { } /* Convert the disk image filename or a snapshot snapshot_name to disk image output_filename using format output_fmt. */ - public void convert(QemuImgFile srcFile, QemuImgFile destFile, List<Map<String, String>> options) { + public void convert(QemuImgFile srcFile, QemuImgFile destFile, Map<String, String> options) { Script s = new Script(_qemuImgPath); s.add("convert"); s.add("-f"); @@ -116,26 +116,46 @@ public class QemuImg { } - /* Give information about the disk image */ - public List<Map<String, String>> info(QemuImgFile file) { + /** + * Execute qemu-img info for the given file + * + * Qemu-img returns human readable output, but this method does it's best + * to turn that into machine readeable data. + * + * Spaces in keys are replaced by underscores (_). + * Sizes (virtual_size and disk_size) are returned in bytes + * Paths (image and backing_file) are the absolute path to the file + * + * @param file + * A QemuImgFile object containing the file to get the information from + * @return A HashMap with String key-value information as returned by 'qemu-img info' + * @throws LibvirtException + */ + public Map<String, String> info(QemuImgFile file) { Script s = new Script(_qemuImgPath); s.add("info"); s.add(file.getFileName()); OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); s.execute(parser); - List<Map<String, String>> list = new ArrayList<Map<String, String>>(); + HashMap<String,String> info = new HashMap<String,String>(); String[] outputBuffer = parser.getLines().trim().split("\n"); for (int i = 0; i < outputBuffer.length; i++) { String[] lineBuffer = outputBuffer[i].split(":", 2); if (lineBuffer.length == 2) { - HashMap<String,String> info = new HashMap<String,String>(); - info.put(lineBuffer[0].trim().replace(" ", "_"), lineBuffer[1].trim()); - list.add(info); + String key = lineBuffer[0].trim().replace(" ", "_"); + String value = null; + + if (key.equals("virtual_size")) { + value = lineBuffer[1].trim().replaceAll("^.*\\(([0-9]+).*$", "$1"); + } else { + value = lineBuffer[1].trim(); + } + + info.put(key, value); } } - return list; - + return info; } /* List, apply, create or delete snapshots in image */
