Ejemplo n.º 1
0
        private void ResizeVolume()
        {
            long additionalNumberOfSectors = m_additionalNumberOfBytes / m_volume.BytesPerSector;

            btnBack.Enabled = false;
            btnNext.Enabled = false;

            m_processing           = true;
            m_resizeTimer          = new System.Windows.Forms.Timer();
            m_resizeTimer.Interval = 1000;
            m_resizeTimer.Tick    += new EventHandler(ResizeTimer_Tick);
            m_resizeTimer.Start();

            Thread thread = new Thread(delegate()
            {
                // Because of performance implications, we must fill the container
                // before writing the updated TrueCrypt header to the end of the container.
                // See here for more details: http://blogs.msdn.com/b/oldnewthing/archive/2011/09/22/10215053.aspx
                long oldSize  = m_disk.Size;
                m_bytesToFill = additionalNumberOfSectors * m_disk.BytesPerSector;
                try
                {
                    m_disk.Extend(m_bytesToFill);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                    this.Invoke((MethodInvoker) delegate()
                    {
                        btnBack.Enabled = true;
                        btnNext.Enabled = true;
                    });
                    return;
                }
                if (chkFillWithRandomData.Checked)
                {
                    TrueCryptResize.FillAllocatedSpaceWithData(m_disk, oldSize, ref m_bytesFilled);
                }
                MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(m_disk);
                if (mbr != null && mbr.IsGPTBasedDisk)
                {
                    GuidPartitionTable.RebaseDisk(m_disk, mbr);
                }
                m_resizeStatus = TrueCryptResize.ExtendVolumeAndFileSystem(m_disk, m_password, additionalNumberOfSectors);
                // reinitialize the partition / volume
                m_partition  = VolumeSelectionHelper.GetLastPartition(m_disk);
                m_volume     = new TrueCryptVolume(m_partition, m_password);
                m_processing = false;
            });

            thread.Start();

            if (chkFillWithRandomData.Checked)
            {
                progressBarFillWithRandomData.Visible = true;
            }
        }
Ejemplo n.º 2
0
        public static TrueCryptResizeStatus ExtendVolumeAndFileSystem(Disk disk, byte[] password, long additionalNumberOfSectors)
        {
            Volume          partition = VolumeSelectionHelper.GetLastPartition(disk);
            TrueCryptVolume volume;

            try
            {
                volume = new TrueCryptVolume(partition, password);
            }
            catch (InvalidDataException)
            {
                return(TrueCryptResizeStatus.InvalidDisk);
            }
            catch (NotSupportedException)
            {
                return(TrueCryptResizeStatus.UnsupportedFormatVersion);
            }

            if (volume.IsHiddenVolume)
            {
                return(TrueCryptResizeStatus.HiddenVolume);
            }

            NTFSVolume ntfsVolume;

            try
            {
                ntfsVolume = new NTFSVolume(volume);
            }
            catch (InvalidDataException)
            {
                return(TrueCryptResizeStatus.UnsupportedFileSystem);
            }
            catch (NotSupportedException)
            {
                return(TrueCryptResizeStatus.UnsupportedFileSystem);
            }

            TrueCryptResizeStatus status = ExtendVolume(disk, password, additionalNumberOfSectors);

            if (status == TrueCryptResizeStatus.Success)
            {
                // Reinitialize the partition / volume
                partition  = VolumeSelectionHelper.GetLastPartition(disk);
                volume     = new TrueCryptVolume(partition, password);
                ntfsVolume = new NTFSVolume(volume);
                long availableBytes   = ntfsVolume.GetMaximumSizeToExtend();
                long availableSectors = availableBytes / disk.BytesPerSector;
                ntfsVolume.Extend(availableSectors);

                return(TrueCryptResizeStatus.Success);
            }
            else
            {
                return(status);
            }
        }