Skip to content

Commit 1990eb0

Browse files
committed
Added tracing to Dokany
1 parent e34ab8c commit 1990eb0

File tree

5 files changed

+170
-109
lines changed

5 files changed

+170
-109
lines changed

SecureFolderFS.Core.Dokany/Callbacks/BaseDokanyCallbacks.cs

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using SecureFolderFS.Core.FileSystem.Statistics;
88
using System;
99
using System.Collections.Generic;
10+
using System.Diagnostics;
1011
using System.IO;
12+
using System.Linq;
1113
using System.Runtime.CompilerServices;
1214
using System.Security.AccessControl;
1315
using System.Security.Cryptography;
@@ -61,12 +63,12 @@ public virtual void CloseFile(string fileName, IDokanFileInfo info)
6163
public virtual NtStatus FlushFileBuffers(string fileName, IDokanFileInfo info)
6264
{
6365
if (handlesManager.GetHandle<FileHandle>(GetContextValue(info)) is not { } fileHandle)
64-
return DokanResult.InvalidHandle;
66+
return Trace(DokanResult.InvalidHandle, nameof(FlushFileBuffers), fileName, info);
6567

6668
try
6769
{
6870
fileHandle.Stream.Flush();
69-
return DokanResult.Success;
71+
return Trace(DokanResult.Success, nameof(FlushFileBuffers), fileName, info);
7072
}
7173
catch (IOException)
7274
{
@@ -84,10 +86,10 @@ public virtual NtStatus FindFiles(string fileName, out IList<FileInformation> fi
8486
public virtual NtStatus SetEndOfFile(string fileName, long length, IDokanFileInfo info)
8587
{
8688
if (handlesManager.GetHandle<FileHandle>(GetContextValue(info)) is not { } fileHandle)
87-
return DokanResult.InvalidHandle;
89+
return Trace(DokanResult.InvalidHandle, nameof(SetEndOfFile), fileName, info);
8890

8991
fileHandle.Stream.SetLength(length);
90-
return DokanResult.Success;
92+
return Trace(DokanResult.Success, nameof(SetEndOfFile), fileName, info);
9193
}
9294

9395
/// <inheritdoc/>
@@ -105,27 +107,27 @@ public virtual NtStatus GetVolumeInformation(out string volumeLabel, out FileSys
105107
maximumComponentLength = volumeModel.MaximumComponentLength;
106108
features = volumeModel.FileSystemFeatures;
107109

108-
return DokanResult.Success;
110+
return Trace(DokanResult.Success, nameof(GetVolumeInformation), null, info);
109111
}
110112

111113
/// <inheritdoc/>
112114
public virtual NtStatus Mounted(string mountPoint, IDokanFileInfo info)
113115
{
114116
_ = mountPoint; // TODO: Check if mountPoint is different and update the RootFolder (?)
115-
return DokanResult.Success;
117+
return Trace(DokanResult.Success, nameof(Mounted), null, info);
116118
}
117119

118120
/// <inheritdoc/>
119121
public virtual NtStatus Unmounted(IDokanFileInfo info)
120122
{
121-
return DokanResult.Success;
123+
return Trace(DokanResult.Success, nameof(Unmounted), null, info);
122124
}
123125

124126
/// <inheritdoc/>
125127
public virtual NtStatus FindStreams(string fileName, out IList<FileInformation> streams, IDokanFileInfo info)
126128
{
127129
streams = Array.Empty<FileInformation>();
128-
return DokanResult.NotImplemented;
130+
return Trace(DokanResult.NotImplemented, nameof(FindStreams), fileName, info);
129131
}
130132

131133
/// <inheritdoc/>
@@ -141,7 +143,7 @@ public virtual unsafe NtStatus ReadFile(string fileName, IntPtr buffer, uint buf
141143
if (ciphertextPath is null)
142144
{
143145
bytesRead = 0;
144-
return DokanResult.PathNotFound;
146+
return Trace(DokanResult.PathNotFound, nameof(ReadFile), fileName, info);
145147
}
146148

147149
// Memory-mapped
@@ -159,7 +161,7 @@ public virtual unsafe NtStatus ReadFile(string fileName, IntPtr buffer, uint buf
159161
if (offset >= fileHandle.Stream.Length)
160162
{
161163
bytesRead = 0;
162-
return FileSystem.Constants.FILE_EOF;
164+
return NtStatus.EndOfFile;
163165
}
164166
else
165167
fileHandle.Stream.Position = offset;
@@ -168,22 +170,22 @@ public virtual unsafe NtStatus ReadFile(string fileName, IntPtr buffer, uint buf
168170
var bufferSpan = new Span<byte>(buffer.ToPointer(), (int)bufferLength);
169171
bytesRead = fileHandle.Stream.Read(bufferSpan);
170172

171-
return DokanResult.Success;
173+
return Trace(DokanResult.Success, nameof(ReadFile), fileName, info);
172174
}
173175
catch (PathTooLongException)
174176
{
175177
bytesRead = 0;
176-
return DokanResult.InvalidName;
178+
return Trace(DokanResult.InvalidName, nameof(ReadFile), fileName, info);
177179
}
178180
catch (CryptographicException)
179181
{
180182
bytesRead = 0;
181-
return NtStatus.CrcError;
183+
return Trace(NtStatus.CrcError, nameof(ReadFile), fileName, info);
182184
}
183185
catch (UnavailableStreamException)
184186
{
185187
bytesRead = 0;
186-
return NtStatus.HandleNoLongerValid;
188+
return Trace(NtStatus.HandleNoLongerValid, nameof(ReadFile), fileName, info);
187189
}
188190
finally
189191
{
@@ -205,7 +207,7 @@ public virtual unsafe NtStatus WriteFile(string fileName, IntPtr buffer, uint bu
205207
if (ciphertextPath is null)
206208
{
207209
bytesWritten = 0;
208-
return DokanResult.PathNotFound;
210+
return Trace(DokanResult.PathNotFound, nameof(WriteFile), fileName, info);
209211
}
210212

211213
// Memory-mapped
@@ -233,29 +235,29 @@ public virtual unsafe NtStatus WriteFile(string fileName, IntPtr buffer, uint bu
233235
fileHandle.Stream.Write(bufferSpan);
234236
bytesWritten = alignedBytesToCopy;
235237

236-
return DokanResult.Success;
238+
return Trace(DokanResult.Success, nameof(WriteFile), fileName, info);
237239
}
238240
catch (PathTooLongException)
239241
{
240242
bytesWritten = 0;
241-
return DokanResult.InvalidName;
243+
return Trace(DokanResult.InvalidName, nameof(WriteFile), fileName, info);
242244
}
243245
catch (CryptographicException)
244246
{
245247
bytesWritten = 0;
246-
return NtStatus.CrcError;
248+
return Trace(NtStatus.CrcError, nameof(WriteFile), fileName, info);
247249
}
248250
catch (UnavailableStreamException)
249251
{
250252
bytesWritten = 0;
251-
return NtStatus.HandleNoLongerValid;
253+
return Trace(NtStatus.HandleNoLongerValid, nameof(WriteFile), fileName, info);
252254
}
253255
catch (IOException ioEx)
254256
{
255257
if (ErrorHandlingHelpers.NtStatusFromException(ioEx, out var ntStatus))
256258
{
257259
bytesWritten = 0;
258-
return (NtStatus)ntStatus;
260+
return Trace((NtStatus)ntStatus, nameof(WriteFile), fileName, info);
259261
}
260262

261263
throw;
@@ -351,5 +353,51 @@ protected static int AlignSizeForPagingIo(int bufferLength, long offset, long st
351353

352354
return bufferLength;
353355
}
356+
357+
protected static NtStatus Trace(NtStatus result, string methodName, string fileName, IDokanFileInfo info,
358+
FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes)
359+
{
360+
#if !DEBUG
361+
return result;
362+
#endif
363+
364+
if (!Core.FileSystem.Constants.OPT_IN_FOR_OPTIONAL_DEBUG_TRACING)
365+
return result;
366+
367+
if (DisallowedTraceMethods.Contains(methodName))
368+
return result;
369+
370+
var message = FormatProviders.DokanFormat($"{methodName}('{fileName}', {info}, [{access}], [{share}], [{mode}], [{options}], [{attributes}]) -> {result}");
371+
Debug.WriteLine(message);
372+
373+
return result;
374+
}
375+
376+
protected static NtStatus Trace(NtStatus result, string methodName, string? fileName, IDokanFileInfo info, params object[]? args)
377+
{
378+
#if !DEBUG
379+
return result;
380+
#endif
381+
382+
if (!Core.FileSystem.Constants.OPT_IN_FOR_OPTIONAL_DEBUG_TRACING)
383+
return result;
384+
385+
if (DisallowedTraceMethods.Contains(methodName))
386+
return result;
387+
388+
var extraParameters = args is not null && args.Length > 0
389+
? ", " + string.Join(", ", args.Select(x => string.Format(FormatProviders.DefaultFormatProvider, "{0}", x)))
390+
: string.Empty;
391+
392+
var message = FormatProviders.DokanFormat($"{methodName}('{fileName}', {info}{extraParameters}) -> {result}");
393+
Debug.WriteLine(message);
394+
395+
return result;
396+
}
397+
398+
private static string[] DisallowedTraceMethods = new[]
399+
{
400+
"GetVolumeInformation"
401+
};
354402
}
355403
}

0 commit comments

Comments
 (0)