public static int CompareByRoot(FsxContext a, FsxContext b) { if (a == null) { Debug.Assert(false); return((b == null) ? 0 : -1); } if (b == null) { Debug.Assert(false); return(1); } return(StrUtil.CompareNaturally(a.RootDirectory, b.RootDirectory)); }
private static void FindInDirectory(string strPath, FsxContext ctx) { if (string.IsNullOrEmpty(strPath)) { Debug.Assert(false); return; } if (ctx == null) { Debug.Assert(false); return; } try { string[] vFiles = Directory.GetFiles(strPath, "*", SearchOption.TopDirectoryOnly); if (vFiles == null) { Debug.Assert(false); vFiles = new string[0]; } foreach (string strFile in vFiles) { if (string.IsNullOrEmpty(strFile)) { Debug.Assert(false); continue; } if (ctx.End) { return; } FsxResult r = ctx.Match(strFile); if (r != null) { ctx.AddResult(r); } } string[] vDirs = Directory.GetDirectories(strPath, "*", SearchOption.TopDirectoryOnly); if (vDirs == null) { Debug.Assert(false); vDirs = new string[0]; } foreach (string strDir in vDirs) { if (string.IsNullOrEmpty(strDir)) { Debug.Assert(false); continue; } if (ctx.End) { return; } string strTerm = UrlUtil.EnsureTerminatingSeparator(strDir, false); if (IsDirTraversal(strTerm)) { Debug.Assert(false); continue; } FindInDirectory(strDir, ctx); } } catch (UnauthorizedAccessException) { } catch (Exception) { Debug.Assert(false); } }
private static List <FsxContext> Find(FsxMatchFn fMatch, IStatusLogger sl, string strRootPath) { if (sl == null) { Debug.Assert(false); sl = new NullStatusLogger(); } List <FsxContext> lContexts = new List <FsxContext>(); if (!string.IsNullOrEmpty(strRootPath)) { lContexts.Add(new FsxContext(strRootPath, string.Empty, fMatch)); } else { DriveInfo[] vDrives = DriveInfo.GetDrives(); if (vDrives == null) { Debug.Assert(false); vDrives = new DriveInfo[0]; } foreach (DriveInfo di in vDrives) { if (di == null) { Debug.Assert(false); continue; } try { if (!di.IsReady) { continue; } string strRoot = di.RootDirectory.FullName; if (string.IsNullOrEmpty(strRoot)) { Debug.Assert(false); continue; } string strVolumeLabel = string.Empty; try { strVolumeLabel = (di.VolumeLabel ?? string.Empty); } catch (Exception) { Debug.Assert(false); } lContexts.Add(new FsxContext(strRoot, strVolumeLabel, fMatch)); } catch (Exception) { Debug.Assert(false); } } } for (int i = lContexts.Count - 1; i >= 0; --i) { FsxContext ctx = lContexts[i]; try { Thread th = new Thread(delegate() { try { try { FindInDirectory(ctx.RootDirectory, ctx); } catch (Exception) { Debug.Assert(false); } ctx.End = true; } catch (Exception) { Debug.Assert(false); } }); th.Start(); } catch (Exception) { Debug.Assert(false); lContexts.RemoveAt(i); } } lContexts.Sort(FsxContext.CompareByRoot); sl.SetText(GetSearchingText(lContexts), LogStatusType.Info); List <FsxContext> lRunning = new List <FsxContext>(lContexts); int msSleep = Math.Max(PwDefs.UIUpdateDelay / 4, 1); while (lRunning.Count != 0) { try { Thread.Sleep(msSleep); for (int i = lRunning.Count - 1; i >= 0; --i) { if (lRunning[i].End) { lRunning.RemoveAt(i); sl.SetText(GetSearchingText(lRunning), LogStatusType.Info); } } if (!sl.ContinueWork()) { foreach (FsxContext ctx in lRunning) { ctx.End = true; } lRunning.Clear(); } } catch (Exception) { Debug.Assert(false); } } return(lContexts); }