-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDiskImgFile.cpp
More file actions
executable file
·7029 lines (6182 loc) · 180 KB
/
DiskImgFile.cpp
File metadata and controls
executable file
·7029 lines (6182 loc) · 180 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
// DiskImgFile.cpp: implementation of the DiskImgFile class.
//
//////////////////////////////////////////////////////////////////////
#include "DiskImgFile.h"
#include <string.h>
#ifdef _MAC_UNIX
#include <CoreFoundation/CoreFoundation.h>
#else
//#include <objbase.h>
//#include <afxwin.h>
//#include <mbstring.h>
#endif
// #ifdef _DEBUG
// #undef THIS_FILE
// static char THIS_FILE[]=__FILE__;
// #define new DEBUG_NEW
// #endif
extern void WINAPI run_log(char* msg);
BOOL 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 IsFile(Flag fAttr)
{
return ((fAttr& (ATTR_DIRECTORY | ATTR_VOLUME_ID)) == 0x00);
}
BOOL IsLongDir(Flag fAttr)
{
return ((fAttr& ATTR_LONG_NAME_MASK) == ATTR_LONG_NAME);
}
BOOL IsFoulder(Flag fAttr)
{
if(IsFile(fAttr))
return FALSE;
return ((fAttr& (ATTR_DIRECTORY | ATTR_VOLUME_ID)) == ATTR_DIRECTORY);
}
BOOL IsReadOnly(Flag fAttr)
{
return (fAttr & ATTR_READ_ONLY == ATTR_READ_ONLY);
}
BOOL IsHide(Flag fAttr)
{
return (fAttr& ATTR_HIDDEN == ATTR_HIDDEN);
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern DskszToSecperClus DskTableFAT16[];
DiskImgFile::DiskImgFile()
{
_lpImgFileName = new TCHAR[MAX_PATH];
memset(_lpImgFileName, 0x00, MAX_PATH * sizeof(TCHAR));
_imgSpace = DISK_16M;
_imgContent = NULL;
_curDirectory.clear();
_hFile = NULL;
#ifdef _MAC_UNIX
m_CodePage = 0;
#else
m_CodePage = CP_ACP;
#endif
}
DiskImgFile::~DiskImgFile()
{
_curDirectory.clear();
_fats.resize(0);
if(_imgContent)
{
delete _imgContent;
}
if(_lpImgFileName)
{
delete _lpImgFileName;
}
}
unsigned char DiskImgFile::ChkSum (LPCSTR pFcbName)
{
short FcbNameLen;
unsigned char Sum;
Sum = 0;
for (FcbNameLen=11; FcbNameLen!=0; FcbNameLen--)
{
// NOTE: The operation is an unsigned char rotate right
Sum = ((Sum & 1) ? 0x80 : 0) + (Sum >> 1) + *pFcbName++;
}
return (Sum);
}
BOOL DiskImgFile::IsExit(LPCTSTR lptName) // 判断目录(文件)名在当前目录下是否存在
{
int nIndex = 0;
Fat_Directory dir;
TCHAR lpLongName[MAX_PATH];
while(1)
{
memset(lpLongName, 0x00, MAX_PATH * sizeof(TCHAR));
if(!GetDirectoryTabEx(dir, lpLongName, nIndex))
{
break;
}
if(strcmp(lpLongName, lptName) == 0)
{
return TRUE;
}
}
return FALSE;
}
BOOL DiskImgFile::GetShortNameNumicTail(LPCSTR basicName, char& tailNumic)
{
/* char firstName[8];
char cmpName[8];
memcpy(firstName, basicName, 6);
firstName[6] = '~';
firstName[7] = '\0';
cmpName[7]='\0';
int nIndex = 0;
Fat_Directory dir;
char cMaxNumic = '0';
while(1)
{
if(!GetDirectoryTab(dir, nIndex))
{
tailNumic = (char)0xff;
break;
}
memcpy(cmpName, dir.DIR_Name, 7);
if(strcmp(firstName, cmpName) == 0)
{
if(cMaxNumic < dir.DIR_Name[7])
{
cMaxNumic = dir.DIR_Name[7];
}
}
}
if(cMaxNumic == '0')
tailNumic = ' ';
else
tailNumic = cMaxNumic + 1;
return TRUE;
*/
char firstName[8];
char cmpName[8];
memcpy(firstName, basicName, 6);
firstName[6] = '~';
firstName[7] = '\0';
cmpName[7]='\0';
int nIndex = 0;
Fat_Directory dir;
char cMaxNumic = '0';
BOOL hasSameName = FALSE;
BYTE hasNumic[4];
memset(hasNumic , 0 , 4);
BYTE bTemp;
while(1)
{
if(!GetDirectoryTab(dir, nIndex))
{
tailNumic = (char)0xff;
break;
}
memcpy(cmpName, dir.DIR_Name, 7);
if(strcmp(firstName, cmpName) == 0)
{
hasSameName = TRUE;
//短命序号为1,2,3或4时, hasNumic[i]赋值 1
bTemp = dir.DIR_Name[7] - 0x30 - 1;
if( bTemp >= 0 && bTemp <= 3)
hasNumic[bTemp] = 1;
}
}
if (hasSameName)
{
for(int i=0;i<4;i++)
{
if(hasNumic[i] == 0)//hasNumic[i]为0时,证明当前序号没有用到,返回当前序号;
{
cMaxNumic += (i+1);
break;
}
}
}
if(!hasSameName)
tailNumic = ' ';//没有相同的名字
else
{
if(cMaxNumic == '0')//序号1,2,3,4都用过了
tailNumic = (char)0xff;
else
tailNumic = cMaxNumic;
}
return TRUE;
}
INT DiskImgFile::CalcNeedTabCount(LPCTSTR lpName) // 根据名字长度计算需要目录项目数目
{
#ifdef _UNICODE
BOOL bUse = TRUE;
int len = WideCharToMultiByte(CP_ACP, 0, lpName, -1, NULL, 0, "_", &bUse);
char* pch = new char[len];
ZeroMemory(pch, len);
bUse = TRUE;
WideCharToMultiByte(CP_ACP, 0, lpName, -1, pch, len, "_", &bUse);
int wLen = wcslen(lpName);
if(len > wLen)
{
delete[] pch;
return ((wLen + 25) / 13);
}
while (_istspace(*pch))
//pch = _tcsinc(pch);
pch = (char*)_mbsinc((unsigned char*)pch);//modify by joelee 2008-03-11
int nLen =strlen(pch);
LPSTR pEnd = pch + nLen;
LPSTR pCur = pEnd;
while (_istspace(*pCur) && (pCur > pch))
{
*pCur = '\0';
pCur = pCur-1 ;
}
//_tcsupr(pch);
_strupr(pch);//modify by joelee 2008-03-11
nLen = strlen(pch);
LPSTR pExt = new char[MAX_PATH];
memset(pExt, 0x00, MAX_PATH);
GetRighStrByFindA(pExt, pch, '.', -1, "");
if(nLen == 0)
{
delete[] pch;
delete[] pExt;
return 0;
}
if((strlen(pExt) == 0) && (nLen <= 8) )
{
delete[] pch;
delete[] pExt;
return /*1*/2;
}
else if((strlen(pExt) >= 1) && (strlen(pExt) <=3) && (nLen <= 8) )
{
delete[] pch;
delete[] pExt;
return /*1*/2;
}
int nCount = (nLen + 25) / 13;
delete[] pch;
delete[] pExt;
//joelee 2008-08-08
if (nCount == 1) nCount = 2;
return nCount;
#else
int nLen = strlen(lpName);
LPSTR pch = new char[nLen + 1];
int nWChLen;
LPWSTR pLongName = new WCHAR[nLen+1];
memset(pLongName, 0x00, (nLen+1) * 2);
nWChLen = ::MultiByteToWideChar(m_CodePage, 0, lpName, nLen, pLongName, nLen);
// printf("---------diskimgfile nLen=%d,lpName=%s,pLongName=",nLen,lpName,pLongName);
// char* p = (char*)pLongName;
// for(int i=0;i<(nLen+1)*2;i++)
// {
// printf("%c", p[i]);
// }
// printf("\n");
//wbt mod
if(nWChLen != nLen) // 非英文字体
{
//printf("nWChLen=%d != nLen=%d !! return %d\n",nWChLen, (nWChLen + 25) / 13);
delete[] pLongName;
delete[] pch;
return ((nWChLen + 25) / 13);
}
memcpy(pch, lpName, nLen);
pch[nLen] = '\0';
while (_istspace(*pch))
pch = (char*)_mbsinc((const unsigned char*)pch);
nLen =strlen(pch);
LPSTR pEnd = pch + nLen;
LPSTR pCur = pEnd;
while (_istspace(*pCur) && (pCur > pch))
{
*pCur = '\0';
pCur = pCur-1 ;
}
_mbsupr((char*)pch);
nLen = strlen(pch);
LPSTR pExt = new char[MAX_PATH];
memset(pExt, 0x00, MAX_PATH);
GetRighStrByFind(pExt, pch, '.', -1, "");
if(nLen == 0)
{
delete[] pLongName;
delete[] pch;
delete[] pExt;
return 0;
}
if((strlen(pExt) == 0) && (nLen <= 8) && (strcmp(lpName, pch)== 0))
{
delete[] pLongName;
delete[] pch;
delete[] pExt;
return /*1*/2;//一定创建长文件名
}
else if((strlen(pExt) >= 1) && (strlen(pExt) <=3) && (nLen <= 8) && (strcmp(lpName, pch) == 0))
{
delete[] pLongName;
delete[] pch;
delete[] pExt;
return /*1*/2; //一定创建长文件名
}
int nCount = (nLen + 25) / 13;
delete[] pLongName;
delete[] pch;
delete[] pExt;
//Add by joelee 2008-08-08
if (nCount == 1) nCount ++;//一定创建长文件名
return nCount;
#endif
}
/*
短名生成原则;
(1).取长文件名的前6 个字符加上"~1"形成短文件名,扩展名不变。
(2).如果已存在这个文件名,则符号"~"后的数字递增,直到5。
(3).如果文件名中"~"后面的数字达到5,则短文件名只使用长文件名的
前两个字母。通过 数学操纵( CRC校验 ) 长文件名的剩余字母生成短文件名的后四个字母,
然后加后缀"~1"(如果有必要,或是其他数字以避免重复的文件名)。
*/
BOOL DiskImgFile::GenerateShortName(LPCTSTR lpName, LPSTR lpShortName)
{
#ifdef _UNICODE
int nLen = wcslen(lpName);
BOOL bUse = TRUE;
int l = WideCharToMultiByte(CP_ACP, 0, lpName, -1, NULL, 0, "_", &bUse);
LPSTR pch = new char[MAX_PATH];
ZeroMemory(pch, MAX_PATH);
bUse = TRUE;
WideCharToMultiByte(CP_ACP, 0, lpName, -1, pch, l, "_", &bUse);
#else
int nLen = strlen(lpName);
LPSTR pch = new char[MAX_PATH];
memset(pch, 0x00, MAX_PATH);
memcpy(pch, lpName, nLen);
#endif
//////////////////////////////////////////////////////////////////////////
// 变大写
_mbsupr((char*)pch);
int pclen = strlen(pch);
LPSTR pstrSource = pch;
LPSTR pstrDest = pch;
//LPTSTR pstrEnd = pch + pclen;
char* pstrEnd = pch + pclen;//modify by joelee 2008-03-11
while (pstrSource < pstrEnd)
{
if(*pstrSource == '+'
|| *pstrSource == ','
|| *pstrSource == ';'
|| *pstrSource == '='
|| *pstrSource == '['
|| *pstrSource == ']'
)
*pstrSource = '_';
pstrSource++;
}
LPSTR pExt = new char[4];
memset(pExt, 0x00, 4);
GetRighStrByFindA(pExt, pch, '.', 3, " ");
//分析后缀名,名字中间不能有空格
//Joelee 2008-07-24
char tempExt[4];
char p;
memcpy(tempExt , pExt , 4);
int i,j;
for (i=0,j=0;i<3;i++)
{
p = tempExt[i];
if (p != ' ')
{
pExt[j++] = p;
}
}
for (i=j;i<3;i++)
{
pExt[i] = ' ';
}
//////////////////////////////////////////////////////////////////////////
// 分析名字
LPSTR pFileName = NULL;
if(strlen(pExt) == 0) // 表示没有后缀名
{
strcat(pExt," ");
pFileName = pch;
}
else
{
pFileName = GetLeftStrA(pch, '.', FALSE);
}
if(pFileName == NULL)
{
delete[] pch;
delete[] pExt;
return NULL;
}
//读取文件名最后一个字符
char lastChar;
LPSTR tempFileName = pFileName;
nLen = strlen(pFileName);
for(int m = 0; m < nLen; ++m)
{
if (m == nLen-1)
{
lastChar = *tempFileName;
}
++tempFileName;
}
//////////////////////////////////////////////////////////////////////////
// 清除空格和.
if (nLen > 8)
{
RemoveCharA(pFileName, ' ');
}
RemoveCharA(pFileName, '.');
//////////////////////////////////////////////////////////////////////////
// 查找当前目录相同名字目录(长名)
if(IsExit(lpName))
{
if( pFileName != pch)
delete pFileName;
delete[] pch;
delete[] pExt;
//wbt dll
//printmsg("exit the same foulder or file name!");
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 生成短名
//nLen = strlen(pFileName);
char* pChar = NULL;
char* CRCSrcBuf = new char[nLen];
memcpy(CRCSrcBuf , pFileName , nLen);
if(nLen < 8)
{
for(int i = 0; i < 8-nLen; i++)
{
strcat(pFileName, " ");
}
}
if(nLen >8)
pFileName[8] = '\0';
char chNumic;
GetShortNameNumicTail(pFileName, chNumic);
if( chNumic == (char)0xff )
{
char CRCStr[5];
pChar = CRCSrcBuf;
pChar += 2;//取第二个字符以后的字符串
GetCRCStr(pChar , nLen-2 , CRCStr);//获取CRC校验后个字符
pChar = pFileName;
pChar+=2;
memcpy(pChar , CRCStr , 4);//从pFileName[2]开始 , 复制CRCStr , 长度4
GetShortNameNumicTail(pFileName, chNumic);
}
if(CRCSrcBuf)
delete[] CRCSrcBuf;
if(chNumic != ' ' )
{
pFileName[7]= chNumic;
pFileName[6]= '~';
pFileName[8]='\0';
}
else
{
if(nLen > 8 || lastChar == 0x20)
{
pFileName[7]= '1';
pFileName[6]= '~';
pFileName[8]='\0';
}
}
strcat(pFileName, pExt);
strcat(lpShortName, pFileName);
if( pFileName != pch)
delete pFileName;
delete[] pch;
delete[] pExt;
return TRUE;
}
UINT DiskImgFile::RelatAddrToCluster(IN UINT uRetAddr) // 映射函数从实际实际偏移位置到簇
{
//_startClusterAddr = _imgBpb.BPB_BytsPerSec + _imgBpb.BPB_FATSz16*_imgBpb.BPB_BytsPerSec*2 - 32*_imgBpb.BPB_BytsPerSec;
UINT relAddrk = uRetAddr - _stOfClusterAddr;
return (relAddrk / _imgBpb.BPB_BytsPerSec/_imgBpb.BPB_SecPerClus + 2);
}
UINT DiskImgFile::ClusterToRelatAddr(IN UINT uCluNm) // 映射函数从簇到实际偏移位置
{
if(uCluNm == 0)
return _stOfRootAddr;
UINT relAddr = (uCluNm - 2 )* _imgBpb.BPB_BytsPerSec * _imgBpb.BPB_SecPerClus;
return relAddr + _stOfClusterAddr;
}
BOOL DiskImgFile::ImgCreateDirectory(IN LPCTSTR lptDirName) // 建立目录函数 ldDirName 为长名文件夹
{
int ns = _curDirectory.size();
int nNeed = this->CalcNeedTabCount(lptDirName); // 需要存放的结构的个数
UINT len = strlen(lptDirName);
LPWSTR pLongName = new WCHAR[len + 1];
#ifdef _UNICODE
wcscpy(pLongName , lptDirName);
int nl = wcslen(pLongName);
#else
memset(pLongName, 0x00, (len + 1) * 2);
int nl = ::MultiByteToWideChar(m_CodePage, 0, lptDirName, len, pLongName, len+1);
#endif
LPSTR pName = new char[MAX_PATH];
memset(pName, 0x00, MAX_PATH);
if(!GenerateShortName(lptDirName, pName))// new char[11];
{
delete[] pLongName;
delete[] pName;
//wbt del
//printmsg("Generate short name faile!");
return FALSE;
}
len = strlen(pName);
ASSERT(len <= 12);
DWORD nRead;
DWORD nWrite;
BYTE chkFlag = ChkSum(pName);
if(ns == 0) // 在根目录下创建字目录
{
Fat_Directory dir;
Fat_DirectoryRW dirRw;
INT nIndex = -1;
//////////////////////////////////////////////////////////////////////////
// 开始建立目录项
memset(&dirRw, 0x00, sizeof(Fat_DirectoryRW));
memset(&dir, 0x00, sizeof(Fat_Directory));
SetDiskFilePointer(NULL, _stOfRootAddr, NULL, (DWORD)FILE_BEGIN);
int i = 0;
for( i = 0; i < ROOTENTCNT; i++)
{
ReadDiskFile(NULL, &dirRw, sizeof(Fat_DirectoryRW), &nRead, NULL);
if(nRead != sizeof(Fat_DirectoryRW))
{
//wbt del
//printmsg("Error In Create RootDirectroy --Read DirRW Error!");
return FALSE;
}
if(dirRw.DIR_Name[0] == 0x00 || ((dirRw.DIR_Name[0] == (char)0xE5) && nNeed == 1)) // 判断是否为空项
{
nIndex = i;
break;
}
}
//if(nIndex == -1)
if(nIndex == -1 || (nIndex + nNeed > ROOTENTCNT) )
{
//wbt del
//printmsg("This Directroy is full!");
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 处理长名目录项目
if(nNeed > 1)
{
Fat_LongDirectory longDir;
memset(&longDir, 0x00, 32);
longDir.LDIR_Attr = ATTR_LONG_NAME;
longDir.LDIR_Chksum = chkFlag;
longDir.LDIR_FstClusLO = 0;
longDir.LDIR_Ord = LAST_LONG_ENTRY|(BYTE)(nNeed -1);
longDir.LDIR_Type = 0;
int npos = (nNeed - 2) * 13;
memset(longDir.LDIR_Name1, 0xff, 26);
memcpy(longDir.LDIR_Name1, pLongName + npos, (nl - npos)*2); // 将剩余的名字全部拷贝进去
if((nl - npos) < 13)
memset(longDir.LDIR_Name1 + (nl - npos), 0x00, 2);
RwInfoFromLongDirInfo(dirRw, longDir);
SetDiskFilePointer(NULL, _stOfRootAddr + 32 * nIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
//wbt del
//printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
nIndex ++;
}
for(i = nNeed - 2; i >0 ; i--)
{
Fat_LongDirectory longDir;
memset(&longDir, 0x00, 32);
longDir.LDIR_Attr = ATTR_LONG_NAME;
longDir.LDIR_Chksum = chkFlag;
longDir.LDIR_FstClusLO = 0;
longDir.LDIR_Ord = (BYTE)i;
longDir.LDIR_Type = 0;
// int nl = strlen(lpDirName);
int npos = (i - 1) * 13;
memset(longDir.LDIR_Name1, 0xff, 26);
memcpy(longDir.LDIR_Name1, pLongName + npos, 13*2); // 将剩余的名字全部拷贝进去
RwInfoFromLongDirInfo(dirRw, longDir);
// SetDiskFilePointer(NULL, _stOfRootAddr + 32 * nIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
delete[] pName;
//wbt del
//printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
nIndex ++;
}
//////////////////////////////////////////////////////////////////////////
memcpy(&dir.DIR_Name, pName, 11);
dir.DIR_Attr = 0x10;
// dir.DIR_WrtDateTime = CTime::GetCurrentTime();
UINT clus = GetFirstFreeClusNum();
ASSERT(clus >= 2);
dir.DIR_FstClusHI = 0;
dir.DIR_FstClusLO = clus;
::GetSystemTime(&dir.DIR_WrtDateTime);
::GetSystemTime(&dir.DIR_CrtDateTime);
::GetSystemTime(&dir.DIR_LstAcceDate);
/*
dir.DIR_WrtDateTime = CTime::GetCurrentTime();
dir.DIR_CrtDateTime = CTime::GetCurrentTime();
dir.DIR_LstAcceDate = CTime::GetCurrentTime();
*/
SetClus(clus, m_EOC_STATUS);
RwInfoFromDirInfo(dirRw, dir);
SetDiskFilePointer(NULL, _stOfRootAddr + 32 * nIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
//wbt del
//printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 初始化分配得到簇的值
InitializeClus(clus);
//////////////////////////////////////////////////////////////////////////
// 建立 .目录
UINT pos = ClusterToRelatAddr(clus);
memcpy(dir.DIR_Name, ". ", 8);
::GetSystemTime(&dir.DIR_WrtDateTime);
::GetSystemTime(&dir.DIR_CrtDateTime);
::GetSystemTime(&dir.DIR_LstAcceDate);
/*
dir.DIR_WrtDateTime = CTime::GetCurrentTime();
dir.DIR_CrtDateTime = CTime::GetCurrentTime();
dir.DIR_LstAcceDate = CTime::GetCurrentTime();
*/
dir.DIR_FstClusHI = 0;
dir.DIR_FstClusLO = clus;
SetDiskFilePointer(NULL, pos, NULL, (DWORD)FILE_BEGIN);
RwInfoFromDirInfo(dirRw, dir);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
//wbt del
//printmsg("Error In create SubDirectory -- write dir Struct");
return FALSE;
}
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 建立 ..目录
memcpy(dir.DIR_Name, ".. ", 8);
dir.DIR_FstClusHI = 0;
dir.DIR_FstClusLO = 0;
::GetSystemTime(&dir.DIR_WrtDateTime);
::GetSystemTime(&dir.DIR_CrtDateTime);
::GetSystemTime(&dir.DIR_LstAcceDate);
/*
dir.DIR_WrtDateTime = CTime::GetCurrentTime();
dir.DIR_CrtDateTime = CTime::GetCurrentTime();
dir.DIR_LstAcceDate = CTime::GetCurrentTime();
*/
RwInfoFromDirInfo(dirRw, dir);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
printmsg("Error In create SubDirectory -- write dir Struct");
return FALSE;
}
SetDiskFilePointer(NULL, 0, NULL, FILE_BEGIN);
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
}
else //在子目录下创建子目录
{
//////////////////////////////////////////////////////////////////////////
// 建立新目录项
Fat_Directory curD = _curDirectory.at(_curDirectory.size() - 1);
UINT dirAdd = this->ClusterToRelatAddr(curD.DIR_FstClusLO);
Fat_Directory dir;
Fat_DirectoryRW dirRw;
INT nIndex = -1;
memset(&dirRw, 0x00, sizeof(Fat_DirectoryRW));
memset(&dir, 0x00, sizeof(Fat_Directory));
SetDiskFilePointer(NULL, dirAdd, NULL, (DWORD)FILE_BEGIN);
UINT maxDircnt = BYSPERSEC * _imgBpb.BPB_SecPerClus / 32;
WORD cClus = curD.DIR_FstClusLO;
int nDirPos = 0; // 记录目录的相对位置 放置在Fat_Directory.DIR_PosIndex中
while(1)
{
for(int i = 0; i < maxDircnt; i++)
{
ReadDiskFile(NULL, &dirRw, sizeof(Fat_DirectoryRW), &nRead, NULL);
if(nRead != sizeof(Fat_DirectoryRW))
{
printmsg("Error In Create RootDirectroy --Read DirRW Error!");
return FALSE;
}
if(dirRw.DIR_Name[0] == 0x00 || ((dirRw.DIR_Name[0] == (char)0xE5) && nNeed == 1)) // 判断是否为空项
{
nIndex = i;
break;
}
nDirPos++;
}
if(nIndex != -1)
break;
DWORD dwClus = _fats.at(cClus);
if(dwClus == m_EOC_STATUS)
break;
cClus = dwClus;
dirAdd = ClusterToRelatAddr(cClus);
SetDiskFilePointer(NULL, dirAdd, NULL, (DWORD)FILE_BEGIN);
}
if(nIndex == -1)
{
UINT c = GetFirstFreeClusNum();
SetClus(cClus, c);
SetClus(c, m_EOC_STATUS);
cClus = c;
InitializeClus(c);
nIndex = nDirPos;
dirAdd = ClusterToRelatAddr(c);
}
//////////////////////////////////////////////////////////////////////////
// 处理长名目录项目
int retIndex = nIndex%maxDircnt;
if(nNeed > 1)
{
Fat_LongDirectory longDir;
memset(&longDir, 0x00, 32);
longDir.LDIR_Attr = ATTR_LONG_NAME;
longDir.LDIR_Chksum = chkFlag;
longDir.LDIR_FstClusLO = 0;
longDir.LDIR_Ord = LAST_LONG_ENTRY|(BYTE)(nNeed - 1);
longDir.LDIR_Type = 0;
// int nl = strlen(lpDirName);
int npos = (nNeed - 2) * 13;
memset(longDir.LDIR_Name1, 0xff, 26);
memcpy(longDir.LDIR_Name1, pLongName + npos, (nl - npos)*2); // 将剩余的名字全部拷贝进去
if((nl - npos) < 13)
memset(longDir.LDIR_Name1 + (nl - npos), 0x00, 2);
RwInfoFromLongDirInfo(dirRw, longDir);
SetDiskFilePointer(NULL, dirAdd + 32 * retIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
retIndex++;
nIndex ++;
if(retIndex >= maxDircnt)
{
UINT c = GetFirstFreeClusNum();
SetClus(cClus, c);
SetClus(c, m_EOC_STATUS);
InitializeClus(c);
retIndex = 0;
dirAdd = ClusterToRelatAddr(c);
}
}
for( int i = nNeed - 2; i >0 ; i--)
{
Fat_LongDirectory longDir;
memset(&longDir, 0x00, 32);
longDir.LDIR_Attr = ATTR_LONG_NAME;
longDir.LDIR_Chksum = chkFlag;
longDir.LDIR_FstClusLO = 0;
longDir.LDIR_Ord = (BYTE)i;
longDir.LDIR_Type = 0;
//int nl = strlen(lptDirName);
int npos = (i - 1) * 13;
memset(longDir.LDIR_Name1, 0xff, 26);
memcpy(longDir.LDIR_Name1, pLongName + npos, 13*2); // 将剩余的名字全部拷贝进去
RwInfoFromLongDirInfo(dirRw, longDir);
SetDiskFilePointer(NULL, dirAdd + 32 * retIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
retIndex++;
nIndex ++;
if(retIndex >= maxDircnt)
{
UINT c = GetFirstFreeClusNum();
SetClus(cClus, c);
SetClus(c, m_EOC_STATUS);
InitializeClus(c);
retIndex = 0;
dirAdd = ClusterToRelatAddr(c);
}
}
//////////////////////////////////////////////////////////////////////////
//SetDiskFilePointer(_hFile, dirAdd + 32 * retIndex, NULL, FILE_BEGIN);
memcpy(&dir.DIR_Name, pName, 11);
dir.DIR_Attr = 0x10;
::GetSystemTime(&dir.DIR_WrtDateTime);
::GetSystemTime(&dir.DIR_CrtDateTime);
::GetSystemTime(&dir.DIR_LstAcceDate);
/*
dir.DIR_WrtDateTime = CTime::GetCurrentTime();
dir.DIR_CrtDateTime = CTime::GetCurrentTime();
dir.DIR_LstAcceDate = CTime::GetCurrentTime();
*/
UINT clus = GetFirstFreeClusNum();
ASSERT(clus >= 2);
dir.DIR_FstClusHI = 0;
dir.DIR_FstClusLO = clus;
SetClus(clus, m_EOC_STATUS);
RwInfoFromDirInfo(dirRw, dir);
//SetDiskFilePointer(NULL, dirAdd + 32 * nIndex, NULL, FILE_BEGIN);
SetDiskFilePointer(_hFile, dirAdd + 32 * retIndex, NULL, FILE_BEGIN);
if(!WriteDiskFile(_hFile, &dirRw, sizeof(Fat_DirectoryRW), &nWrite, NULL))
{
printmsg("Error In create RootDirectory -- write dir Struct");
return FALSE;
}
//////////////////////////////////////////////////////////////////////////
// 初始化分配得到簇的值
InitializeClus(clus);
//////////////////////////////////////////////////////////////////////////
// 建立 .目录
UINT pos = ClusterToRelatAddr(clus);
memcpy(dir.DIR_Name, ". ", 11);
::GetSystemTime(&dir.DIR_WrtDateTime);
::GetSystemTime(&dir.DIR_CrtDateTime);
::GetSystemTime(&dir.DIR_LstAcceDate);
/*
dir.DIR_WrtDateTime = CTime::GetCurrentTime();
dir.DIR_CrtDateTime = CTime::GetCurrentTime();
dir.DIR_LstAcceDate = CTime::GetCurrentTime();
*/
dir.DIR_FstClusHI = 0;