What am I doing wrong?

Mar 20, 2012 at 7:14 PM
Edited Mar 21, 2012 at 10:00 PM

please tell me what i am doing wrong! 

I keep getting a DivideByZero Exception. I dont understand why, I followed the same what other Devs used.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscUtils;
using System.IO;
using DiscUtils.Partitions;
using DiscUtils.Ntfs;

namespace FYP_VIE_1._0.Business_Logic
{
    class Controler
    {


        //private DiskImageFile OpenFile(string path)
        //{
        //    return new DiscUtils.Raw.Disk(new FileStream(path, FileMode.Open, FileAccess.Read), Ownership.Dispose, null);
        //}

        private VirtualDisk OpenDisk(string path)
        {
            return new DiscUtils.Vmdk.Disk(new FileStream(path, FileMode.Open, FileAccess.ReadWrite), Ownership.None);
        }


        /**
         * 
         * Lists the Current Directory
         * 
         */
        public void listDir()
        {
            using (VirtualDisk disc = OpenDisk(@"C:\Users\Paul Phillips\Desktop\FYP_VIE_1.0\FYP_VIE_1.0\TestData\FYPDATA.vmdk"))
            {
                Console.WriteLine(disc.Partitions.Count);
                using (SparseStream stream = disc.Partitions[0].Open())
                {
                    try
                    {
                        DiscUtils.Ntfs.NtfsFileSystem ntfs = new DiscUtils.Ntfs.NtfsFileSystem(stream);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
            }
        }




        /** export a File in to the image
         * @returns
         *  True - if the file is successfully exported
         *  False - if the file cannot be exported.
         * 
         * @param name="fileExport"
         *  An object with the information about the file to be Exported
         */
        public bool exportFile(FileItem fileExport)
        {
            return false;
        }

        /** Imports a File in to the image
         * @returns
         *  True - if the file is successfully imported
         *  False - if the file cannot be imported.
         * 
         * @param name="fileImport"
         *  An object with the information about the file to be imported
         */ 
         public bool importFile(FileItem fileImport)
        {
            return false;
        }

    }
}
Coordinator
Mar 22, 2012 at 9:18 AM

Probably a bug in DiscUtils - can you post the details of the exception you're seeing, please.

Ken

Mar 22, 2012 at 10:19 AM
Edited Mar 22, 2012 at 1:08 PM

Hi, Thanks,

This is what i get with the VMDK.

