public FsItem(string path, FsItem parent, bool isDir, bool IsRoot = false) { Parent = parent; if (IsRoot) { //Special path is root *Root FullPath = path; ItemType = FsItemType.Root; } else { FullPath = System.IO.Path.GetFullPath(path); ItemType = isDir ? FsItemType.Folder : FsItemType.File; //Hash = path.GetHashCode(); LastModifyTime = System.IO.File.GetLastWriteTime(FullPath); Hash = FullPath.GetHashCode(); if (isDir) { FileOrDir = BottomFolder(FullPath).Trim();; LastModifyTime = Directory.GetLastWriteTime(path); } else { FileOrDir = System.IO.Path.GetFileName(FullPath).Trim();; LastModifyTime = File.GetLastWriteTime(path); } } }
private void BuildFileTree(bool buildOnly) { PollResults results = new PollResults(); if (Root == null) { Root = new FsItem("*Root", null, true, true); } try { //Scan for changes and update tree SetStatus("Scanning.."); Stopwatch s = new Stopwatch(); s.Start(); results.TotalFileCount = Globals.FileCountInAllRoots(); Root.Build(ref results); Root.UpdateFolderContentsChanged(); s.Stop(); SetStatus("Scan complete."); //Do work to show what's changed. BeginInvoke(new Action(() => { StatsForm.LastScanTimeMs = s.ElapsedMilliseconds; StatsForm.AverageScanTimeMs = (StatsForm.AverageScanTimeMs + StatsForm.LastScanTimeMs) / 2; StatsForm.NumberOfScans++; StatsForm.FileCount = results.ScannedFileCountLastScan; StatsForm.NewCount = results.NewFileCountLastScan; StatsForm.AvgAddedPerScan = (StatsForm.AvgAddedPerScan + results.NumAdded) / 2; StatsForm.AvgDeletedPerScan = (StatsForm.AvgDeletedPerScan + results.NumDeleted) / 2; StatsForm.AvgChangedPerScan = (StatsForm.AvgChangedPerScan + results.NumChanged) / 2; StatsForm.TopFileWriteList = results.TopFileChanges; UpdateStats(); })); if (buildOnly == false) { if (Root.HasChanges()) { PrintResults(); ExecShellCommand(); } } //MUST clean tree after building to remove changed files. Root.CleanAndSync(true); } catch (Exception ex) { Globals.LogError(ex.ToString()); } }
public void RemoveChild(FsItem r) { Children.Remove(r); if (r.IsFolder()) { Folders.Remove(r); } else { Files.Remove(r); } r.Parent = null; }
private void CheckRemoved(ref PollResults results, List <FsItem> items) { //Check Removed Items foreach (FsItem c in Children) { FsItem found = items.Where(x => x.Hash == c.Hash).FirstOrDefault(); if (found == null) { //Item was removed c.FsItemState = FsItemState.Removed; results.NumDeleted++; } } }
private void CheckHotFile(FsItem child, ref PollResults results) { //Cache the top changed files if (child.WriteCount > 0) { if (results.TopFileChanges.Count == 0 || child.WriteCount > results.TopFileChanges[results.TopFileChanges.Count - 1].WriteCount) { results.TopFileChanges.Add(child); if (results.TopFileChanges.Count > Globals.Settings.NumTopFiles) { results.TopFileChanges.RemoveRange(Globals.Settings.NumTopFiles, results.TopFileChanges.Count - Globals.Settings.NumTopFiles); } } } }
private void CheckForNewOrChangedFiles(ref PollResults results, List <FsItem> items) { //Compare our tree with the FS items. foreach (FsItem item in items) { if ((!item.IsFolder() && Globals.Settings.PollFiles) || (item.IsFolder() && Globals.Settings.PollDirectories)) { if (FsItemState == FsItemState.Added) { //We are a new Dir //Simply add everything to the new parent. AddChildNode(item); item.FsItemState = FsItemState.Added; results.NumAdded++; } else { FsItem child = FindChild(item, false); if (child != null) { //item is there. //*We will ignore changes to folders (whcih in windows, shows only parent folder changed if contents changed). //Instead we show the whole parent branch as modified if a file gets changed if (child.IsFolder() == false) { if (item.LastModifyTime > child.LastModifyTime) { child.FsItemState = FsItemState.Changed; child.NewModifyTime = item.LastModifyTime; results.NumChanged++; child.WriteCount++; } CheckHotFile(child, ref results); } } else { AddChildNode(item); item.FsItemState = FsItemState.Added; results.NumAdded++; } } } } }
public FsItem AddChildNode(FsItem item) { System.Diagnostics.Debug.Assert(item.Parent == null); item.Parent = this; if (item.IsFolder()) { Folders.Add(item); } else { Files.Add(item); } Children.Add(item); return(item); }
public FsItem FindChild(FsItem h_no_parent, bool recursive = false) { System.Diagnostics.Debug.Assert(h_no_parent.Parent == null); foreach (FsItem f in Files) { if (f.Hash == h_no_parent.Hash) { return(f); } } foreach (FsItem f in Folders) { if (f.Hash == h_no_parent.Hash) { return(f); } } return(null); }