-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharpDirLister.cs
285 lines (243 loc) · 10.3 KB
/
SharpDirLister.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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.IO.Compression;
using System.Collections.Generic;
using static SharpDirLister.Interop;
namespace SharpDirLister
{
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 class FileInformation
{
public string FullPath;
public DateTime LastWriteTime;
public long Size;
public static string type = "F";
public override string ToString()
{
return string.Format("{0} | {1} | {2} | {3}", FullPath, LastWriteTime, Size, type);
}
}
public class DirectoryInformation
{
public string FullPath;
public DateTime LastWriteTime;
public static string type = "D";
public override string ToString()
{
return string.Format("{0} | {1} | {2}", FullPath, LastWriteTime, type);
}
}
class List
{
static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
//Code based heavily on https://stackoverflow.com/q/47471744
static bool FindNextFilePInvokeRecursive(string path, out List<FileInformation> files, out List<DirectoryInformation> directories, Log.Logger logger)
{
List<FileInformation> fileList = new List<FileInformation>();
List<DirectoryInformation> directoryList = new List<DirectoryInformation>();
IntPtr findHandle = INVALID_HANDLE_VALUE;
List<Tuple<string, DateTime>> info = new List<Tuple<string, DateTime>>();
try
{
findHandle = FindFirstFileW(path + @"\*", out WIN32_FIND_DATAW findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
do
{
if (findData.cFileName != "." && findData.cFileName != "..")
{
string fullPath = path + @"\" + findData.cFileName;
if (findData.dwFileAttributes.HasFlag(FileAttributes.Directory) && !findData.dwFileAttributes.HasFlag(FileAttributes.ReparsePoint))
{
var dirdata = new DirectoryInformation { FullPath = fullPath, LastWriteTime = findData.ftLastWriteTime.ToDateTime() };
directoryList.Add(dirdata);
List<FileInformation> subDirectoryFileList = new List<FileInformation>();
List<DirectoryInformation> subDirectoryDirectoryList = new List<DirectoryInformation>();
if (FindNextFilePInvokeRecursive(fullPath, out subDirectoryFileList, out subDirectoryDirectoryList, logger))
{
fileList.AddRange(subDirectoryFileList);
directoryList.AddRange(subDirectoryDirectoryList);
}
}
else if (!findData.dwFileAttributes.HasFlag(FileAttributes.Directory))
{
var filedata = new FileInformation { FullPath = fullPath, LastWriteTime = findData.ftLastWriteTime.ToDateTime(), Size = (long)findData.nFileSizeLow + (long)findData.nFileSizeHigh * 4294967296 };
fileList.Add(filedata);
}
}
} while (FindNextFile(findHandle, out findData));
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
if (findHandle != INVALID_HANDLE_VALUE)
{
FindClose(findHandle);
}
files = null;
directories = null;
return false;
}
if (findHandle != INVALID_HANDLE_VALUE)
{
FindClose(findHandle);
}
files = fileList;
directories = directoryList;
return true;
}
static bool FindNextFilePInvokeRecursiveParalleled(string path, out List<FileInformation> files, out List<DirectoryInformation> directories, Log.Logger logger)
{
List<FileInformation> fileList = new List<FileInformation>();
object fileListLock = new object();
List<DirectoryInformation> directoryList = new List<DirectoryInformation>();
object directoryListLock = new object();
IntPtr findHandle = INVALID_HANDLE_VALUE;
List<Tuple<string, DateTime>> info = new List<Tuple<string, DateTime>>();
try
{
path = path.EndsWith(@"\") ? path : path + @"\";
findHandle = FindFirstFileW(path + @"*", out WIN32_FIND_DATAW findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
do
{
if (findData.cFileName != "." && findData.cFileName != "..")
{
string fullPath = path + findData.cFileName;
if (findData.dwFileAttributes.HasFlag(FileAttributes.Directory) && !findData.dwFileAttributes.HasFlag(FileAttributes.ReparsePoint))
{
var dirdata = new DirectoryInformation { FullPath = fullPath, LastWriteTime = findData.ftLastWriteTime.ToDateTime() };
directoryList.Add(dirdata);
}
else if (!findData.dwFileAttributes.HasFlag(FileAttributes.Directory))
{
var filedata = new FileInformation { FullPath = fullPath, LastWriteTime = findData.ftLastWriteTime.ToDateTime() };
fileList.Add(filedata);
}
}
} while (FindNextFile(findHandle, out findData));
directoryList.AsParallel().ForAll(x =>
{
List<FileInformation> subDirectoryFileList = new List<FileInformation>();
List<DirectoryInformation> subDirectoryDirectoryList = new List<DirectoryInformation>();
if (FindNextFilePInvokeRecursive(x.FullPath, out subDirectoryFileList, out subDirectoryDirectoryList, logger))
{
lock (fileListLock)
{
fileList.AddRange(subDirectoryFileList);
}
lock (directoryListLock)
{
directoryList.AddRange(subDirectoryDirectoryList);
}
}
});
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
if (findHandle != INVALID_HANDLE_VALUE)
{
FindClose(findHandle);
}
files = null;
directories = null;
return false;
}
if (findHandle != INVALID_HANDLE_VALUE)
{
FindClose(findHandle);
}
files = fileList;
directories = directoryList;
return true;
}
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile, CompressionMode.Compress))
{
Console.WriteLine("Compressing to {0}", destinationFile.Name);
output.Write(buffer, 0, buffer.Length);
}
sourceFile.Close();
destinationFile.Close();
}
public static void Usage()
{
Console.WriteLine("Usage: SharpDirLister.exe target outputfolder\n" +
"Examples:\n" +
"SharpDirLister.exe c:\\path\\to\\directory c:\\outputfolder\n" +
"SharpDirLister.exe \\\\path\\to\\share c:\\outputfolder\n" +
"Will output two files with the format MMddTHHmmss_listing.{.txt,.txt.gz} in the output folder.\n"+
"Remember to clean up after yourself.\n");
Environment.Exit(1); //Be careful if running without fork & run
}
static void Main(string[] args)
{
try
{
string filename = DateTime.Now.ToString("MMddTHHmmss") + "_listing.txt";
List<FileInformation> files1 = new List<FileInformation>();
List<DirectoryInformation> directories1 = new List<DirectoryInformation>();
if (args.Length != 2)
{
Usage();
}
else
{
if (!Directory.Exists(args[0])) {
Console.WriteLine("Error: Directory \"{0}\" not found.\n", args[0]);
Usage();
}
if (!Directory.Exists(args[1]))
{
Console.WriteLine("Error: Output directory \"{0}\" not found.\n", args[1]);
Usage();
}
Log.Logger logger = new Log.Logger(Path.Combine(args[1], filename));
while (!FindNextFilePInvokeRecursiveParalleled(args[0], out files1, out directories1, logger))
{
Thread.Sleep(1000);
}
files1.Sort((a, b) => string.Compare(a.FullPath, b.FullPath));
directories1.Sort((a, b) => string.Compare(a.FullPath, b.FullPath));
foreach (var filedata in files1)
{
logger.WriteLine(filedata.ToString());
}
foreach (var filedata in directories1)
{
logger.WriteLine(filedata.ToString());
}
logger.Close();
CompressFile(args[1] + "\\" + filename);
Console.WriteLine("Done!");
}
}
catch (Exception exception)
{
//TODO: If I crash I will not output the listing
Console.WriteLine(exception.Message);
Console.WriteLine(exception.StackTrace);
}
}
}
}