예제 #1
0
 /// <summary>
 /// Initializes a new FAT directory.
 /// </summary>
 /// <param name="aFileSystem">The FAT file system to which the directory belongs.</param>
 /// <param name="parent">The FAT directory which is the parent of the directory. Null for the root directory.</param>
 /// <param name="aName">The name of the directory.</param>
 /// <param name="aFirstCluster">The first cluster number of the directory.</param>
 public FATDirectory(FATFileSystem aFileSystem, FATDirectory parent, FOS_System.String aName, UInt32 aFirstCluster)
     : base(aFileSystem, parent, aName)
 {
     _theFile = new FATFile(aFileSystem, parent, Name, 0, aFirstCluster)
     {
         IsDirectoryFile = true
     };
 }
 public ActionResult Edit(FATFileSystem aBSWinFileSystem)
 {
     if (ModelState.IsValid)
     {
         db.Entry(aBSWinFileSystem).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(aBSWinFileSystem));
 }
        public ActionResult Create(FATFileSystem aBSWinFileSystem)
        {
            if (ModelState.IsValid)
            {
                db.ABSWinFileSystems.Add(aBSWinFileSystem);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(aBSWinFileSystem));
        }
예제 #4
0
파일: FATFile.cs 프로젝트: zrbruce/FlingOS
 /// <summary>
 /// Initializes a new FAT file.
 /// </summary>
 /// <param name="aFileSystem">The FAT file system to which the file belongs.</param>
 /// <param name="parent">The parent directory of the file.</param>
 /// <param name="aName">The name of the file.</param>
 /// <param name="aSize">The size of the file.</param>
 /// <param name="aFirstCluster">The first cluster number of the file.</param>
 /// <remarks>
 /// Size is UInt32 because FAT doesn't support bigger. Don't change to UInt64.
 /// </remarks>
 public FATFile(FATFileSystem aFileSystem, FATDirectory parent, FOS_System.String aName, UInt32 aSize, UInt32 aFirstCluster)
     : base(aFileSystem, parent, aName, aSize)
 {
     TheFATFileSystem = aFileSystem;
     FirstClusterNum  = aFirstCluster;
 }