   Attempted to divide by zero.
   at DiscUtils.BlockCacheStream..ctor(SparseStream toWrap, Ownership ownership, BlockCacheSettings settings)
   at DiscUtils.Ntfs.NtfsFileSystem..ctor(Stream stream)
   at FYP_VIE_1._0.Business_Logic.Controler.listDir() in C:\Users\Paul Phillips\Desktop\FYP_VIE_1.0\FYP_VIE_1.0\Business_Logic\Controler.cs:line 118

Mar 22, 2012 at 8:19 PM

I've used diferent files now, VMDK, VHD and even RAW IMG file. All seem to get the same error above.

Coordinator
Mar 23, 2012 at 12:20 AM

Hi,

I think the most likely thing is that there's no NTFS file system on the partition - the error checking is inadequate in the NtfsFileSystem class, so you're seeing a misleading exception.  It looks like a field in the NTFS header is Zero - causing the exception.

Can you try running the diskdump utility on one of the images to confirm the partition structure and which partitions are formatted:

ntfsdump -sf FYPDATA.vmdk

If the disk is formatted, can you send me a sample, I can take a further look.

 

Cheers,

Ken

Mar 23, 2012 at 12:43 AM

Hi,

The image is NTFS formated as it loads within a virtual machine.

When i run the ntfs dump i get the following error.

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at DiscUtils.BlockCacheStream..ctor(SparseStream toWrap, Ownership ownership
 BlockCacheSettings settings)
   at DiscUtils.Ntfs.NtfsFileSystem..ctor(Stream stream)
   at NTFSDump.Program.DoRun()
   at DiscUtils.Common.ProgramBase.Run(String[] args)
   at NTFSDump.Program.Main(String[] args)

Its very puzzeling, I managed to create a FAT VHD image and everything works OK, Just seems strange that externally created files dont seem to work.

 

Thanks for helping,

Paul

Coordinator
Mar 23, 2012 at 12:48 AM

The output of diskdump would be useful - should show the partition struture (if you avoid the -sf param, then it shouldn't attempt to read the file system).

It's possible the partition structure is confusing DiscUtils...

Cheers,

Ken

Mar 23, 2012 at 12:52 AM

it errors saying that there is no switch -sf

 

Unhandled Exception: System.Exception: Unrecognized command-line switch '-sf'
   at DiscUtils.Common.CommandLineParser.Parse(String[] args)
   at DiscUtils.Common.ProgramBase.Run(String[] args)
   at NTFSDump.Program.Main(String[] args)

Coordinator
Mar 23, 2012 at 12:53 AM

Ah - can you run diskdump, not ntfsdump.  DiskDump should show the partition structure, and accepts the '-sf' parameter.

Cheers,

Ken

Mar 23, 2012 at 12:56 AM

Ahh, Should of thought of that.

 

DiskDump v0.10.0, available from http://discutils.codeplex.com
Copyright (c) Kenneth Bell, 2008-2011
Free software issued under the MIT License, see LICENSE.TXT for details.


DISK: FYPDATA.vmdk

       Capacity: 0000000080000000
       Geometry: (4161/16/63)
  BIOS Geometry: (520/128/63)
      Signature: AB816D04
           GUID: 00000000-0000-0000-0000-000000000000


  Stored Extents

    0000000000000000 - 0000000000040000
    0000000000A30000 - 0000000000A40000
    0000000001530000 - 0000000001540000
    0000000001570000 - 0000000001590000
    0000000029130000 - 0000000029D70000
    0000000029DA0000 - 0000000029DB0000
    0000000029EA0000 - 0000000029EC0000
    000000002A8B0000 - 000000002A8D0000
    000000002A9A0000 - 000000002AA00000
    000000007FD00000 - 000000007FD10000


  Partitions

    T   Start (bytes)     End (bytes)       Type
    ==  ================  ================  ==================
    07  0000000000007E00  000000007FF80000  IFS (NTFS or HPFS)
        (0/1/1)           (519/127/63)

 

VOLUMES


  Physical Volumes

  VPD:DSAB816D04:PO7E00
    Type: BiosPartition
    BIOS Type: 07 [IFS (NTFS or HPFS)]
    Size: 2146927104
    Disk Id: 00000000-0000-0000-0000-000000000000
    Disk Sig: AB816D04
    Partition: 00000000-0000-0000-0000-000000000000
    Disk Geometry: (4161/16/63)
    BIOS Geometry: (520/128/63)
    First Sector: 63


  Logical Volumes

  VLP:VPD:DSAB816D04:PO7E00
    BIOS Type: 07 [IFS (NTFS or HPFS)]
    Status: Healthy
    Size: 2146927104
    Disk Geometry: (4161/16/63)
    BIOS Geometry: (520/128/63)
    First Sector: 63
    File Systems:

 

Coordinator
Mar 23, 2012 at 1:02 AM

Thanks - it's not detecting the file system, but the Stored Extents definately suggest there's something there.

I'll need to get a copy of a VMDK that's showing the problem to investigate further.

 

Cheers,

Ken

Mar 23, 2012 at 1:12 AM

Hi.

you should be able to download it from here,

 

http://educatedchimp.co.uk/downloads/

Coordinator
Mar 23, 2012 at 1:30 AM

It looks like the partition table doesn't match the actual partition layout.  If you look at the output of 'diskdump -db', you can see that:

The partition table is self-consistent:

  1. There are 63 sectors per track
  2. The first partition starts at offset 0x7E00 (which is 0/1/1 in sector geometry)

However, the output of diskdump -db shows the partition data actually starts at offset 0x10000 (look for the string '.R.NTFS').  Key values in red.

DiskDump v0.11.0, available from http://discutils.codeplex.com
Copyright (c) Kenneth Bell, 2008-2011
Free software issued under the MIT License, see LICENSE.TXT for details.


DISK: c:\temp\FYPDATA.vmdk

       Capacity: 0000000080000000
       Geometry: (4161/16/63)
  BIOS Geometry: (520/128/63)
      Signature: AB816D04
           GUID: 00000000-0000-0000-0000-000000000000


  Stored Extents

    0000000000000000 - 0000000000040000
    0000000000A30000 - 0000000000A40000
    0000000001530000 - 0000000001540000
    0000000001570000 - 0000000001590000
    0000000029130000 - 0000000029D70000
    0000000029DA0000 - 0000000029DB0000
    0000000029EA0000 - 0000000029EC0000
    000000002A8B0000 - 000000002A8D0000
    000000002A9A0000 - 000000002AA00000
    000000007FD00000 - 000000007FD10000


  Partitions

    T   Start (bytes)     End (bytes)       Type
    ==  ================  ================  ==================
    07  0000000000007E00  000000007FF80000  IFS (NTFS or HPFS)
        (0/1/1)           (519/127/63)



VOLUMES


  Physical Volumes

  VPD:DSAB816D04:PO7E00
    Type: BiosPartition
    BIOS Type: 07 [IFS (NTFS or HPFS)]
    Size: 2146927104
    Disk Id: 00000000-0000-0000-0000-000000000000
    Disk Sig: AB816D04
    Partition: 00000000-0000-0000-0000-000000000000
    Disk Geometry: (4161/16/63)
    BIOS Geometry: (520/128/63)
    First Sector: 63


  Logical Volumes

  VLP:VPD:DSAB816D04:PO7E00
    BIOS Type: 07 [IFS (NTFS or HPFS)]
    Status: Healthy
    Size: 2146927104
    Disk Geometry: (4161/16/63)
    BIOS Geometry: (520/128/63)
    First Sector: 63
    File Systems: 


DISK CONTENTS (c:\temp\FYPDATA.vmdk)

00000000  33 c0 8e d0 bc 00 7c 8e  c0 8e d8 be 00 7c bf 00  |3.....|. .....|..|
00000010  06 b9 00 02 fc f3 a4 50  68 1c 06 cb fb b9 04 00  |.......P h.......|
00000020  bd be 07 80 7e 00 00 7c  0b 0f 85 0e 01 83 c5 10  |....~..| ........|
00000030  e2 f1 cd 18 88 56 00 55  c6 46 11 05 c6 46 10 00  |.....V.U .F...F..|
00000040  b4 41 bb aa 55 cd 13 5d  72 0f 81 fb 55 aa 75 09  |.A..U..] r...U.u.|
00000050  f7 c1 01 00 74 03 fe 46  10 66 60 80 7e 10 00 74  |....t..F .f`.~..t|
00000060  26 66 68 00 00 00 00 66  ff 76 08 68 00 00 68 00  |&fh....f .v.h..h.|
00000070  7c 68 01 00 68 10 00 b4  42 8a 56 00 8b f4 cd 13  ||h..h... B.V.....|
00000080  9f 83 c4 10 9e eb 14 b8  01 02 bb 00 7c 8a 56 00  |........ ....|.V.|
00000090  8a 76 01 8a 4e 02 8a 6e  03 cd 13 66 61 73 1c fe  |.v..N..n ...fas..|
000000a0  4e 11 75 0c 80 7e 00 80  0f 84 8a 00 b2 80 eb 84  |N.u..~.. ........|
000000b0  55 32 e4 8a 56 00 cd 13  5d eb 9e 81 3e fe 7d 55  |U2..V... ]...>.}U|
000000c0  aa 75 6e ff 76 00 e8 8d  00 75 17 fa b0 d1 e6 64  |.un.v... .u.....d|
000000d0  e8 83 00 b0 df e6 60 e8  7c 00 b0 ff e6 64 e8 75  |......`. |....d.u|
000000e0  00 fb b8 00 bb cd 1a 66  23 c0 75 3b 66 81 fb 54  |.......f #.u;f..T|
000000f0  43 50 41 75 32 81 f9 02  01 72 2c 66 68 07 bb 00  |CPAu2... .r,fh...|
00000100  00 66 68 00 02 00 00 66  68 08 00 00 00 66 53 66  |.fh....f h....fSf|
00000110  53 66 55 66 68 00 00 00  00 66 68 00 7c 00 00 66  |SfUfh... .fh.|..f|
00000120  61 68 00 00 07 cd 1a 5a  32 f6 ea 00 7c 00 00 cd  |ah.....Z 2...|...|
00000130  18 a0 b7 07 eb 08 a0 b6  07 eb 03 a0 b5 07 32 e4  |........ ......2.|
00000140  05 00 07 8b f0 ac 3c 00  74 09 bb 07 00 b4 0e cd  |......<. t.......|
00000150  10 eb f2 f4 eb fd 2b c9  e4 64 eb 00 24 02 e0 f8  |......+. .d..$...|
00000160  24 02 c3 49 6e 76 61 6c  69 64 20 70 61 72 74 69  |$..Inval id parti|
00000170  74 69 6f 6e 20 74 61 62  6c 65 00 45 72 72 6f 72  |tion tab le.Error|
00000180  20 6c 6f 61 64 69 6e 67  20 6f 70 65 72 61 74 69  | loading  operati|
00000190  6e 67 20 73 79 73 74 65  6d 00 4d 69 73 73 69 6e  |ng syste m.Missin|
000001a0  67 20 6f 70 65 72 61 74  69 6e 67 20 73 79 73 74  |g operat ing syst|
000001b0  65 6d 00 00 00 63 7b 9a  04 6d 81 ab 00 00 80 01  |em...c{. .m......|
000001c0  01 00 07 7f bf 07 3f 00  00 00 c1 fb 3f 00 00 00  |......?. ....?...|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |........ ........|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |........ ......U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |........ ........|
00010000  eb 52 90 4e 54 46 53 20  20 20 20 00 02 08 00 00  |.R.NTFS     .....|
00010010  00 00 00 00 00 f8 00 00  3f 00 ff 00 80 00 00 00  |........ ?.......|
00010020  00 00 00 00 80 00 80 00  ff e7 3f 00 00 00 00 00  |........ ..?.....|
00010030  aa a9 02 00 00 00 00 00  02 00 00 00 00 00 00 00  |........ ........|
00010040  f6 00 00 00 01 00 00 00  85 cd 6a 7c 13 6b 7c 76  |........ ..j|.k|v|
00010050  00 00 00 00 fa 33 c0 8e  d0 bc 00 7c fb 68 c0 07  |.....3.. ...|.h..|

 

I'm not sure how you created the VMDK, but I think it's likely there's a problem there somewhere.

 

Cheers,

Ken

 

Mar 23, 2012 at 1:34 AM

Hmm, it was created using virtualbox. I'll try VMware to see if that works.

 

Cheers,

Thank you very much,

 

Paul

Coordinator
Mar 23, 2012 at 1:38 AM

For what it's worth, I just tried to map the disk into Windows using VMware Workstation - Windows reckons the disk is blank and needs to be formatted...

Cheers,

Ken

Mar 23, 2012 at 4:04 PM

Hi Ken,

I've tried with vmware workstation and it seems to work. Yay.

 

cheers,

paul