-
Notifications
You must be signed in to change notification settings - Fork 2
/
ext4Plugin.cpp
409 lines (351 loc) · 11.5 KB
/
ext4Plugin.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
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
/**
*
This file is part of Ext4tc - plugin for Total Commander.
Ext4tc is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation version 3 of the License
Ext4tc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Ext4tc. If not, see <http://www.gnu.org/licenses/>.
Ext4tc Copyright (C) 2010 Krzysztof Stasiak mail: [email protected]
*/
// ext4Plugin.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <shellapi.h>
#include "ext4plugin.h"
#include "ext4PluginDescr.h"
#include "logger.h"
#include <fstream>
#define pluginrootlen 1
#ifdef _MANAGED
#pragma managed(push, off)
#endif
ext4PluginDescr *pluginDescr;
struct thCopyParam
{
partition_linux_ext2 *partition;
unsigned int inode_num;
HANDLE ext2_hnd;
int error;
int int_flag;
};
typedef struct {
char Path[MAX_PATH];
char LastFoundName[MAX_PATH];
int disk_no;
int part_no_map;
int num_last_entry;
HANDLE searchhandle;
} tLastFindStuct,*pLastFindStuct;
DWORD WINAPI copy_thread_subroutine(LPVOID p);
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
BOOL IsUserAdmin(VOID)
/*++
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token.
Arguments: None.
Return Value:
TRUE - Caller has Administrators local group.
FALSE - Caller does not have Administrators local group. --
*/
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if(b)
{
if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}
return(b);
}
/**
* FsInit - plugin init function
*/
int __stdcall FsInit(int PluginNr,tProgressProc pProgressProc,tLogProc pLogProc,tRequestProc pRequestProc)
{
pluginDescr = new ext4PluginDescr();
int hdd_count = -1;
pluginDescr->ProgressProc = pProgressProc;
pluginDescr->LogProc = pLogProc;
pluginDescr->RequestProc = pRequestProc;
pluginDescr->PluginNumber = PluginNr;
if (!IsUserAdmin())
{
MessageBox(NULL, (LPCTSTR)"You are not admin", TEXT("INFO"), MB_OK);
return -1;
}
#ifdef _DEBUG
pluginDescr->dWin.show(SW_NORMAL);
#endif
LOG_MESSAGE("----======= Ext2Viewer written by Krzysztof Stasiak =======----\n");
if (pluginDescr->search_system_for_linux_ext2_partitions() == -1)
{
return -1;
}
pluginDescr->set_plugin_initialized(true);
return 0;
}
HANDLE __stdcall FsFindFirst(char* Path, WIN32_FIND_DATA *FindData)
{
pLastFindStuct lf;
partition_linux_ext2 **ext2_partitions = NULL;
int disk_no, part_no;
char convPath[MAX_PATH];
if (!pluginDescr || !pluginDescr->get_plugin_initialized() || pluginDescr->ext4_partitions_id_map == NULL)
{
return INVALID_HANDLE_VALUE;
}
int len = MultibyteToMultibyte(CP_ACP, Path, strlen(Path), CP_UTF8, convPath, sizeof(convPath));
convPath[len] = '\0';
memset(FindData,0,sizeof(WIN32_FIND_DATA));
if (strcmp(convPath, "\\") == 0) {
int hd_index = -1, part_index = -1;
FindData->dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY;
FindData->ftLastWriteTime.dwHighDateTime=0xFFFFFFFF;
FindData->ftLastWriteTime.dwLowDateTime=0xFFFFFFFE;
pluginDescr->clear_current_path();
if (pluginDescr->get_first_ext4_disk_and_part_no(&disk_no, &part_no) == -1)
return INVALID_HANDLE_VALUE;
ext2_partitions = pluginDescr->hard_disks[disk_no]->get_partitions_ext2();
if (ext2_partitions == NULL) {
return INVALID_HANDLE_VALUE;
}
char buff[10] = {0};
//TODO
char disk_id_char = disk_no + 'a';
sprintf_s(FindData->cFileName, sizeof(FindData->cFileName), "%s%c%d", DEVICE_HDD_DISPLAY_PREFIX, disk_id_char, part_no);
lf=(pLastFindStuct)malloc(sizeof(tLastFindStuct));
strncpy_s(lf->Path, strlen(Path)+1, Path, strlen(Path));
lf->searchhandle=INVALID_HANDLE_VALUE;
lf->disk_no = disk_no;
lf->part_no_map = pluginDescr->get_partition_index_via_real_number(disk_no, part_no);
strcpy(lf->LastFoundName, FindData->cFileName);
return (HANDLE)lf;
}
else
{
ext4_dir_entry_2 *dir = NULL;
int num_last_entry = 0;
int ret = pluginDescr->extract_disk_and_part_no(Path, &disk_no, &part_no);
if (ret == -1)
return INVALID_HANDLE_VALUE;
int part_no_map = pluginDescr->get_partition_index_via_real_number(disk_no, part_no);
ext2_partitions = pluginDescr->hard_disks[disk_no]->get_partitions_ext2();
if (ext2_partitions == NULL) {
return INVALID_HANDLE_VALUE;
}
#ifdef _DEBUG
LOG_MESSAGE("Path %s ret: %d disk_no: %d part_no %d part_inumndex %d\n",
Path, ret, disk_no, part_no, pluginDescr->get_partition_index_via_real_number(disk_no, part_no));
#endif
if (pluginDescr->get_current_path() == NULL)
{
dir = pluginDescr->get_first_dir_entry(convPath, disk_no, part_no_map, &num_last_entry);
}
else
{
//if goes up
//should be optimalized with get_first_dir_entry2
dir = pluginDescr->get_first_dir_entry(convPath, disk_no, part_no_map, &num_last_entry);
}
if (dir != NULL){
if (dir->file_type != EXT4_FT_DIR)
{
ext4_inode *inode = NULL;
inode = pluginDescr->get_ext2_inode(disk_no, part_no_map, dir->inode);
FindData->nFileSizeLow = inode->i_size_lo;
if (dir->file_type == EXT4_FT_REG_FILE)
FindData->nFileSizeHigh = inode->i_size_high;
}
else
{
FindData->nFileSizeLow = 0;
}
RETURN_FILE(FindData, dir);
}
else
{
strcpy(FindData->cFileName, "..");
FindData->dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY;
FindData->ftLastWriteTime.dwHighDateTime=0xFFFFFFFF;
FindData->ftLastWriteTime.dwLowDateTime=0xFFFFFFFE;
}
lf=(pLastFindStuct)malloc(sizeof(tLastFindStuct));
strncpy_s(lf->Path, strlen(Path)+1, Path, strlen(Path));
lf->searchhandle = 0;
lf->disk_no = disk_no;
lf->part_no_map = pluginDescr->get_partition_index_via_real_number(disk_no, part_no);
lf->num_last_entry = num_last_entry;
return (HANDLE)lf;
}
return INVALID_HANDLE_VALUE;
}
BOOL __stdcall FsFindNext(HANDLE Hdl,WIN32_FIND_DATA *FindData)
{
pLastFindStuct lf;
int disk_no, part_no_map;
if (Hdl == (HANDLE)1)
return false;
lf=(pLastFindStuct)Hdl;
disk_no = lf->disk_no;
part_no_map = lf->part_no_map;
if (lf->searchhandle == INVALID_HANDLE_VALUE)
{
if(pluginDescr->get_next_ext4_disk_and_part_no(&disk_no, &part_no_map) == -1)
return false;
partition_linux_ext2 **ext2_partitions = pluginDescr->hard_disks[disk_no]->get_partitions_ext2();
if (ext2_partitions[part_no_map] == NULL)
return false;
lf->disk_no = disk_no;
lf->part_no_map = part_no_map;
char disk_id_char = disk_no + 'a';
sprintf_s(FindData->cFileName, sizeof(FindData->cFileName), "%s%c%d", DEVICE_HDD_DISPLAY_PREFIX,
disk_id_char, ext2_partitions[part_no_map]->get_part_no());
strcpy(lf->LastFoundName, FindData->cFileName);
return true;
}
else
{
ext4_dir_entry_2 *dir = NULL;
dir = pluginDescr->get_next_dir_entry(&lf->num_last_entry);
if (dir != NULL)
{
if (dir->file_type != EXT4_FT_DIR)
{
ext4_inode *inode = NULL;
inode = pluginDescr->get_ext2_inode(lf->disk_no, lf->part_no_map, dir->inode);
FindData->nFileSizeLow = inode->i_size_lo;
if (dir->file_type == EXT4_FT_REG_FILE)
FindData->nFileSizeHigh = inode->i_size_high;
}
else
{
FindData->nFileSizeLow = 0;
}
RETURN_FILE(FindData, dir);
return true;
}
}
return false;
}
int __stdcall FsFindClose(HANDLE Hdl)
{
if (Hdl == INVALID_HANDLE_VALUE)
{
return 0;
}
pLastFindStuct lf;
lf=(pLastFindStuct)Hdl;
if (lf->searchhandle!=INVALID_HANDLE_VALUE) {
//FindClose(lf->searchhandle);
lf->searchhandle=INVALID_HANDLE_VALUE;
}
free(lf);
return 0;
}
int __stdcall FsGetFile(char* RemoteName,char* LocalName,int CopyFlags, RemoteInfoStruct* ri)
{
thCopyParam params = {0};
HANDLE hTh = INVALID_HANDLE_VALUE;
unsigned int inode_num = 0;
unsigned long long fsize = 0, pos = 0;
ext4_inode *inode = NULL;
HANDLE hnd = INVALID_HANDLE_VALUE;
LARGE_INTEGER large_pos = {0};
int disk_no, part_no;
char convRemoteName[MAX_PATH];
if (pluginDescr->extract_disk_and_part_no(RemoteName, &disk_no, &part_no) == -1)
return FS_FILE_NOTFOUND;
int len = MultibyteToMultibyte(CP_ACP, RemoteName, strlen(RemoteName), CP_UTF8, convRemoteName, sizeof(convRemoteName));
convRemoteName[len] = '\0';
int part_no_map = pluginDescr->get_partition_index_via_real_number(disk_no, part_no);
if((inode = pluginDescr->get_ext2_inode(disk_no, part_no_map, convRemoteName, &inode_num)) == NULL)
{
LOG_MESSAGE("File %s was not found\n", RemoteName);
return FS_FILE_NOTFOUND;
}
LOG_MESSAGE("inode: %u mode: %#0x, %o\n", inode_num, inode->i_mode, inode->i_mode);
if(!S_ISREG(inode->i_mode))
{
return FS_FILE_NOTSUPPORTED;
}
if ((hnd = CreateFile(LocalName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
return FS_FILE_WRITEERROR;
}
params.ext2_hnd = hnd;
partition_linux_ext2 **ext2_partitions = pluginDescr->hard_disks[disk_no]->get_partitions_ext2();
params.partition = ext2_partitions[part_no_map];
params.inode_num = inode_num;
params.int_flag = 0;
pluginDescr->ProgressProc(pluginDescr->PluginNumber, RemoteName, LocalName, 0);
#ifdef _DEBUG
int startTicks = GetTickCount();
#endif
hTh = CreateThread(NULL, 0, copy_thread_subroutine, ¶ms, 0, NULL);
fsize = (unsigned long long)inode->i_size_high << 32;
fsize |= (unsigned long long)inode->i_size_lo;
while(1)
{
if (GetFileSizeEx(hnd, &large_pos) == 0 || params.error != 0)
{
LOG_MESSAGE("An error %d occured while reading %s\n", params.error, RemoteName);
params.int_flag = 1;
WaitForSingleObject(hTh, INFINITE);
CloseHandle(hnd);
CloseHandle(hTh);
return FS_FILE_READERROR;
}
if ((unsigned long long)large_pos.QuadPart >= fsize)
break;
if(pluginDescr->ProgressProc(pluginDescr->PluginNumber, RemoteName,
LocalName, (int)(((double)large_pos.QuadPart / fsize)*100)) != 0)
{
params.int_flag = 1;
WaitForSingleObject(hTh, INFINITE);
CloseHandle(hnd);
CloseHandle(hTh);
return FS_FILE_USERABORT;
}
}
LOG_MESSAGE("Finished copying file %s in %d ms\n", RemoteName, GetTickCount() - startTicks);
CloseHandle(hnd);
WaitForSingleObject(hTh, INFINITE);
CloseHandle(hTh);
return FS_FILE_OK;
}
DWORD WINAPI copy_thread_subroutine(LPVOID p)
{
thCopyParam *params = (thCopyParam *)p;
params->partition->read_file(params->inode_num, params->ext2_hnd, ¶ms->error, ¶ms->int_flag);
return 0;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif