Ejemplo n.º 1
0
        public void Test1()
        {
            File.Delete(@"C:\Temp\fileTemp.~d2i");
            File.Delete(@"C:\Temp\fileTemp.d2i");
            using (SimplifiedFileWriter writer = new SimplifiedFileWriter(@"C:\Temp\fileTemp.~d2i", @"C:\Temp\fileTemp.d2i", 4096, FileFlags.ManualRollover))
            {
                using (ISupportsBinaryStream file = writer.CreateFile(SubFileName.CreateRandom()))
                    using (BinaryStream bs = new BinaryStream(file))
                    {
                        bs.Write(1);
                    }

                writer.Commit();
            }


            using (TransactionalFileStructure reader = TransactionalFileStructure.OpenFile(@"C:\Temp\fileTemp.d2i", true))
            {
                using (SubFileStream file = reader.Snapshot.OpenFile(0))
                    using (BinaryStream bs = new BinaryStream(file))
                    {
                        if (bs.ReadInt32() != 1)
                        {
                            throw new Exception();
                        }
                    }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new file on the file system and returns the <see cref="SubFileHeader"/> associated with the new file.
        /// </summary>
        /// <param name="fileName">Represents the nature of the data that will be stored in this file.</param>
        /// <returns></returns>
        /// <remarks>A file system only supports 64 files. This is a fundamental limitation and cannot be changed easily.</remarks>
        public SubFileHeader CreateNewFile(SubFileName fileName)
        {
            base.TestForEditable();
            if (!CanWrite)
            {
                throw new Exception("Writing to this file type is not supported");
            }
            if (IsReadOnly)
            {
                throw new Exception("File is read only");
            }
            if (m_files.Count >= 64)
            {
                throw new OverflowException("Only 64 files per file system is supported");
            }
            if (ContainsSubFile(fileName))
            {
                throw new DuplicateNameException("Name already exists");
            }

            SubFileHeader node = new SubFileHeader(m_nextFileId, fileName, isImmutable: false, isSimplified: IsSimplifiedFileFormat);

            m_nextFileId++;
            m_files.Add(node);
            return(node);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates and Opens a new file on the current file system.
 /// </summary>
 /// <returns></returns>
 public SubFileStream CreateFile(SubFileName fileName)
 {
     if (m_disposed)
     {
         throw new ObjectDisposedException(GetType().FullName);
     }
     m_fileHeaderBlock.CreateNewFile(fileName);
     return(OpenFile(fileName));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new <see cref="SubFileHeader"/>.
 /// </summary>
 /// <param name="fileId"></param>
 /// <param name="fileName"></param>
 /// <param name="isImmutable">Determines if this class will be immutable upon creation</param>
 /// <param name="isSimplified">if this header is a simplified header.</param>
 public SubFileHeader(ushort fileId, SubFileName fileName, bool isImmutable, bool isSimplified)
 {
     if (fileName == null)
     {
         throw new ArgumentException("The feature type cannot be an empty GUID value", "fileName");
     }
     m_isSimplified = isSimplified;
     IsReadOnly     = isImmutable;
     m_fileIdNumber = fileId;
     m_fileName     = fileName;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Opens a ArchiveFileStream that can be used to read/write to the file passed to this function.
 /// </summary>
 /// <returns></returns>
 public SubFileStream OpenFile(SubFileName fileName)
 {
     for (int x = 0; x < m_fileHeaderBlock.Files.Count; x++)
     {
         SubFileHeader file = m_fileHeaderBlock.Files[x];
         if (file.FileName == fileName)
         {
             return(OpenFile(x));
         }
     }
     throw new Exception("File does not exist");
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates and Opens a new file on the current file system.
        /// </summary>
        /// <returns></returns>
        public ISupportsBinaryStream CreateFile(SubFileName fileName)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            CloseCurrentFile();

            SubFileHeader subFile = m_fileHeaderBlock.CreateNewFile(fileName);

            subFile.DirectBlock = m_fileHeaderBlock.LastAllocatedBlock + 1;
            m_subFileStream     = new SimplifiedSubFileStream(m_stream, subFile, m_fileHeaderBlock);
            return(m_subFileStream);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Opens a ArchiveFileStream that can be used to read/write to the file passed to this function.
 /// </summary>
 /// <returns></returns>
 public SubFileStream OpenFile(SubFileName fileName)
 {
     if (m_disposed)
     {
         throw new ObjectDisposedException(GetType().FullName);
     }
     for (int x = 0; x < Files.Count; x++)
     {
         SubFileHeader file = Files[x];
         if (file.FileName == fileName)
         {
             return(OpenFile(x));
         }
     }
     throw new Exception("File does not exist");
 }
Ejemplo n.º 8
0
        public void TestMultipleFiles()
        {
            Random r = new Random(1);

            File.Delete(@"C:\Temp\fileTemp.~d2i");
            File.Delete(@"C:\Temp\fileTemp.d2i");
            using (SimplifiedFileWriter writer = new SimplifiedFileWriter(@"C:\Temp\fileTemp.~d2i", @"C:\Temp\fileTemp.d2i", 4096, FileFlags.ManualRollover))
            {
                for (int i = 0; i < 10; i++)
                {
                    using (ISupportsBinaryStream file = writer.CreateFile(SubFileName.CreateRandom()))
                        using (BinaryStream bs = new BinaryStream(file))
                        {
                            bs.Write((byte)1);
                            for (int x = 0; x < 100000; x++)
                            {
                                bs.Write(r.NextDouble());
                            }
                        }
                }
                writer.Commit();
            }

            r = new Random(1);
            using (TransactionalFileStructure reader = TransactionalFileStructure.OpenFile(@"C:\Temp\fileTemp.d2i", true))
            {
                for (int i = 0; i < 10; i++)
                {
                    using (SubFileStream file = reader.Snapshot.OpenFile(i))
                        using (BinaryStream bs = new BinaryStream(file))
                        {
                            if (bs.ReadUInt8() != 1)
                            {
                                throw new Exception();
                            }

                            for (int x = 0; x < 100000; x++)
                            {
                                if (bs.ReadDouble() != r.NextDouble())
                                {
                                    throw new Exception();
                                }
                            }
                        }
                }
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a <see cref="SubFileHeader"/> from the data stream.
 /// </summary>
 /// <param name="dataReader"></param>
 /// <param name="isImmutable">Determines if this class will be immutable upon creation</param>
 /// <param name="isSimplified">gets if the file structure is the simplified type</param>
 public SubFileHeader(BinaryReader dataReader, bool isImmutable, bool isSimplified)
 {
     m_isSimplified   = isSimplified;
     m_fileName       = SubFileName.Load(dataReader);
     m_fileIdNumber   = dataReader.ReadUInt16();
     m_dataBlockCount = dataReader.ReadUInt32();
     if (!isSimplified)
     {
         m_totalBlocksCount = dataReader.ReadUInt32();
     }
     m_directBlock = dataReader.ReadUInt32();
     if (!isSimplified)
     {
         m_singleIndirectBlock    = dataReader.ReadUInt32();
         m_doubleIndirectBlock    = dataReader.ReadUInt32();
         m_tripleIndirectBlock    = dataReader.ReadUInt32();
         m_quadrupleIndirectBlock = dataReader.ReadUInt32();
     }
     IsReadOnly = isImmutable;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Determines if the file contains the subfile
 /// </summary>
 /// <param name="fileName">the subfile to look for</param>
 /// <returns>true if contained, false otherwise</returns>
 public bool ContainsSubFile(SubFileName fileName)
 {
     return(m_files.Any(file => file.FileName == fileName));
 }