Пример #1
0
        public void VhdFooter_Checksum()
        {
            HardDiskFooter target        = new HardDiskFooter();
            var            firstChecksum = BitConverter.ToString(target.Checksum);

            target.BeginUpdate();
            target.UniqueId = Guid.NewGuid();
            Assert.AreEqual(firstChecksum, BitConverter.ToString(target.Checksum));
            Assert.AreEqual(false, target.IsChecksumCorrect);
            target.EndUpdate();
            Assert.AreNotEqual(firstChecksum, BitConverter.ToString(target.Checksum));
            Assert.AreEqual(true, target.IsChecksumCorrect);
        }
Пример #2
0
        private bool CreateVhd()
        {
            using (var stream = new FileStream(this.FileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 1, FileOptions.WriteThrough)) {
                ReFS.RemoveIntegrityStream(stream.SafeFileHandle);

                var footer = new HardDiskFooter();
                footer.BeginUpdate();
                footer.CreatorApplication = VhdCreatorApplication.JosipMedvedVhdAttach;
                footer.CreatorVersion     = Medo.Reflection.EntryAssembly.Version;
                footer.SetSize((UInt64)this.SizeInBytes);
                footer.OriginalSize = footer.CurrentSize;
                footer.DiskType     = VhdDiskType.FixedHardDisk;

                footer.EndUpdate();

                var    lastReport = DateTime.UtcNow;
                byte[] buffer     = new byte[Settings.WriteBufferSize];
                ulong  remaining  = footer.CurrentSize;
                while (remaining > 0)
                {
                    if (bgw.CancellationPending)
                    {
                        stream.Dispose();
                        File.Delete(this.FileName);
                        return(false);
                    }
                    ulong count = (ulong)buffer.Length;
                    if ((ulong)count > remaining)
                    {
                        count = remaining;
                    }
                    stream.Write(buffer, 0, (int)count);
                    remaining -= count;
                    if (lastReport.AddSeconds(1) < DateTime.UtcNow)
                    {
                        bgw.ReportProgress(100 - (int)(remaining * 100 / footer.CurrentSize));
                        lastReport = DateTime.UtcNow;
                    }
                }
                buffer = footer.Bytes;
                stream.Write(buffer, 0, buffer.Length);
            }
            return(true);
        }