Example #1
0
        /// <summary>
        /// Creates a new file within the file system.
        /// </summary>
        /// <param name="name">The name of the file to create.</param>
        /// <param name="parent">The parent directory of the new file.</param>
        /// <returns>The new file listing.</returns>
        public override File NewFile(String name, Directory parent)
        {
            if (FATType != FATTypeEnum.FAT32)
            {
                ExceptionMethods.Throw(new Exceptions.NotSupportedException("FATFileSystem.NewFile for non-FAT32 not supported!"));
            }
            if (parent == null)
            {
                ExceptionMethods.Throw(new Exceptions.NullReferenceException());
            }
            if (!(parent is FATDirectory))
            {
                ExceptionMethods.Throw(new Exceptions.NotSupportedException("FATFileSystem.NewFile parent directory must be of type FATDirectory!"));
            }

            //BasicConsole.WriteLine("Getting directory listings...");
            
            List listings = null;
            if (parent == null)
            {
                listings = GetRootDirectoryListings();
            }
            else
            {
                listings = parent.GetListings();
            }

            //BasicConsole.WriteLine("Got directory listings. Converting name...");

            name = name.ToUpper();

            //BasicConsole.WriteLine("Converted name. Checking if file exists...");

            bool exists = Directory.ListingExists(name, listings);

            //BasicConsole.WriteLine("Check done.");

            if (!exists)
            {
                //BasicConsole.WriteLine("Getting next free cluster...");
                UInt32 freeCluster = GetNextFreeCluster(2);
                //BasicConsole.WriteLine("Got next free. Clearing cluster...");
                WriteCluster(freeCluster, null);
                //BasicConsole.WriteLine("Cleared. Setting FAT entry...");
                SetFATEntryAndSave(freeCluster, GetFATEntryEOFValue(FATType));
                //BasicConsole.WriteLine("Set FAT entry. Creating new file...");
                File newFile = new FATFile(this, (FATDirectory)parent, name, 0, freeCluster);
                //BasicConsole.WriteLine("File created. Adding listing to parent...");
                if (parent == null)
                {
                    listings.Add(newFile);
                    //BasicConsole.WriteLine("Added. Writing listings...");
                    _rootDirectoryFAT32.WriteListings();
                }
                else
                {
                    parent.AddListing(newFile);
                    //BasicConsole.WriteLine("Added. Writing listings...");
                    parent.WriteListings();
                }
                //BasicConsole.WriteLine("Written listings.");
                return newFile;
            }
            else
            {
                ExceptionMethods.Throw(new IOException("Listing (directory/file) with specified name already exists!"));
            }
            return null;
        }