Esempio n. 1
0
        internal Directory CreateChildDirectory(string normalizedName)
        {
            long id = FindEntryByNormalizedName(normalizedName);

            if (id >= 0)
            {
                if ((_entries[id].Attributes & FatAttributes.Directory) == 0)
                {
                    throw new IOException("A file exists with the same name");
                }
                else
                {
                    return(_fileSystem.GetDirectory(this, id));
                }
            }
            else
            {
                try
                {
                    uint firstCluster;
                    if (!_fileSystem.Fat.TryGetFreeCluster(out firstCluster))
                    {
                        throw new IOException("Failed to allocate first cluster for new directory");
                    }

                    _fileSystem.Fat.SetEndOfChain(firstCluster);

                    DirectoryEntry newEntry = new DirectoryEntry(_fileSystem.FatOptions, normalizedName, FatAttributes.Directory);
                    newEntry.FirstCluster  = firstCluster;
                    newEntry.CreationTime  = _fileSystem.ConvertFromUtc(DateTime.UtcNow);
                    newEntry.LastWriteTime = newEntry.CreationTime;

                    id = AddEntry(newEntry);

                    PopulateNewChildDirectory(newEntry);

                    // Rather than just creating a new instance, pull it through the fileSystem cache
                    // to ensure the cache model is preserved.
                    return(_fileSystem.GetDirectory(this, id));
                }
                finally
                {
                    _fileSystem.Fat.Flush();
                }
            }
        }