public static void Run(string Dirname, Opts opts, ref Stats stats, ref bool CrtlC_pressed, Action<string> OutputHandler, Action<int, string> ErrorHandler, Action<string> ProgressCallback) { Spi.IO.StatusLineWriter StatusWriter = new Spi.IO.StatusLineWriter(); foreach (var entry in Spi.IO.Directory.Entries(Dirname, ErrorHandler, opts.FollowJunctions)) { if (CrtlC_pressed) { break; } if (entry.isDirectory) { stats.AllDirs += 1; if (ProgressCallback != null) { ProgressCallback(entry.Dirname); } continue; } stats.AllBytes += entry.Filesize; stats.AllFiles += 1; bool PrintEntry = (opts.Pattern == null) ? true : Regex.IsMatch(entry.Filename, opts.Pattern); if (PrintEntry) { stats.MatchedBytes += entry.Filesize; stats.MatchedFiles += 1; HandleMatchedFile(entry, opts.FormatString, OutputHandler); } } }
static int Main(string[] args) { Opts opts; if ( (opts=GetOpts(args)) == null) { return 8; } try { bool CrtlC_pressed = false; Stats stats = new Stats(); Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; // means the program execution should go on Console.Error.WriteLine("CTRL-C pressed. closing files. shutting down..."); CrtlC_pressed = true; }; using (var ErrWriter = new ConsoleAndFileWriter(Console.Error, ErrFilename)) using (var OutWriter = new ConsoleAndFileWriter(Console.Out, opts.OutFilename)) { Spi.IO.StatusLineWriter StatusWriter = new Spi.IO.StatusLineWriter(); foreach (string dir in opts.Dirs) { EnumDir.Run(dir, opts, ref stats, ref CrtlC_pressed, (filenamefound) => OutWriter.WriteLine(filenamefound), (rc, ErrDir) => ErrWriter.WriteLine("rc {0}\t{1}", rc, ErrDir), (dirname) => StatusWriter.WriteWithDots(dirname)); } if (ErrWriter.hasDataWritten()) { Console.Error.WriteLine("\nerrors were logged to file [{0}]\n", ErrFilename); } } WriteStats(stats); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); Console.Error.WriteLine(ex.StackTrace); return 12; } return 0; }
static void WriteStats(Stats stats) { Console.Error.WriteLine( "dirs/files {0}/{1} ({2})\n" + "files matched {3} ({4})", stats.AllDirs, stats.AllFiles, Spi.IO.Misc.GetPrettyFilesize(stats.AllBytes), stats.MatchedFiles, Spi.IO.Misc.GetPrettyFilesize(stats.MatchedBytes)); }