Repository navigation
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
210 lines (161 loc) · 5.51 KB
/
Copy pathExtensionMethods.cs
File metadata and controls
210 lines (161 loc) · 5.51 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Collections.Generic;
using System.Linq;
using MFT.Attributes;
namespace MFT.Other;
public static class ExtensionMethods
{
public static int GetReferenceCount(this FileRecord record)
{
var fns = record.Attributes.Where(t => t.AttributeType == AttributeType.FileName);
var hashes = new HashSet<string>();
foreach (var attribute in fns)
{
var fn = (FileName)attribute;
if (fn.FileInfo.NameType == NameTypes.Dos)
{
continue;
}
var key = $"{fn.FileInfo.FileName}-{fn.FileInfo.ParentMftRecord.GetKey()}";
hashes.Add(key);
}
return hashes.Count;
}
public static ReparsePoint GetReparsePoint(this FileRecord record)
{
var reparseAttr =
record.Attributes.Where(t =>
t.AttributeType == AttributeType.ReparsePoint).ToList();
return (ReparsePoint)reparseAttr.FirstOrDefault();
}
public static FileName GetFileNameAttributeFromFileRecord(this FileRecord fr) //, int attributeNumber = -1
{
// if (attributeNumber > -1)
// {
// var fin = fr.Attributes.SingleOrDefault(t =>
// t.AttributeType == AttributeType.FileName && ((FileName) t).AttributeNumber == attributeNumber);
//
// return (FileName) fin;
// }
var fi = fr.Attributes.FirstOrDefault(t =>
t.AttributeType == AttributeType.FileName && ((FileName)t).FileInfo.NameType == NameTypes.DosWindows);
if (fi != null)
{
return (FileName)fi;
}
fi = fr.Attributes.FirstOrDefault(t =>
t.AttributeType == AttributeType.FileName && ((FileName)t).FileInfo.NameType == NameTypes.Windows);
if (fi != null)
{
return (FileName)fi;
}
fi = fr.Attributes.FirstOrDefault(t =>
t.AttributeType == AttributeType.FileName && ((FileName)t).FileInfo.NameType == NameTypes.Posix);
if (fi != null)
{
return (FileName)fi;
}
fi = fr.Attributes.SingleOrDefault(t =>
t.AttributeType == AttributeType.FileName && ((FileName)t).FileInfo.NameType == NameTypes.Dos);
return (FileName)fi;
}
public static string GetKey(this MftEntryInfo mftInfo, bool asDecimal = false)
{
if (asDecimal)
{
return $"{mftInfo.MftEntryNumber}-{mftInfo.MftSequenceNumber}";
}
return $"{mftInfo.MftEntryNumber:X8}-{mftInfo.MftSequenceNumber:X8}";
}
public static string GetKey(this FileRecord record, bool asDecimal = false)
{
if (asDecimal)
{
if (record.IsDeleted())
{
return $"{record.EntryNumber}-{record.SequenceNumber - 1}";
}
return $"{record.EntryNumber}-{record.SequenceNumber}";
}
if (record.IsDeleted())
{
return $"{record.EntryNumber:X8}-{record.SequenceNumber - 1:X8}";
}
return $"{record.EntryNumber:X8}-{record.SequenceNumber:X8}";
}
public static bool IsDirectory(this FileRecord record)
{
if (record == null)
{
return false;
}
return (record.EntryFlags & FileRecord.EntryFlag.IsDirectory) ==
FileRecord.EntryFlag.IsDirectory;
}
public static bool IsDeleted(this FileRecord record)
{
return (record.EntryFlags & FileRecord.EntryFlag.InUse) !=
FileRecord.EntryFlag.InUse;
}
public static bool HasAds(this FileRecord record)
{
var dataAttrs =
record.Attributes.Where(t =>
t.AttributeType == AttributeType.Data && t.NameSize > 0).ToList();
return dataAttrs.Count > 0;
}
public static List<AdsInfo> GetAlternateDataStreams(this FileRecord record)
{
var l = new List<AdsInfo>();
var dataAttrs =
record.Attributes.Where(t =>
t.AttributeType == AttributeType.Data && t.NameSize > 0).ToList();
foreach (var attribute in dataAttrs)
{
var da = (Data)attribute;
if (da.IsResident == false && da.NonResidentData.StartingVirtualClusterNumber > 0)
{
continue;
}
ulong size;
if (da.IsResident)
{
size = (ulong)da.AttributeContentLength;
}
else
{
size = da.NonResidentData.ActualSize;
}
var adsi = new AdsInfo(da.Name, size, da.ResidentData, da.NonResidentData, da.AttributeNumber);
l.Add(adsi);
}
return l;
}
public static ulong GetFileSize(this FileRecord record)
{
if (record.IsDirectory())
{
return 0;
}
var fn = record.Attributes.FirstOrDefault(t => t.AttributeType == AttributeType.FileName);
var datas = record.Attributes.Where(t => t.AttributeType == AttributeType.Data).ToList();
if (datas.Count >= 1)
{
var data = (Data)datas.First();
if (data.IsResident)
{
return (ulong)data.ResidentData.Data.LongLength;
}
return data.NonResidentData.ActualSize;
}
if (datas.Count != 0)
{
return 0;
}
if (fn != null)
{
var fna = (FileName)fn;
return fna.FileInfo.LogicalSize;
}
return 0;
}
}