Program.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // See https://aka.ms/new-console-template for more information
  2. using System.Runtime.InteropServices;
  3. using System.Security.Cryptography;
  4. [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
  5. static extern bool CreateHardLink(
  6. string lpFileName,
  7. string lpExistingFileName,
  8. IntPtr lpSecurityAttributes
  9. );
  10. Console.WriteLine("Installer is suck");
  11. if (args.Length == 0)
  12. {
  13. Console.WriteLine("Usage: <inDir> <outDir>");
  14. return;
  15. }
  16. var files = Directory.GetFiles(args[0]);
  17. foreach (var file in files)
  18. {
  19. try
  20. {
  21. Console.WriteLine($"> {file}");
  22. var info = new FileInfo(file);
  23. Console.WriteLine($" Len: {info.Length:N0}");
  24. Console.Write(" SHA256...");
  25. string hexHash;
  26. {
  27. using var inputStream = info.OpenRead();
  28. using var sha256 = SHA256.Create();
  29. var hash = sha256.ComputeHash(inputStream);
  30. hexHash = Convert.ToHexString(hash);
  31. }
  32. Console.WriteLine($" {hexHash}");
  33. var targetPath = Path.Combine(args[1], $"{info.Length:0-000-000-000}_{hexHash}{info.Extension}");
  34. if (File.Exists(targetPath))
  35. {
  36. File.Delete(file);
  37. }
  38. else
  39. {
  40. File.Move(file, targetPath);
  41. }
  42. CreateHardLink(file, targetPath, nint.Zero);
  43. Console.WriteLine($" Collapse to {targetPath} <");
  44. }
  45. catch (Exception e)
  46. {
  47. Console.WriteLine($"Error:{e.Message}");
  48. }
  49. Console.WriteLine();
  50. }