예제 #5
0
        /// <summary>
        /// Writes the specified number of the bytes from the buffer starting at offset in the buffer.
        /// </summary>
        /// <param name="buffer">The data to write.</param>
        /// <param name="offset">The offset within the buffer to start writing from.</param>
        /// <param name="count">The number of bytes to write.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (count < 0)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Write: aCount must be > 0"));
            }
            else if (offset < 0)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Write: anOffset must be > 0"));
            }
            else if (buffer == null)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Write: aBuffer must not be null!"));
            }
            else if (buffer.Length - offset < count)
            {
                ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Write: Invalid offset / length values!"));
            }

            //BasicConsole.WriteLine("Checks passed.");

            FATFileSystem mFS   = (FATFileSystem)TheFile.TheFileSystem;
            FATFile       mFile = TheFATFile;

            if (ClusterNums == null)
            {
                //BasicConsole.WriteLine("Getting cluster nums...");

                GetClusterNums();
                if (ClusterNums == null)
                {
                    //BasicConsole.WriteLine("Failed to get cluster nums.");
                    return;
                }

                //BasicConsole.WriteLine("Got cluster nums.");
            }

            //BasicConsole.WriteLine("Creating write buffer...");

            UInt32 xClusterSize = mFS.BytesPerCluster;

            byte[] writeBuffer = mFS.NewClusterArray();

            //BasicConsole.WriteLine("Writing data...");

            while (count > 0)
            {
                UInt32 clusterIdx   = (UInt32)position / xClusterSize;
                UInt32 posInCluster = (UInt32)position % xClusterSize;

                bool newCluster = false;
                while (clusterIdx >= ClusterNums.Count)
                {
                    //BasicConsole.WriteLine("Expanding clusters...");

                    UInt32 lastClusterNum = ClusterNums[ClusterNums.Count - 1];
                    UInt32 nextClusterNum = mFS.GetNextFreeCluster(lastClusterNum);

                    //Clear cluster
                    mFS.WriteCluster(nextClusterNum, null);

                    //Set last FAT entry to point to next cluster
                    mFS.SetFATEntryAndSave(lastClusterNum, nextClusterNum);

                    //Set next cluster as EOF
                    mFS.SetFATEntryAndSave(nextClusterNum, FATFileSystem.GetFATEntryEOFValue(mFS.FATType));

                    //Add next cluster num to our list
                    ClusterNums.Add(nextClusterNum);
                    newCluster = true;
                }

                if ((posInCluster != 0 || count < xClusterSize) && !newCluster)
                {
                    //BasicConsole.WriteLine("Reading existing data...");

                    mFS.ReadClusters(ClusterNums[(int)clusterIdx], 1, writeBuffer);

                    //BasicConsole.WriteLine("Read existing data.");
                }

                //BasicConsole.WriteLine("Calculating write size...");
                int writeSize = count < (xClusterSize - posInCluster) ? count :
                                (int)(xClusterSize - posInCluster);
                //BasicConsole.WriteLine("Calculated write size. Copying data to write...");
                Array.Copy(buffer, offset, writeBuffer, (int)posInCluster, writeSize);
                //BasicConsole.WriteLine("Data copied. Writing data to disk...");

                mFS.WriteCluster(ClusterNums[(int)clusterIdx], writeBuffer);

                //BasicConsole.WriteLine("Written data.");

                count    -= writeSize;
                offset   += writeSize;
                position += (uint)writeSize;
            }

            mFS.CleanDiskCaches();

            //BasicConsole.WriteLine("Write completed.");

            if (!IgnoreFileSize)
            {
                if (position > mFile.Size)
                {
                    //Update file info
                    mFile.Size = position;
                    //Update directory entry
                    mFile.Parent.WriteListings();
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Reads the specified number of bytes from the stream from the current position into the buffer at the
        /// specified offset or as many bytes as are available before the end of the stream is met.
        /// </summary>
        /// <param name="buffer">The byte array to read into.</param>
        /// <param name="offset">The offset within the buffer to start storing read data at.</param>
        /// <param name="count">The number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            //Don't attempt to read a file of 0 size.
            if (TheFile.Size > 0 || IgnoreFileSize)
            {
                //Load cluster chain if it hasn't already been loaded.
                if (ClusterNums == null)
                {
                    GetClusterNums();
                    //If loading cluster nums failed, don't throw an exception,
                    //  just return nothing read.
                    if (ClusterNums == null)
                    {
                        return(0);
                    }
                }

                FATFileSystem TheFS = (FATFileSystem)TheFile.TheFileSystem;

#if FATFileStream_TRACE
                BasicConsole.WriteLine("Checking params...");
#endif

                //Conditions for being able to read from the stream.
                if (count < 0)
                {
                    ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Read: aCount must be > 0"));
                }
                else if (offset < 0)
                {
                    ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Read: anOffset must be > 0"));
                }
                else if (buffer == null)
                {
                    ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Read: aBuffer must not be null!"));
                }
                else if (buffer.Length - offset < count)
                {
                    ExceptionMethods.Throw(new Exceptions.ArgumentException("FATFileStream.Read: Invalid offset / length values!"));
                }
                else if (TheFATFile.FirstClusterNum == 0)
                {
                    // First cluster number can be 0 for 0 length files
                    return(0);
                }
                else if (!IgnoreFileSize && position == TheFile.Size)
                {
                    // EOF
                    return(0);
                }

#if FATFileStream_TRACE
                BasicConsole.WriteLine("Params OK.");
#endif

                // Clamp the count value so that no out of bounds exceptions occur
                ulong FileSize = 0;
                if (IgnoreFileSize)
                {
                    FileSize = (ulong)ClusterNums.Count * TheFATFileSystem.BytesPerCluster;
                }
                else
                {
                    FileSize = TheFile.Size;
                }
                ulong MaxReadableBytes = FileSize - position;
                ulong ActualCount      = (ulong)count;
                if (ActualCount > MaxReadableBytes)
                {
                    ActualCount = MaxReadableBytes;
                }

#if FATFileStream_TRACE
                BasicConsole.WriteLine("Creating new cluster array...");
#endif

                //Temporary store of cluster data since we can only
                //  read entire clusters at a time.
                if (ReadClusterBuffer == null)
                {
                    ReadClusterBuffer = new byte[TheFS.BytesPerCluster * NumReadClusters];
                    ReadClusterSize   = TheFS.BytesPerCluster;
                }

                int read = 0;

                while (ActualCount > 0)
                {
                    uint NumClustersToRead = (uint)ActualCount / ReadClusterSize;
                    if ((uint)ActualCount % ReadClusterSize != 0)
                    {
                        NumClustersToRead++;
                    }

                    uint StartPosition   = (uint)position;
                    int  StartClusterIdx = (int)(StartPosition / ReadClusterSize);
                    uint StartClusterNum = ClusterNums[StartClusterIdx];

                    uint ContiguousClusters = 1;
                    int  CurrentClusterIdx  = StartClusterIdx;
                    uint CurrentClusterNum  = ClusterNums[CurrentClusterIdx];

                    while (ContiguousClusters < NumClustersToRead && ContiguousClusters < NumReadClusters)
                    {
                        if (CurrentClusterNum + 1 != ClusterNums[CurrentClusterIdx + 1])
                        {
                            break;
                        }
                        else
                        {
                            CurrentClusterIdx++;
                            CurrentClusterNum++;
                            ContiguousClusters++;
                        }
                    }

                    TheFS.ReadClusters(StartClusterNum, ContiguousClusters, ReadClusterBuffer);

                    uint StartClusterOffset    = StartPosition % ReadClusterSize;
                    uint ContiguousClusterSize = ContiguousClusters * ReadClusterSize;
                    uint ReadSize = ContiguousClusterSize;
                    if (StartClusterOffset > 0)
                    {
                        ReadSize -= StartClusterOffset;
                    }
                    if (ReadSize > ActualCount)
                    {
                        ReadSize = (uint)ActualCount;
                    }
                    Array.Copy(ReadClusterBuffer, (int)StartClusterOffset, buffer, offset, (int)ReadSize);

                    position    += ReadSize;
                    offset      += (int)ReadSize;
                    ActualCount -= ReadSize;
                    read        += (int)ReadSize;
                }

//#if FATFileStream_TRACE
//                BasicConsole.WriteLine("Reading data...");
//#endif
//                //Loop reading in the data
//                while (ActualCount > 0)
//                {
//                    UInt32 ClusterIdx = (UInt32)position / ReadClusterSize;
//                    UInt32 PosInCluster = (UInt32)position % ReadClusterSize;
//#if FATFileStream_TRACE
//                    BasicConsole.WriteLine(((FOS_System.String)"Reading cluster ") + ClusterNums[(int)xClusterIdx]);
//#endif
//                    TheFS.ReadClusters(ClusterNums[(int)ClusterIdx], ReadClusterBuffer);
//#if FATFileStream_TRACE
//                    BasicConsole.WriteLine("Read cluster.");
//#endif
//                    uint ReadSize;
//                    if (PosInCluster + ActualCount > ReadClusterSize)
//                    {
//                        ReadSize = ReadClusterSize - PosInCluster;
//                    }
//                    else
//                    {
//                        ReadSize = (uint)ActualCount;
//                    }

//                    // TODO: Should we do an argument check here just in case?
//                    FOS_System.Array.Copy(ReadClusterBuffer, (int)PosInCluster, buffer, offset, (int)ReadSize);
//                    offset += (int)ReadSize;
//                    ActualCount -= (ulong)ReadSize;
//                    read += (int)ReadSize;
//                    position += ReadSize;
//                }

//#if FATFileStream_TRACE
//                BasicConsole.WriteLine("Read data.");
//#endif

                return(read);
            }
            else
            {
                return(0);
            }
        }