forked from terrisWayne/objidlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objidlib.cpp
226 lines (193 loc) · 5.31 KB
/
objidlib.cpp
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
/** @file objidlib.cpp
* @brief A library for working with NTFS Object Idenitifers
* @author amdf
* @version 0.2
* @date September 2011
*/
#include "stdafx.h"
#include "objidlib.h"
static HANDLE OpenFileForWrite(IN LPCWSTR sFileName, IN BOOL bBackup)
{
return CreateFile(
sFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
(bBackup)
? FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS
: FILE_FLAG_OPEN_REPARSE_POINT, 0);
}
static HANDLE OpenFileForRead(IN LPCWSTR sFileName, IN BOOL bBackup)
{
return CreateFile(
sFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
(bBackup)
? FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS
: FILE_FLAG_OPEN_REPARSE_POINT, 0);
}
/**
* @brief Checks an existence of an object id of a specified file
* @param[in] sFileName File name
* @return TRUE if exists, FALSE otherwise
*/
OBJIDLIB_API BOOL ObjectIdExists(IN LPCWSTR sFileName)
{
HANDLE hFile = OpenFileForRead(sFileName,
(GetFileAttributes(sFileName) & FILE_ATTRIBUTE_DIRECTORY));
OBJECTID_ATTRIBUTE Obj;
DWORD dwRet;
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if (!DeviceIoControl(hFile, FSCTL_GET_OBJECT_ID,
NULL, 0, &Obj, sizeof(OBJECTID_ATTRIBUTE), &dwRet, NULL))
{
CloseHandle(hFile);
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}
/**
* @brief Request a system to create a new object id in a specified file
* @param[in] sFileName File name
* @param[out] pObjId Pointer to a buffer receiving a content of a new object id.
* If this parameter is NULL, it is ignored.
* @return TRUE if success, FALSE otherwise
*/
OBJIDLIB_API BOOL CreateObjectId(IN LPCWSTR sFileName, OUT POBJECTID_ATTRIBUTE pObjId)
{
OBJECTID_ATTRIBUTE Obj;
DWORD dwRet;
HANDLE hNew = OpenFileForRead(sFileName,
(GetFileAttributes(sFileName) & FILE_ATTRIBUTE_DIRECTORY));
if (hNew == INVALID_HANDLE_VALUE)
{
return FALSE;
}
ZeroMemory(&Obj, sizeof(OBJECTID_ATTRIBUTE));
if (!DeviceIoControl(hNew,
FSCTL_CREATE_OR_GET_OBJECT_ID,
NULL, 0, &Obj, sizeof(OBJECTID_ATTRIBUTE), &dwRet, NULL))
{
CloseHandle(hNew);
return FALSE;
}
if (NULL != pObjId)
{
memcpy(pObjId, &Obj, sizeof(OBJECTID_ATTRIBUTE));
}
CloseHandle(hNew);
return TRUE;
}
/**
* @brief Get a contents of an object id of a specified file
* @param[in] sFileName File name
* @param[out] pObjId Pointer to a buffer receiving a content of a new object id.
* If this parameter is NULL, it is ignored.
* @return TRUE if success, FALSE otherwise
*/
OBJIDLIB_API BOOL GetObjectId(IN LPCWSTR sFileName, OUT POBJECTID_ATTRIBUTE pObjId)
{
if (NULL == pObjId)
{
return FALSE;
}
HANDLE hNew = OpenFileForRead(sFileName,
(GetFileAttributes(sFileName) & FILE_ATTRIBUTE_DIRECTORY));
DWORD dwRet;
if (hNew == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if (!DeviceIoControl(hNew,
FSCTL_GET_OBJECT_ID,
NULL, 0, pObjId, sizeof(OBJECTID_ATTRIBUTE), &dwRet, NULL))
{
CloseHandle(hNew);
return FALSE;
}
CloseHandle(hNew);
return TRUE;
}
/**
* @brief Delete an object id of a specified file
* @param[in] sFileName File name
* @return TRUE if success, FALSE otherwise
*/
OBJIDLIB_API BOOL DeleteObjectId(IN LPCWSTR sFileName)
{
HANDLE hDel = OpenFileForWrite(sFileName,
(GetFileAttributes(sFileName) & FILE_ATTRIBUTE_DIRECTORY));
DWORD dwRet;
if (hDel == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if (!DeviceIoControl(hDel,
FSCTL_DELETE_OBJECT_ID,
NULL, 0, NULL, 0, &dwRet, NULL))
{
CloseHandle(hDel);
return FALSE;
}
CloseHandle(hDel);
return TRUE;
}
/**
* @brief Set an object id of a specified file. SE_RESTORE_PRIVILEGE required.
* @param[in] sFileName File name
* @param[in] pObjId An OBJECTID_ATTRIBUTE structure.
* @return TRUE if success, FALSE otherwise
*/
OBJIDLIB_API BOOL SetObjectId(IN LPCWSTR sFileName, IN POBJECTID_ATTRIBUTE pObjId)
{
if (NULL == pObjId)
{
return FALSE;
}
DWORD dwRet;
HANDLE hNew = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if (hNew == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if (!DeviceIoControl(hNew,
FSCTL_SET_OBJECT_ID,
pObjId, sizeof(OBJECTID_ATTRIBUTE), NULL, 0, &dwRet, NULL))
{
CloseHandle(hNew);
return FALSE;
}
CloseHandle(hNew);
return TRUE;
}
/**
* @brief Set an extended part of an object id of a specified file
* @param[in] sFileName File name
* @param[in] pObjId An OBJECTID_ATTRIBUTE structure containing an extended part.
* ObjectId member is ignored.
* @return TRUE if success, FALSE otherwise
*/
OBJIDLIB_API BOOL SetObjectIdExt(IN LPCWSTR sFileName, IN POBJECTID_ATTRIBUTE pObjId)
{
if (NULL == pObjId)
{
return FALSE;
}
DWORD dwRet;
HANDLE hNew = OpenFileForWrite(sFileName,
(GetFileAttributes(sFileName) & FILE_ATTRIBUTE_DIRECTORY));
if (hNew == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if (!DeviceIoControl(hNew,
FSCTL_SET_OBJECT_ID_EXTENDED,
pObjId->ExtendedInfo, sizeof(pObjId->ExtendedInfo), NULL, 0, &dwRet, NULL))
{
CloseHandle(hNew);
return FALSE;
}
CloseHandle(hNew);
return TRUE;
}