605
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include "fsnode.h"
|
|
18 |
#include "fatdefines.h"
|
|
19 |
#include "utf16string.h"
|
|
20 |
#include <stdio.h>
|
|
21 |
#include <iostream>
|
|
22 |
#include <iomanip>
|
|
23 |
#include <stdio.h>
|
|
24 |
#include <stdlib.h>
|
|
25 |
|
|
26 |
#include <ctype.h>
|
|
27 |
|
|
28 |
using namespace std;
|
|
29 |
|
|
30 |
const TUint KBytesPerEntry = 13 ;
|
617
|
31 |
|
|
32 |
static inline bool is_a_long_file_name_char(unsigned char ch){
|
|
33 |
return ( ch >= ' ' && ch != '"' && ch != '*' && ch != ':' && ch != '<' \
|
|
34 |
&& ch != '>' && ch != '?' && ch != '|' && ch != 127) ;
|
|
35 |
|
|
36 |
}
|
605
|
37 |
//
|
|
38 |
TFSNode::TFSNode(TFSNode* aParent, const char* aFileName, TUint8 aAttrs, const char* aPCSideName) :
|
|
39 |
iParent(aParent),iFirstChild(0),iSibling(0),iAttrs(aAttrs), iPCSideName(0), iWideName(0){
|
|
40 |
|
|
41 |
// According to the FAT specification, short name should be inited with empty string (' ' string)
|
|
42 |
memset(iShortName,0x20,11);
|
|
43 |
iShortName[11] = 0 ;
|
|
44 |
if(aFileName) {
|
617
|
45 |
const unsigned char* ptr = reinterpret_cast<const unsigned char*>(aFileName);
|
|
46 |
bool allSpaces = true ;
|
|
47 |
while(*ptr){
|
|
48 |
if( !is_a_long_file_name_char(*ptr))
|
|
49 |
throw "Illegal filename or dir name! \n";
|
|
50 |
if(*ptr != ' ')
|
|
51 |
allSpaces = false ;
|
|
52 |
ptr++ ;
|
|
53 |
}
|
|
54 |
if(allSpaces)
|
|
55 |
throw "Illegal filename or dir name(all spaces)!\n";
|
|
56 |
iFileName = strdup(aFileName);
|
605
|
57 |
GenerateBasicName() ;
|
|
58 |
}
|
|
59 |
if(aPCSideName) {
|
|
60 |
iPCSideName = strdup(aPCSideName);
|
|
61 |
}
|
|
62 |
iFATEntry = 0;
|
|
63 |
iCrtTimeTenth = 0;
|
|
64 |
iCrtTime.iImageTime = 0 ;
|
|
65 |
iCrtDate.iImageDate = 0 ;
|
|
66 |
iLstAccDate.iImageDate = 0 ;
|
|
67 |
iWrtTime.iImageTime = 0 ;
|
|
68 |
iWrtDate.iImageDate = 0 ;
|
|
69 |
iFileSize = 0;
|
|
70 |
if(!iParent) return ;
|
|
71 |
|
|
72 |
if(!iParent->iFirstChild)
|
|
73 |
iParent->iFirstChild = this ;
|
|
74 |
else {
|
|
75 |
TFSNode* sibling = iParent->iFirstChild;
|
|
76 |
while(sibling->iSibling)
|
|
77 |
sibling = sibling->iSibling ;
|
|
78 |
sibling->iSibling = this ;
|
|
79 |
}
|
|
80 |
|
|
81 |
}
|
|
82 |
TFSNode::~TFSNode(){
|
|
83 |
if(iFirstChild)
|
|
84 |
delete iFirstChild ;
|
|
85 |
if(iSibling)
|
|
86 |
delete iSibling ;
|
|
87 |
if(iFileName)
|
|
88 |
free(iFileName) ;
|
|
89 |
if(iWideName)
|
|
90 |
delete iWideName;
|
617
|
91 |
if(iPCSideName)
|
|
92 |
free(iPCSideName);
|
605
|
93 |
}
|
617
|
94 |
|
605
|
95 |
/** GenerateBasicName : Generate the short name according to long name
|
|
96 |
*
|
|
97 |
* algorithm :
|
|
98 |
*
|
|
99 |
* 1. The UNICODE name passed to the file system is converted to upper case.
|
|
100 |
* 2. The upper cased UNICODE name is converted to OEM.
|
|
101 |
* if (the uppercased UNICODE glyph does not exist as an OEM glyph in the OEM code page)
|
|
102 |
* or (the OEM glyph is invalid in an 8.3 name)
|
|
103 |
* {
|
|
104 |
* Replace the glyph to an OEM '_' (underscore) character.
|
|
105 |
* Set a "lossy conversion" flag.
|
|
106 |
* }
|
|
107 |
* 3. Strip all leading and embedded spaces from the long name.
|
|
108 |
* 4. Strip all leading periods from the long name.
|
|
109 |
* 5. While (not at end of the long name)
|
|
110 |
* and (char is not a period)
|
|
111 |
* and (total chars copied < 8)
|
|
112 |
* {
|
|
113 |
* Copy characters into primary portion of the basis name
|
|
114 |
* }
|
|
115 |
* 6. Insert a dot at the end of the primary components of the basis-name
|
|
116 |
* if the basis name has an extension after the last period in the name.
|
|
117 |
*
|
|
118 |
* 7. Scan for the last embedded period in the long name.
|
|
119 |
* If (the last embedded period was found)
|
|
120 |
* {
|
|
121 |
* While (not at end of the long name) and (total chars copied < 3)
|
|
122 |
* {
|
|
123 |
* Copy characters into extension portion of the basis name
|
|
124 |
* }
|
|
125 |
* }
|
|
126 |
*
|
|
127 |
*/
|
|
128 |
void TFSNode::GenerateBasicName() {
|
617
|
129 |
const char* filename = iFileName ;
|
605
|
130 |
TUint length = strlen(filename);
|
|
131 |
if(0 == length)
|
|
132 |
return ;
|
|
133 |
if(0 == strcmp(filename,".")){
|
|
134 |
iShortName[0] = '.' ;
|
|
135 |
return ;
|
|
136 |
}
|
|
137 |
if(0 == strcmp(filename,"..")){
|
|
138 |
iShortName[0] = '.' ;
|
|
139 |
iShortName[1] = '.' ;
|
|
140 |
return ;
|
617
|
141 |
}
|
605
|
142 |
iWideName = new UTF16String(filename,length); // The unicode string
|
|
143 |
char base[10];
|
|
144 |
const char* ext = filename + length;
|
|
145 |
|
|
146 |
//Strip all leading periods and spaces from the long name.
|
|
147 |
while(*filename == '.' || *filename == ' ' || *filename == '\t') {
|
|
148 |
filename ++ ;
|
|
149 |
length -- ;
|
|
150 |
}
|
|
151 |
//find the extension
|
|
152 |
while(ext > filename && *ext != '.')
|
|
153 |
ext -- ;
|
|
154 |
if(ext == filename){
|
|
155 |
ext = "" ;
|
|
156 |
}
|
|
157 |
else {
|
|
158 |
length = ext - filename;
|
|
159 |
ext ++ ;
|
|
160 |
}
|
|
161 |
bool lossyConv = false ;
|
|
162 |
TUint bl = 0;
|
|
163 |
for(TUint i = 0 ; i < length ; i++) {
|
|
164 |
if(filename[i] >= 'a' && filename[i] <= 'z')
|
|
165 |
base[bl++] = filename[i] + 'A' - 'a';
|
|
166 |
else if(filename[i] >= 'A' && filename[i] <= 'Z')
|
|
167 |
base[bl++] = filename[i];
|
|
168 |
else if(filename[i] == '$' || filename[i] == '%' ||
|
|
169 |
filename[i] == '-' || filename[i] == '_' || filename[i] == '@' ||
|
|
170 |
filename[i] == '~' || filename[i] == '`' || filename[i] == '!' ||
|
|
171 |
filename[i] == '(' || filename[i] == ')' || filename[i] == '{' ||
|
|
172 |
filename[i] == '}' || filename[i] == '^' || filename[i] == '#' ||
|
|
173 |
filename[i] == '&' ||filename[i] == '\'')
|
|
174 |
base[bl++] = filename[i];
|
|
175 |
else if(filename[i] != ' ' && filename[i] != '.'){
|
|
176 |
base[bl++] = '_';
|
|
177 |
lossyConv = true ;
|
|
178 |
}
|
|
179 |
if(bl > 8){
|
|
180 |
bl -- ;
|
|
181 |
lossyConv = true ;
|
|
182 |
break ;
|
|
183 |
}
|
|
184 |
}
|
|
185 |
if(lossyConv){
|
|
186 |
if(bl > 6) bl = 6 ;
|
|
187 |
iShortName[bl] = '~';
|
|
188 |
iShortName[bl+1] = '1';
|
|
189 |
}
|
|
190 |
memcpy(iShortName,base,bl);
|
|
191 |
|
|
192 |
//Copy the extension part.
|
|
193 |
TUint ei = 8;
|
|
194 |
for(TUint e = 0; ei < 11 && ext[e] != 0 ; e++){
|
|
195 |
if(ext[e] >= 'a' && ext[e] <= 'z')
|
|
196 |
iShortName[ei++] = ext[e] + 'A' - 'a';
|
|
197 |
else if(ext[e] >= 'A' && ext[e] <= 'Z')
|
|
198 |
iShortName[ei++] = ext[e] ;
|
|
199 |
else if(ext[e] == '$' || ext[e] == '%' || ext[e] == '-' || ext[e] == '_' ||
|
|
200 |
ext[e] == '@' || ext[e] == '~' || ext[e] == '`' || ext[e] == '!' ||
|
|
201 |
ext[e] == '(' || ext[e] == ')' || ext[e] == '{' || ext[e] == '}' ||
|
|
202 |
ext[e] == '^' || ext[e] == '#' || ext[e] == '&' ||ext[e] == '\'')
|
|
203 |
iShortName[ei++] = ext[e] ;
|
|
204 |
}
|
|
205 |
|
|
206 |
if(iParent)
|
617
|
207 |
iParent->MakeUniqueShortName(iShortName,bl);
|
605
|
208 |
}
|
|
209 |
|
|
210 |
#ifdef _DEBUG
|
|
211 |
void TFSNode::PrintTree(int nTab) {
|
|
212 |
for( int i = 0 ; i < nTab ; i++ )
|
|
213 |
cout << " " ;
|
|
214 |
cout << (iFileName ? iFileName : "") << " [" << hex << setw(2) << setfill('0') << (unsigned short)iAttrs << "] \n" ;
|
|
215 |
if(iFirstChild)
|
|
216 |
iFirstChild->PrintTree(nTab + 2);
|
|
217 |
if(iSibling)
|
|
218 |
iSibling->PrintTree(nTab);
|
|
219 |
}
|
|
220 |
#endif
|
|
221 |
bool TFSNode::IsDirectory() const {
|
617
|
222 |
return (0 != (iAttrs & ATTR_DIRECTORY) || ATTR_VOLUME_ID == iAttrs) ;
|
605
|
223 |
}
|
|
224 |
int TFSNode::GetWideNameLength() const {
|
|
225 |
if(!iWideName)
|
|
226 |
return 0 ;
|
|
227 |
return iWideName->length() ;
|
|
228 |
}
|
|
229 |
TUint TFSNode::GetSize() const {
|
|
230 |
|
617
|
231 |
if( !IsDirectory())
|
605
|
232 |
return iFileSize ;
|
|
233 |
TUint retVal = sizeof(TShortDirEntry) ; // the tailed entry
|
|
234 |
if(iParent)
|
|
235 |
retVal += sizeof(TShortDirEntry) * 2 ;
|
|
236 |
TFSNode* child = iFirstChild ;
|
|
237 |
while(child) {
|
|
238 |
TUint longNameEntries = (child->GetWideNameLength() + KBytesPerEntry) / KBytesPerEntry ;
|
|
239 |
retVal += longNameEntries * sizeof(TLongDirEntry) ;
|
|
240 |
retVal += sizeof(TShortDirEntry);
|
|
241 |
child = child->iSibling ;
|
|
242 |
}
|
|
243 |
return retVal ;
|
|
244 |
}
|
|
245 |
|
|
246 |
void TFSNode::Init(time_t aCreateTime, time_t aAccessTime, time_t aWriteTime, TUint aSize ) {
|
|
247 |
|
|
248 |
struct tm* temp = localtime(&aCreateTime);
|
|
249 |
iCrtDate.iCurrentDate.Day = temp->tm_mday;
|
|
250 |
iCrtDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
251 |
iCrtDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
252 |
iCrtTime.iCurrentTime.Hour = temp->tm_hour;
|
|
253 |
iCrtTime.iCurrentTime.Minute = temp->tm_min;
|
|
254 |
iCrtTime.iCurrentTime.Seconds = temp->tm_sec / 2;//As per FAT spec
|
|
255 |
iCrtTimeTenth = 0;
|
|
256 |
|
|
257 |
temp = localtime(&aAccessTime);
|
|
258 |
iLstAccDate.iCurrentDate.Day = temp->tm_mday;
|
|
259 |
iLstAccDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
260 |
iLstAccDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
261 |
|
|
262 |
temp = localtime(&aWriteTime);
|
|
263 |
iWrtDate.iCurrentDate.Day = temp->tm_mday;
|
|
264 |
iWrtDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
265 |
iWrtDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
266 |
iWrtTime.iCurrentTime.Hour = temp->tm_hour;
|
|
267 |
iWrtTime.iCurrentTime.Minute = temp->tm_min;
|
|
268 |
iWrtTime.iCurrentTime.Seconds = temp->tm_sec / 2;//As per FAT spec
|
|
269 |
|
|
270 |
iFileSize = aSize ;
|
|
271 |
}
|
|
272 |
/** WriteDirEntries : Write FAT information for this node to a cluster buffer
|
|
273 |
* aStartIndex : [in],the beginning index of the outputed cluster
|
|
274 |
* aClusterData : [in,out] the cluster buffer
|
|
275 |
*
|
|
276 |
* notice, aClusterData is only required if node is a directory node.
|
|
277 |
* for a file node, no data will be written out.
|
|
278 |
* in this case, only corresponding cluster index information is updated.
|
|
279 |
*/
|
|
280 |
void TFSNode::WriteDirEntries(TUint aStartIndex,TUint8* aClusterData){
|
|
281 |
if(iFATEntry){
|
|
282 |
*((TUint16*)iFATEntry->DIR_FstClusHI) = (aStartIndex >> 16) ;
|
|
283 |
*((TUint16*)iFATEntry->DIR_FstClusLO) = (aStartIndex & 0xFFFF) ;
|
|
284 |
}
|
|
285 |
|
617
|
286 |
if(IsDirectory()) { // Directory , write dir entries ;
|
605
|
287 |
TShortDirEntry* entry = reinterpret_cast<TShortDirEntry*>(aClusterData);
|
|
288 |
if(iParent != NULL) {
|
|
289 |
//Make
|
|
290 |
GetShortEntry(entry);
|
|
291 |
//TODO: Add comments to avoid mistaken deleting.
|
|
292 |
memcpy(entry->DIR_Name,". ",sizeof(entry->DIR_Name));
|
|
293 |
entry ++ ;
|
|
294 |
iParent->GetShortEntry(entry);
|
|
295 |
memcpy(entry->DIR_Name,".. ",sizeof(entry->DIR_Name));
|
|
296 |
entry ++ ;
|
|
297 |
}
|
|
298 |
TFSNode* child = iFirstChild ;
|
|
299 |
while(child){
|
|
300 |
int items = child->GetLongEntries(reinterpret_cast<TLongDirEntry*>(entry));
|
|
301 |
entry += items ;
|
|
302 |
child->GetShortEntry(entry);
|
|
303 |
child->iFATEntry = entry ;
|
|
304 |
entry ++ ;
|
|
305 |
child = child->iSibling ;
|
|
306 |
|
|
307 |
}
|
|
308 |
|
|
309 |
}
|
|
310 |
}
|
|
311 |
/** GetShortEntry : Make a short directory entry (FAT16/32 conception)
|
|
312 |
* aEntry : the entry buffer
|
|
313 |
*/
|
|
314 |
void TFSNode::GetShortEntry(TShortDirEntry* aEntry) {
|
|
315 |
if(!aEntry) return ;
|
|
316 |
if(iFATEntry){
|
|
317 |
if(iFATEntry != aEntry)
|
|
318 |
memcpy(aEntry,iFATEntry,sizeof(TShortDirEntry));
|
|
319 |
return ;
|
|
320 |
}
|
|
321 |
memcpy(aEntry->DIR_Name,iShortName,sizeof(aEntry->DIR_Name));
|
|
322 |
aEntry->DIR_Attr = iAttrs;
|
|
323 |
aEntry->DIR_NTRes = 0 ;
|
|
324 |
aEntry->DIR_CrtTimeTenth = 0 ;
|
|
325 |
memcpy(aEntry->DIR_CrtTime,&iCrtTime,sizeof(aEntry->DIR_CrtTime));
|
|
326 |
memcpy(aEntry->DIR_CrtDate,&iCrtDate,sizeof(aEntry->DIR_CrtDate));
|
|
327 |
memcpy(aEntry->DIR_LstAccDate,&iLstAccDate,sizeof(aEntry->DIR_LstAccDate));
|
|
328 |
memset(aEntry->DIR_FstClusHI,0,sizeof(aEntry->DIR_FstClusHI));
|
|
329 |
memcpy(aEntry->DIR_WrtTime,&iWrtTime,sizeof(aEntry->DIR_WrtTime));
|
|
330 |
memcpy(aEntry->DIR_WrtDate,&iWrtDate,sizeof(aEntry->DIR_WrtDate));
|
|
331 |
memset(aEntry->DIR_FstClusLO,0,sizeof(aEntry->DIR_FstClusLO));
|
|
332 |
memcpy(aEntry->DIR_FileSize,&iFileSize,sizeof(aEntry->DIR_FileSize));
|
|
333 |
}
|
|
334 |
TUint8 FATChkSum(const char* pFcbName) {
|
|
335 |
short fcbNameLen ;
|
|
336 |
TUint8 sum = 0 ;
|
|
337 |
for(fcbNameLen = 11 ; fcbNameLen != 0 ; fcbNameLen --) {
|
|
338 |
sum = ((sum & 1) ? 0x80 : 0 ) + (sum >> 1 ) + *pFcbName++ ;
|
|
339 |
}
|
|
340 |
return sum ;
|
|
341 |
}
|
|
342 |
/** GetLongEntries : Make a series of long directory entries (FAT16/32 conception)
|
|
343 |
* aEntries : the start addr of the long directory entries buffer
|
|
344 |
*
|
|
345 |
* return value : actual entris count.
|
|
346 |
*/
|
|
347 |
int TFSNode::GetLongEntries(TLongDirEntry* aEntries) {
|
|
348 |
|
|
349 |
if(!aEntries) return 0;
|
|
350 |
int packs = (GetWideNameLength() + KBytesPerEntry) / KBytesPerEntry ;
|
|
351 |
|
|
352 |
TUint buflen = packs * KBytesPerEntry;
|
|
353 |
TUint16* buffer = new(std::nothrow) TUint16[buflen];
|
|
354 |
if(!buffer)
|
|
355 |
return 0 ;
|
|
356 |
memset(buffer,0xff,(buflen << 1));
|
|
357 |
if(iWideName) {
|
|
358 |
memcpy(buffer,iWideName->c_str(),iWideName->bytes());
|
|
359 |
buffer[iWideName->length()] = 0;
|
|
360 |
}
|
|
361 |
TUint8 chkSum = FATChkSum(iShortName);;
|
|
362 |
|
|
363 |
TUint16* ptr = buffer ;
|
|
364 |
TLongDirEntry* entry = aEntries +(packs - 1);
|
|
365 |
for(int i = 1 ; i <= packs ; i++, entry--) {
|
|
366 |
entry->LDIR_Ord = i ;
|
|
367 |
entry->LDIR_Chksum = chkSum ;
|
|
368 |
entry->LDIR_Attr = (TUint8)ATTR_LONG_NAME;
|
|
369 |
*((TUint16*)(entry->LDIR_FstClusLO)) = 0;
|
|
370 |
entry->LDIR_Type = 0;
|
|
371 |
memcpy(entry->LDIR_Name1,ptr,10);
|
|
372 |
memcpy(entry->LDIR_Name2,&ptr[5],12);
|
|
373 |
memcpy(entry->LDIR_Name3,&ptr[11],4);
|
|
374 |
ptr += 13;
|
|
375 |
}
|
|
376 |
aEntries->LDIR_Ord |= 0x40 ;
|
|
377 |
|
|
378 |
delete []buffer ;
|
|
379 |
return packs ;
|
|
380 |
}
|
|
381 |
/** Make a unique name for a new child which has not been added.
|
|
382 |
* to avoid same short names under a directory
|
|
383 |
* rShortName : [in,out] , The new short name to be checked and changed.
|
|
384 |
* baseNameLength: [in], the length of the base part of the short name
|
|
385 |
* not including the "~n"
|
|
386 |
* for example,
|
|
387 |
* "ABC.LOG" => baseNameLength == 3 ("ABC")
|
|
388 |
* "AB~1.TXT" => baseNameLength == 2 ("AB")
|
|
389 |
*
|
|
390 |
*
|
|
391 |
*The Numeric-Tail Generation Algorithm
|
|
392 |
|
|
393 |
* If (a "lossy conversion" was not flagged)
|
|
394 |
* and (the long name fits within the 8.3 naming conventions)
|
|
395 |
* and (the basis-name does not collide with any existing short name)
|
|
396 |
* {
|
|
397 |
* The short name is only the basis-name without the numeric tail.
|
|
398 |
* }
|
|
399 |
* else {
|
|
400 |
* Insert a numeric-tail "~n" to the end of the primary name such that the value of
|
|
401 |
* the "~n" is chosen so that the name thus formed does not collide with
|
|
402 |
* any existing short name and that the primary name does not exceed eight
|
|
403 |
* characters in length.
|
|
404 |
* }
|
|
405 |
* The "~n" string can range from "~1" to "~999999".
|
|
406 |
*
|
|
407 |
*/
|
|
408 |
|
|
409 |
void TFSNode::MakeUniqueShortName(char rShortName[12],TUint baseNameLength) const {
|
|
410 |
bool dup ;
|
|
411 |
char nstring[10];
|
|
412 |
int n = 0 ;
|
|
413 |
do {
|
|
414 |
TFSNode* child = iFirstChild ;
|
|
415 |
dup = false ;
|
|
416 |
while(child){
|
|
417 |
if(0 == memcmp(rShortName,child->iShortName,11)) {
|
|
418 |
dup = true ;
|
|
419 |
break ;
|
|
420 |
}
|
|
421 |
child = child->iSibling ;
|
|
422 |
}
|
|
423 |
if(dup){ //duplex , increase the index , make a new name
|
|
424 |
int nlen = sprintf(nstring,"~%u",++n);
|
|
425 |
while((baseNameLength + nlen > 8) && baseNameLength > 1)
|
|
426 |
baseNameLength -- ;
|
|
427 |
memcpy(&rShortName[baseNameLength],nstring,nlen);
|
|
428 |
|
|
429 |
}
|
|
430 |
}while(dup) ;
|
|
431 |
|
|
432 |
}
|
|
433 |
|