|
From: <fd...@us...> - 2007-03-31 16:55:19
|
Revision: 3148
http://jnode.svn.sourceforge.net/jnode/?rev=3148&view=rev
Author: fduminy
Date: 2007-03-31 09:55:17 -0700 (Sat, 31 Mar 2007)
Log Message:
-----------
new version with some refactoring, more tests ...
- working disk type detection
- working read/write of headers only
- read/write of sectors still buggy
Added Paths:
-----------
trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/
trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/BaseReadWriteTest.java
trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/TestHeader.java
trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/
trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskCopier.java
trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskFactory.java
Added: trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/BaseReadWriteTest.java
===================================================================
--- trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/BaseReadWriteTest.java (rev 0)
+++ trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/BaseReadWriteTest.java 2007-03-31 16:55:17 UTC (rev 3148)
@@ -0,0 +1,163 @@
+package org.jnode.apps.vmware.disk.test.readwrite;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.apache.log4j.Logger;
+import org.jnode.apps.vmware.disk.IOUtils;
+import org.jnode.apps.vmware.disk.VMWareDisk;
+import org.jnode.apps.vmware.disk.handler.IOHandler;
+import org.jnode.apps.vmware.disk.test.BaseTest;
+import org.jnode.apps.vmware.disk.test.Utils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Wrote from the 'Virtual Disk Format 1.0' specifications (from VMWare)
+ *
+ * @author Fabien DUMINY (fduminy at jnode dot org)
+ *
+ */
+@RunWith(value = Parameterized.class)
+abstract public class BaseReadWriteTest extends BaseTest {
+ private static final Logger LOG = Logger.getLogger(BaseReadWriteTest.class);
+
+ private static final String DISKS_PACKAGE = "/org/jnode/apps/vmware/disk/test/disks/";
+ private static final String DISK_BASE_NAME = "Menuet32-";
+
+ private static final String RESTRICT_TO_FILE_NAME = "Menuet32-0";
+ //private static final String RESTRICT_TO_FILE_NAME = null;
+
+ private static final String DISKS_PATH;
+ static
+ {
+ String classpath = System.getProperty("java.class.path");
+ StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator, false);
+ File foundPath = null;
+ while(tokenizer.hasMoreTokens())
+ {
+ File path = new File(tokenizer.nextToken());
+ if(path.isDirectory())
+ {
+ path = new File(path, DISKS_PACKAGE);
+ if(path.exists())
+ {
+ foundPath = path;
+ break;
+ }
+ }
+ }
+ if(foundPath == null)
+ {
+ throw new RuntimeException("directory not found for package "+DISKS_PACKAGE);
+ }
+
+ DISKS_PATH = foundPath.getAbsolutePath();
+
+ System.out.println("\ndirectory for package "+DISKS_PACKAGE+" : "+DISKS_PATH);
+ }
+
+ @Parameters
+ public static List<File[]> data()
+ {
+ File directory = new File(DISKS_PATH);
+ File[] files = directory.listFiles(new FilenameFilter()
+ {
+ public boolean accept(File dir, String name) {
+ boolean ok = name.matches(DISK_BASE_NAME + "[0-9]*.vmdk");
+
+ if(RESTRICT_TO_FILE_NAME != null)
+ {
+ ok &= name.startsWith(RESTRICT_TO_FILE_NAME);
+ }
+
+ return ok;
+ }
+ });
+ List<File[]> list = new ArrayList<File[]>(files.length);
+ for(File f : files)
+ {
+ list.add(new File[]{f});
+ }
+
+ return list;
+ }
+
+ public BaseReadWriteTest(File diskFile) throws IOException
+ {
+ super(diskFile, true);
+ }
+
+ @Test
+ @Ignore
+ public void read() throws Exception
+ {
+ VMWareDisk disk = new VMWareDisk(diskFile);
+
+ ByteBuffer data = IOUtils.allocate(IOHandler.SECTOR_SIZE * 100);
+ disk.read(0, data);
+
+ Assert.assertEquals(toString()+": buffer should be filled", 0, data.remaining());
+ }
+
+ @Test
+ @Ignore
+ public void write() throws Exception
+ {
+ VMWareDisk disk = new VMWareDisk(diskFile);
+
+ ByteBuffer data = IOUtils.allocate(IOHandler.SECTOR_SIZE * 100);
+ disk.write(0, data);
+
+ Assert.assertEquals(toString()+": buffer should be fully copied", 0, data.remaining());
+ }
+
+ @Test
+ public void writeAndRead() throws Exception
+ {
+ Utils.DO_CLEAR = false;
+
+ LOG.info("BEGIN writeAndRead");
+ VMWareDisk disk = new VMWareDisk(diskFile);
+
+ // write
+ LOG.info("writeAndRead: writing...");
+ int size = IOHandler.SECTOR_SIZE * 100;
+ ByteBuffer expectedData = IOUtils.allocate(size);
+ for(int i = 0 ; i < (size / 4) ; i++)
+ {
+ expectedData.putInt(i);
+ }
+ expectedData.rewind();
+ disk.write(0, expectedData);
+ disk.flush();
+
+ // read
+ LOG.info("writeAndRead: reading...");
+ VMWareDisk disk2 = new VMWareDisk(diskFile);
+ Assert.assertEquals("disk has different size", disk.getLength(), disk2.getLength());
+ Assert.assertEquals("disk has different descriptor", disk.getDescriptor(), disk2.getDescriptor());
+
+ expectedData.rewind();
+ ByteBuffer actualData = IOUtils.allocate(size);
+ disk2.read(0, actualData);
+ for(int i = 0 ; i < (size / 4) ; i++)
+ {
+ int actual = actualData.getInt(i);
+ int expected = expectedData.getInt();
+ Assert.assertEquals("bad data at index "+(i*4), expected, actual);
+ }
+ LOG.info("END writeAndRead");
+ }
+}
Added: trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/TestHeader.java
===================================================================
--- trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/TestHeader.java (rev 0)
+++ trunk/distr/src/apps/org/jnode/apps/vmware/disk/test/readwrite/TestHeader.java 2007-03-31 16:55:17 UTC (rev 3148)
@@ -0,0 +1,73 @@
+package org.jnode.apps.vmware.disk.test.readwrite;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jnode.apps.vmware.disk.IOUtils;
+import org.jnode.apps.vmware.disk.VMWareDisk;
+import org.jnode.apps.vmware.disk.handler.IOHandler;
+import org.jnode.apps.vmware.disk.test.Utils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Wrote from the 'Virtual Disk Format 1.0' specifications (from VMWare)
+ *
+ * @author Fabien DUMINY (fduminy at jnode dot org)
+ *
+ */
+public class TestHeader extends BaseReadWriteTest
+{
+ private static final Logger LOG = Logger.getLogger(TestHeader.class);
+
+ public TestHeader(File diskFile) throws IOException
+ {
+ super(diskFile);
+ }
+
+ @Test
+ public void read() throws Exception
+ {
+ VMWareDisk disk = new VMWareDisk(diskFile);
+ disk.flush();
+ }
+
+ @Test
+ public void write() throws Exception
+ {
+ VMWareDisk disk = new VMWareDisk(diskFile);
+ disk.flush();
+ }
+
+ @Test
+ public void writeAndRead() throws Exception
+ {
+ Utils.DO_CLEAR = false;
+
+ LOG.info("BEGIN writeAndRead");
+ VMWareDisk disk = new VMWareDisk(diskFile);
+
+ // write
+ LOG.info("writeAndRead: writing...");
+ disk.flush();
+
+ // read
+ LOG.info("writeAndRead: reading...");
+ VMWareDisk disk2 = new VMWareDisk(diskFile);
+ Assert.assertEquals("disk has different size", disk.getLength(), disk2.getLength());
+ Assert.assertEquals("disk has different descriptor", disk.getDescriptor(), disk2.getDescriptor());
+ disk2.flush();
+ LOG.info("END writeAndRead");
+ }
+}
Added: trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskCopier.java
===================================================================
--- trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskCopier.java (rev 0)
+++ trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskCopier.java 2007-03-31 16:55:17 UTC (rev 3148)
@@ -0,0 +1,90 @@
+package org.jnode.apps.vmware.disk.tools;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+import org.apache.log4j.Logger;
+import org.jnode.apps.vmware.disk.test.Utils;
+
+public class DiskCopier {
+ private static final Logger LOG = Logger.getLogger(Utils.class);
+
+ public static File copyDisk(final File mainFile, final File toDirectory)
+ throws IOException
+ {
+ final String name = mainFile.getName();
+ final int idx = name.lastIndexOf('.');
+ final String beginName = name.substring(0, idx);
+ final String endName = name.substring(idx);
+
+ final File parentDir = mainFile.getParentFile();
+ File[] files = parentDir.listFiles(new FilenameFilter()
+ {
+ public boolean accept(File dir, String name) {
+ boolean ok = name.startsWith(beginName) &&
+ name.endsWith(endName);
+ return ok;
+ }
+ });
+
+ File mainFileCopy = null;
+ for(File file : files)
+ {
+ File f = copyFile(file, toDirectory);
+ if(file.getName().equals(mainFile.getName()))
+ {
+ mainFileCopy = f;
+ }
+ }
+
+ return mainFileCopy;
+ }
+
+
+ public static File copyFile(File file, File dir) throws IOException
+ {
+ LOG.debug("copying file "+file.getName()+" to "+dir.getName());
+ FileInputStream fis = null;
+ FileOutputStream fos = null;
+ File outFile = null;
+
+ try
+ {
+ fis = new FileInputStream(file);
+ FileChannel inCh = fis.getChannel();
+
+ outFile = new File(dir, file.getName());
+ fos = new FileOutputStream(outFile);
+ FileChannel outCh = fos.getChannel();
+
+ outCh.transferFrom(inCh, 0, inCh.size());
+
+ return outFile;
+ }
+ catch(IOException ioe)
+ {
+ ioe.printStackTrace();
+ throw ioe;
+ }
+ finally
+ {
+ try {
+ if(fos != null)
+ {
+ fos.close();
+ }
+ }
+ finally
+ {
+ if(fis != null)
+ {
+ fis.close();
+ }
+ }
+ }
+ }
+}
Added: trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskFactory.java
===================================================================
--- trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskFactory.java (rev 0)
+++ trunk/distr/src/apps/org/jnode/apps/vmware/disk/tools/DiskFactory.java 2007-03-31 16:55:17 UTC (rev 3148)
@@ -0,0 +1,42 @@
+package org.jnode.apps.vmware.disk.tools;
+
+import java.io.File;
+
+import org.jnode.apps.vmware.disk.handler.sparse.SparseDiskFactory;
+
+/**
+ * Wrote from the 'Virtual Disk Format 1.0' specifications (from VMWare)
+ *
+ * @author Fabien DUMINY (fduminy at jnode dot org)
+ *
+ */
+abstract public class DiskFactory
+{
+ private static final SparseDiskFactory SPARSE_FACTORY = new SparseDiskFactory();
+
+ public static File createSparseDisk(File directory, String name, long size)
+ {
+ return SPARSE_FACTORY.createDisk(directory, name, size);
+ }
+
+ public File createDisk(File directory, String name, long size)
+ {
+ if(!directory.isDirectory())
+ {
+ throw new IllegalArgumentException(directory.getAbsolutePath()+" is not a directory");
+ }
+ if(!directory.canWrite())
+ {
+ throw new IllegalArgumentException(directory.getAbsolutePath()+" must be writable");
+ }
+
+ File mainFile = new File(directory, name);
+ if(!createDiskImpl(mainFile, size))
+ {
+ mainFile = null;
+ }
+ return mainFile;
+ }
+
+ abstract protected boolean createDiskImpl(File mainFile, long size);
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|