-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathProjFSNative.cs
More file actions
493 lines (432 loc) · 20.1 KB
/
ProjFSNative.cs
File metadata and controls
493 lines (432 loc) · 20.1 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
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
using System;
using System.Runtime.InteropServices;
namespace Microsoft.Windows.ProjFS
{
/// <summary>
/// Native P/Invoke declarations for ProjectedFSLib.dll.
/// This replaces the C++/CLI mixed-mode ProjectedFSLib.Managed.dll with pure C# P/Invoke.
/// On .NET 7+, uses LibraryImport source generators for AOT compatibility.
/// On .NET Standard 2.0, falls back to traditional DllImport.
/// </summary>
internal static partial class ProjFSNative
{
private const string ProjFSLib = "ProjectedFSLib.dll";
internal const int PlaceholderIdLength = 128;
// ============================
// Core virtualization lifetime
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjStartVirtualizing(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjStartVirtualizing(
#endif
string virtualizationRootPath,
ref PRJ_CALLBACKS callbacks,
IntPtr instanceContext,
ref PRJ_STARTVIRTUALIZING_OPTIONS options,
out SafeProjFsHandle namespaceVirtualizationContext);
// PrjStopVirtualizing takes raw IntPtr (not SafeProjFsHandle) because it is
// called from SafeProjFsHandle.ReleaseHandle(), where the SafeHandle is already
// closed and cannot be marshaled. All other ProjFS APIs use SafeProjFsHandle.
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial void PrjStopVirtualizing(IntPtr namespaceVirtualizationContext);
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern void PrjStopVirtualizing(IntPtr namespaceVirtualizationContext);
#endif
// ============================
// Placeholder management
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjWritePlaceholderInfo(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjWritePlaceholderInfo(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
string destinationFileName,
ref PRJ_PLACEHOLDER_INFO placeholderInfo,
uint length);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjWritePlaceholderInfo2(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjWritePlaceholderInfo2(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
string destinationFileName,
ref PRJ_PLACEHOLDER_INFO placeholderInfo,
uint placeholderInfoSize,
ref PRJ_EXTENDED_INFO extendedInfo);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, EntryPoint = "PrjWritePlaceholderInfo2")]
internal static partial int PrjWritePlaceholderInfo2Raw(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "PrjWritePlaceholderInfo2")]
internal static extern int PrjWritePlaceholderInfo2Raw(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
IntPtr destinationFileName,
IntPtr placeholderInfo,
uint placeholderInfoSize,
IntPtr extendedInfo);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjUpdateFileIfNeeded(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjUpdateFileIfNeeded(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
string destinationFileName,
ref PRJ_PLACEHOLDER_INFO placeholderInfo,
uint length,
uint updateFlags,
out uint failureReason);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjDeleteFile(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjDeleteFile(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
string destinationFileName,
uint updateFlags,
out uint failureReason);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjMarkDirectoryAsPlaceholder(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjMarkDirectoryAsPlaceholder(
#endif
string rootPathName,
string targetPathName,
ref PRJ_PLACEHOLDER_VERSION_INFO versionInfo,
ref Guid virtualizationInstanceID);
// Overload for MarkDirectoryAsVirtualizationRoot (versionInfo = null)
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16, EntryPoint = "PrjMarkDirectoryAsPlaceholder")]
internal static partial int PrjMarkDirectoryAsVirtualizationRoot(
string rootPathName,
string? targetPathName,
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "PrjMarkDirectoryAsPlaceholder")]
internal static extern int PrjMarkDirectoryAsVirtualizationRoot(
string rootPathName,
[MarshalAs(UnmanagedType.LPWStr)] string targetPathName,
#endif
IntPtr versionInfo,
ref Guid virtualizationInstanceID);
// ============================
// File data streaming
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial int PrjWriteFileData(
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern int PrjWriteFileData(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
ref Guid dataStreamId,
IntPtr buffer,
ulong byteOffset,
uint length);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial IntPtr PrjAllocateAlignedBuffer(
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern IntPtr PrjAllocateAlignedBuffer(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
UIntPtr size);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial void PrjFreeAlignedBuffer(IntPtr buffer);
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern void PrjFreeAlignedBuffer(IntPtr buffer);
#endif
// ============================
// Command completion
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial int PrjCompleteCommand(
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern int PrjCompleteCommand(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
int commandId,
int completionResult,
IntPtr extendedParameters);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, EntryPoint = "PrjCompleteCommand")]
internal static partial int PrjCompleteCommandWithNotification(
#else
[DllImport(ProjFSLib, ExactSpelling = true, EntryPoint = "PrjCompleteCommand")]
internal static extern int PrjCompleteCommandWithNotification(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
int commandId,
int completionResult,
ref PRJ_COMPLETE_COMMAND_EXTENDED_PARAMETERS extendedParameters);
// ============================
// Cache management
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial int PrjClearNegativePathCache(
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern int PrjClearNegativePathCache(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
out uint totalEntryNumber);
// ============================
// Directory enumeration
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjFillDirEntryBuffer(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjFillDirEntryBuffer(
#endif
string fileName,
ref PRJ_FILE_BASIC_INFO fileBasicInfo,
IntPtr dirEntryBufferHandle);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjFillDirEntryBuffer2(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjFillDirEntryBuffer2(
#endif
IntPtr dirEntryBufferHandle,
string fileName,
ref PRJ_FILE_BASIC_INFO fileBasicInfo,
ref PRJ_EXTENDED_INFO extendedInfo);
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16, EntryPoint = "PrjFillDirEntryBuffer2")]
internal static partial int PrjFillDirEntryBuffer2NoExtInfo(
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "PrjFillDirEntryBuffer2")]
internal static extern int PrjFillDirEntryBuffer2NoExtInfo(
#endif
IntPtr dirEntryBufferHandle,
string fileName,
ref PRJ_FILE_BASIC_INFO fileBasicInfo,
IntPtr extendedInfo);
// ============================
// Filename utilities
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.U1)]
internal static partial bool PrjDoesNameContainWildCards(string fileName);
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern bool PrjDoesNameContainWildCards(string fileName);
#endif
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.U1)]
internal static partial bool PrjFileNameMatch(string fileNameToCheck, string pattern);
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern bool PrjFileNameMatch(string fileNameToCheck, string pattern);
#endif
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjFileNameCompare(string fileName1, string fileName2);
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjFileNameCompare(string fileName1, string fileName2);
#endif
// ============================
// File state query
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib, StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PrjGetOnDiskFileState(string destinationFileName, out uint fileState);
#else
[DllImport(ProjFSLib, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int PrjGetOnDiskFileState(string destinationFileName, out uint fileState);
#endif
// ============================
// Virtualization instance info
// ============================
#if NET7_0_OR_GREATER
[LibraryImport(ProjFSLib)]
internal static partial int PrjGetVirtualizationInstanceInfo(
#else
[DllImport(ProjFSLib, ExactSpelling = true)]
internal static extern int PrjGetVirtualizationInstanceInfo(
#endif
SafeProjFsHandle namespaceVirtualizationContext,
ref PRJ_VIRTUALIZATION_INSTANCE_INFO virtualizationInstanceInfo);
// ============================
// Native structures
// ============================
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_CALLBACK_DATA
{
public uint Size;
public uint Flags;
public IntPtr namespaceVirtualizationContext; // PRJ_NAMESPACE_VIRTUALIZATION_CONTEXT (native handle, NOT SafeHandle)
public int CommandId;
public Guid FileId;
public Guid DataStreamId;
public IntPtr FilePathName; // PCWSTR
public IntPtr VersionInfo; // PRJ_PLACEHOLDER_VERSION_INFO*
public uint TriggeringProcessId;
public IntPtr TriggeringProcessImageFileName; // PCWSTR
public IntPtr InstanceContext;
}
internal const uint PRJ_CB_DATA_FLAG_ENUM_RESTART_SCAN = 0x00000001;
internal const uint PRJ_CB_DATA_FLAG_ENUM_RETURN_SINGLE_ENTRY = 0x00000002;
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct PRJ_PLACEHOLDER_VERSION_INFO
{
public fixed byte ProviderID[PlaceholderIdLength];
public fixed byte ContentID[PlaceholderIdLength];
}
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_FILE_BASIC_INFO
{
public byte IsDirectory; // BOOLEAN (1 byte)
private byte _pad1;
private byte _pad2;
private byte _pad3;
private int _pad4; // Pad to 8-byte alignment for FileSize
public long FileSize; // INT64
public long CreationTime; // LARGE_INTEGER (FileTime)
public long LastAccessTime; // LARGE_INTEGER (FileTime)
public long LastWriteTime; // LARGE_INTEGER (FileTime)
public long ChangeTime; // LARGE_INTEGER (FileTime)
public uint FileAttributes; // UINT32
}
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_PLACEHOLDER_INFO
{
public PRJ_FILE_BASIC_INFO FileBasicInfo;
// EaInformation
public uint EaBufferSize;
public uint OffsetToFirstEa;
// SecurityInformation
public uint SecurityBufferSize;
public uint OffsetToSecurityDescriptor;
// StreamsInformation
public uint StreamsInfoBufferSize;
public uint OffsetToFirstStreamInfo;
// VersionInfo
public PRJ_PLACEHOLDER_VERSION_INFO VersionInfo;
// VariableData[1] — flexible array member in the native struct
// Must be included so that sizeof matches the native sizeof(PRJ_PLACEHOLDER_INFO) = 344
public byte VariableData;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_STARTVIRTUALIZING_OPTIONS
{
public uint Flags;
public uint PoolThreadCount;
public uint ConcurrentThreadCount;
public IntPtr NotificationMappings; // PRJ_NOTIFICATION_MAPPING*
public uint NotificationMappingsCount;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_VIRTUALIZATION_INSTANCE_INFO
{
public Guid InstanceID;
public uint WriteAlignment;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_EXTENDED_INFO
{
public uint InfoType;
public uint NextInfoOffset;
public IntPtr SymlinkTargetName; // PCWSTR
}
internal const uint PRJ_EXT_INFO_TYPE_SYMLINK = 1;
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_NOTIFICATION_MAPPING_NATIVE
{
public uint NotificationBitMask; // PRJ_NOTIFY_TYPES
public IntPtr NotificationRoot; // PCWSTR
}
internal const uint PRJ_FLAG_NONE = 0;
internal const uint PRJ_FLAG_USE_NEGATIVE_PATH_CACHE = 1;
// PRJ_NOTIFICATION values (for the notification callback)
internal const int PRJ_NOTIFICATION_FILE_OPENED = 0x00000002;
internal const int PRJ_NOTIFICATION_NEW_FILE_CREATED = 0x00000004;
internal const int PRJ_NOTIFICATION_FILE_OVERWRITTEN = 0x00000008;
internal const int PRJ_NOTIFICATION_PRE_DELETE = 0x00000010;
internal const int PRJ_NOTIFICATION_PRE_RENAME = 0x00000020;
internal const int PRJ_NOTIFICATION_PRE_SET_HARDLINK = 0x00000040;
internal const int PRJ_NOTIFICATION_FILE_RENAMED = 0x00000080;
internal const int PRJ_NOTIFICATION_HARDLINK_CREATED = 0x00000100;
internal const int PRJ_NOTIFICATION_FILE_HANDLE_CLOSED_NO_MODIFICATION = 0x00000200;
internal const int PRJ_NOTIFICATION_FILE_HANDLE_CLOSED_FILE_MODIFIED = 0x00000400;
internal const int PRJ_NOTIFICATION_FILE_HANDLE_CLOSED_FILE_DELETED = 0x00000800;
internal const int PRJ_NOTIFICATION_FILE_PRE_CONVERT_TO_FULL = 0x00001000;
// PRJ_COMPLETE_COMMAND_TYPE
internal const int PRJ_COMPLETE_COMMAND_TYPE_NOTIFICATION = 1;
internal const int PRJ_COMPLETE_COMMAND_TYPE_ENUMERATION = 2;
[StructLayout(LayoutKind.Explicit)]
internal struct PRJ_COMPLETE_COMMAND_EXTENDED_PARAMETERS
{
[FieldOffset(0)]
public int CommandType;
// For Notification type: notification mask at offset 8
[FieldOffset(8)]
public uint NotificationMask;
// For Enumeration type: dir entry buffer handle at offset 8
[FieldOffset(8)]
public IntPtr DirEntryBufferHandle;
}
// ============================
// Callback function pointer struct
// ============================
[StructLayout(LayoutKind.Sequential)]
internal struct PRJ_CALLBACKS
{
public IntPtr StartDirectoryEnumerationCallback;
public IntPtr EndDirectoryEnumerationCallback;
public IntPtr GetDirectoryEnumerationCallback;
public IntPtr GetPlaceholderInfoCallback;
public IntPtr GetFileDataCallback;
public IntPtr QueryFileNameCallback;
public IntPtr NotificationCallback;
public IntPtr CancelCommandCallback;
}
// Managed delegate types for callbacks (these get marshaled to native function pointers)
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int StartDirectoryEnumerationDelegate(PRJ_CALLBACK_DATA* callbackData, Guid* enumerationId);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int EndDirectoryEnumerationDelegate(PRJ_CALLBACK_DATA* callbackData, Guid* enumerationId);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int GetDirectoryEnumerationDelegate(PRJ_CALLBACK_DATA* callbackData, Guid* enumerationId, IntPtr searchExpression, IntPtr dirEntryBufferHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int GetPlaceholderInfoDelegate(PRJ_CALLBACK_DATA* callbackData);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int GetFileDataDelegate(PRJ_CALLBACK_DATA* callbackData, ulong byteOffset, uint length);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int QueryFileNameDelegate(PRJ_CALLBACK_DATA* callbackData);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate int NotificationDelegate(PRJ_CALLBACK_DATA* callbackData, byte isDirectory, int notification, IntPtr destinationFileName, IntPtr operationParameters);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal unsafe delegate void CancelCommandDelegate(PRJ_CALLBACK_DATA* callbackData);
}
}