-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAufsImpl.cs
768 lines (681 loc) · 31.2 KB
/
AufsImpl.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/* Copyright 2013 Corey Bonnell
This file is part of Aufs4Win.
Aufs4Win is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Aufs4Win is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Aufs4Win. If not, see <http://www.gnu.org/licenses/>.
*/
using Dokan;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace Cbonnell.Aufs4Win
{
internal class AufsImpl : DokanOperations
{
private readonly MergedVolumeInfo volumeInfo;
private readonly Dictionary<ulong, FileStream> openHandles = new Dictionary<ulong, FileStream>();
private readonly ReadOnlyCollection<Member> members;
private readonly CreationPolicy policy;
private readonly object contextWriteLock = new object();
private ulong context = 0;
private delegate void FileStreamOperationDelegate(FileStream stream);
private delegate void GenericFileSystemOperationDelegate();
private delegate void GenericPathInformationOperationDelegate(PathInformation pathInfo);
internal AufsImpl(MergedVolumeInfo volumeInfo)
{
if (volumeInfo == null)
{
throw new ArgumentNullException("volumeInfo");
}
this.volumeInfo = volumeInfo;
this.members = volumeInfo.Members;
this.policy = volumeInfo.Policy;
}
public int CreateFile(string path, FileAccess access, FileShare sharing, FileMode mode, FileOptions options, DokanFileInfo info)
{
Trace.TraceInformation("CreateFile called for file " + path);
lock (this.contextWriteLock)
{
info.Context = this.context++;
}
Trace.TraceInformation("Context ID for handle is: " + info.Context);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo != null && pathInfo.ObjectKind == FileSystemObjectKind.Directory)
{
Trace.TraceInformation("It's a directory at " + pathInfo.RealPath);
info.IsDirectory = true;
return 0;
}
info.IsDirectory = false;
if (pathInfo == null && mode != FileMode.Create && mode != FileMode.CreateNew && mode != FileMode.OpenOrCreate)
{
Trace.TraceInformation(String.Format("File system object not found and file mode did not specify creation of new file (file mode was {0}), returning error", mode));
return -DokanNet.ERROR_FILE_NOT_FOUND;
}
if (pathInfo != null && pathInfo.ObjectKind == FileSystemObjectKind.File && pathInfo.ContainingMember.ReadOnly && ((access & FileAccess.Write) != 0))
{
Trace.TraceInformation(String.Format("Write access (access mode was {0}) was specified for a file ({1}) on read-only member at {2}, returning error", access, pathInfo.RealPath, pathInfo.ContainingMember.MemberPath));
return -DokanNet.ERROR_ACCESS_DENIED;
}
if (pathInfo == null)
{
pathInfo = this.policy.GetPathInformationForNewFile(path);
if (pathInfo != null && pathInfo.ContainingMember.ReadOnly)
{
Trace.TraceError(String.Format("Creation policy specified the creation of file {0} on read-only member at {1}", pathInfo.RealPath, pathInfo.ContainingMember.MemberPath));
return -DokanNet.ERROR_ACCESS_DENIED;
}
if (pathInfo == null)
{
Trace.TraceInformation(String.Format("No suitable member for new file \"{0}\" found", path));
return -DokanNet.ERROR_ACCESS_DENIED;
}
}
SafeFileHandle handle = NativeMethods.CreateFile(pathInfo.RealPath, access, sharing, IntPtr.Zero, mode, (uint)options, IntPtr.Zero);
if (handle.IsInvalid)
{
int errorCode = AufsImpl.getLastError();
Trace.TraceInformation(String.Format("CreateFile on file {0} failed with error code of {1}", pathInfo.RealPath, errorCode));
return errorCode;
}
FileStream stream;
try
{
stream = new FileStream(handle, access);
}
catch (ArgumentException e)
{
Trace.TraceWarning(String.Format("Could not instantiate FileStream object for {0} because: {1} (managed exception; no Win32 error code)", pathInfo.RealPath, e.Message));
return -2;
}
catch (Exception e)
{
int errorCode = AufsImpl.getLastError();
Trace.TraceWarning(String.Format("Could not instantiate FileStream object for {0} because: {1} (error code {2})", pathInfo.RealPath, e.Message, errorCode));
return errorCode;
}
lock (this.openHandles)
{
this.openHandles.Add((ulong)info.Context, stream);
}
Trace.TraceInformation("CreateFile succeeded");
return 0; // there's the right way, and then there's the wrong way... the right way busts Notepad, so we're gonna use the wrong way. The commented line below is supposedly the right way
// return (alreadyExists && (mode == FileMode.Create || mode == FileMode.OpenOrCreate)) ? -DokanNet.ERROR_ALREADY_EXISTS : 0;
}
public int OpenDirectory(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering OpenDirectory for path " + path);
lock (this.contextWriteLock)
{
info.Context = this.context++;
}
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo == null || pathInfo.ObjectKind != FileSystemObjectKind.Directory)
{
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
return 0;
}
public int CreateDirectory(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering CreateDirectory for path " + path);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo != null)
{
Trace.TraceInformation(String.Format("Could not create the directory at {0} because it already exists", path));
return -DokanNet.ERROR_ALREADY_EXISTS;
}
PathInformation newPathInfo = this.policy.GetPathInformationForNewDirectory(path);
if (newPathInfo == null)
{
return -DokanNet.ERROR_ACCESS_DENIED;
}
return this.executeGenericFileSystemOperation(newPathInfo, true, delegate()
{
Directory.CreateDirectory(newPathInfo.RealPath);
});
}
public int GetDiskFreeSpace(ref ulong freeBytesAvailable, ref ulong totalBytes, ref ulong totalFreeBytes, DokanFileInfo info)
{
Trace.TraceInformation("Entering GetDiskFreeSpace");
freeBytesAvailable = 0;
totalBytes = 0;
totalFreeBytes = 0;
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
{
foreach(Member m in this.members)
{
if(driveInfo.Name.StartsWith(m.MemberDriveLetter.ToString()))
{
freeBytesAvailable += m.ReadOnly ? 0 : (ulong)driveInfo.AvailableFreeSpace;
totalBytes += (ulong)driveInfo.TotalSize;
totalFreeBytes += (ulong)driveInfo.TotalFreeSpace;
continue;
}
}
}
return 0;
}
public int ReadFile(string path, byte[] buffer, ref uint readBytes, long offset, DokanFileInfo info)
{
Trace.TraceInformation("Entering ReadFile for context ID " + info.Context);
readBytes = 0;
uint readBytesTemp = 0;
int result = this.executeFileStreamOperation(info, delegate(FileStream stream)
{
if (stream.Position != offset)
{
stream.Seek(offset, SeekOrigin.Begin);
}
readBytesTemp = (uint)stream.Read(buffer, 0, buffer.Length);
});
if (result == 0)
{
readBytes = readBytesTemp;
}
return result;
}
public int WriteFile(string path, byte[] buffer, ref uint writtenBytes, long offset, DokanFileInfo info)
{
Trace.TraceInformation("Entering WriteFile for context ID " + info.Context);
writtenBytes = 0;
uint writtenBytesTemp = 0;
int result = this.executeFileStreamOperation(info, delegate(FileStream stream)
{
if (stream.Position != offset)
{
stream.Seek(offset, SeekOrigin.Begin);
}
stream.Write(buffer, 0, buffer.Length);
writtenBytesTemp = (uint)buffer.Length;
});
if (result == 0)
{
writtenBytes = writtenBytesTemp;
}
return result;
}
public int FlushFileBuffers(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering FlushFileBuffers for context ID " + info.Context);
return this.executeFileStreamOperation(info, delegate(FileStream stream)
{
stream.Flush();
});
}
public int GetFileInformation(string path, Dokan.FileInformation realFileInfo, Dokan.DokanFileInfo info)
{
Trace.TraceInformation("Entering GetFileInformation for path " + path);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo == null)
{
Trace.TraceInformation(String.Format("The path {0} does not exist, cannot get file information", path));
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
GenericPathInformationOperationDelegate operation = delegate(PathInformation pInfo)
{
FileSystemInfo objectInfo = AufsImpl.getFileSystemInfoForPathInfo(pInfo);
if (pInfo.ObjectKind == FileSystemObjectKind.File)
{
realFileInfo.Length = (objectInfo as FileInfo).Length;
}
else
{
realFileInfo.Length = 0;
}
realFileInfo.FileName = objectInfo.Name;
realFileInfo.Attributes = objectInfo.Attributes;
realFileInfo.CreationTime = objectInfo.CreationTime;
realFileInfo.LastAccessTime = objectInfo.LastAccessTime;
realFileInfo.LastWriteTime = objectInfo.LastWriteTime;
};
return this.executeGenericPathInformationOperation(pathInfo, false, operation);
}
public int FindFiles(string path, ArrayList fileInfoArrList, DokanFileInfo info)
{
Trace.TraceInformation("Entering FindFiles for path " + path);
if (!this.mergeDirectoryListing(path, fileInfoArrList))
{
Trace.TraceInformation(String.Format("The path {0} does not exist, cannot enumerate directory objects", path));
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
return 0;
}
public int SetFileAttributes(string path, FileAttributes attributes, DokanFileInfo info)
{
Trace.TraceInformation("Entering SetFileAttributes for path " + path);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo == null)
{
Trace.TraceInformation(String.Format("The path {0} does not exist, cannot change file time", path));
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
GenericPathInformationOperationDelegate operation = delegate(PathInformation pInfo)
{
FileSystemInfo objectInfo = AufsImpl.getFileSystemInfoForPathInfo(pInfo);
objectInfo.Attributes = attributes;
};
if (pathInfo.ObjectKind == FileSystemObjectKind.File)
{
return this.executeGenericPathInformationOperation(pathInfo, true, operation);
}
PathInformation[] dirsToModify = this.getAllDirectoriesAtPath(path);
return this.executeGenericMultipleFileObjectOperation(dirsToModify, true, operation);
}
public int SetFileTime(string path, DateTime creationTime, DateTime accessTime, DateTime modifiedTime, DokanFileInfo info)
{
Trace.TraceInformation("Entering SetFileTime for path " + path);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo == null)
{
Trace.TraceInformation(String.Format("The path {0} does not exist, cannot change file time", path));
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
GenericPathInformationOperationDelegate operation = delegate(PathInformation pInfo)
{
FileSystemInfo objectInfo = AufsImpl.getFileSystemInfoForPathInfo(pInfo);
objectInfo.CreationTime = creationTime;
objectInfo.LastAccessTime = accessTime;
objectInfo.LastWriteTime = modifiedTime;
};
if (pathInfo.ObjectKind == FileSystemObjectKind.File)
{
return this.executeGenericPathInformationOperation(pathInfo, true, operation);
}
PathInformation[] dirsToModify = this.getAllDirectoriesAtPath(path);
return this.executeGenericMultipleFileObjectOperation(dirsToModify, true, operation);
}
public int DeleteFile(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering DeleteFile for path " + path);
PathInformation pathInfo = this.getPathInformation(path);
if (pathInfo == null || pathInfo.ObjectKind != FileSystemObjectKind.File)
{
Trace.TraceInformation(String.Format("The path {0} is not a file, cannot delete", path));
return -DokanNet.ERROR_FILE_NOT_FOUND;
}
return this.executeGenericFileSystemOperation(pathInfo, true, delegate()
{
File.Delete(pathInfo.RealPath);
});
}
public int DeleteDirectory(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering DeleteDirectory for path " + path);
PathInformation[] dirsToDelete = this.getAllDirectoriesAtPath(path);
if (dirsToDelete.Length == 0)
{
Trace.TraceInformation(String.Format("The path {0} is not a directory, cannot delete", path));
return -DokanNet.ERROR_PATH_NOT_FOUND;
}
return this.executeGenericMultipleFileObjectOperation(dirsToDelete, true, delegate(PathInformation pathInfo)
{
Directory.Delete(pathInfo.RealPath);
});
}
public int SetEndOfFile(string path, long length, DokanFileInfo info)
{
Trace.TraceInformation("Entering SetEndOfFile for context ID " + info.Context);
return this.executeFileStreamOperation(info, delegate(FileStream stream)
{
stream.SetLength(length);
});
}
public int LockFile(string file, long offset, long length, DokanFileInfo info)
{
Trace.TraceInformation("Entering LockFile for context ID " + info.Context);
return this.executeFileStreamOperation(info, delegate(FileStream stream)
{
stream.Lock(offset, length);
});
}
public int UnlockFile(string file, long offset, long length, DokanFileInfo info)
{
Trace.TraceInformation("Entering UnlockFile for context ID " + info.Context);
return this.executeFileStreamOperation(info, delegate(FileStream stream)
{
stream.Unlock(offset, length);
});
}
public int MoveFile(string oldPath, string newPath, bool replace, DokanFileInfo info)
{
Trace.TraceInformation("EnteringMoveFile for path " + oldPath);
PathInformation oldPathInfo = this.getPathInformation(oldPath);
if (oldPathInfo == null)
{
Trace.TraceInformation(String.Format("The file {0} does not exist, could not move", oldPath));
return -DokanNet.ERROR_FILE_NOT_FOUND;
}
PathInformation newPathInfo = this.getPathInformation(newPath);
if (!replace && newPathInfo != null)
{
Trace.TraceInformation(String.Format("The file {0} exists and the replace file flag was off, could not move", newPath));
return -DokanNet.ERROR_FILE_EXISTS;
}
Func<PathInformation, int> directoryCreator = delegate(PathInformation pInfo)
{
if (pInfo.ContainingMember.ReadOnly)
{
Trace.TraceInformation(String.Format("The path \"{0}\" is contained on a read-only member, cannot move", pInfo.RealPath));
return -DokanNet.ERROR_ACCESS_DENIED;
}
string destinationDirectory = Path.GetDirectoryName(pInfo.ContainingMember.GetRootedPath(newPath));
int error = this.executeGenericFileSystemOperation(pInfo, true, delegate()
{
if (!Directory.Exists(destinationDirectory))
{
Trace.TraceInformation(String.Format("The destination directory {0} does not exist, creating directory", destinationDirectory));
Directory.CreateDirectory(destinationDirectory);
}
});
bool success = NativeMethods.MoveFileEx(pInfo.RealPath, pInfo.ContainingMember.GetRootedPath(newPath), replace ? NativeMethods.MOVEFILE_REPLACE_EXISTING : 0);
return success ? 0 : AufsImpl.getLastError();
};
if(oldPathInfo.ObjectKind == FileSystemObjectKind.File)
{
int error = directoryCreator.Invoke(oldPathInfo);
if(error != 0)
{
Trace.TraceInformation(String.Format("MoveFile failed, old path: {0}, new path: {1}, error: {2}", oldPath, newPath, error));
}
return error;
}
// if the code is executing here, then we are moving a directory
PathInformation[] dirsToMove = this.getAllDirectoriesAtPath(oldPath);
foreach(PathInformation pInfo in dirsToMove)
{
int error = directoryCreator.Invoke(pInfo);
if(error != 0)
{
Trace.TraceInformation(String.Format("MoveFile failed, old path: {0}, new path: {1}, error: {2}", oldPath, newPath, error));
return error;
}
}
return 0;
}
public int CloseFile(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering CloseFile for context ID " + info.Context);
FileStream str = null;
bool foundStream = false;
lock (this.openHandles)
{
if (info.Context != null && this.openHandles.TryGetValue((ulong)info.Context, out str))
{
foundStream = true;
this.openHandles.Remove((ulong)info.Context);
}
}
Trace.TraceInformation("Found handle: " + foundStream);
try
{
if (foundStream)
{
str.Close();
}
}
catch (ArgumentException e)
{
Trace.TraceWarning(String.Format("Could not close stream because: {0} (managed exception; no Win32 error code)", e.Message));
return -2;
}
catch (Exception e)
{
int errorCode = AufsImpl.getLastError();
Trace.TraceWarning(String.Format("Could not close stream because: {0} (error code {1})", e.Message, errorCode));
return errorCode;
}
return 0;
}
public int Cleanup(string path, DokanFileInfo info)
{
Trace.TraceInformation("Entering Cleanup for context ID " + info.Context);
return 0;
}
public int Unmount(DokanFileInfo info)
{
Trace.TraceInformation("Unmounting drive");
lock (this.openHandles)
{
if (this.openHandles.Count == 0)
{
Trace.TraceInformation("No open handles, drive unmounted successfully");
return 0;
}
}
Trace.TraceError("One or more open handles, could not unmount drive");
return -1;
}
public int SetAllocationSize(string path, long allocationSize, DokanFileInfo info)
{
Trace.TraceInformation(String.Format("SetAllocationSize of {0} for context ID {1} called, ignoring and returning success", allocationSize, info.Context));
return 0;
}
private int executeGenericMultipleFileObjectOperation(IEnumerable<PathInformation> pathInfos, bool mutableOperation, GenericPathInformationOperationDelegate operation)
{
if (pathInfos == null)
{
throw new ArgumentNullException("pathInfos");
}
if (operation == null)
{
throw new ArgumentNullException("operation");
}
foreach (PathInformation pathInfo in pathInfos)
{
int result = this.executeGenericPathInformationOperation(pathInfo, mutableOperation, operation);
if (result != 0)
{
return result;
}
}
return 0;
}
private int executeGenericPathInformationOperation(PathInformation pathInfo, bool mutableOperation, GenericPathInformationOperationDelegate operation)
{
if (pathInfo == null)
{
throw new ArgumentNullException("pathInfo");
}
if (operation == null)
{
throw new ArgumentNullException("operation");
}
Trace.TraceInformation("Executing operation on path " + pathInfo.RealPath);
if (mutableOperation && pathInfo.ContainingMember.ReadOnly)
{
Trace.TraceInformation("Attempted to modify read-only object");
return -DokanNet.ERROR_ACCESS_DENIED;
}
try
{
operation.Invoke(pathInfo);
}
catch (ArgumentException e)
{
Trace.TraceWarning(String.Format("Operation on path {0} failed because: {1} (managed exception; no Win32 error code)", pathInfo.RealPath, e.Message));
return -2;
}
catch (Exception e)
{
int errorCode = AufsImpl.getLastError();
Trace.TraceWarning(String.Format("Operation on path {0} failed because: {1} (error code of {2})", pathInfo.RealPath, e.Message, errorCode));
return errorCode;
}
Trace.TraceInformation("Operation completed successfully");
return 0;
}
private int executeGenericFileSystemOperation(PathInformation pathInfo, bool mutableOperation, GenericFileSystemOperationDelegate operation)
{
return this.executeGenericPathInformationOperation(pathInfo, mutableOperation, delegate(PathInformation pInfo)
{
operation.Invoke();
});
}
private int executeFileStreamOperation(DokanFileInfo info, FileStreamOperationDelegate operation)
{
if (info == null)
{
throw new ArgumentNullException("info");
}
if (operation == null)
{
throw new ArgumentNullException("operation");
}
Trace.TraceInformation("Executing generic stream operation on handle with context ID " + info.Context);
FileStream stream;
lock (this.openHandles)
{
if (!this.openHandles.TryGetValue((ulong)info.Context, out stream))
{
Trace.TraceWarning("Could not find corresponding handle for context ID " + info.Context);
return -DokanNet.ERROR_ACCESS_DENIED;
}
}
try
{
operation.Invoke(stream);
}
catch (ArgumentException e)
{
Trace.TraceWarning(String.Format("Operation on handle for context ID {0} failed because: {1} (managed exception; no Win32 error code)", info.Context, e.Message));
return -2;
}
catch (Exception e)
{
int errorCode = AufsImpl.getLastError();
Trace.TraceWarning(String.Format("Operation on handle for context ID {0} failed because: {1} (error code of {2})", info.Context, e.Message, errorCode));
return errorCode;
}
Trace.TraceInformation("Operation completed successfully");
return 0;
}
private bool mergeDirectoryListing(string path, ArrayList fileArrList)
{
bool found = false;
KeyedCollectionImpl<string, FileInformation> directoryItems = new KeyedCollectionImpl<string, FileInformation>(delegate(FileInformation itemInfo)
{
return itemInfo.FileName;
});
Trace.TraceInformation(String.Format("Creating merged directory listing for " + path));
foreach (Member m in this.members)
{
if(m.GetFileSystemObjectKind(path) != FileSystemObjectKind.Directory)
{
continue;
}
string realPath = m.GetRootedPath(path);
found = true;
foreach (FileSystemInfo fsInfo in new DirectoryInfo(realPath).GetFileSystemInfos())
{
FileInformation fi = new FileInformation();
fi.Attributes = fsInfo.Attributes;
fi.CreationTime = fsInfo.CreationTime;
fi.FileName = fsInfo.Name;
fi.LastAccessTime = fsInfo.LastAccessTime;
fi.LastWriteTime = fsInfo.LastWriteTime;
if (fsInfo is FileInfo)
{
fi.Length = (fsInfo as FileInfo).Length;
}
else
{
fi.Length = 0;
}
if (!directoryItems.Contains(fi.FileName))
{
directoryItems.Add(fi);
Trace.TraceInformation(String.Format("Found object {0}, size {1} bytes", fi.FileName, fi.Length));
}
}
}
foreach (FileInformation fi in directoryItems)
{
fileArrList.Add(fi);
}
return found;
}
private PathInformation[] getAllDirectoriesAtPath(string fakePath)
{
List<PathInformation> dirPathInfos = new List<PathInformation>();
foreach (Member m in this.members)
{
if (m.GetFileSystemObjectKind(fakePath) == FileSystemObjectKind.Directory)
{
dirPathInfos.Add(new PathInformation(m, FileSystemObjectKind.Directory, m.GetRootedPath(fakePath)));
}
}
return dirPathInfos.ToArray();
}
private PathInformation getPathInformation(string fakePath)
{
Trace.TraceInformation(String.Format("Searching for path {0} in members", fakePath));
foreach (Member m in this.members)
{
Trace.TraceInformation(String.Format("Checking existance of object in member at {0}", m.MemberPath));
FileSystemObjectKind objectKind = m.GetFileSystemObjectKind(fakePath);
if (objectKind == FileSystemObjectKind.None)
{
continue;
}
string realPath = m.GetRootedPath(fakePath);
PathInformation pathInfo = new PathInformation(m, objectKind, realPath);
Trace.TraceInformation(String.Format("Object {0} found on member at {1}", pathInfo, pathInfo.ContainingMember.MemberPath));
return pathInfo;
}
Trace.TraceInformation("Could not find object in members");
return null;
}
private static FileSystemInfo getFileSystemInfoForPathInfo(PathInformation pathInfo)
{
if (pathInfo == null)
{
throw new ArgumentNullException("pathInfo");
}
switch (pathInfo.ObjectKind)
{
case FileSystemObjectKind.File:
return new FileInfo(pathInfo.RealPath);
case FileSystemObjectKind.Directory:
return new DirectoryInfo(pathInfo.RealPath);
default:
return null;
}
}
private static int getLastError()
{
return -Marshal.GetLastWin32Error();
}
private class NativeMethods
{
private NativeMethods() { }
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateFile(
string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] uint flags,
IntPtr template);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool MoveFileEx(
string lpExistingFileName,
string lpNewFileName,
int dwFlags);
public const int MOVEFILE_REPLACE_EXISTING = 1;
}
}
}