#!/usr/bin/perl -w

use strict;
use Sys::Guestfs;

if (@ARGV != 2) {
    die "usage: $0 guestname C|D|E|...\n"
}

my $guest = $ARGV[0];
my $drive = $ARGV[1];

my $g = new Sys::Guestfs ();

# Attach the disk image read-only to libguestfs.
$g->add_domain ($guest, readonly => 1);

# Run the libguestfs back-end.
$g->launch ();

# Ask libguestfs to inspect for operating systems.
my @roots = $g->inspect_os ();
if (@roots == 0) {
    die "$0: no operating systems found\n";
}
if (@roots != 1) {
    die "$0: multi-boot operating systems not supported\n";
}
my $root = $roots[0];
if ($g->inspect_get_type ($root) ne "windows") {
    die "$0: guest operating system must be windows\n";
}

# Get drive mappings.
my %map = $g->inspect_get_drive_mappings ($root);
unless (exists $map{$drive}) {
    print STDERR "$0: drive mapping $drive not found\n";
    print STDERR "Did you mean one of these?\n";
    foreach my $d (sort keys %map) {
        print STDERR "    $d  (", $map{$d}, ")\n";
    }
    exit 1
}

# Mount the chosen disk.
$g->mount_ro ($map{$drive}, "/");

# Display list of files.  These are NUL-terminated so we
# must translate the output.
open PIPE, "| tr '\\0' '\\n'" or die "tr: $!";
my $fd = fileno PIPE;
$g->find0 ("/", "/dev/fd/$fd");
close PIPE or die "tr: $!";
