/// <summary> /// Registers a file to be watched with a callback for when it is modified /// </summary> /// <param name="webFile"></param> /// <param name="fileInfo"></param> /// <param name="bundleOptions"></param> /// <param name="fileModifiedCallback"></param> /// <returns> /// Returns true if a watcher was added, false if the file is already being watched /// </returns> public bool Watch(IWebFile webFile, IFileInfo fileInfo, BundleOptions bundleOptions, Action <WatchedFile> fileModifiedCallback) { var path = webFile.FilePath.TrimStart(new[] { '~' }).ToLowerInvariant(); //don't double watch if there's already a watcher for this file if (_fileWatchers.ContainsKey(path)) { return(false); } var watchedFile = new WatchedFile(webFile, fileInfo, bundleOptions); var changeToken = _fileProvider.Watch(path); _fileWatchers.TryAdd(path, changeToken.RegisterChangeCallback(o => { //try to remove the item from the dictionary so it can be added again IDisposable watcher; _fileWatchers.TryRemove(path, out watcher); //call the callback with the strongly typed object fileModifiedCallback((WatchedFile)o); }, watchedFile)); return(true); }
private WatchedFile Clone(WatchedFile original) { return(new WatchedFile(original.DateiName, original.Verzeichnis, original.ZeitStempel, original.Zustand)); }
bool CheckFile(WatchedFile file) { if (!File.Exists(file.FileName)) { OnFileDeleted(file.FileName); return(false); } else { DateTime fileWriteTime = File.GetLastWriteTime(file.FileName); FileAttributes fileAttributes = File.GetAttributes(file.FileName); if (fileWriteTime.CompareTo(file.LastWriteTime) > 0) { OnFileModified(file.FileName); } if (fileAttributes != file.Attributes) { OnFileAttributesChanged(file.FileName, file.Attributes, fileAttributes); } file.Attributes = fileAttributes; file.LastWriteTime = fileWriteTime; return(true); } }
public void WatchedFileParameterlessConstructorCreatesObject() { // I know this test may seem silly but NHibernate requires a parameterless constructor on objects it persists. var obj = new WatchedFile(); Assert.IsNotNull(obj); }
public void AddFile(string fileName) { if (!File.Exists(fileName)) { return; } WatchedFile fileInfo = new WatchedFile(); try { fileInfo.FileName = fileName; fileInfo.LastWriteTime = File.GetLastWriteTime(fileName); fileInfo.Attributes = File.GetAttributes(fileName); } catch (Exception) { return; } lock (mLock) { mWatchedFiles.Add(fileInfo); UpdateTimer(); } }
private void InitializeFileWatcher() { if (WatchedFile.IsBlank()) { throw Fault.NullRef(nameof(WatchedFile)); } if (!File.Exists(WatchedFile)) { throw Fault.MissingFile(WatchedFile); } var abs = WatchedFile.MakeAbsolute(); var dir = Path.GetDirectoryName(abs); var nme = Path.GetFileName(abs); _watchr = new FileSystemWatcher(dir, nme); _watchr.NotifyFilter = NotifyFilters.LastWrite; _watchr.Changed += async(s, e) => { if (!_isDelaying) { _isDelaying = true; await Task.Delay(1000); IsFileChanged = true; OnFileChanged(); _isDelaying = false; } }; _watchr.EnableRaisingEvents = true; }
private bool PingFile(WatchedFile file, FileStream fs) { const int maxCountBeforeNewline = 1024; int b; long lastNewlinePos = -1; long end = Math.Min(file.LastLength, fs.Length); int countBeforeNewline = 0; fs.Position = file.LastPosition; IPBanLog.Info("Processing watched file {0}, len = {1}, pos = {2}", file.FileName, file.LastLength, file.LastPosition); while (fs.Position < end && countBeforeNewline++ != maxCountBeforeNewline) { // read until last \n is found b = fs.ReadByte(); if (b == '\n') { lastNewlinePos = fs.Position - 1; countBeforeNewline = 0; } } if (countBeforeNewline == maxCountBeforeNewline) { throw new InvalidOperationException($"Log file '{fileMask}' may not be a plain text new line delimited file"); } if (lastNewlinePos > -1) { try { // we could read line by line by going one byte at a time, but the hope here is that by taking // advantage of stream reader and binary reader read bytes we can get some improved cpu usage // at the expense of having to store all the bytes in memory for a small time fs.Position = file.LastPosition; byte[] bytes = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position)); using (StreamReader reader = new StreamReader(new MemoryStream(bytes), Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (!OnProcessLine(line) || (ProcessLine != null && !ProcessLine(line))) { break; } } } } finally { // set file position for next ping fs.Position = file.LastPosition = ++lastNewlinePos; } } return(maxFileSize > 0 && fs.Length > maxFileSize); }
private Dictionary <string, WatchedFile> GetDictionary(WatchedFile file) { return(new Dictionary <string, WatchedFile> { [file.DateiName] = file }); }
/// <summary> /// Resumes the polling of watched files. /// </summary> /// <remarks> /// If it has been longer than the polling interval since the FileWatcher was paused, /// all watched files are immediately polled for changes and the results dispatched immediately /// in the callers' thread (before the function returns). /// </remarks> public void Resume() { lock (mLock) { if (mPaused) { if (DateTime.Now.Subtract(mPauseTime).TotalMilliseconds > mUpdatePeriod) { // Go through all the watched files and check them for (int index = 0; index < mWatchedFiles.Count;) { WatchedFile file = mWatchedFiles[index]; if (!CheckFile(file)) { // Remove from queue mWatchedFiles.RemoveAt(index); } else { // Leave on queue ++index; } } } mPaused = false; UpdateTimer(); } } }
/// <summary> /// Enables or disables a file from being watched (such as when the client code is about to save it). /// </summary> /// <param name="fileName">Files' absolute path name.</param> /// <param name="enabled">Whether to enable or disable watching.</param> /// <remarks>Enabling a file that is not monitored causes it to be added to the watch list.</remarks> public void EnableFile(string fileName, bool enabled) { lock (mLock) { if (!enabled) { WatchedFile fileInfo = mWatchedFiles.Find(delegate(WatchedFile file) { return(PathUtils.Compare(file.FileName, fileName) == 0); }); if (fileInfo != null) { mWatchedFiles.Remove(fileInfo); mIgnoredFiles.Add(fileInfo); UpdateTimer(); } } else { WatchedFile fileInfo = mIgnoredFiles.Find(delegate(WatchedFile file) { return(PathUtils.Compare(file.FileName, fileName) == 0); }); if (fileInfo == null) { AddFile(fileName); } else { mIgnoredFiles.Remove(fileInfo); mWatchedFiles.Add(fileInfo); fileInfo.Attributes = File.GetAttributes(fileInfo.FileName); fileInfo.LastWriteTime = File.GetLastWriteTime(fileInfo.FileName); UpdateTimer(); } } } }
public void WatchFile(string path, Action callback) { lock (_locker) { var file = new WatchedFile(path, callback); _files.Add(file); } }
public WatchedFileTestBundle() { MockLog = new Mock <ILog>(); MockFile = new Mock <IFile>(); MockServiceProvider = new Mock <IEntityProvider>(); MockRequestProvider = new Mock <IRequestProvider>(); MockEnvironmentUtility = new Mock <IEnvironmentUtility>(); MockDirectory = new Mock <IDirectory>(); WatchedFile = new WatchedFile(MockLog.Object, MockServiceProvider.Object, MockFile.Object, MockRequestProvider.Object, MockEnvironmentUtility.Object, MockDirectory.Object); }
public void WatchedfileShouldMatchOnFileNameAlone() { var watchedFile1 = new WatchedFile(new FileInfo("test1"), 1, false); var watchedFile2 = new WatchedFile(new FileInfo("test1"), 2, false); watchedFile1.Equals(watchedFile2).ShouldBeTrue(); var watchedFile3 = new WatchedFile(new FileInfo("test2"), 2, false); watchedFile2.Equals(watchedFile3).ShouldBeFalse(); }
public void ComparererShouldWorkInContainsStatements() { var watchedList1 = new List <WatchedFile> { new WatchedFile(new FileInfo("test1"), 1, false), new WatchedFile(new FileInfo("test2"), 2, false) }; var watchedFile = new WatchedFile(new FileInfo("test2"), 1, false); watchedList1.Contains(watchedFile, WatchedFileNameComparer.Default).ShouldBeTrue(); }
private bool PingFile(WatchedFile file, FileStream fs) { const int maxCountBeforeNewline = 1024; int b; long lastNewlinePos = -1; long end = Math.Min(file.LastLength, fs.Length); int countBeforeNewline = 0; fs.Position = file.LastPosition; Logger.Info("Processing watched file {0}, len = {1}, pos = {2}", file.FileName, file.LastLength, file.LastPosition); while (fs.Position < end && countBeforeNewline++ != maxCountBeforeNewline) { // read until last \n is found b = fs.ReadByte(); if (b == '\n') { lastNewlinePos = fs.Position - 1; countBeforeNewline = 0; } } if (countBeforeNewline == maxCountBeforeNewline) { throw new InvalidOperationException($"Log file '{file.FileName}' may not be a plain text new line delimited file"); } if (lastNewlinePos > -1) { try { // read all the text for the current set of lines into a string for processing fs.Position = file.LastPosition; // create giant text blob with all lines trimmed byte[] bytes = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position)); string text = "\n" + string.Join('\n', Encoding.UTF8.GetString(bytes).Split('\n').Select(l => l.Trim())) + "\n"; OnProcessText(text); ProcessText?.Invoke(text); } finally { // set file position for next ping fs.Position = file.LastPosition = ++lastNewlinePos; } } return(maxFileSize > 0 && fs.Length > maxFileSize); }
private void ProcessFile(WatchedFile file, FileStream fs) { int b; long lastNewlinePos = -1; long end = Math.Min(file.LastLength, fs.Length); int countBeforeNewline = 0; fs.Position = file.LastPosition; Logger.Trace("Processing log file {0}, len = {1}, pos = {2}", file.FileName, file.LastLength, file.LastPosition); while (fs.Position < end && countBeforeNewline++ != maxLineLength) { // read until last \n is found b = fs.ReadByte(); if (b == '\n') { lastNewlinePos = fs.Position - 1; countBeforeNewline = 0; } } if (countBeforeNewline > maxLineLength) { file.IsBinaryFile = true; Logger.Warn($"Aborting parsing log file {file.FileName}, file may be a binary file"); return; } // if we found a newline, process all the text up until that newline if (lastNewlinePos > -1) { try { // read all the text for the current set of lines into a string for processing fs.Position = file.LastPosition; // create giant text blob with all lines trimmed byte[] bytes = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position)); string text = "\n" + string.Join('\n', encoding.GetString(bytes).Split('\n').Select(l => l.Trim())) + "\n"; OnProcessText(text); ProcessText?.Invoke(text); } finally { // set file position for next processing fs.Position = file.LastPosition = ++lastNewlinePos; } } }
/// <summary>Notify observers about the changed items</summary> public bool NotifyChanged() { // Scan the directory and notify about created, deleted, or changed files var current_files = Path_.EnumFileSystem(Path, SearchOption.TopDirectoryOnly, exclude: FileAttributes.Hidden | FileAttributes.Directory) .Select(x => x.FullName).ToList(); var existing = current_files .ToHashSet(x => x.ToLowerInvariant()); foreach (var path in current_files) { // If there is an existing watcher for the file, simply check for changed if (m_files.TryGetValue(path.ToLowerInvariant(), out var file)) { // File unchanged if (!file.HasChanged) { continue; } // File changed, but change not handled if (!file.NotifyChanged()) { continue; } // File no longer exists, remove from 'm_files' if (!Path_.FileExists(file.Path)) { m_files.Remove(file.Path.ToLowerInvariant()); } continue; } // If this is a new file, notify then add file = new WatchedFile(path, m_onchange, Id, m_ctx); if (file.NotifyChanged()) { m_files[path.ToLowerInvariant()] = file; } } return(true); }
private void RelaunchInTemp() { if (WatchedFile.IsBlank()) { return; } var exeNow = CurrentExe.GetFullPath(); var cfgNow = exeNow + ".config"; var tmpExe = WatchedFile.MakeTempCopy(".exe"); var tmpCfg = tmpExe + ".config"; if (File.Exists(cfgNow)) { File.Copy(cfgNow, tmpCfg, true); } Process.Start(tmpExe, GetCommandLineArgs()); Application.Current.Shutdown(); }
public void Delete(WatchedFile watchedFile) { if (watchedFile == null) { throw new ArgumentNullException(nameof(watchedFile)); } else if (watchedFile.Id == 0) { throw new ArgumentOutOfRangeException(nameof(watchedFile.Id)); } else { this._logger.Info($"Executing {DELETE_WATCHED_FILE_SP}"); using (System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(this._connectionString)) { _sqlExecuter.Execute(dbConnection, sql: DELETE_WATCHED_FILE_SP, param: new { id = watchedFile.Id }, commandType: System.Data.CommandType.StoredProcedure); } } }
public WatchedFile Create(WatchedFile watchedFile) { if (watchedFile == null) { throw new ArgumentNullException(nameof(watchedFile)); } else if (string.IsNullOrWhiteSpace(watchedFile.Path)) { throw new ArgumentNullException(nameof(watchedFile.Path)); } else { this._logger.Info($"Executing {CREATE_WATCHED_FILE_SP}"); using (System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(this._connectionString)) { return(_sqlExecuter.Query <WatchedFile>(dbConnection, sql: CREATE_WATCHED_FILE_SP, param: new { path = watchedFile.Path }, commandType: System.Data.CommandType.StoredProcedure).FirstOrDefault()); } } }
void UpdateTimer_Callback(object state) { lock (mLock) { if (!mPaused && mWatchedFiles.Count > 0) { WatchedFile file = mWatchedFiles[0]; if (!CheckFile(file)) { // Remove from queue mWatchedFiles.RemoveAt(0); } else { // Put on back of queue mWatchedFiles.RemoveAt(0); mWatchedFiles.Add(file); } } } }
private void RelaunchInTemp() { if (WatchedFile.IsBlank()) { return; } var tmpExe = ""; try { tmpExe = WatchedFile.MakeTempCopy(".exe"); } catch (Exception ex) { Alert.Show(ex, $"Creating temp copy of {WatchedFile}"); return; } CopyCfgToTemp(tmpExe); Process.Start(tmpExe, GetCommandLineArgs()); Application.Current.Shutdown(); }
private void handleWatchedFile(WatchedFile incomingFile) { var result = WatchedFiles.Files.FirstOrDefault(file => file.Key.Equals(incomingFile.FileName)); WatchedFiles.Files.AddOrUpdate( incomingFile.FileName, (incomingFile.LineCount, incomingFile.LastWriteTime), (key, oldValue) => (incomingFile.LineCount, incomingFile.LastWriteTime)); if (!incomingFile.ShouldWriteResultToConsole) { return; } if (result.Key.IsNotNullOrEmpty()) { handleExistingFile(incomingFile, result); } else { handleAddedFile(incomingFile); } }
public void AddFile(string fileName) { if (!File.Exists(fileName)) return; WatchedFile fileInfo = new WatchedFile(); try { fileInfo.FileName = fileName; fileInfo.LastWriteTime = File.GetLastWriteTime(fileName); fileInfo.Attributes = File.GetAttributes(fileName); } catch(Exception) { return; } lock (mLock) { mWatchedFiles.Add(fileInfo); UpdateTimer(); } }
private bool PingFile(WatchedFile file, FileStream fs) { const int maxCountBeforeNewline = 1024; int b; long lastNewlinePos = -1; byte[] bytes; long end = Math.Min(file.LastLength, fs.Length); int countBeforeNewline = 0; fs.Position = file.LastPosition; while (fs.Position < end && countBeforeNewline++ != maxCountBeforeNewline) { // read until last \n is found b = fs.ReadByte(); if (b == '\n') { lastNewlinePos = fs.Position - 1; countBeforeNewline = 0; } } if (countBeforeNewline == maxCountBeforeNewline) { throw new InvalidOperationException("Log file " + this.fileMask + " may not be a plain text new line delimited file"); } if (lastNewlinePos > -1) { // set file position ready for the next read right after the newline fs.Position = file.LastPosition; bytes = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position)); // set position for next ping file.LastPosition = lastNewlinePos + 1; // read text and run regex to find ip addresses to ban string subString = Encoding.UTF8.GetString(bytes); string[] lines = subString.Split('\n'); string ipAddress = null; string userName = null; bool foundOne = false; // find ip and user name from all lines foreach (string line in lines) { IPBanLog.Write(LogLevel.Debug, "Parsing log file line {0}...", line); bool foundMatch = IPBanService.GetIPAddressAndUserNameFromRegex(Regex, line.Trim(), ref ipAddress, ref userName); if (foundMatch) { IPBanLog.Write(LogLevel.Debug, "Found match, ip: {0}, user: {1}", ipAddress, userName); service.AddFailedLogin(ipAddress, Source, userName); foundOne = true; } else { IPBanLog.Write(LogLevel.Debug, "No match!"); } } if (foundOne) { // signal that we have found ip addresses ipEvent.Set(); } } return(maxFileSize > 0 && fs.Length > maxFileSize); }
private void handleExistingFile(WatchedFile newValue, KeyValuePair <string, (long LineCount, DateTime LastModified)> oldValue)
public void WatchedFileShouldLowerCaseFileNames() { var watchedFile = new WatchedFile(new FileInfo("TOTALLY UPPERCASE"), 0, false); watchedFile.LowerCaseFileName.ShouldBe("totally uppercase"); }
private bool PingFile(WatchedFile file, FileStream fs) { const int maxCountBeforeNewline = 1024; int b; long lastNewlinePos = -1; byte[] bytes; long end = Math.Min(file.LastLength, fs.Length); int countBeforeNewline = 0; fs.Position = file.LastPosition; IPBanLog.Info("Processing watched file {0}, len = {1}, pos = {2}", file.FileName, file.LastLength, file.LastPosition); while (fs.Position < end && countBeforeNewline++ != maxCountBeforeNewline) { // read until last \n is found b = fs.ReadByte(); if (b == '\n') { lastNewlinePos = fs.Position - 1; countBeforeNewline = 0; } } if (countBeforeNewline == maxCountBeforeNewline) { throw new InvalidOperationException("Log file " + this.fileMask + " may not be a plain text new line delimited file"); } if (lastNewlinePos > -1) { // set file position ready for the next read right after the newline fs.Position = file.LastPosition; bytes = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position)); // set position for next ping file.LastPosition = lastNewlinePos + 1; // read text and run regex to find ip addresses to ban string subString = Encoding.UTF8.GetString(bytes); string[] lines = subString.Split('\n'); bool foundOne = false; // find ip and user name from all lines foreach (string line in lines) { string trimmedLine = line.Trim(); IPBanLog.Debug("Parsing log file line {0}...", trimmedLine); IPAddressLogInfo info = IPBanService.GetIPAddressInfoFromRegex(dns, Regex, trimmedLine); if (info.FoundMatch) { info.Source = info.Source ?? Source; IPBanLog.Debug("Log file found match, ip: {0}, user: {1}, source: {2}, count: {3}", info.IPAddress, info.UserName, info.Source, info.Count); failedLogin.AddFailedLogin(info); foundOne = true; } else { IPBanLog.Debug("No match for line {0}", line); } } if (foundOne) { // signal that we have found ip addresses ipEvent.Set(); } } return(maxFileSize > 0 && fs.Length > maxFileSize); }
public FileEvent(WatchedFile watchedFile, Alphabet @event) { WatchedFile = watchedFile; Event = @event; }
/// <summary> /// Executed when a processed file is modified /// </summary> /// <param name="file"></param> private void FileModified(WatchedFile file) { //Raise the event on the file watch options file.BundleOptions.FileWatchOptions.Changed(new FileWatchEventArgs(file, _fileSystemHelper)); }
bool CheckFile(WatchedFile file) { if (!File.Exists(file.FileName)) { OnFileDeleted(file.FileName); return false; } else { DateTime fileWriteTime = File.GetLastWriteTime(file.FileName); FileAttributes fileAttributes = File.GetAttributes(file.FileName); if (fileWriteTime.CompareTo(file.LastWriteTime) > 0) { OnFileModified(file.FileName); } if (fileAttributes != file.Attributes) { OnFileAttributesChanged(file.FileName, file.Attributes, fileAttributes); } file.Attributes = fileAttributes; file.LastWriteTime = fileWriteTime; return true; } }