modify how reading from file works to prevent accidental overreads

This commit is contained in:
RaidMax 2019-07-31 20:15:29 -05:00
parent 06af995202
commit 034d887abd
2 changed files with 21 additions and 5 deletions

View File

@ -65,7 +65,7 @@ namespace IW4MAdmin.Application.IO
previousFileSize = fileSize; previousFileSize = fileSize;
var events = await _reader.ReadEventsFromLog(_server, fileDiff, 0); var events = await _reader.ReadEventsFromLog(_server, fileDiff, fileSize - fileDiff);
foreach (var ev in events) foreach (var ev in events)
{ {

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IW4MAdmin.Application.IO namespace IW4MAdmin.Application.IO
@ -37,13 +38,28 @@ namespace IW4MAdmin.Application.IO
// open the file as a stream // open the file as a stream
using (var rd = new StreamReader(new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Utilities.EncodingType)) using (var rd = new StreamReader(new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Utilities.EncodingType))
{ {
// take the old start position and go back the number of new characters char[] buff = new char[fileSizeDiff];
rd.BaseStream.Seek(-fileSizeDiff, SeekOrigin.End); rd.BaseStream.Seek(-fileSizeDiff, SeekOrigin.End);
await rd.ReadAsync(buff, 0, (int)fileSizeDiff);
string newLine; var stringBuilder = new StringBuilder();
while (!string.IsNullOrEmpty(newLine = await rd.ReadLineAsync())) foreach (char c in buff)
{ {
logLines.Add(newLine); if (c == '\n')
{
logLines.Add(stringBuilder.ToString());
stringBuilder = new StringBuilder();
}
else if (c != '\r')
{
stringBuilder.Append(c);
}
}
if (stringBuilder.Length > 0)
{
logLines.Add(stringBuilder.ToString());
} }
} }