-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandShrink.cs
50 lines (49 loc) · 1.75 KB
/
CommandShrink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace de.intronik.hashlinkcopy
{
[Description(@"tries to replace duplicates by hardlinks")]
class CommandShrink : CommandTreeWalker
{
static System.Security.Cryptography.SHA1 SHA1 = System.Security.Cryptography.SHA1.Create();
protected override void ProcessFile(FileInfo info, int level)
{
var hi = new HashInfo(info, CommandShrink.SHA1);
var hf = Path.GetFullPath(hi.GetHashPath(HashDir));
// check if we need to copy the file
if (!File.Exists(hf))
{
Directory.CreateDirectory(Path.GetDirectoryName(hf));
Monitor.Root.MoveFile(info.FullName, hf, info.Length);
File.SetAttributes(hf, FileAttributes.Normal);
}
else
{
var hInfo = new FileInfo(hf);
if (hInfo.Length != info.Length)
{
Monitor.Root.HashCollision(hf, info.FullName);
return;
}
Monitor.Root.DeleteFile(info);
}
// create link
if (Monitor.Root.LinkFile(hf, info.FullName, info.Length)!=0)
// 10bit link count overrun => move the hash file
Monitor.Root.MoveFile(hf, info.FullName, info.Length);
// adjust file attributes and the last write time
try
{
info.LastAccessTimeUtc = info.LastWriteTimeUtc;
info.Attributes = info.Attributes;
}
catch
{
}
}
}
}