using System; using System.IO; using System.Linq; using UAF; using UAF.Interfaces; using System.Collections.Generic; namespace DataFileAnalyzer { /// /// Update basic info of a data file /// public class BasicFileUpdate : UpdateOperation { protected DateTime m_lastCheckTime = DateTimeExtensions.Invalid; protected FileInfo m_fileInfo = null; /// /// Constructor /// public BasicFileUpdate(FileUpdater _updater, string _filePath) : base(_updater, _filePath) { } /// /// Check that the file still exists and collect info /// public override void Run() { SetProgress(States.Running); string fullPath = Path.Combine(m_updater.Library.DataFolder, FilePath); m_lastCheckTime = DateTime.UtcNow; m_fileInfo = new FileInfo(fullPath); SetProgress(States.WaitUpdate); } /// /// Update the library with the collected info /// public override void UpdateLibrary() { bool skipped = (State & States.Skipped) != States.None; bool error = (State & States.Error) != States.None; bool isMissing = false; DataFile dataFile = m_library.GetFile(NormalizedFilePath); if (dataFile != null) { isMissing = dataFile.IsMissing; dataFile.ClearErrorsAndWarnings(); } if (skipped ||error) { if (error && dataFile != null) { dataFile.AddError(DataFileError.Codes.BasicUpdate); } SetProgress(States.Finished); return; } SetProgress(States.Updating); if (dataFile == null && m_fileInfo.Exists) { // New file for the library //m_logger.Message("New file detected: " + FilePath); if (!m_library.CreateNewDataFile(NormalizedFilePath, out dataFile)) { m_logger.Error("Could not add file to library: " + FilePath); State |= States.Error; } } if (dataFile != null) { if (isMissing) { // File is already in the library (probably missing). dataFile.RemoveError(DataFileError.Codes.MissingFile); // Update dependencies on parents. foreach (DataFileDependency parentFileDep in dataFile.Parents) { DataFile parentFile = m_library.GetFile(parentFileDep.Key); if (parentFile != null) { DataFileDependency childFileDep = parentFile.Children.FirstOrDefault(dep => dep.Key == dataFile.Key); if (childFileDep != null) childFileDep.Missing = false; if (parentFile.HasMissingDependencies) { if (!dataFile.Children.Any(child => child.Missing)) parentFile.RemoveError(DataFileError.Codes.UnresolvedDependencies); } } } } if (m_fileInfo.Exists) { // Update the file if it has been modified since last time we checked on it. // NB: If we overwrite a file with one whose latest modification time is // older, then the overwritten file has this older modification time. So we // need to update it even if the modification time is older than last time we // updated it. //m_logger.Message("File updated: " + FilePath); dataFile.LastModificationTime = m_fileInfo.LastWriteTimeUtc; dataFile.LastCheckTime = m_lastCheckTime; dataFile.Length = m_fileInfo.Length; // Launch dependencies update dataFile.LastDependenciesCheckTime = DateTimeExtensions.Invalid; m_updater.AskForFileDependenciesUpdate(dataFile.FilePath); dataFile.AddWarning(DataFileError.Codes.NeedDependencies); } else { // The file has been deleted //m_logger.Message("File delete detected: " + FilePath); m_library.RemoveFile(dataFile.Key); } } SetProgress(States.Finished); //m_logger.Message("[Basic Update] Updated {0}", NormalizedFilePath); } } }