-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviousVersionOnRemote.cs
More file actions
122 lines (114 loc) · 3.89 KB
/
PreviousVersionOnRemote.cs
File metadata and controls
122 lines (114 loc) · 3.89 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
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace LibEnumRemotePreviousVersion
{
public static class PreviousVersionOnRemote
{
const FileAttributes FILE_FLAG_BACKUP_SEMANTICS = (FileAttributes)0x02000000;
const int FSCTL_SRV_ENUMERATE_SNAPSHOTS = 0x00144064;
public static string[] Enum(string uncShareRoot)
{
IntPtr dirHandle = CreateFileW(
uncShareRoot,
FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete,
IntPtr.Zero,
FileMode.Open,
FILE_FLAG_BACKUP_SEMANTICS,
IntPtr.Zero
);
if (dirHandle.ToInt64() == -1)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
try
{
var ioStatus = new IO_STATUS_BLOCK();
var buff = new byte[16];
int status = NtFsControlFile(
dirHandle,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
ref ioStatus,
FSCTL_SRV_ENUMERATE_SNAPSHOTS,
IntPtr.Zero,
0,
buff,
buff.Length
);
if (status != 0)
{
throw new Win32Exception(status);
}
var numVolumes = BitConverter.ToInt32(buff, 0);
var numVolumesReturned = BitConverter.ToInt32(buff, 4);
var numBodyBytes = BitConverter.ToInt32(buff, 8);
buff = new byte[12 + numBodyBytes];
status = NtFsControlFile(
dirHandle,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
ref ioStatus,
FSCTL_SRV_ENUMERATE_SNAPSHOTS,
IntPtr.Zero,
0,
buff,
buff.Length
);
if (status != 0)
{
throw new Win32Exception(status);
}
return Encoding.Unicode.GetString(
buff,
12,
buff.Length - 12
)
.Split(
new char[] { '\0' },
StringSplitOptions.RemoveEmptyEntries
);
}
finally
{
CloseHandle(dirHandle);
}
}
[DllImport("ntdll.dll")]
private static extern int NtFsControlFile(
IntPtr FileHandle,
IntPtr Event,
IntPtr ApcRoutine,
IntPtr ApcContext,
ref IO_STATUS_BLOCK IoStatusBlock,
int FsControlCode,
IntPtr InputBuffer,
int InputBufferLength,
byte[] OutputBuffer,
int OutputBufferLength
);
private struct IO_STATUS_BLOCK
{
IntPtr StatusOrPointer;
IntPtr Information;
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile
);
}
}