Example #1
0
        public override void SetCapacity(long value)
        {
            if (!CanWrite)
            {
                throw new IOException("Attempt to change length of file not opened for write");
            }

            if (value == Capacity)
            {
                return;
            }

            long bytesPerCluster = _file.Context.BiosParameterBlock.BytesPerCluster;
            long lastVcn         = Utilities.Ceil(value, bytesPerCluster);

            Dictionary <AttributeReference, AttributeRecord> extentCache = new Dictionary <AttributeReference, AttributeRecord>(_attribute.Extents);

            foreach (var extent in extentCache)
            {
                if (extent.Value.StartVcn > lastVcn)
                {
                    extent.Value.GetDataBuffer(_file).SetCapacity(0);
                    _file.RemoveAttributeExtent(extent.Key);
                    _attribute.RemoveExtent(extent.Key);
                }
            }

            var     record     = _attribute.Record;
            var     lastExtent = _attribute.LastExtent;
            IBuffer buffer     = lastExtent.GetDataBuffer(_file);

            _file.MarkMftRecordDirty();

            if (!record.IsNonResident)
            {
                buffer.SetCapacity(value);
                _file.MarkMftRecordDirty();
            }
            else
            {
                if (_file.Context.ClusterBitmap != null)
                {
                    long clusterLength = lastVcn * bytesPerCluster;
                    buffer.SetCapacity(clusterLength);
                    record.AllocatedLength = clusterLength;
                }

                record.DataLength            = value;
                record.InitializedDataLength = Math.Min(record.InitializedDataLength, value);
            }
        }