-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileExtensions.cs
641 lines (572 loc) · 21.1 KB
/
FileExtensions.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace Communary
{
internal sealed class Win32Native
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFindHandle FindFirstFileExW(
string lpFileName,
FINDEX_INFO_LEVELS fInfoLevelId,
out WIN32_FIND_DATAW lpFindFileData,
FINDEX_SEARCH_OPS fSearchOp,
IntPtr lpSearchFilter,
FINDEX_ADDITIONAL_FLAGS dwAdditionalFlags);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool FindNextFile(SafeFindHandle hFindFile, out WIN32_FIND_DATAW lpFindFileData);
[DllImport("kernel32.dll")]
public static extern bool FindClose(IntPtr hFindFile);
[DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
public static extern bool PathMatchSpec([In] String pszFileParam, [In] String pszSpec);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool GetDiskFreeSpace(
string lpRootPathName,
out uint lpSectorsPerCluster,
out uint lpBytesPerSector,
out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFileW([MarshalAs(UnmanagedType.LPWStr)]string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool RemoveDirectoryW(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetFileAttributesW(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetFileAttributesW(string lpFileName);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern uint GetSecurityInfo(
IntPtr hFindFile,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
out IntPtr pSidOwner,
out IntPtr pSidGroup,
out IntPtr pDacl,
out IntPtr pSacl,
out IntPtr pSecurityDescriptor);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
public static extern uint GetNamedSecurityInfoW(
string pObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
out IntPtr pSidOwner,
out IntPtr pSidGroup,
out IntPtr pDacl,
out IntPtr pSacl,
out IntPtr pSecurityDescriptor);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint LookupAccountSid(
string lpSystemName,
IntPtr psid,
StringBuilder lpName,
ref uint cchName,
[Out] StringBuilder lpReferencedDomainName,
ref uint cchReferencedDomainName,
out uint peUse);
[DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ConvertSidToStringSid(
IntPtr sid,
out IntPtr sidString);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LocalFree(
IntPtr handle
);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WIN32_FIND_DATAW
{
public FileAttributes dwFileAttributes;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
public enum FINDEX_INFO_LEVELS
{
FindExInfoStandard, // Return a standard set of attribute information.
FindExInfoBasic, // Does not return the short file name, improving overall enumeration speed. cAlternateFileName is always a NULL string.
FindExInfoMaxInfoLevel // This value is used for validation. Supported values are less than this value.
}
public enum FINDEX_SEARCH_OPS
{
FindExSearchNameMatch, // The search for a file that matches a specified file name.
FindExSearchLimitToDirectories, // This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.
FindExSearchLimitToDevices // This filtering type is not available.
}
[Flags]
public enum FINDEX_ADDITIONAL_FLAGS
{
FindFirstExCaseSensitive,
FindFirstExLargeFetch
}
public enum SE_OBJECT_TYPE
{
SE_UNKNOWN_OBJECT_TYPE,
SE_FILE_OBJECT,
SE_SERVICE,
SE_PRINTER,
SE_REGISTRY_KEY,
SE_LMSHARE,
SE_KERNEL_OBJECT,
SE_WINDOW_OBJECT,
SE_DS_OBJECT,
SE_DS_OBJECT_ALL,
SE_PROVIDER_DEFINED_OBJECT,
SE_WMIGUID_OBJECT,
SE_REGISTRY_WOW64_32KEY
}
public enum SECURITY_INFORMATION
{
OWNER_SECURITY_INFORMATION = 1, // The owner identifier of the object is being referenced. Right required to query: READ_CONTROL. Right required to set: WRITE_OWNER.
GROUP_SECURITY_INFORMATION = 2, // The primary group identifier of the object is being referenced. Right required to query: READ_CONTROL. Right required to set: WRITE_OWNER.
DACL_SECURITY_INFORMATION = 4, // The DACL of the object is being referenced. Right required to query: READ_CONTROL. Right required to set: WRITE_DAC.
SACL_SECURITY_INFORMATION = 8, // The SACL of the object is being referenced. Right required to query: ACCESS_SYSTEM_SECURITY. Right required to set: ACCESS_SYSTEM_SECURITY.
}
}
[SecurityCritical]
internal class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
{
[SecurityCritical]
public SafeFindHandle() : base(true)
{ }
[SecurityCritical]
protected override bool ReleaseHandle()
{
return Win32Native.FindClose(base.handle);
}
}
public static class FILETIMEExtensions
{
public static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.FILETIME time)
{
ulong high = (ulong)time.dwHighDateTime;
ulong low = (ulong)time.dwLowDateTime;
long fileTime = (long)((high << 32) + low);
return DateTime.FromFileTimeUtc(fileTime);
}
}
public static class FileExtensions
{
// prefix for long path support
private const string normalPrefix = @"\\?\";
private const string uncPrefix = @"\\?\UNC\";
public static uint GetSectorSize(string path)
{
// add prefix to allow for maximum path of up to 32,767 characters
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
uint lpSectorsPerCluster;
uint lpBytesPerSector;
uint lpNumberOfFreeClusters;
uint lpTotalNumberOfClusters;
string pathRoot = Path.GetPathRoot(path);
if (!pathRoot.EndsWith(@"\"))
{
pathRoot = pathRoot + @"\";
}
bool result = Win32Native.GetDiskFreeSpace(pathRoot, out lpSectorsPerCluster, out lpBytesPerSector, out lpNumberOfFreeClusters, out lpTotalNumberOfClusters);
if (result)
{
uint clusterSize = lpSectorsPerCluster * lpBytesPerSector;
return clusterSize;
}
else
{
return 0;
}
}
public static void DeleteFile(string path)
{
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
bool success = Win32Native.DeleteFileW(prefixedPath);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(lastError);
}
}
public static void DeleteDirectory(string path)
{
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
bool success = Win32Native.RemoveDirectoryW(prefixedPath);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(lastError);
}
}
public static void AddFileAttributes(string path, FileAttributes fileAttributes)
{
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
bool success = Win32Native.SetFileAttributesW(prefixedPath, fileAttributes);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(lastError);
}
}
public static uint GetFileAttributes(string path)
{
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
return (Win32Native.GetFileAttributesW(prefixedPath));
}
public static ArrayList ReadLastLines(string path, int last)
{
ArrayList output = new ArrayList();
int count = 0;
foreach (var line in File.ReadLines(path).Reverse())
{
count++;
output.Add(line);
if (count == last)
{
break;
}
}
output.Reverse();
return output;
}
public static string GetFileOwner(string path)
{
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
IntPtr NA = IntPtr.Zero;
IntPtr sidOwner;
var errorCode = Win32Native.GetNamedSecurityInfoW(prefixedPath, Win32Native.SE_OBJECT_TYPE.SE_FILE_OBJECT, Win32Native.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, out sidOwner, out NA, out NA, out NA, out NA);
if (errorCode == 0)
{
const uint bufferLength = 64;
StringBuilder fileOwner = new StringBuilder();
var accountLength = bufferLength;
var domainLength = bufferLength;
StringBuilder ownerAccount = new StringBuilder((int)bufferLength);
StringBuilder ownerDomain = new StringBuilder((int)bufferLength);
uint peUse;
errorCode = Win32Native.LookupAccountSid(null, sidOwner, ownerAccount, ref accountLength, ownerDomain, ref domainLength, out peUse);
if (errorCode != 0)
{
fileOwner.Append(ownerDomain);
fileOwner.Append(@"\");
fileOwner.Append(ownerAccount);
return fileOwner.ToString();
}
else
{
IntPtr sidString = IntPtr.Zero;
if (Win32Native.ConvertSidToStringSid(sidOwner, out sidString))
{
//string account = new System.Security.Principal.SecurityIdentifier(sidOwner).Translate(typeof(System.Security.Principal.NTAccount)).ToString();
//Console.WriteLine(account);
return Marshal.PtrToStringAuto(sidString);
}
else
{
int lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(lastError);
}
}
}
else
{
int lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(lastError);
}
}
public static List<FileInformation> FastFind(string path, string searchPattern, bool getFile, bool getDirectory, bool recurse, int? depth, bool parallel, bool suppressErrors, bool largeFetch, bool getHidden, bool getSystem, bool getReadOnly, bool getCompressed, bool getArchive, bool getReparsePoint, string filterMode)
{
object resultListLock = new object();
Win32Native.WIN32_FIND_DATAW lpFindFileData;
Win32Native.FINDEX_ADDITIONAL_FLAGS additionalFlags = 0;
if (largeFetch)
{
additionalFlags = Win32Native.FINDEX_ADDITIONAL_FLAGS.FindFirstExLargeFetch;
}
// add prefix to allow for maximum path of up to 32,767 characters
string prefixedPath;
if (path.StartsWith(@"\\"))
{
prefixedPath = path.Replace(@"\\", uncPrefix);
}
else
{
prefixedPath = normalPrefix + path;
}
var handle = Win32Native.FindFirstFileExW(prefixedPath + @"\*", Win32Native.FINDEX_INFO_LEVELS.FindExInfoBasic, out lpFindFileData, Win32Native.FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags);
List<FileInformation> resultList = new List<FileInformation>();
List<FileInformation> subDirectoryList = new List<FileInformation>();
if (!handle.IsInvalid)
{
do
{
// skip "." and ".."
if (lpFindFileData.cFileName != "." && lpFindFileData.cFileName != "..")
{
// if directory...
if ((lpFindFileData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
{
// ...and if we are performing a recursive search...
if (recurse)
{
// ... populate the subdirectory list
string fullName = Path.Combine(path, lpFindFileData.cFileName);
subDirectoryList.Add(new FileInformation { Path = fullName });
}
}
// skip folders if only the getFile parameter is used
if (getFile && !getDirectory)
{
if ((lpFindFileData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
{
continue;
}
}
// if file matches search pattern and attribute filter, add it to the result list
if (MatchesFilter(lpFindFileData.dwFileAttributes, lpFindFileData.cFileName, searchPattern, getFile, getDirectory, getHidden, getSystem, getReadOnly, getCompressed, getArchive, getReparsePoint, filterMode))
{
string fullName = Path.Combine(path, lpFindFileData.cFileName);
long? thisFileSize = null;
if ((lpFindFileData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory)
{
thisFileSize = (lpFindFileData.nFileSizeHigh * (2 ^ 32) + lpFindFileData.nFileSizeLow);
}
resultList.Add(new FileInformation { Name = lpFindFileData.cFileName, Path = Path.Combine(path, lpFindFileData.cFileName), Parent = path, Attributes = lpFindFileData.dwFileAttributes, FileSize = thisFileSize, CreationTime = lpFindFileData.ftCreationTime.ToDateTime(), LastAccessTime = lpFindFileData.ftLastAccessTime.ToDateTime(), LastWriteTime = lpFindFileData.ftLastWriteTime.ToDateTime() });
}
}
}
while (Win32Native.FindNextFile(handle, out lpFindFileData));
// close the file handle
handle.Dispose();
// handle recursive search
if (recurse)
{
// handle depth of recursion
if (depth > 0)
{
if (parallel)
{
subDirectoryList.AsParallel().ForAll(x =>
{
List<FileInformation> resultSubDirectory = new List<FileInformation>();
resultSubDirectory = FastFind(x.Path, searchPattern, getFile, getDirectory, recurse, (depth - 1), false, suppressErrors, largeFetch, getHidden, getSystem, getReadOnly, getCompressed, getArchive, getReparsePoint, filterMode);
lock (resultListLock)
{
resultList.AddRange(resultSubDirectory);
}
});
}
else
{
foreach (FileInformation directory in subDirectoryList)
{
foreach (FileInformation result in FastFind(directory.Path, searchPattern, getFile, getDirectory, recurse, (depth - 1), false, suppressErrors, largeFetch, getHidden, getSystem, getReadOnly, getCompressed, getArchive, getReparsePoint, filterMode))
{
resultList.Add(result);
}
}
}
}
// if no depth are specified
else if (depth == null)
{
if (parallel)
{
subDirectoryList.AsParallel().ForAll(x =>
{
List<FileInformation> resultSubDirectory = new List<FileInformation>();
resultSubDirectory = FastFind(x.Path, searchPattern, getFile, getDirectory, recurse, null, false, suppressErrors, largeFetch, getHidden, getSystem, getReadOnly, getCompressed, getArchive, getReparsePoint, filterMode);
lock (resultListLock)
{
resultList.AddRange(resultSubDirectory);
}
});
}
else
{
foreach (FileInformation directory in subDirectoryList)
{
foreach (FileInformation result in FastFind(directory.Path, searchPattern, getFile, getDirectory, recurse, null, false, suppressErrors, largeFetch, getHidden, getSystem, getReadOnly, getCompressed, getArchive, getReparsePoint, filterMode))
{
resultList.Add(result);
}
}
}
}
}
}
// error handling
else if (handle.IsInvalid && !suppressErrors)
{
int hr = Marshal.GetLastWin32Error();
if (hr != 2 && hr != 0x12)
{
//throw new Win32Exception(hr);
Console.WriteLine("{0}: {1}", path, (new Win32Exception(hr)).Message);
}
}
return resultList;
}
private static bool MatchesFilter(FileAttributes fileAttributes, string name, string searchPattern, bool aFile, bool aDirectory, bool aHidden, bool aSystem, bool aReadOnly, bool aCompressed, bool aArchive, bool aReparsePoint, string filterMode)
{
// first make sure that the name matches the search pattern
if (Win32Native.PathMatchSpec(name, searchPattern))
{
// then we build our filter attributes enumeration
FileAttributes filterAttributes = new FileAttributes();
if (aDirectory)
{
filterAttributes |= FileAttributes.Directory;
}
if (aHidden)
{
filterAttributes |= FileAttributes.Hidden;
}
if (aSystem)
{
filterAttributes |= FileAttributes.System;
}
if (aReadOnly)
{
filterAttributes |= FileAttributes.ReadOnly;
}
if (aCompressed)
{
filterAttributes |= FileAttributes.Compressed;
}
if (aReparsePoint)
{
filterAttributes |= FileAttributes.ReparsePoint;
}
if (aArchive)
{
filterAttributes |= FileAttributes.Archive;
}
// based on the filtermode, we match the file with our filter attributes a bit differently
switch (filterMode)
{
case "Include":
if ((fileAttributes & filterAttributes) == filterAttributes)
{
return true;
}
else
{
return false;
}
case "Exclude":
if ((fileAttributes & filterAttributes) != filterAttributes)
{
return true;
}
else
{
return false;
}
case "Strict":
if (fileAttributes == filterAttributes)
{
return true;
}
else
{
return false;
}
}
return false;
}
else
{
return false;
}
}
[Serializable]
public class FileInformation
{
public string Name;
public string Path;
public string Parent;
public FileAttributes Attributes;
public long? FileSize;
public DateTime CreationTime;
public DateTime LastAccessTime;
public DateTime LastWriteTime;
}
[Serializable]
public class SecurityInformation
{
public string Path;
public string Owner;
public string Access;
}
}
}