/// <summary>
        /// Opens a file
        /// </summary>
        /// <param name="fileName">the name of the file.</param>
        /// <param name="ioBlockSize">the number of bytes to do all of the io</param>
        /// <param name="fileStructureBlockSize">The number of bytes in the file structure</param>
        /// <param name="isReadOnly">if the file should be opened in read only</param>
        /// <param name="isSharingEnabled">if the file should be opened with read sharing permissions.</param>
        /// <returns></returns>
        public static CustomFileStream OpenFile(string fileName, int ioBlockSize, out int fileStructureBlockSize, bool isReadOnly, bool isSharingEnabled)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open, isReadOnly ? FileAccess.Read : FileAccess.ReadWrite, isSharingEnabled ? FileShare.Read : FileShare.None, 2048, true))
            {
                fileStructureBlockSize = FileHeaderBlock.SearchForBlockSize(fileStream);
            }

            return(new CustomFileStream(ioBlockSize, fileStructureBlockSize, fileName, isReadOnly, isSharingEnabled));
        }
Esempio n. 2
0
        /// <summary>
        /// Opens a file
        /// </summary>
        /// <param name="fileName">the name of the file.</param>
        /// <param name="ioBlockSize">the number of bytes to do all of the io</param>
        /// <param name="fileStructureBlockSize">The number of bytes in the file structure</param>
        /// <param name="isReadOnly">if the file should be opened in read only</param>
        /// <param name="isSharingEnabled">if the file should be opened with read sharing permissions.</param>
        /// <returns></returns>
        public static CustomFileStream OpenFile(string fileName, int ioBlockSize, out int fileStructureBlockSize, bool isReadOnly, bool isSharingEnabled)
        {
            //Exclusive opening to prevent duplicate opening.

            FileStream fileStream = new FileStream(fileName, FileMode.Open, isReadOnly ? FileAccess.Read : FileAccess.ReadWrite, isSharingEnabled ? FileShare.Read : FileShare.None, 2048, true);

            try
            {
                fileStructureBlockSize = FileHeaderBlock.SearchForBlockSize(fileStream);
            }
            catch (Exception)
            {
                fileStream.Dispose();
                throw;
            }
            return(new CustomFileStream(fileStream, ioBlockSize, fileStructureBlockSize, fileName, isReadOnly, isSharingEnabled));
        }
        private unsafe string TestFile(string fileName)
        {
            ShortTime lastReport          = ShortTime.Now;
            int       errorBlocks         = 0;
            uint      firstBlockWithError = uint.MaxValue;

            try
            {
                using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    int blockSize = FileHeaderBlock.SearchForBlockSize(stream);
                    stream.Position = 0;
                    byte[] data = new byte[blockSize];
                    stream.ReadAll(data, 0, blockSize);
                    var header = FileHeaderBlock.Open(data);

                    fixed(byte *lp = data)
                    {
                        stream.Position = header.HeaderBlockCount * blockSize;

                        uint startingBlock = header.HeaderBlockCount;

                        if (m_shouldQuickScan)
                        {
                            uint blocksToScan = (uint)Math.Ceiling(m_mbToScan * 1024 * 1024 / blockSize);
                            if (blocksToScan < header.LastAllocatedBlock)
                            {
                                startingBlock = Math.Max(header.HeaderBlockCount, header.LastAllocatedBlock - blocksToScan);
                            }
                        }

                        for (uint x = startingBlock; x <= header.LastAllocatedBlock; x++)
                        {
                            long checksum1;
                            int  checksum2;

                            stream.ReadAll(data, 0, blockSize);

                            Footer.ComputeChecksum((IntPtr)lp, out checksum1, out checksum2, blockSize - 16);

                            long checksumInData1 = *(long *)(lp + blockSize - 16);
                            int  checksumInData2 = *(int *)(lp + blockSize - 8);
                            if (!(checksum1 == checksumInData1 && checksum2 == checksumInData2))
                            {
                                firstBlockWithError = Math.Min(firstBlockWithError, x);
                                errorBlocks++;
                            }
                            if (m_quit)
                            {
                                if (errorBlocks == 0)
                                {
                                    return("Quit Early - No Errors Found");
                                }
                                return($"Quit Early - Blocks With Errors {errorBlocks} Starting At {firstBlockWithError}");
                            }
                            if (lastReport.ElapsedSeconds() > .25)
                            {
                                lastReport       = ShortTime.Now;
                                lblProgress.Text = $"Completed {m_filesScanned} of {m_filesToScan} files. Current File: {(stream.Position / (double)stream.Length).ToString("0.0%")}";
                                Application.DoEvents();
                            }
                        }
                    }
                }

                if (errorBlocks == 0)
                {
                    return("No Errors Found");
                }
                return($"Blocks With Errors {errorBlocks} Starting At {firstBlockWithError}");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return("Error");
            }
        }