public void EndPhase() { currentPhase++; if (currentPhase <= numPhases) progress = (double)currentPhase / (double)numPhases; if (phaseProgress != null) phaseProgress = null; }
public void Reset(int phases) { progress = 0.0f; numPhases = phases; if (phases > 0) progressPerPhase = 1.0 / phases; else progressPerPhase = 0.0; currentPhase = 0; phaseProgress = null; }
public void EndPhase() { currentPhase++; if (currentPhase <= numPhases) { progress = (double)currentPhase / (double)numPhases; } if (phaseProgress != null) { phaseProgress = null; } }
public void Reset(int phases) { progress = 0.0f; numPhases = phases; if (phases > 0) { progressPerPhase = 1.0 / phases; } else { progressPerPhase = 0.0; } currentPhase = 0; phaseProgress = null; }
public ProgressEstimator BeginSubPhase(int phases) { phaseProgress = new ProgressEstimator(); phaseProgress.Reset(phases); return(phaseProgress); }
public ProgressEstimator BeginSubPhase(int phases) { phaseProgress = new ProgressEstimator(); phaseProgress.Reset(phases); return phaseProgress; }
private void Scan(DirectoryInfo dir, List <Regex> ignores, ProgressEstimator progress = null) { if (cancelScan) { return; } // never allow scanning of our own parity folder if (Utils.PathsAreEqual(dir.FullName, config.ParityDir)) { LogFile.Log("Warning: skipping " + dir.FullName + " because it is the parity folder."); return; } Status = "Scanning " + dir.FullName; if (scanProgress != null) { Progress = scanProgress.Progress; } DirectoryInfo[] subDirs; try { subDirs = dir.GetDirectories(); } catch (Exception e) { if (progress == null) { throw; } LogFile.Log("Warning: Could not enumerate subdirectories of {0}: {1}", dir.FullName, e.Message); return; } FileInfo[] fileInfos; try { fileInfos = dir.GetFiles(); } catch (Exception e) { LogFile.Log("Warning: Could not enumerate files in {0}: {1}", dir.FullName, e.Message); return; } ProgressEstimator folderProgress; if (scanProgress == null) { scanProgress = new ProgressEstimator(); scanProgress.Reset(subDirs.Length); folderProgress = scanProgress; } else { folderProgress = progress.BeginSubPhase(subDirs.Length); } foreach (DirectoryInfo d in subDirs) { if (cancelScan) { return; } if ((config.IgnoreHidden && (d.Attributes & FileAttributes.Hidden) != 0) || ((d.Attributes & FileAttributes.System) != 0)) { folderProgress.EndPhase(); continue; } string subDir = Path.Combine(dir.FullName, d.Name); if (subDir.Length >= MAX_FOLDER) { LogFile.Log("Warning: skipping folder \"" + subDir + "\" because the path is too long."); } else { Scan(d, ignores, folderProgress); } folderProgress.EndPhase(); } Progress = scanProgress.Progress; string relativePath = Utils.StripRoot(root, dir.FullName); foreach (FileInfo f in fileInfos) { if (cancelScan) { return; } // have to use Path.Combine here because accessing the f.FullName property throws // an exception if the path is too long string fullName = Path.Combine(dir.FullName, f.Name); try { if (fullName.Length >= MAX_PATH) { LogFile.Log("Warning: skipping file \"" + fullName + "\" because the path is too long"); continue; } if (f.Attributes == (FileAttributes)(-1)) { continue; } if (config.IgnoreHidden && (f.Attributes & FileAttributes.Hidden) != 0) { continue; } if ((f.Attributes & FileAttributes.System) != 0) { continue; } bool ignore = false; foreach (Regex regex in ignores) { if (regex.IsMatch(f.Name.ToLower())) { ignore = true; break; } } if (ignore) { if (LogFile.Verbose) { LogFile.Log("Skipping \"{0}\" because it matches an ignore", f.FullName); } ignoreCount++; continue; } FileRecord r = new FileRecord(f, relativePath, this); scanFiles.Add(r); seenFileNames[r.Name.ToLower()] = r; } catch (Exception e) { errorFiles.Add(fullName.ToLower()); FireErrorMessage(String.Format("Error scanning \"{0}\": {1}", fullName, e.Message)); } } }
/// <summary> /// Scans the drive from 'root' down, generating the current list of files on the drive. /// Files that should not be protected (e.g. Hidden files) are not included. /// </summary> public void Scan(bool auto = false) { Debug.Assert(!Scanning); if (Scanning) { return; // shouldn't happen } Scanning = true; DriveStatus = DriveStatus.Scanning; cancelScan = false; Progress = 0; LastScanStart = DateTime.Now; ignoreCount = 0; bool error = false; try { // Convert list of ignores to a list of Regex List <Regex> ignores = new List <Regex>(); foreach (string i in config.Ignores) { string pattern = Regex.Escape(i.ToLower()); // Escape the original string pattern = pattern.Replace(@"\?", "."); // Replace all \? with . pattern = pattern.Replace(@"\*", ".*"); // Replace all \* with .* ignores.Add(new Regex(pattern)); } scanFiles.Clear(); LogFile.Log("Scanning {0}...", root); scanProgress = null; try { DirectoryInfo rootInfo = new DirectoryInfo(root); // try enumerating the sub directories of root, for the sole purpose of triggering an exception // (caught below) if there is some reason why we can't access the drive rootInfo.GetDirectories(); Scan(rootInfo, ignores); if (!cancelScan) { long totalSize = 0; foreach (FileRecord f in scanFiles) { totalSize += f.Length; } Status = "Scan complete. Analyzing results..."; Progress = 1; AnalyzingResults = true; Compare(); LogFile.Log("Scan of {0} complete. Found {1} file{2} ({3} total) Adds: {4} Deletes: {5} Moves: {6} Edits: {7} Ignored: {8}", Root, scanFiles.Count, scanFiles.Count == 1 ? "" : "s", Utils.SmartSize(totalSize), adds.Count, deletes.Count, moves.Count, editCount, ignoreCount); if (cancelScan) { Status = "Scan required"; DriveStatus = DriveStatus.ScanRequired; return; } // process moves now as part of the scan, since they don't require changes to parity ProcessMoves(); } else { LogFile.Log("{0}: Scan cancelled", Root); } } catch (Exception e) { FireErrorMessage(String.Format("Could not scan {0}: {1}", root, e.Message)); LogFile.Log(e.StackTrace); LastError = e.Message; DriveStatus = DriveStatus.AccessError; error = true; return; } } finally { AnalyzingResults = false; Progress = 0; UpdateStatus(); scanFiles.Clear(); seenFileNames.Clear(); errorFiles.Clear(); FireScanCompleted(cancelScan, error, adds.Count > 0 || deletes.Count > 0, auto); Scanning = false; } }
/// <summary> /// Scans the drive from 'root' down, generating the current list of files on the drive. /// Files that should not be protected (e.g. Hidden files) are not included. /// </summary> public void Scan(bool auto = false) { Debug.Assert(!Scanning); if (Scanning) return; // shouldn't happen Scanning = true; DriveStatus = DriveStatus.Scanning; cancelScan = false; Progress = 0; LastScanStart = DateTime.Now; ignoreCount = 0; bool error = false; try { // Convert list of ignores to a list of Regex List<Regex> ignores = new List<Regex>(); foreach (string i in config.Ignores) { string pattern = Regex.Escape(i.ToLower()); // Escape the original string pattern = pattern.Replace(@"\?", "."); // Replace all \? with . pattern = pattern.Replace(@"\*", ".*"); // Replace all \* with .* ignores.Add(new Regex(pattern)); } scanFiles.Clear(); LogFile.Log("Scanning {0}...", root); scanProgress = null; try { DirectoryInfo rootInfo = new DirectoryInfo(root); // try enumerating the sub directories of root, for the sole purpose of triggering an exception // (caught below) if there is some reason why we can't access the drive rootInfo.GetDirectories(); Scan(rootInfo, ignores); if (!cancelScan) { long totalSize = 0; foreach (FileRecord f in scanFiles) totalSize += f.Length; Status = "Scan complete. Analyzing results..."; Progress = 1; AnalyzingResults = true; Compare(); LogFile.Log("Scan of {0} complete. Found {1} file{2} ({3} total) Adds: {4} Deletes: {5} Moves: {6} Edits: {7} Ignored: {8}", Root, scanFiles.Count, scanFiles.Count == 1 ? "" : "s", Utils.SmartSize(totalSize), adds.Count, deletes.Count, moves.Count, editCount, ignoreCount); if (cancelScan) { Status = "Scan required"; DriveStatus = DriveStatus.ScanRequired; return; } // process moves now as part of the scan, since they don't require changes to parity ProcessMoves(); } else LogFile.Log("{0}: Scan cancelled", Root); } catch (Exception e) { FireErrorMessage(String.Format("Could not scan {0}: {1}", root, e.Message)); LogFile.Log(e.StackTrace); LastError = e.Message; DriveStatus = DriveStatus.AccessError; error = true; return; } } finally { AnalyzingResults = false; Progress = 0; UpdateStatus(); scanFiles.Clear(); seenFileNames.Clear(); errorFiles.Clear(); FireScanCompleted(cancelScan, error, adds.Count > 0 || deletes.Count > 0, auto); Scanning = false; } }
private void Scan(DirectoryInfo dir, List<Regex> ignores, ProgressEstimator progress = null) { if (cancelScan) return; // never allow scanning of our own parity folder if (Utils.PathsAreEqual(dir.FullName, config.ParityDir)) { LogFile.Log("Warning: skipping " + dir.FullName + " because it is the parity folder."); return; } Status = "Scanning " + dir.FullName; if (scanProgress != null) Progress = scanProgress.Progress; DirectoryInfo[] subDirs; try { subDirs = dir.GetDirectories(); } catch (Exception e) { if (progress == null) throw; LogFile.Log("Warning: Could not enumerate subdirectories of {0}: {1}", dir.FullName, e.Message); return; } FileInfo[] fileInfos; try { fileInfos = dir.GetFiles(); } catch (Exception e) { LogFile.Log("Warning: Could not enumerate files in {0}: {1}", dir.FullName, e.Message); return; } ProgressEstimator folderProgress; if (scanProgress == null) { scanProgress = new ProgressEstimator(); scanProgress.Reset(subDirs.Length); folderProgress = scanProgress; } else folderProgress = progress.BeginSubPhase(subDirs.Length); foreach (DirectoryInfo d in subDirs) { if (cancelScan) return; if ((config.IgnoreHidden && (d.Attributes & FileAttributes.Hidden) != 0) || ((d.Attributes & FileAttributes.System) != 0)) { folderProgress.EndPhase(); continue; } string subDir = Path.Combine(dir.FullName, d.Name); if (subDir.Length >= MAX_FOLDER) LogFile.Log("Warning: skipping folder \"" + subDir + "\" because the path is too long."); else Scan(d, ignores, folderProgress); folderProgress.EndPhase(); } Progress = scanProgress.Progress; string relativePath = Utils.StripRoot(root, dir.FullName); foreach (FileInfo f in fileInfos) { if (cancelScan) return; // have to use Path.Combine here because accessing the f.FullName property throws // an exception if the path is too long string fullName = Path.Combine(dir.FullName, f.Name); try { if (fullName.Length >= MAX_PATH) { LogFile.Log("Warning: skipping file \"" + fullName + "\" because the path is too long"); continue; } if (f.Attributes == (FileAttributes)(-1)) continue; if (config.IgnoreHidden && (f.Attributes & FileAttributes.Hidden) != 0) continue; if ((f.Attributes & FileAttributes.System) != 0) continue; bool ignore = false; foreach (Regex regex in ignores) if (regex.IsMatch(f.Name.ToLower())) { ignore = true; break; } if (ignore) { if (LogFile.Verbose) LogFile.Log("Skipping \"{0}\" because it matches an ignore", f.FullName); ignoreCount++; continue; } FileRecord r = new FileRecord(f, relativePath, this); scanFiles.Add(r); seenFileNames[r.Name.ToLower()] = r; } catch (Exception e) { errorFiles.Add(fullName.ToLower()); FireErrorMessage(String.Format("Error scanning \"{0}\": {1}", fullName, e.Message)); } } }