static int Main(string[] args) { Console.WriteLine("Redate"); try { CmdLineParser cmd = null; try { cmd = new CmdLineParser(args); } catch { Console.WriteLine(); Console.WriteLine("Error parsing command line."); Console.WriteLine(); CmdLineParser.PrintHelp(); throw; } switch (cmd.RunMode) { case RunMode.Init: Console.WriteLine("Init"); { Console.WriteLine("Collecting Data"); string targetDir = System.IO.Path.GetDirectoryName(cmd.RedateFile) + '\\'; FileCollectionInfoData files = new FileCollectionInfoData(); files.Collect(cmd.SourceDirs); files.SourceDirsToRelative(targetDir); Console.WriteLine("Compute MD5s"); foreach (var f in files.Files) { f.ComputeMd5Hash(); f.PathToRelative(targetDir); } Console.WriteLine("Saving " + System.IO.Path.GetFileName(cmd.RedateFile)); files.FileDate = DateTime.Now; JsonSerializerSettings s = new JsonSerializerSettings(); s.Culture = CultureInfo.InvariantCulture; s.Formatting = Formatting.Indented; System.IO.File.WriteAllText(cmd.RedateFile, JsonConvert.SerializeObject(files, s), new UTF8Encoding(false)); } Console.WriteLine("Done"); break; case RunMode.Run: Console.WriteLine("Run"); { Console.WriteLine("Loading " + System.IO.Path.GetFileName(cmd.RedateFile)); FileCollectionInfoData knownFiles = JsonConvert.DeserializeObject <FileCollectionInfoData>(System.IO.File.ReadAllText(cmd.RedateFile)); string targetDir = System.IO.Path.GetDirectoryName(cmd.RedateFile) + '\\'; knownFiles.SourceDirsToAbsolute(targetDir); foreach (var f in knownFiles.Files) { f.PathToAbsolute(targetDir); } Console.WriteLine("Collecting Data"); FileCollectionInfoData files = new FileCollectionInfoData(); files.Collect(knownFiles.SourceDirs); Console.WriteLine("Compute MD5s"); foreach (var f in files.Files) { f.ComputeMd5Hash(); } Console.WriteLine("Updating"); bool isUpdated = knownFiles.Update(files); if (isUpdated || cmd.ForceFileDateUpdate) { Console.WriteLine("Saving " + System.IO.Path.GetFileName(cmd.RedateFile)); knownFiles.SourceDirsToRelative(targetDir); foreach (var f in knownFiles.Files) { f.PathToRelative(targetDir); } knownFiles.FileDate = DateTime.Now; JsonSerializerSettings s = new JsonSerializerSettings(); s.Culture = CultureInfo.InvariantCulture; s.Formatting = Formatting.Indented; System.IO.File.WriteAllText(cmd.RedateFile, JsonConvert.SerializeObject(knownFiles, s), new UTF8Encoding(false)); } else { Console.WriteLine("Files unchanged - No need to save."); } } Console.WriteLine("Done"); break; case RunMode.FileReg: Console.WriteLine("File Type Registration"); FileTypeReg.Register(); Console.WriteLine("Done"); break; case RunMode.FileUnreg: Console.WriteLine("File Type Unregistration"); FileTypeReg.Unregister(); Console.WriteLine("Done"); break; default: throw new NotImplementedException(); } } catch (Exception ex) { Console.Error.WriteLine("Fatal error: " + ex); WaitBeforeClosingConsole(); return(-1); } WaitBeforeClosingConsole(); return(0); }
/// <summary> /// </summary> /// <param name="files"></param> /// <returns>True if the information of any file has been updated</returns> internal bool Update(FileCollectionInfoData files) { bool retval = false; // this is the old/known state // files is the potentially new state // Update: // File only in this --> delete // File only in files --> add // Files in both // length inequal --> files // md5 inequal --> files // use known --> set date & attributes on existing file Console.WriteLine(); List <FileInfoData> kfs = new List <FileInfoData>(); foreach (FileInfoData fid in Files) { Console.Write(fid.Path + " - "); FileInfoData newfid = files.Files.FirstOrDefault((FileInfoData i) => { return(i.Path.Equals(fid.Path, StringComparison.InvariantCultureIgnoreCase)); }); if (newfid == null) { Console.WriteLine("Removed"); retval = true; continue; // skip fid, will kill it } if (newfid.Length != fid.Length) { kfs.Add(newfid); Console.WriteLine("Size changed"); retval = true; continue; } if (!newfid.Md5Hash.Equals(fid.Md5Hash, StringComparison.InvariantCultureIgnoreCase)) { kfs.Add(newfid); Console.WriteLine("Content changed"); retval = true; continue; } // length and md5 are equal if (newfid.Attributes != fid.Attributes || newfid.WriteTime != fid.WriteTime) { Console.WriteLine("Restoring Date and Attributes"); // set file info System.IO.File.SetLastWriteTimeUtc(fid.Path, fid.WriteTime); System.IO.File.SetAttributes(fid.Path, fid.Attributes); // update and take newfid.Collect(); kfs.Add(newfid); continue; } Console.WriteLine("Unchanged"); kfs.Add(fid); } foreach (FileInfoData fid in files.Files) { FileInfoData oldfid = Files.FirstOrDefault((FileInfoData i) => { return(i.Path.Equals(fid.Path, StringComparison.InvariantCultureIgnoreCase)); }); if (oldfid != null) { continue; // already there } Console.WriteLine(fid.Path + " - Added"); retval = true; kfs.Add(fid); // new --> add } Console.WriteLine(); Files = kfs.ToArray(); return(retval); }