-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFsys.cpp
More file actions
3507 lines (2673 loc) · 78.6 KB
/
Copy pathFsys.cpp
File metadata and controls
3507 lines (2673 loc) · 78.6 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Fsys.cpp: implementation of the CFsys class.
//
//////////////////////////////////////////////////////////////////////
#include "pub.h"
#include "io.h"
#include <locale.h>
HANDLE hLogWrite_Mutex;
time_t FILETIME_to_time_t(const FILETIME *ft);
extern PATH_TOKEN PT_TOK;
#include "CDiskRepartition.h"
CDiskRepartition *theDiskPartition; // if not null - read from %physicaldrive1% device by theDiskPartition
LARGE_INTEGER filePos; // set by crypt_SetFilePointer()
LARGE_INTEGER diskDataOffset; // set by CFsys::OpenVolume();
BOOL is_encrypted_partition; // TRUE this is encrypted rohos disk. (False, if rohos is opening 2nd partition from usb drive)
#define O_RDONLY 0x0000 /* open for reading only */
#define O_WRONLY 0x0001 /* open for writing only */
#define O_RDWR 0x0002 /* open for reading and writing */
#define O_APPEND 0x0008 /* writes done at eof */
#define O_CREAT 0x0100 /* create and open file */
#define O_TRUNC 0x0200 /* open and truncate */
#define O_EXCL 0x0400 /* open only if file doesn't already exist */
typedef void ( WINAPI * P_SHAddToRecentDocs) (UINT uFlags, LPCVOID pv);
void My_SHAddToRecentDocs( UINT uFlags, LPCVOID pv)
{
return;
}
HANDLE hHook__SHAddToRecentDocs;
#ifdef _USE_BOXEDAPP_SDK
// file/registry virtualization SDK
#include ".\\BoxedAppSDK\\include\\BoxedAppSDK.h"
#pragma comment(lib, ".\\BoxedAppSDK\\lib\\static_lib\\BoxedAppSDK.lib")
#endif // _BOXEDAPP_USE
typedef void (_stdcall *PFUNCTION_ENTRYPOINT)(
HWND hwndStub,
HINSTANCE hAppInstance,
LPSTR lpCmdLine,
int nCmdShow
);
CPtrArray arrayIFiles; // array with Isteam files.
/**
On-the-fly encryption for virtual files.
CIStreamFile Required for BoxedAppSDK_CreateVirtualFileBasedOnIStream()
see CFSys::CreateVirtualFile()
*/
class CIStreamFile : public IStream
{
// Istream
public:
LONG m_nRefCount;
TCHAR fileName[500];
TCHAR virtualFileName[500];
LARGE_INTEGER m_dwPosition;
bool m_setsize_zero; // TRUE if file were zeroed
CFsys* fsys;
time_t ctime, mtime, atime;
_reent *_fat_entry;
int _fat_fd;
ntfs_attr *_ntfs_attr;
ntfs_inode *_ntfs_inode;
LARGE_INTEGER m_dwSize; // file size;
bool _isNTFS;
bool was_read, was_write;
protected:
// IUnknown
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) ;
virtual ULONG STDMETHODCALLTYPE AddRef();
virtual ULONG STDMETHODCALLTYPE Release();
virtual HRESULT STDMETHODCALLTYPE Read(void* pv, ULONG cb, ULONG* pcbRead) ;
virtual HRESULT STDMETHODCALLTYPE Write(const void* pv, ULONG cb, ULONG* pcbWritten) ;
virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition) ;
virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize)
{
was_write =1;
if (libNewSize.QuadPart == 0)
{
//WriteLog("ISetSize0 ");
m_setsize_zero = 1;
return S_OK;
}
m_dwSize.QuadPart = libNewSize.QuadPart;
int ret;
//WriteLog("ISetSize %X", libNewSize.LowPart );
if (_isNTFS)
ret = ntfs_attr_truncate(_ntfs_attr, m_dwSize.QuadPart);
else
ret = _FAT_ftruncate_r(_fat_entry, _fat_fd, m_dwSize.LowPart);
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE Clone(IStream** ppstm)
{
CIStreamFile *file = new CIStreamFile(fsys);
file->_fat_entry = _fat_entry;
file->_fat_fd = _fat_fd;
file->_ntfs_attr = _ntfs_attr;
file->_ntfs_inode = _ntfs_inode;
file->m_dwSize = m_dwSize;
file->_isNTFS = _isNTFS;
file->m_setsize_zero = m_setsize_zero;
_tcscpy(file->fileName, fileName);
*ppstm = file;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten){
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE Revert()
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
return E_NOTIMPL;
}
//
virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG* pstatstg, DWORD grfStatFlag)
{
return E_NOTIMPL;
/*
// Appling file times failed... :(
FILETIME ftCreated, tfLastAccess, ftLastWrite;
HANDLE m_hFile = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
GetFileTime(m_hFile, &ftCreated, &tfLastAccess, &ftLastWrite);
CloseHandle(m_hFile);
WriteLog("IStream::Stat %d", grfStatFlag);
pstatstg->ctime = ftCreated;
pstatstg->atime = tfLastAccess;
pstatstg->mtime = ftLastWrite;
pstatstg->cbSize.QuadPart = m_dwSize.QuadPart;
//pstatstg->
return S_OK;*/
}
public:
virtual ~CIStreamFile() {
//m_pFile->Release();
int i= -1;
}
CIStreamFile(CFsys* pFsys) :
m_nRefCount(1),
fsys(pFsys)
{
m_dwPosition.QuadPart = 0;
m_dwSize.QuadPart = 0xFFFFFFFF;
_isNTFS = pFsys->_isNTFS;
was_read = was_write = 0;
_fat_entry = NULL;
_ntfs_attr = NULL;
_ntfs_inode = NULL;
m_setsize_zero = 0;
}
/** if the FS attribute is not open still, oen it (for files that was creates in quick mode)
*/
void openFileAttr()
{
if (_isNTFS)
{
if (_ntfs_attr == NULL)
{
if (_ntfs_inode == NULL)
_ntfs_inode = ntfs_pathname_to_inode(fsys->ntfs_volume, NULL, fileName);
_ntfs_attr = ntfs_attr_open (_ntfs_inode, AT_DATA, NULL, 0);
}
}
else {
}
}
/** return current file size (real)
*/
LARGE_INTEGER getFileSize()
{
LARGE_INTEGER li;
li.QuadPart =0;
if (_isNTFS)
{
if (_ntfs_attr == NULL)
{
if (_ntfs_inode)
li.QuadPart = _ntfs_inode->data_size;
else
li.QuadPart = 1;
}
li.QuadPart = _ntfs_attr->data_size;
//WriteLog("%X", li.LowPart);
return li;
}
else
{
FILE_STRUCT* file = (FILE_STRUCT*)_fat_fd;
li.QuadPart = file->filesize;
return li;
}
return li;
}
};
// IUnknown
HRESULT STDMETHODCALLTYPE CIStreamFile::QueryInterface(REFIID riid, void** ppvObject)
{
*ppvObject = NULL;
if (IsEqualIID(IID_IUnknown, riid))
*ppvObject = this;
else if (IsEqualIID(IID_IStream, riid))
*ppvObject = this;
else if (IsEqualIID(IID_ISequentialStream, riid))
*ppvObject = this;
if (NULL != *ppvObject)
{
AddRef();
return S_OK;
}
else
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE CIStreamFile::AddRef()
{
InterlockedIncrement(&m_nRefCount);
return m_nRefCount;
}
ULONG STDMETHODCALLTYPE CIStreamFile::Release()
{
InterlockedDecrement(&m_nRefCount);
LONG nRefCount = m_nRefCount;
if (0 == m_nRefCount)
delete this;
return nRefCount;
}
// ISequentialStream
HRESULT STDMETHODCALLTYPE CIStreamFile::Read(void* pv, ULONG cb, ULONG* pcbRead)
{
was_read = 1;
DWORD dwWaitResult = WaitForSingleObject( hLogWrite_Mutex, 100000L);
if ( dwWaitResult!=WAIT_OBJECT_0)
{
return S_OK;
}
DWORD dwBytesToRead;
openFileAttr();
//WriteLog("%X Read %X %X", this, m_dwPosition, cb);
if (cb==0)
{
if (NULL != pcbRead)
*pcbRead = 0;
ReleaseMutex(hLogWrite_Mutex);
return S_OK;
}
m_dwSize = getFileSize();
if (m_dwPosition.QuadPart < 0 || m_dwPosition.QuadPart > m_dwSize.QuadPart)
dwBytesToRead = 0;
else
dwBytesToRead = cb < m_dwSize.QuadPart - m_dwPosition.QuadPart ? cb : m_dwSize.QuadPart - m_dwPosition.QuadPart;
int ret =0;
if (_isNTFS)
{
int offset=0;
while (dwBytesToRead) {
ret = ntfs_attr_pread( _ntfs_attr, m_dwPosition.QuadPart+offset, dwBytesToRead, (char*)pv+offset);
//WriteLog("Read %X %X %X (%X)", ret, offset, dwBytesToRead, cb);
if (ret<=0)
break;
dwBytesToRead -= ret;
offset += ret;
}
if (ret)
ret=cb;
}
else
{
FILE_STRUCT* file = (FILE_STRUCT*)_fat_fd;
if ( file->currentPosition != m_dwPosition.LowPart)
_FAT_seek_r(_fat_entry, _fat_fd, m_dwPosition.LowPart, SEEK_SET);
ret = _FAT_read_r(_fat_entry, _fat_fd, (char*)pv, dwBytesToRead);
}
if (ret != cb )
{
int i = ret;
WriteLog("Read err %X (cb:%X) (size:%X pos:%X)", ret, cb, m_dwSize, m_dwPosition.QuadPart);
//ret = cb;
//if (ret > cb)
// return S_FALSE;
}
m_dwPosition.QuadPart += ret;
if (NULL != pcbRead)
*pcbRead = ret;
ReleaseMutex(hLogWrite_Mutex);
return S_OK;
}
HRESULT STDMETHODCALLTYPE CIStreamFile::Write(const void* pv, ULONG cb, ULONG* pcbWritten)
{
was_write = 1;
openFileAttr();
DWORD dwBytesToWrite;
if (cb==0)
{
if (NULL != pcbWritten)
*pcbWritten = 0;
return S_OK;
}
if (m_dwPosition.QuadPart < 0 /*|| m_dwPosition > m_dwSize*/)
dwBytesToWrite = 0;
else
dwBytesToWrite = cb; // < m_dwSize - m_dwPosition ? cb : m_dwSize - m_dwPosition;
//CopyMemory(m_pFile->m_p + m_dwPosition, pv, dwBytesToWrite);
int ret;
if (_isNTFS)
{
ret = ntfs_attr_pwrite( _ntfs_attr, m_dwPosition.QuadPart, dwBytesToWrite, pv);
}
else{
_FAT_seek_r(_fat_entry, _fat_fd, m_dwPosition.LowPart, SEEK_SET);
ret = _FAT_write_r(_fat_entry, _fat_fd, (char*)pv, dwBytesToWrite);
}
if (m_setsize_zero) // after a Write() do setsize if it SetSize(0) were called before.
{
if (_isNTFS)
ret = ntfs_attr_truncate(_ntfs_attr, cb);
else
ret = _FAT_ftruncate_r(_fat_entry, _fat_fd, cb);
m_setsize_zero = 0;
}
LARGE_INTEGER liFileSize = getFileSize();
if ( liFileSize.QuadPart != m_dwSize.QuadPart)
{
m_dwSize.QuadPart = liFileSize.QuadPart;
}
/*if ( _ntfs_inode->data_size != m_dwSize)
m_dwSize = _ntfs_inode->data_size;*/
if (ret < cb)
{
ret = cb;
}
m_dwPosition.QuadPart += ret;
if (NULL != pcbWritten)
*pcbWritten = ret;
return S_OK;
}
// IStream
HRESULT STDMETHODCALLTYPE CIStreamFile::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
{
//CMemoryFileLock lock(m_pFile);
//Note: new position can be more than m_dwSize
//WriteLog("%X Seek %d %X", this, dwOrigin, dlibMove.QuadPart);
switch (dwOrigin)
{
case STREAM_SEEK_CUR:
{
if (dlibMove.QuadPart == 0)
break;
/*if ( !_isNTFS )
{
m_dwPosition = _FAT_seek_r(_fat_entry, _fat_fd, dlibMove.QuadPart, SEEK_CUR);
}else */
m_dwPosition.QuadPart += dlibMove.QuadPart;
break;
}
case STREAM_SEEK_END:
{
/*if ( !_isNTFS )
{
if ( m_dwPosition != getFileSize())
m_dwPosition = _FAT_seek_r(_fat_entry, _fat_fd, dlibMove.QuadPart, SEEK_END);
}else */
LARGE_INTEGER liFileSize;
liFileSize = getFileSize();
m_dwPosition.QuadPart = liFileSize.QuadPart + dlibMove.QuadPart;
break;
}
case STREAM_SEEK_SET:
{
/*if ( !_isNTFS )
{
if ( m_dwPosition != dlibMove.QuadPart)
m_dwPosition = _FAT_seek_r(_fat_entry, _fat_fd, dlibMove.QuadPart, SEEK_SET);
}else */
m_dwPosition.QuadPart = dlibMove.QuadPart;
break;
}
default:
{
return E_FAIL;
}
}
if (NULL != plibNewPosition)
plibNewPosition->QuadPart = m_dwPosition.QuadPart;
return S_OK;
}
// end of CIStreamFile
// ---------------------------------------------------------------
void decrypt(char *buf, u64 sectorNo , DWORD sz);
void encrypt(char *buf, u64 sectorNo , DWORD sz);
char init_crypto(char *key,char *iv);
extern char IV[64];
extern char Key[32];
extern "C" {
// this is Win32 API ReadFile() WriteFile()... replacement with Rohos encryption layer
//
// called from :
// filesys\fat\disc.cpp(43)
// filesys\ntfs\win32_io.c
BOOL crypt_ReadFile(HANDLE hFile, LPVOID buf, DWORD psz, LPDWORD sz, LPOVERLAPPED lpOverlapped)
{
BOOL ret;
DWORD n_pos,n_sz,nr_sz,n__sz;
DWORD *zn,tmp;
DWORD pl,ph,pph,ppl;
u64 pos;
char *n_buf;
ret = 1;
{
n__sz=n_sz = psz;
if (theDiskPartition)
{
ret = theDiskPartition->ReadData( (LPBYTE)buf,n_sz,&theDiskPartition->partOffset);
pos = theDiskPartition->partOffset.QuadPart;
nr_sz = n_sz;
//return ret;
} else
{
pos = filePos.QuadPart;
// ïîçèöèÿ ôàéëà äîëæíà áûòü êðàòíàÿ 512
nr_sz=0;
ret = ReadFile(hFile, buf,n_sz,&nr_sz,lpOverlapped);
}
if (is_encrypted_partition == false)
return ret;
// encryption key was created here - FindBlob_CheckPassword()
decrypt((char *)buf, (pos >> 9), nr_sz); // (>>9 - conver to sector number / 512)
//WriteLog("decrypt %X, %s", LOWORD(pos), (char *)buf);
(*sz)=nr_sz;
}
return ret;
}
BOOL crypt_WriteFile(HANDLE hFile, LPVOID buf, DWORD psz, LPDWORD sz, LPOVERLAPPED lpOverlapped)
{
BOOL ret;
DWORD n_pos,n_sz,nr_sz,n__sz;
DWORD *zn,tmp;
DWORD pl,ph,pph,ppl;
u64 pos;
char *n_buf;
ret = 1;
{
if (theDiskPartition)
{
pos = theDiskPartition->partOffset.QuadPart;
} else
{
pos = filePos.QuadPart;
}
n_buf = (char *)malloc(psz+32);
memcpy(n_buf, buf, psz);
if (is_encrypted_partition)
{
// encryption key was created here - FindBlob_CheckPassword()
encrypt((char *)n_buf, (pos >>9), psz); // >>9 - to convert position to sector number (/512)
}
if (theDiskPartition)
{
ret = theDiskPartition->WriteData( (LPBYTE)n_buf,psz, &theDiskPartition->partOffset);
*sz = psz;
free(n_buf);
return ret;
} else
{
nr_sz=0;
ret = WriteFile(hFile, n_buf,psz, sz, lpOverlapped);
*sz = psz;
}
free(n_buf);
}
return ret;
}
DWORD crypt_SetFilePointer( HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod )
{
DWORD ret;
if (theDiskPartition)
{
if ( dwMoveMethod == FILE_CURRENT && lDistanceToMove == 0)
{
*lpDistanceToMoveHigh = theDiskPartition->partOffset.HighPart;
return theDiskPartition->partOffset.LowPart;
}
LARGE_INTEGER offs2;
offs2.QuadPart=0;
offs2.LowPart = lDistanceToMove;
if (lpDistanceToMoveHigh)
offs2.HighPart = *lpDistanceToMoveHigh;
theDiskPartition->SetFilePointer2(&offs2);
return lDistanceToMove;
}
filePos.QuadPart =0;
filePos.LowPart = lDistanceToMove;
if (lpDistanceToMoveHigh)
filePos.HighPart = *lpDistanceToMoveHigh;
filePos.QuadPart += diskDataOffset.QuadPart;
ret = SetFilePointerEx( hFile, filePos, NULL, dwMoveMethod );
filePos.QuadPart -= diskDataOffset.QuadPart; // return to initial position becase of 'position' should be as real offset in disk image
return ret;
}
BOOL crypt_SetFilePointerEx( HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
{
DWORD ret;
LONG moveHight = liDistanceToMove.HighPart;
ret = crypt_SetFilePointer( hFile, liDistanceToMove.LowPart, &liDistanceToMove.HighPart, dwMoveMethod );
// lpNewFilePointer - in our case it is NULL
if (liDistanceToMove.LowPart == INVALID_SET_FILE_POINTER &&
GetLastError() != NO_ERROR) {
return FALSE;
}
return true;
}
} // extern "C"
//*////////////////////////////////////////////////////////////////////
// Construction/Destruction //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------
CFsys::CFsys()
{
_isNTFS = false;// Âðåìåííî. Äëÿ òåñòèðîâàíèÿ FAT îáðàçîâ
_NTFS_inited = false;
_showHiddenSystemFiles = boxed_app = partition_container = false;
//i_file = NULL;
theDiskPartition = NULL;
partition_ = NULL;
ntfs_volume = NULL;
}
CFsys::~CFsys()
{
}
void CFsys::CloseVolume(bool leave_handle /* = false do not close FileImage handles */)
{
CIStreamFile* i_file =0;
if (partition_ == NULL && ntfs_volume == NULL )
return;
if ( _isNTFS )
{
if (!ntfs_volume)
{
if (leave_handle == false)
{
if (_modification_time.dwHighDateTime)
SetFileTime(ntfsvolume_handle, NULL, NULL, &_modification_time);
CloseHandle(ntfsvolume_handle);
}
return;
}
WriteLog("Close n Volume %X", _modification_time.dwHighDateTime);
#ifdef _USE_BOXEDAPP_SDK
while ( arrayIFiles.GetCount() )
{
i_file = (CIStreamFile*)arrayIFiles.GetAt(0);
BoxedAppSDK_DeleteFileFromVirtualFileSystem(i_file->virtualFileName);
if (i_file->_ntfs_attr ){
ntfs_time_update_flags tuf1=(ntfs_time_update_flags)0;
if (i_file->was_read)
tuf1 = NTFS_UPDATE_ATIME;
if (i_file->was_write)
tuf1 = (ntfs_time_update_flags) (NTFS_UPDATE_ATIME | NTFS_UPDATE_MTIME);
ntfs_inode_update_times(i_file->_ntfs_attr->ni, tuf1);
ntfs_attr_close(i_file->_ntfs_attr);
}
if (i_file->_ntfs_inode);
ntfs_inode_close(i_file->_ntfs_inode);
arrayIFiles.RemoveAt(0);
}
#endif
ntfsvolume_handle = (HANDLE)ntfs_volume->dev->d_handle;
if ( ntfs_umount(ntfs_volume, (BOOL1)1) )
{
WriteLog("error CloseVolume");
}
if (leave_handle == false)
{
//if (_modification_time.dwHighDateTime)
BOOL b = SetFileTime(ntfsvolume_handle, NULL, NULL, &_modification_time);
WriteLog("tm %i %X", b, GetLastError());
CloseHandle(ntfsvolume_handle);
}
ntfs_volume = NULL;
}
else
{
if (!partition_)
{
if (leave_handle == false)
{
if (_modification_time.dwHighDateTime)
SetFileTime(fatvolume_handle, NULL, NULL, &_modification_time);
CloseHandle(fatvolume_handle);
}
return;
}
WriteLog("Close f Volume");
#ifdef _USE_BOXEDAPP_SDK
while ( arrayIFiles.GetCount() )
{
i_file = (CIStreamFile*)arrayIFiles.GetAt(0);
BoxedAppSDK_DeleteFileFromVirtualFileSystem(i_file->virtualFileName);
_FAT_close_r(i_file->_fat_entry,i_file->_fat_fd);
arrayIFiles.RemoveAt(0);
}
#endif
if (partition_)
_FAT_partition_destructor(partition_);
if (leave_handle == false)
{
if (_modification_time.dwHighDateTime)
SetFileTime(fatvolume_handle, NULL, NULL, &_modification_time);
CloseHandle(fatvolume_handle);
}
partition_ = NULL;
}
#ifdef _USE_BOXEDAPP_SDK
if (boxed_app)
{
//BoxedAppSDK_UnhookFunction(hHook__SHAddToRecentDocs);
//if (_recent_docs_policy)
//AfxMessageBox("1");
WriteLog("Boxed App close %d", _recent_docs_policy);
BoxedAppSDK_Exit();
WriteReg(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", "NoRecentDocsHistory", (DWORD)_recent_docs_policy);
boxed_app = false;
TCHAR virtFileName[500];
strcpy(virtFileName, "c:\\");
virtFileName[0] = _curr_folder[0];
strcat(virtFileName, LS(IDS_VIRTUAL_FOLDER));
::RemoveDirectory(virtFileName);
}
#endif
if (theDiskPartition)
{
if (leave_handle == false)
theDiskPartition->CloseDisk();
_pDiskPartitionClass = NULL;
TCHAR rohos_file_path[MAX_PATH];
GetPodNogamiPath(rohos_file_path, false);
_tcscat(rohos_file_path, "0x111.dat");
DeleteFile(rohos_file_path);
}
}
//---------------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////
// Ïðîâåðÿåò ôàéëîâóþ ñèñòåìó îáðàçà è ìàóíòèò åãî ///////////////////
//////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------
int CFsys::OpenVolume(const char *path, bool encrypted_partition/* = true*/, int disk_data_offset_mb/*=0*/)
{
HANDLE hFile;
hLogWrite_Mutex = CreateMutex( NULL, FALSE, "RohosDiskBrowserMutex");
_tcscpy(rohos_file_path, path);
_encrypted_disk = is_encrypted_partition = encrypted_partition;
GetPodNogamiPath(_curr_folder, false );
_pDiskPartitionClass = NULL;
// offset of Disk Data inside of disk blob
diskDataOffset.QuadPart = disk_data_offset_mb;
diskDataOffset.QuadPart *= 1048576; /*1Mb*/
if ( _tcslen(path) <=4 /*just drive letter - partition based encryption */)
{
// open partition
if (theDiskPartition == NULL)
{
theDiskPartition = new CDiskRepartition();
}
else
theDiskPartition->CloseDisk();
_pDiskPartitionClass = theDiskPartition;
theDiskPartition->SetDriveLetter(path[0]);
if ( theDiskPartition->CheckDisk() && theDiskPartition->GetUnpartitionedSpace().QuadPart )
{
// create fake file
GetPodNogamiPath(rohos_file_path, false);
_tcscat(rohos_file_path, "0x111.dat");
HANDLE h = CreateFile(rohos_file_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
CloseHandle(h);
}
else
{
theDiskPartition->CloseDisk();
_pDiskPartitionClass = NULL;
return false;
}
theDiskPartition->partOffset.QuadPart = 0;
LARGE_INTEGER liOffset;
liOffset.QuadPart = disk_data_offset_mb;
liOffset.QuadPart *= 1048576; /*1Mb*/
theDiskPartition->SetDataOffset(&liOffset);
WriteLog("Opening partition container");
}
// îòêðûòü ôàéë è äåøèôðîâàòü ïåðâûå 512 áàéò
hFile = CreateFile(rohos_file_path, // file to open
GENERIC_READ, // open for reading and writing
FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
_modification_time.dwHighDateTime = _modification_time.dwLowDateTime = 0;
if ( IsFileUseSteganos(rohos_file_path) == true)
GetFileTime(hFile, NULL, NULL, &_modification_time);
//
if (hFile != INVALID_HANDLE_VALUE)
{
BYTE buff[512];
DWORD dwRead=0;
// read first 512 byte to detect FS
crypt_SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
int i=0;
for (i =1; i<=100; i++ )
{
crypt_ReadFile(hFile, (LPVOID)buff, 512, &dwRead, NULL);
_isNTFS=false;
// ïðîâåðèòü NTFS èëè FAT ?
if( (buff[3]=='N')&&(buff[3+1]=='T')&&(buff[3+2]=='F')&&(buff[3+3]=='S') )
{
_isNTFS=true;
WriteLog("Found NTFS %X", theDiskPartition->partOffset2.QuadPart);
break;
}
if ((buff[0x36] == 'F') && (buff[0x37] == 'A') && (buff[0x38] == 'T'))
{
WriteLog("Found FAT32 %X", theDiskPartition->partOffset.QuadPart);
//theDiskPartition->partOffset2.QuadPart = theDiskPartition->partOffset.QuadPart;
break;
}
if ((buff[0x52] == 'F') && (buff[0x53] == 'A') && (buff[0x54] == 'T'))
{
// Check for FAT32
WriteLog("Found FAT %X", theDiskPartition->partOffset.QuadPart);
//theDiskPartition->partOffset2.QuadPart = theDiskPartition->partOffset.QuadPart;
break;
}
//WriteLog("%X %X", partOffset.QuadPart, partOffset2.QuadPart);
if (theDiskPartition)
theDiskPartition->partOffset2.QuadPart += 512;
}
CloseHandle(hFile);
if (i == 100)
{
WriteLog("partition not found");
//return 0;
}