My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
namespace BlogSharp.Core.Impl.Services.FileSystem.Native
{
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public static class NativeMethods
{
private const string KERNEL32 = "kernel32.dll";

// Error codes
internal const int ERROR_SUCCESS = 0;
internal const int ERROR_FILE_NOT_FOUND = 2;
internal const int ERROR_NO_MORE_FILES = 18;
internal const int ERROR_RECOVERY_NOT_NEEDED = 6821;

// Create file flags
internal const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;

// Function control codes / masks
private const int METHOD_BUFFERED = 0x00000000;
private const int FILE_WRITE_DATA = 0x00000002;
private const int FILE_DEVICE_FILE_SYSTEM = 0x00000009;

private const int ROLLFORWARD_REDO_FUNCTION = 84;
private const int ROLLFORWARD_UNDO_FUNCTION = 85;
private const int START_RM_FUNCTION = 86;
private const int SHUTDOWN_RM_FUNCTION = 87;
private const int CREATE_SECONDARY_RM_FUNCTION = 90;

// KTM IOCTLs
internal const int FSCTL_TXFS_ROLLFORWARD_REDO =
(FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_WRITE_DATA << 14) | (ROLLFORWARD_REDO_FUNCTION << 2) | METHOD_BUFFERED;

internal const int FSCTL_TXFS_ROLLFORWARD_UNDO =
(FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_WRITE_DATA << 14) | (ROLLFORWARD_UNDO_FUNCTION << 2) | METHOD_BUFFERED;

internal const int FSCTL_TXFS_START_RM =
(FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_WRITE_DATA << 14) | (START_RM_FUNCTION << 2) | METHOD_BUFFERED;

internal const int FSCTL_TXFS_SHUTDOWN_RM =
(FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_WRITE_DATA << 14) | (SHUTDOWN_RM_FUNCTION << 2) | METHOD_BUFFERED;

internal const int FSCTL_TXFS_CREATE_SECONDARY_RM =
(FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_WRITE_DATA << 14) | (CREATE_SECONDARY_RM_FUNCTION << 2) | METHOD_BUFFERED;

// KTM Start flags
internal const int TXFS_START_RM_FLAG_LOG_CONTAINER_COUNT_MAX = 0x00000001;
internal const int TXFS_START_RM_FLAG_LOG_CONTAINER_COUNT_MIN = 0x00000002;
internal const int TXFS_START_RM_FLAG_LOG_CONTAINER_SIZE = 0x00000004;
internal const int TXFS_START_RM_FLAG_LOG_GROWTH_INCREMENT_NUM_CONTAINERS = 0x00000008;
internal const int TXFS_START_RM_FLAG_LOG_GROWTH_INCREMENT_PERCENT = 0x00000010;
internal const int TXFS_START_RM_FLAG_LOG_AUTO_SHRINK_PERCENTAGE = 0x00000020;
internal const int TXFS_START_RM_FLAG_LOG_NO_CONTAINER_COUNT_MAX = 0x00000040;
internal const int TXFS_START_RM_FLAG_LOG_NO_CONTAINER_COUNT_MIN = 0x00000080;

internal const int TXFS_START_RM_FLAG_RECOVER_BEST_EFFORT = 0x00000200;
internal const int TXFS_START_RM_FLAG_LOGGING_MODE = 0x00000400;
internal const int TXFS_START_RM_FLAG_PRESERVE_CHANGES = 0x00000800;

// KTM Logging modes
internal const int TXFS_LOGGING_MODE_SIMPLE = 0x0001;
internal const int TXFS_LOGGING_MODE_FULL = 0x0002;

public enum FileAccess
{
GENERIC_READ = unchecked((int) 0x80000000),
GENERIC_WRITE = 0x40000000
}

[Flags]
public enum FileShare
{
FILE_SHARE_NONE = 0x00,
FILE_SHARE_READ = 0x01,
FILE_SHARE_WRITE = 0x02,
FILE_SHARE_DELETE = 0x04
}

public enum FileMode
{
CREATE_NEW = 1,
CREATE_ALWAYS = 2,
OPEN_EXISTING = 3,
OPEN_ALWAYS = 4,
TRUNCATE_EXISTING = 5
}

[Flags]
internal enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008,
COPY_FILE_COPY_SYMLINK = 0x00000800
}

[Flags]
public enum MoveFileFlags : uint
{
MOVEFILE_REPLACE_EXISTING = 0x00000001,
MOVEFILE_COPY_ALLOWED = 0x00000002,
MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
MOVEFILE_WRITE_THROUGH = 0x00000008,
MOVEFILE_CREATE_HARDLINK = 0x00000010,
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
}

public enum FINDEX_INFO_LEVELS
{
FindExInfoStandard,
FindExInfoMaxInfoLevel
}

public enum FINDEX_SEARCH_OPS
{
FindExSearchNameMatch,
FindExSearchLimitToDirectories,
FindExSearchLimitToDevices,
FindExSearchMaxSearchOp
}
#pragma warning disable 0649
public struct FILETIME
{
public uint DateTimeHigh;
public uint DateTimeLow;
}
#pragma warning restore 0649

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public int dwFileAttributes;
public NativeMethods.FILETIME ftCreationTime;
public NativeMethods.FILETIME ftLastAccessTime;
public NativeMethods.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;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TXFS_START_RM_INFORMATION
{
public UInt32 Flags;

public UInt64 LogContainerSize;
public UInt32 LogContainerCountMin;
public UInt32 LogContainerCountMax;

public UInt32 LogGrowthIncrement;
public UInt32 LogAutoShrinkPercentage;

public UInt32 TmLogPathOffset;
public UInt16 TmLogPathLength;

public UInt16 LoggingMode;
public UInt16 LogPathLength;
public UInt16 Reserved;

public IntPtr LogPath;
}

//
// Standard file operations
//

[DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFile(
[In] string lpFileName,
[In] NativeMethods.FileAccess dwDesiredAccess,
[In] NativeMethods.FileShare dwShareMode,
[In] IntPtr lpSecurityAttributes,
[In] NativeMethods.FileMode dwCreationDisposition,
[In] int dwFlagsAndAttributes,
[In] IntPtr hTemplateFile);

[DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FindNextFile(
[In] SafeFileHandle hFindFile,
[Out] out WIN32_FIND_DATA lpFindFileData);


//
// Transacted file operations
//

#region transactional operations

[DllImport(KERNEL32, EntryPoint = "CreateFileTransacted", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFileTransacted(
[In] string lpFileName,
[In] NativeMethods.FileAccess dwDesiredAccess,
[In] NativeMethods.FileShare dwShareMode,
[In] IntPtr lpSecurityAttributes,
[In] NativeMethods.FileMode dwCreationDisposition,
[In] int dwFlagsAndAttributes,
[In] IntPtr hTemplateFile,
[In] KtmTransactionHandle hTransaction,
[In] IntPtr pusMiniVersion,
[In] IntPtr pExtendedParameter);

[DllImport(KERNEL32, EntryPoint = "CopyFileTransacted", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CopyFileTransacted(
[In] string lpExistingFileName,
[In] string lpNewFileName,
[In] IntPtr lpProgressRoutine,
[In] IntPtr lpData,
[In] [MarshalAs(UnmanagedType.Bool)] ref bool pbCancel,
[In] CopyFileFlags dwCopyFlags,
[In] KtmTransactionHandle hTransaction);

[DllImport(KERNEL32, EntryPoint = "DeleteFileTransacted", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteFileTransacted(
[In] string lpFileName,
[In] KtmTransactionHandle hTransaction);

[DllImport(KERNEL32, EntryPoint = "FindFirstFileTransacted", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeFileHandle FindFirstFileTransacted(
[In] string lpDirSpec,
[In] FINDEX_INFO_LEVELS fInfoLevelId,
[Out] out WIN32_FIND_DATA lpFindFileData,
[In] FINDEX_SEARCH_OPS fSearchOp,
[In] IntPtr lpSearchFilter,
[In] int dwAdditionalFlags,
[In] KtmTransactionHandle hTransaction);

[DllImport(KERNEL32, EntryPoint = "MoveFileTransacted", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MoveFileTransacted(
[In] string lpExistingFileName,
[In] string lpNewFileName,
[In] IntPtr lpProgressRoutine,
[In] IntPtr lpData,
[In] MoveFileFlags dwFlags,
[In] KtmTransactionHandle hTransaction);

#endregion

#region nontransactional operations

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeleteFile(string path);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern int GetFileSize(SafeFileHandle hFile, out int highSize);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeFileHandle FindFirstFile(string fileName, [In, Out] WIN32_FIND_DATA data);

[DllImport("kernel32.dll")]
internal static extern int GetFileType(SafeFileHandle handle);

#endregion

[DllImport(KERNEL32, EntryPoint = "DeviceIoControl", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl(
[In] SafeFileHandle hDevice,
[In] int dwIoControlCode,
[In] IntPtr lpInBuffer,
[In] int nInBufferSize,
[Out] IntPtr lpOutBuffer,
[In] int nOutBufferSize,
[Out] out int lpBytesReturned,
[In] IntPtr lpOverlapped);

//
// Close handles
//

[DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(
[In] IntPtr handle);

[DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool FindClose(
[In] SafeFileHandle handle);

internal static void HandleCOMError(int error)
{
throw new System.ComponentModel.Win32Exception(error);
}
}
}

Change log

r108 by tehlike on Jul 17, 2009   Diff
Huge code clean (via resharper)
Go to: 
Project members, sign in to write a code review

Older revisions

r103 by gbrusella on Jul 13, 2009   Diff
Added Tag Managrment (Save & validate)
Added More Comments
Mappings improved (slightly)
r86 by tehlike on Feb 22, 2009   Diff
Some cleanup + StyleCop goodies. More
to come.
r58 by tehlike on Jan 30, 2009   Diff
Reverting back FileSystem cleanups for
future feature additions.
All revisions of this file

File info

Size: 10039 bytes, 294 lines
Powered by Google Project Hosting