public static IEnumerable <string> ReadLines(string filename) { using (StreamReader reader = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan))) { foreach (string line in CopyFiles.ReadLines(reader)) { yield return(line); } } }
public static void ReadFilesAndSizes(string filename, out UInt64 numberFiles, out UInt64?totalFilesize) { numberFiles = 0; totalFilesize = null; foreach (CopyItem ci in CopyFiles.ReadInputfile(CopyFiles.ReadLines(filename))) { ++numberFiles; if (ci.filesize.HasValue) { if (!totalFilesize.HasValue) { totalFilesize = 0; } totalFilesize += ci.filesize.Value; } } }
static int Main(string[] args) { int rc = 99; try { Opts opts; if ((opts = CommandlineOpts.GetOpts(args)) == null) { return(1); } string FullSrcDir = Misc.GetLongFilenameNotation(Path.GetFullPath(opts.SrcBase)); string FullTrgDir = Misc.GetLongFilenameNotation(Path.GetFullPath(opts.TrgBase)); int checkArgs = 0; if (!File.Exists(opts.FilenameWithFiles)) { Console.Error.WriteLine($"input file does not exists. [{opts.FilenameWithFiles}]"); checkArgs = 2; } if (!Misc.IsDirectory(FullSrcDir)) { Console.Error.WriteLine($"source base is not a directory. [{FullSrcDir}]"); checkArgs += 2; } if (!Misc.IsDirectory(FullTrgDir)) { Console.Error.WriteLine($"target base is not a directory. [{FullTrgDir}]"); checkArgs += 2; } if (checkArgs > 0) { return(checkArgs); } Console.Error.WriteLine($"I: setting ThreadPool.SetMinThreads() to {opts.MaxThreads}"); ThreadPool.SetMinThreads(opts.MaxThreads, opts.MaxThreads); Console.Error.WriteLine( $"source base: {FullSrcDir}\n" + $"target base: {FullTrgDir}"); Stats stats = new Stats(); Task <bool> copyFileTask = null; Task.Run(() => CopyFiles.ReadFilesAndSizes(opts.FilenameWithFiles, out stats.totalFiles, out stats.totalBytes)).ConfigureAwait(false); DateTime started = DateTime.Now; using (TextWriter errorWriter = TextWriter.Synchronized(new StreamWriter(@".\cpError.txt", append: false, encoding: System.Text.Encoding.UTF8))) { copyFileTask = CopyFiles.Start(FullSrcDir, FullTrgDir, files: CopyFiles.ReadInputfile(CopyFiles.ReadLines(opts.FilenameWithFiles)), OnCopy: (string filename, UInt64? filesize) => { Interlocked.Increment(ref stats.copiedCount); if (filesize.HasValue) { Interlocked.Add(ref stats.copiedBytes, (long)filesize.Value); } }, OnWin32Error: (LastErrorCode, Api, Message) => { errorWriter.WriteLine($"{LastErrorCode}\t{Api}\t{Message}"); Interlocked.Increment(ref stats.errorsCount); }, OnDirectoryCreated: () => { Interlocked.Increment(ref stats.CreateDirectoryCalled); }, MaxThreads: opts.MaxThreads, dryRun: opts.dryrun); Spi.StatusLineWriter linewriter = new StatusLineWriter(Console.Error); DateTime StartTime = DateTime.Now; WaitUntilFinished(copyFileTask, 2000, ExecEvery: () => linewriter.Write(GetStatsLine(stats, StartTime)), ExecAtEnd: () => Console.Error.WriteLine()); } WriteEndReport(started, stats); rc = copyFileTask.Result ? 8 : 0; } catch (Exception ex) { Console.Error.WriteLine("Na so wirst ned ooohhooiidd... so dastehst Di boohooid..."); ConsoleAndFileWriter.WriteException(ex, Console.Error); rc = 999; } return(rc); }