public static void CheckFooter(ChecksumIndexInput input) { var scratch = new BytesRef(); var expectedChecksum = string.Format(CultureInfo.InvariantCulture, "{0:D20}", input.Checksum); ReadLine(input, scratch); if (StringHelper.StartsWith(scratch, CHECKSUM) == false) { throw new CorruptIndexException("SimpleText failure: expected checksum line but got " + scratch.Utf8ToString() + " (resource=" + input + ")"); } var actualChecksum = (new BytesRef(scratch.Bytes, CHECKSUM.Length, scratch.Length - CHECKSUM.Length)).Utf8ToString(); if (!expectedChecksum.Equals(actualChecksum)) { throw new CorruptIndexException("SimpleText checksum failure: " + actualChecksum + " != " + expectedChecksum + " (resource=" + input + ")"); } if (input.Length() != input.FilePointer) { throw new CorruptIndexException( "Unexpected stuff at the end of file, please be careful with your text editor! (resource=" + input + ")"); } }
/// <summary> /// Check integrity of the data. The iterator is not usable after this method has been called. /// </summary> internal void CheckIntegrity() { if (OuterInstance.Version_Renamed >= CompressingStoredFieldsWriter.VERSION_CHECKSUM) { FieldsStream.Seek(FieldsStream.Length() - CodecUtil.FooterLength()); CodecUtil.CheckFooter(FieldsStream); } }
/// <summary> /// Validates the codec footer previously written by <seealso cref="#writeFooter"/>. </summary> /// <returns> actual checksum value </returns> /// <exception cref="IOException"> if the footer is invalid, if the checksum does not match, /// or if {@code in} is not properly positioned before the footer /// at the end of the stream. </exception> public static long CheckFooter(ChecksumIndexInput @in) { ValidateFooter(@in); long actualChecksum = @in.Checksum; long expectedChecksum = @in.ReadLong(); if (expectedChecksum != actualChecksum) { throw new System.IO.IOException("checksum failed (hardware problem?) : expected=" + expectedChecksum.ToString("x") + " actual=" + actualChecksum.ToString("x") + " (resource=" + @in + ")"); } if (@in.FilePointer != @in.Length()) { throw new System.IO.IOException("did not read all bytes from file: read " + @in.FilePointer + " vs size " + @in.Length() + " (resource: " + @in + ")"); } return(actualChecksum); }
/// <summary> Read a particular segmentFileName. Note that this may /// throw an IOException if a commit is in process. /// /// </summary> /// <param name="directory">-- directory containing the segments file /// </param> /// <param name="segmentFileName">-- segment file to load /// </param> /// <throws> CorruptIndexException if the index is corrupt </throws> /// <throws> IOException if there is a low-level IO error </throws> public void Read(Directory directory, System.String segmentFileName) { bool success = false; // Clear any previous segments: Clear(); var input = new ChecksumIndexInput(directory.OpenInput(segmentFileName)); generation = GenerationFromSegmentsFileName(segmentFileName); lastGeneration = generation; try { int format = input.ReadInt(); if (format < 0) { // file contains explicit format info // check that it is a format we can understand if (format < CURRENT_FORMAT) { throw new CorruptIndexException("Unknown format version: " + format); } version = input.ReadLong(); // read version counter = input.ReadInt(); // read counter } else { // file is in old format without explicit format info counter = format; } for (int i = input.ReadInt(); i > 0; i--) { // read segmentInfos Add(new SegmentInfo(directory, format, input)); } if (format >= 0) { // in old format the version number may be at the end of the file if (input.FilePointer >= input.Length()) { version = (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond); } // old file format without version number else { version = input.ReadLong(); // read version } } if (format <= FORMAT_USER_DATA) { if (format <= FORMAT_DIAGNOSTICS) { userData = input.ReadStringStringMap(); } else if (0 != input.ReadByte()) { // TODO: Should be read-only map userData = new HashMap <string, string> { { "userData", input.ReadString() } }; } else { // TODO: Should be empty read-only map userData = new HashMap <string, string>(); } } else { // TODO: Should be empty read-only map userData = new HashMap <string, string>(); } if (format <= FORMAT_CHECKSUM) { long checksumNow = input.Checksum; long checksumThen = input.ReadLong(); if (checksumNow != checksumThen) { throw new CorruptIndexException("checksum mismatch in segments file"); } } success = true; } finally { input.Close(); if (!success) { // Clear any segment infos we had loaded so we // have a clean slate on retry: Clear(); } } }