-
Notifications
You must be signed in to change notification settings - Fork 11
/
Sysfs12.cpp
executable file
·500 lines (435 loc) · 12.9 KB
/
Sysfs12.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
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
// Sysfs12.cpp: implementation of the CSysfs12 class.
//
//////////////////////////////////////////////////////////////////////
#include "Sysfs12.h"
#include "DiskImgFile.h"
// #ifdef _DEBUG
// #undef THIS_FILE
// static char THIS_FILE[]=__FILE__;
// #define new DEBUG_NEW
// #endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSysfs12::CSysfs12()
{
_pMirrorFile = NULL;
_diskSize = 0;
strcpy(_szcurDir , "\\");
}
CSysfs12::~CSysfs12()
{
if(_pMirrorFile)
{
RefreshFatTable();
_pMirrorFile->CloseImgFile();
delete _pMirrorFile;
_pMirrorFile = NULL;
}
}
BOOL CSysfs12::Release()
{
if (_pMirrorFile)
{
RefreshFatTable();
_pMirrorFile->CloseImgFile();
delete _pMirrorFile;
_pMirrorFile = NULL;
_diskSize = 0;
}
return TRUE;
}
BOOL CSysfs12::Mount(
LPCTSTR lpImageFile, // in, pointer to name of the image file, if file not exist, create it.
LPCTSTR lpRoot, // out,pointer to name of the root
LONGLONG uVDiskSize, // in/out, size of virtual disk
UINT nBlockSize,
UINT nJournalBlkSize,
CFSService::FSType type
)
{
if(_pMirrorFile == NULL)
{
_pMirrorFile = new DiskImgFile();
_pMirrorFile->IniFatType(FAT12_TYPE);
_pMirrorFile->IniFSService(type);
}
else
_pMirrorFile->CloseImgFile();
_pMirrorFile->OpenImgFile(lpImageFile,FAT12_TYPE, uVDiskSize);
_diskSize = uVDiskSize;
return TRUE;
}
BOOL CSysfs12::UnMount(
LPCTSTR lpRoot // pointer to name of the root
)
{
if(_pMirrorFile== NULL)
return TRUE;
RefreshFatTable();
_pMirrorFile->CloseImgFile();
delete _pMirrorFile;
_pMirrorFile = NULL;
return TRUE;
}
BOOL CSysfs12::Format(
UINT uFileSystem, //system id(FAT16:0, FAT32:1)
LPCTSTR lpLabel, // pointer to name of the label
BYTE uSectorPerCluster, // sector of per cluster
UINT nBlockSize
)
{
ASSERT(_pMirrorFile);
_pMirrorFile->FormatImgFile(lpLabel, uFileSystem, _diskSize);
return TRUE;
}
BOOL CSysfs12::FindClose(
HANDLE hFindFile // file search handle
)
{
// return _pMirrorFile->CloseFileEx(hFindFile);// _fileFind.CloseFind();
return _pMirrorFile->FindClose(hFindFile);
}
BOOL CSysfs12::CopyFile(
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
)
{
HANDLE hf = this->CreateFile(lpExistingFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hf == INVALID_HANDLE_VALUE)
return FALSE;
DWORD fSize = 0;
this->GetFileSize(hf, &fSize);
LPBYTE pBuffer = new BYTE[fSize];
DWORD nRead;
if(!ReadFile(hf, pBuffer, fSize, &nRead, NULL))
return FALSE;
FindClose(hf);
hf = this->CreateFile(lpNewFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if(hf == INVALID_HANDLE_VALUE)
return FALSE;
if(!this->WriteFile(hf, pBuffer, fSize, &nRead, NULL))
return FALSE;
FindClose(hf);
return TRUE;
}
BOOL CSysfs12::CreateDirectory(
LPCTSTR lpPathName, // pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor
)
{
return _pMirrorFile->CreateDirectoryEx(lpPathName);
}
HANDLE CSysfs12::CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
)
{
HANDLE hFile = _pMirrorFile->CreateFileEx(lpFileName, dwDesiredAccess,
dwShareMode,
NULL,
dwCreationDisposition,
dwFlagsAndAttributes,
NULL);
return hFile;
}
BOOL CSysfs12::DeleteFile(
LPCTSTR lpFileName // pointer to name of file to delete
)
{
return _pMirrorFile->DeleteFileEx(lpFileName);
//return TRUE;
}
HANDLE CSysfs12::FindFirstFile(
LPCTSTR lpFileName, // pointer to name of file to search for
LPWIN32_FIND_DATA lpFindFileData // pointer to returned information
)
{
char szFullPath[MAX_PATH];
if (lpFileName == NULL )
{
strcpy(szFullPath , _szcurDir);
}
else if (lpFileName[0] != '\\')
{
strcpy(szFullPath , _szcurDir);
//sprintf(szFullPath , "%s%s" , _szcurDir , lpFileName);
}
else
{
strcpy(szFullPath , lpFileName);
}
HANDLE hf = _pMirrorFile->FindFirstFile(szFullPath, lpFindFileData);
return hf;
}
/*
HANDLE CSysfs12::FindFirstFileEx(
LPCTSTR lpFileName, // pointer to the name of the file to search for
FINDEX_INFO_LEVELS fInfoLevelId, // information level of the returned data
LPVOID lpFindFileData, // pointer to the returned information
FINDEX_SEARCH_OPS fSearchOp, // type of filtering to perform
LPVOID lpSearchFilter, // pointer to search criteria
DWORD dwAdditionalFlags // additional search control flags
)
{
return NULL;
}
*/
BOOL CSysfs12::FindNextFile(
HANDLE hFindFile, // handle to search
LPWIN32_FIND_DATA lpFindFileData // pointer to structure for data on found file
)
{
return _pMirrorFile->FindNextFile(hFindFile, lpFindFileData);
//return TRUE;
}
DWORD CSysfs12::GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
)
{
_pMirrorFile->GetCurrentDirectory(lpBuffer, nBufferLength);
return 1;
}
BOOL CSysfs12::GetDiskFreeSpace(
LPTSTR lpRootPathName, // pointer to root path
LPDWORD lpSectorsPerCluster, // pointer to sectors per cluster
LPDWORD lpBytesPerSector, // pointer to bytes per sector
LPDWORD lpNumberOfFreeClusters, // pointer to number of free clusters
LPDWORD lpTotalNumberOfClusters // pointer to total number of clusters
)
{
strcpy(lpRootPathName ,_pMirrorFile->RootPath());
*lpSectorsPerCluster = _pMirrorFile->SectorsPerCluster();
*lpBytesPerSector = _pMirrorFile->BytesPerSector();
*lpNumberOfFreeClusters = _pMirrorFile->NumberOfFreeClusters();
*lpTotalNumberOfClusters = _pMirrorFile->TotalNumberOfClusters();
return TRUE;
}
DWORD CSysfs12::GetFileSize(
HANDLE hFile, // handle of file to get size of
LPDWORD lpFileSizeHigh // pointer to high-order word for file size
)
{
ImgFileHandle* handle= (ImgFileHandle*)(hFile);
if(handle)
{
if (lpFileSizeHigh) *lpFileSizeHigh = 0;
return handle->_fileTab.DIR_FileSize;
}
//return *lpFileSizeHigh;
return 0;
}
DWORD CSysfs12::GetFullPathName(
LPCTSTR lpFileName, // pointer to name of file to find path for
DWORD nBufferLength, // size, in characters, of path buffer
LPTSTR lpBuffer, // pointer to path buffer
LPTSTR *lpFilePart // pointer to filename in path
)
{
return 0;
}
BOOL CSysfs12::MoveFile(
LPCTSTR lpExistingFileName, // pointer to the name of the existing file
LPCTSTR lpNewFileName // pointer to the new name for the file
)
{
_pMirrorFile->MoveFileEx(lpExistingFileName, lpNewFileName);
return TRUE;
}
BOOL CSysfs12::ReadFile(
HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // pointer to buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
LPOVERLAPPED lpOverlapped // pointer to structure for data
)
{
return _pMirrorFile->ReadFileEx(hFile, (LPBYTE)lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead);
}
BOOL CSysfs12::RemoveDirectory(
LPCTSTR lpPathName // pointer to directory to remove
)
{
_pMirrorFile->DeleteDirectoryEx(lpPathName);
return TRUE;
}
BOOL CSysfs12::SetCurrentDirectory(
LPCTSTR lpPathName // pointer to name of new current directory
)
{
char szPath[MAX_PATH];
strcpy(szPath , lpPathName);
int len = strlen(szPath);
if (len>1)
{
if (szPath[len-1] == '\\')
szPath[len-1] = 0;
}
if (_pMirrorFile->SetCurrentDirectory(szPath))
{
strcpy(_szcurDir , szPath);
return TRUE;
}
else
{
strcpy(_szcurDir , "\\");
return FALSE;
}
}
DWORD CSysfs12::SetFilePointer(
HANDLE hFile, // handle of file
LONG lDistanceToMove, // number of bytes to move file pointer
PLONG lpDistanceToMoveHigh, // pointer to high-order DWORD of distance to move
DWORD dwMoveMethod // how to move
)
{
return _pMirrorFile->SetFilePointerEx(hFile, lDistanceToMove, lpDistanceToMoveHigh, dwMoveMethod);
}
BOOL CSysfs12::SetVolumeLabel(
LPCTSTR lpRootPathName, // pointer to name of root directory for volume
LPCTSTR lpVolumeName // name for the volume
)
{
_pMirrorFile->SetVolLabel(lpVolumeName);
return TRUE;
}
BOOL CSysfs12::WriteFile(
HANDLE hFile, // handle to file to write to
LPCVOID lpBuffer, // pointer to data to write to file
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O
)
{
return _pMirrorFile->WriteFileEx(hFile, (LPBYTE)lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten);
}
BOOL CSysfs12::LockFile(
HANDLE hFile, // handle of file to lock
DWORD dwFileOffsetLow, // low-order word of lock region offset
DWORD dwFileOffsetHigh, // high-order word of lock region offset
DWORD nNumberOfBytesToLockLow, // low-order word of length to lock
DWORD nNumberOfBytesToLockHigh // high-order word of length to lock
)
{
return TRUE;
}
BOOL CSysfs12::UnlockFile(
HANDLE hFile, // handle of file to unlock
DWORD dwFileOffsetLow, // low-order word of lock region offset
DWORD dwFileOffsetHigh, // high-order word of lock region offset
DWORD nNumberOfBytesToUnlockLow, // low-order word of length to unlock
DWORD nNumberOfBytesToUnlockHigh // high-order word of length to unlock
)
{
return TRUE;
}
BOOL CSysfs12::CreateDirectoryEx( // In order to increase speed of creating directory in current directory
LPCTSTR lpPathName, // pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor
)
{
return _pMirrorFile->ImgCreateDirectory(lpPathName);
}
BOOL CSysfs12::PathFileExist(LPCTSTR pszPath)
{
HANDLE hfile = CreateFile(pszPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if ( (HANDLE)-1 == hfile) return FALSE;
else
{
CloseFile(hfile);
return TRUE;
}
}
BOOL CSysfs12::InsertFile(IN const LPCSTR lpFileName,
IN PBYTE pBuffer,
IN const WORD bFileAttr,
IN const UINT nSize,
HANDLE& hFile
)
{
DWORD dwNumerOfFreeCluster,dwBytesPerSector,dwSectorPerCluster
,dwTotalNumberOfClusters;
char chRoot[256];
if(GetDiskFreeSpace(chRoot,&dwSectorPerCluster,&dwBytesPerSector,
&dwNumerOfFreeCluster,&dwTotalNumberOfClusters)){
if(dwNumerOfFreeCluster*dwSectorPerCluster*dwBytesPerSector<nSize)
return FALSE;
}
//return _pMirrorFile->ImgCreateFile(lpFileName, pBuffer, (BYTE)bFileAttr, nSize, hFile);
char szFullPath[MAX_PATH];
if (lpFileName[0] != '\\')
{
sprintf(szFullPath , "%s\\%s" , _szcurDir , lpFileName);
}
else
{
strcpy(szFullPath , lpFileName);
}
if (PathFileExist(szFullPath))
{
DeleteFile(szFullPath);
}
HANDLE h = CreateFile(szFullPath, GENERIC_WRITE|GENERIC_READ, NULL, NULL, CREATE_ALWAYS, ATTR_ARCHIVE, NULL);
if ( (HANDLE)-1 == h)
{
//AfxMessageBox("CreateFile Fail");
return FALSE;
}
DWORD nWritten = 0;
if (!WriteFile(h , pBuffer , nSize , &nWritten , NULL))
return FALSE;
//CloseFile(h);
hFile = h;
return TRUE;
}
//
BOOL CSysfs12::GetVolLabel(LPSTR lpLabel)
{
return _pMirrorFile->GetVolLabel(lpLabel);
}
BOOL CSysfs12::CloseFile(HANDLE handle)
{
return _pMirrorFile->CloseFileEx(handle);
}
void* CSysfs12::MirrorFile()
{
return _pMirrorFile;
}
BOOL CSysfs12::IsFoulder(Flag fAttr)
{
if(IsFile(fAttr))
return FALSE;
return ((fAttr& (ATTR_DIRECTORY | ATTR_VOLUME_ID)) == ATTR_DIRECTORY);
}
BOOL CSysfs12::IsVolLabel(Flag fAttr)
{
BOOL isVol = FALSE;
if(IsLongDir(fAttr))
return FALSE;
else if(IsFile(fAttr))
return FALSE;
else if((fAttr& (ATTR_DIRECTORY | ATTR_VOLUME_ID)) == ATTR_DIRECTORY)
return FALSE;
else if((fAttr& (ATTR_DIRECTORY | ATTR_VOLUME_ID)) == ATTR_VOLUME_ID)
return TRUE;
return FALSE;
}
BOOL CSysfs12::RenameFile(LPCTSTR lpSrcFileName, LPCTSTR lpNewFileName)
{
return _pMirrorFile->RenameFileEx(lpSrcFileName, lpNewFileName);
}
BOOL CSysfs12::RefreshFatTable()
{
return _pMirrorFile->RefreshFatTable();
}
// BOOL CSysfs12::WriteFile()
// {
// return FALSE;
// }