Exemple #1
0
        /// <summary>
        /// Adds a new cluster to the end of the existing chain, by allocating a free cluster.
        /// </summary>
        /// <returns>The cluster allocated</returns>
        /// <remarks>This method does not initialize the data in the cluster, the caller should
        /// perform a write to ensure the cluster data is in known state.</remarks>
        private uint ExtendChain()
        {
            // Sanity check - make sure the final known cluster is the EOC marker
            if (!_fat.IsEndOfChain(_knownClusters[_knownClusters.Count - 1]))
            {
                throw new IOException("Corrupt file system: final cluster isn't End-of-Chain");
            }

            uint cluster;

            if (!_fat.TryGetFreeCluster(out cluster))
            {
                throw new IOException("Out of disk space");
            }

            _fat.SetEndOfChain(cluster);
            if (_knownClusters.Count == 1)
            {
                FireFirstClusterAllocated(cluster);
            }
            else
            {
                _fat.SetNext(_knownClusters[_knownClusters.Count - 2], cluster);
            }

            _knownClusters[_knownClusters.Count - 1] = cluster;
            _knownClusters.Add(_fat.GetNext(cluster));

            return(cluster);
        }