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 |
|
|
29 |
#ifdef __LINUX__
|
|
30 |
#include <dirent.h>
|
|
31 |
#include <sys/stat.h>
|
|
32 |
#include <unistd.h>
|
|
33 |
#define SPLIT_CHAR '/'
|
|
34 |
#else
|
|
35 |
#include <io.h>
|
|
36 |
#include <direct.h> //TODO: check under MinGW4 + stlport 5.2
|
|
37 |
#include <conio.h>
|
|
38 |
#define SPLIT_CHAR '\\'
|
|
39 |
#endif
|
|
40 |
|
|
41 |
using namespace std;
|
|
42 |
|
|
43 |
const TUint KBytesPerEntry = 13 ;
|
617
|
44 |
|
|
45 |
static inline bool is_a_long_file_name_char(unsigned char ch){
|
|
46 |
return ( ch >= ' ' && ch != '"' && ch != '*' && ch != ':' && ch != '<' \
|
|
47 |
&& ch != '>' && ch != '?' && ch != '|' && ch != 127) ;
|
|
48 |
|
|
49 |
}
|
605
|
50 |
//
|
|
51 |
TFSNode::TFSNode(TFSNode* aParent, const char* aFileName, TUint8 aAttrs, const char* aPCSideName) :
|
|
52 |
iParent(aParent),iFirstChild(0),iSibling(0),iAttrs(aAttrs), iPCSideName(0), iWideName(0){
|
|
53 |
|
|
54 |
// According to the FAT specification, short name should be inited with empty string (' ' string)
|
|
55 |
memset(iShortName,0x20,11);
|
|
56 |
iShortName[11] = 0 ;
|
|
57 |
if(aFileName) {
|
617
|
58 |
const unsigned char* ptr = reinterpret_cast<const unsigned char*>(aFileName);
|
|
59 |
bool allSpaces = true ;
|
|
60 |
while(*ptr){
|
|
61 |
if( !is_a_long_file_name_char(*ptr))
|
|
62 |
throw "Illegal filename or dir name! \n";
|
|
63 |
if(*ptr != ' ')
|
|
64 |
allSpaces = false ;
|
|
65 |
ptr++ ;
|
|
66 |
}
|
|
67 |
if(allSpaces)
|
|
68 |
throw "Illegal filename or dir name(all spaces)!\n";
|
|
69 |
iFileName = strdup(aFileName);
|
605
|
70 |
GenerateBasicName() ;
|
|
71 |
}
|
|
72 |
if(aPCSideName) {
|
|
73 |
iPCSideName = strdup(aPCSideName);
|
|
74 |
}
|
|
75 |
iFATEntry = 0;
|
|
76 |
iCrtTimeTenth = 0;
|
|
77 |
iCrtTime.iImageTime = 0 ;
|
|
78 |
iCrtDate.iImageDate = 0 ;
|
|
79 |
iLstAccDate.iImageDate = 0 ;
|
|
80 |
iWrtTime.iImageTime = 0 ;
|
|
81 |
iWrtDate.iImageDate = 0 ;
|
|
82 |
iFileSize = 0;
|
|
83 |
if(!iParent) return ;
|
|
84 |
|
|
85 |
if(!iParent->iFirstChild)
|
|
86 |
iParent->iFirstChild = this ;
|
|
87 |
else {
|
|
88 |
TFSNode* sibling = iParent->iFirstChild;
|
|
89 |
while(sibling->iSibling)
|
|
90 |
sibling = sibling->iSibling ;
|
|
91 |
sibling->iSibling = this ;
|
|
92 |
}
|
|
93 |
|
|
94 |
}
|
|
95 |
TFSNode::~TFSNode(){
|
|
96 |
if(iFirstChild)
|
|
97 |
delete iFirstChild ;
|
|
98 |
if(iSibling)
|
|
99 |
delete iSibling ;
|
|
100 |
if(iFileName)
|
|
101 |
free(iFileName) ;
|
|
102 |
if(iWideName)
|
|
103 |
delete iWideName;
|
617
|
104 |
if(iPCSideName)
|
|
105 |
free(iPCSideName);
|
605
|
106 |
}
|
617
|
107 |
|
605
|
108 |
TFSNode* TFSNode::CreateFromFolder(const char* aPath,TFSNode* aParent) {
|
617
|
109 |
|
605
|
110 |
int len = strlen(aPath);
|
|
111 |
#ifdef __LINUX__
|
|
112 |
DIR* dir = opendir(aPath);
|
|
113 |
if(dir == NULL) {
|
|
114 |
cout << aPath << " does not contain any subfolder/file.\n";
|
|
115 |
return aParent;
|
|
116 |
}
|
|
117 |
if(!aParent)
|
617
|
118 |
aParent = new TFSNode(NULL,"/",ATTR_DIRECTORY);
|
605
|
119 |
dirent* entry;
|
|
120 |
struct stat statbuf ;
|
617
|
121 |
char* fileName = new(nothrow) char[len + 200];
|
|
122 |
if(!fileName) return NULL ;
|
|
123 |
memcpy(fileName,aPath,len);
|
|
124 |
fileName[len] = SPLIT_CHAR;
|
605
|
125 |
while ((entry = readdir(dir)) != NULL) {
|
617
|
126 |
if(strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0)
|
|
127 |
continue ;
|
|
128 |
strcpy(&fileName[len+1],entry->d_name);
|
|
129 |
stat(fileName , &statbuf);
|
|
130 |
TFSNode* pNewItem = new TFSNode(aParent,fileName,S_ISDIR(statbuf.st_mode) ? ATTR_DIRECTORY : 0);
|
|
131 |
pNewItem->Init(statbuf.st_ctime,statbuf.st_atime,statbuf.st_mtime,statbuf.st_size);
|
|
132 |
if(S_ISDIR(statbuf.st_mode)){
|
|
133 |
CreateFromFolder(fileName,pNewItem);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
delete []fileName ;
|
605
|
137 |
closedir(dir);
|
|
138 |
#else
|
|
139 |
struct _finddata_t data ;
|
|
140 |
memset(&data, 0, sizeof(data));
|
617
|
141 |
char* fileName = new(nothrow) char[len + 200];
|
|
142 |
if(!fileName) return NULL ;
|
|
143 |
memcpy(fileName,aPath,len);
|
|
144 |
fileName[len] = SPLIT_CHAR;
|
|
145 |
fileName[len+1] = '*';
|
|
146 |
fileName[len+2] = 0;
|
|
147 |
intptr_t hFind = _findfirst(fileName,&data);
|
605
|
148 |
|
|
149 |
if(hFind == (intptr_t)-1 ) {
|
|
150 |
cout << aPath << " does not contain any subfolder/file.\n";
|
617
|
151 |
delete []fileName;
|
605
|
152 |
return aParent;
|
617
|
153 |
}
|
605
|
154 |
if(!aParent)
|
617
|
155 |
aParent = new TFSNode(NULL,"/",ATTR_DIRECTORY);
|
|
156 |
|
605
|
157 |
do {
|
617
|
158 |
if(strcmp(data.name,".") == 0 || strcmp(data.name,"..") == 0)
|
605
|
159 |
continue ;
|
617
|
160 |
|
605
|
161 |
strcpy(&fileName[len+1],data.name);
|
|
162 |
TUint8 attr = 0;
|
|
163 |
if(data.attrib & _A_SUBDIR)
|
|
164 |
attr |= ATTR_DIRECTORY;
|
|
165 |
if(data.attrib & _A_RDONLY)
|
|
166 |
attr |= ATTR_READ_ONLY ;
|
|
167 |
if(data.attrib & _A_HIDDEN)
|
|
168 |
attr |= ATTR_HIDDEN ;
|
|
169 |
if(data.attrib & _A_SYSTEM)
|
|
170 |
attr |= ATTR_SYSTEM ;
|
|
171 |
if(data.attrib & _A_ARCH)
|
|
172 |
attr |= ATTR_ARCHIVE;
|
617
|
173 |
TFSNode* pNewItem = new TFSNode(aParent,data.name,attr,fileName);
|
605
|
174 |
pNewItem->Init(data.time_create,data.time_access,data.time_write,data.size);
|
|
175 |
if(data.attrib & _A_SUBDIR){
|
|
176 |
CreateFromFolder(fileName,pNewItem);
|
|
177 |
}
|
|
178 |
|
|
179 |
} while(-1 != _findnext(hFind, &data));
|
617
|
180 |
delete []fileName ;
|
605
|
181 |
_findclose(hFind);
|
|
182 |
#endif
|
|
183 |
|
|
184 |
return aParent;
|
|
185 |
}
|
617
|
186 |
|
605
|
187 |
/** GenerateBasicName : Generate the short name according to long name
|
|
188 |
*
|
|
189 |
* algorithm :
|
|
190 |
*
|
|
191 |
* 1. The UNICODE name passed to the file system is converted to upper case.
|
|
192 |
* 2. The upper cased UNICODE name is converted to OEM.
|
|
193 |
* if (the uppercased UNICODE glyph does not exist as an OEM glyph in the OEM code page)
|
|
194 |
* or (the OEM glyph is invalid in an 8.3 name)
|
|
195 |
* {
|
|
196 |
* Replace the glyph to an OEM '_' (underscore) character.
|
|
197 |
* Set a "lossy conversion" flag.
|
|
198 |
* }
|
|
199 |
* 3. Strip all leading and embedded spaces from the long name.
|
|
200 |
* 4. Strip all leading periods from the long name.
|
|
201 |
* 5. While (not at end of the long name)
|
|
202 |
* and (char is not a period)
|
|
203 |
* and (total chars copied < 8)
|
|
204 |
* {
|
|
205 |
* Copy characters into primary portion of the basis name
|
|
206 |
* }
|
|
207 |
* 6. Insert a dot at the end of the primary components of the basis-name
|
|
208 |
* if the basis name has an extension after the last period in the name.
|
|
209 |
*
|
|
210 |
* 7. Scan for the last embedded period in the long name.
|
|
211 |
* If (the last embedded period was found)
|
|
212 |
* {
|
|
213 |
* While (not at end of the long name) and (total chars copied < 3)
|
|
214 |
* {
|
|
215 |
* Copy characters into extension portion of the basis name
|
|
216 |
* }
|
|
217 |
* }
|
|
218 |
*
|
|
219 |
*/
|
|
220 |
void TFSNode::GenerateBasicName() {
|
617
|
221 |
const char* filename = iFileName ;
|
605
|
222 |
TUint length = strlen(filename);
|
|
223 |
if(0 == length)
|
|
224 |
return ;
|
|
225 |
if(0 == strcmp(filename,".")){
|
|
226 |
iShortName[0] = '.' ;
|
|
227 |
return ;
|
|
228 |
}
|
|
229 |
if(0 == strcmp(filename,"..")){
|
|
230 |
iShortName[0] = '.' ;
|
|
231 |
iShortName[1] = '.' ;
|
|
232 |
return ;
|
617
|
233 |
}
|
605
|
234 |
iWideName = new UTF16String(filename,length); // The unicode string
|
|
235 |
char base[10];
|
|
236 |
const char* ext = filename + length;
|
|
237 |
|
|
238 |
//Strip all leading periods and spaces from the long name.
|
|
239 |
while(*filename == '.' || *filename == ' ' || *filename == '\t') {
|
|
240 |
filename ++ ;
|
|
241 |
length -- ;
|
|
242 |
}
|
|
243 |
//find the extension
|
|
244 |
while(ext > filename && *ext != '.')
|
|
245 |
ext -- ;
|
|
246 |
if(ext == filename){
|
|
247 |
ext = "" ;
|
|
248 |
}
|
|
249 |
else {
|
|
250 |
length = ext - filename;
|
|
251 |
ext ++ ;
|
|
252 |
}
|
|
253 |
bool lossyConv = false ;
|
|
254 |
TUint bl = 0;
|
|
255 |
for(TUint i = 0 ; i < length ; i++) {
|
|
256 |
if(filename[i] >= 'a' && filename[i] <= 'z')
|
|
257 |
base[bl++] = filename[i] + 'A' - 'a';
|
|
258 |
else if(filename[i] >= 'A' && filename[i] <= 'Z')
|
|
259 |
base[bl++] = filename[i];
|
|
260 |
else if(filename[i] == '$' || filename[i] == '%' ||
|
|
261 |
filename[i] == '-' || filename[i] == '_' || filename[i] == '@' ||
|
|
262 |
filename[i] == '~' || filename[i] == '`' || filename[i] == '!' ||
|
|
263 |
filename[i] == '(' || filename[i] == ')' || filename[i] == '{' ||
|
|
264 |
filename[i] == '}' || filename[i] == '^' || filename[i] == '#' ||
|
|
265 |
filename[i] == '&' ||filename[i] == '\'')
|
|
266 |
base[bl++] = filename[i];
|
|
267 |
else if(filename[i] != ' ' && filename[i] != '.'){
|
|
268 |
base[bl++] = '_';
|
|
269 |
lossyConv = true ;
|
|
270 |
}
|
|
271 |
if(bl > 8){
|
|
272 |
bl -- ;
|
|
273 |
lossyConv = true ;
|
|
274 |
break ;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
if(lossyConv){
|
|
278 |
if(bl > 6) bl = 6 ;
|
|
279 |
iShortName[bl] = '~';
|
|
280 |
iShortName[bl+1] = '1';
|
|
281 |
}
|
|
282 |
memcpy(iShortName,base,bl);
|
|
283 |
|
|
284 |
//Copy the extension part.
|
|
285 |
TUint ei = 8;
|
|
286 |
for(TUint e = 0; ei < 11 && ext[e] != 0 ; e++){
|
|
287 |
if(ext[e] >= 'a' && ext[e] <= 'z')
|
|
288 |
iShortName[ei++] = ext[e] + 'A' - 'a';
|
|
289 |
else if(ext[e] >= 'A' && ext[e] <= 'Z')
|
|
290 |
iShortName[ei++] = ext[e] ;
|
|
291 |
else if(ext[e] == '$' || ext[e] == '%' || ext[e] == '-' || ext[e] == '_' ||
|
|
292 |
ext[e] == '@' || ext[e] == '~' || ext[e] == '`' || ext[e] == '!' ||
|
|
293 |
ext[e] == '(' || ext[e] == ')' || ext[e] == '{' || ext[e] == '}' ||
|
|
294 |
ext[e] == '^' || ext[e] == '#' || ext[e] == '&' ||ext[e] == '\'')
|
|
295 |
iShortName[ei++] = ext[e] ;
|
|
296 |
}
|
|
297 |
|
|
298 |
if(iParent)
|
617
|
299 |
iParent->MakeUniqueShortName(iShortName,bl);
|
605
|
300 |
}
|
|
301 |
|
|
302 |
#ifdef _DEBUG
|
|
303 |
void TFSNode::PrintTree(int nTab) {
|
|
304 |
for( int i = 0 ; i < nTab ; i++ )
|
|
305 |
cout << " " ;
|
|
306 |
cout << (iFileName ? iFileName : "") << " [" << hex << setw(2) << setfill('0') << (unsigned short)iAttrs << "] \n" ;
|
|
307 |
if(iFirstChild)
|
|
308 |
iFirstChild->PrintTree(nTab + 2);
|
|
309 |
if(iSibling)
|
|
310 |
iSibling->PrintTree(nTab);
|
|
311 |
}
|
|
312 |
#endif
|
|
313 |
bool TFSNode::IsDirectory() const {
|
617
|
314 |
return (0 != (iAttrs & ATTR_DIRECTORY) || ATTR_VOLUME_ID == iAttrs) ;
|
605
|
315 |
}
|
|
316 |
int TFSNode::GetWideNameLength() const {
|
|
317 |
if(!iWideName)
|
|
318 |
return 0 ;
|
|
319 |
return iWideName->length() ;
|
|
320 |
}
|
|
321 |
TUint TFSNode::GetSize() const {
|
|
322 |
|
617
|
323 |
if( !IsDirectory())
|
605
|
324 |
return iFileSize ;
|
|
325 |
TUint retVal = sizeof(TShortDirEntry) ; // the tailed entry
|
|
326 |
if(iParent)
|
|
327 |
retVal += sizeof(TShortDirEntry) * 2 ;
|
|
328 |
TFSNode* child = iFirstChild ;
|
|
329 |
while(child) {
|
|
330 |
TUint longNameEntries = (child->GetWideNameLength() + KBytesPerEntry) / KBytesPerEntry ;
|
|
331 |
retVal += longNameEntries * sizeof(TLongDirEntry) ;
|
|
332 |
retVal += sizeof(TShortDirEntry);
|
|
333 |
child = child->iSibling ;
|
|
334 |
}
|
|
335 |
return retVal ;
|
|
336 |
}
|
|
337 |
|
|
338 |
void TFSNode::Init(time_t aCreateTime, time_t aAccessTime, time_t aWriteTime, TUint aSize ) {
|
|
339 |
|
|
340 |
struct tm* temp = localtime(&aCreateTime);
|
|
341 |
iCrtDate.iCurrentDate.Day = temp->tm_mday;
|
|
342 |
iCrtDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
343 |
iCrtDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
344 |
iCrtTime.iCurrentTime.Hour = temp->tm_hour;
|
|
345 |
iCrtTime.iCurrentTime.Minute = temp->tm_min;
|
|
346 |
iCrtTime.iCurrentTime.Seconds = temp->tm_sec / 2;//As per FAT spec
|
|
347 |
iCrtTimeTenth = 0;
|
|
348 |
|
|
349 |
temp = localtime(&aAccessTime);
|
|
350 |
iLstAccDate.iCurrentDate.Day = temp->tm_mday;
|
|
351 |
iLstAccDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
352 |
iLstAccDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
353 |
|
|
354 |
temp = localtime(&aWriteTime);
|
|
355 |
iWrtDate.iCurrentDate.Day = temp->tm_mday;
|
|
356 |
iWrtDate.iCurrentDate.Month = temp->tm_mon+1; //As per FAT spec
|
|
357 |
iWrtDate.iCurrentDate.Year = temp->tm_year - 80;//As per FAT spec
|
|
358 |
iWrtTime.iCurrentTime.Hour = temp->tm_hour;
|
|
359 |
iWrtTime.iCurrentTime.Minute = temp->tm_min;
|
|
360 |
iWrtTime.iCurrentTime.Seconds = temp->tm_sec / 2;//As per FAT spec
|
|
361 |
|
|
362 |
iFileSize = aSize ;
|
|
363 |
}
|
|
364 |
/** WriteDirEntries : Write FAT information for this node to a cluster buffer
|
|
365 |
* aStartIndex : [in],the beginning index of the outputed cluster
|
|
366 |
* aClusterData : [in,out] the cluster buffer
|
|
367 |
*
|
|
368 |
* notice, aClusterData is only required if node is a directory node.
|
|
369 |
* for a file node, no data will be written out.
|
|
370 |
* in this case, only corresponding cluster index information is updated.
|
|
371 |
*/
|
|
372 |
void TFSNode::WriteDirEntries(TUint aStartIndex,TUint8* aClusterData){
|
|
373 |
if(iFATEntry){
|
|
374 |
*((TUint16*)iFATEntry->DIR_FstClusHI) = (aStartIndex >> 16) ;
|
|
375 |
*((TUint16*)iFATEntry->DIR_FstClusLO) = (aStartIndex & 0xFFFF) ;
|
|
376 |
}
|
|
377 |
|
617
|
378 |
if(IsDirectory()) { // Directory , write dir entries ;
|
605
|
379 |
TShortDirEntry* entry = reinterpret_cast<TShortDirEntry*>(aClusterData);
|
|
380 |
if(iParent != NULL) {
|
|
381 |
//Make
|
|
382 |
GetShortEntry(entry);
|
|
383 |
//TODO: Add comments to avoid mistaken deleting.
|
|
384 |
memcpy(entry->DIR_Name,". ",sizeof(entry->DIR_Name));
|
|
385 |
entry ++ ;
|
|
386 |
iParent->GetShortEntry(entry);
|
|
387 |
memcpy(entry->DIR_Name,".. ",sizeof(entry->DIR_Name));
|
|
388 |
entry ++ ;
|
|
389 |
}
|
|
390 |
TFSNode* child = iFirstChild ;
|
|
391 |
while(child){
|
|
392 |
int items = child->GetLongEntries(reinterpret_cast<TLongDirEntry*>(entry));
|
|
393 |
entry += items ;
|
|
394 |
child->GetShortEntry(entry);
|
|
395 |
child->iFATEntry = entry ;
|
|
396 |
entry ++ ;
|
|
397 |
child = child->iSibling ;
|
|
398 |
|
|
399 |
}
|
|
400 |
|
|
401 |
}
|
|
402 |
}
|
|
403 |
/** GetShortEntry : Make a short directory entry (FAT16/32 conception)
|
|
404 |
* aEntry : the entry buffer
|
|
405 |
*/
|
|
406 |
void TFSNode::GetShortEntry(TShortDirEntry* aEntry) {
|
|
407 |
if(!aEntry) return ;
|
|
408 |
if(iFATEntry){
|
|
409 |
if(iFATEntry != aEntry)
|
|
410 |
memcpy(aEntry,iFATEntry,sizeof(TShortDirEntry));
|
|
411 |
return ;
|
|
412 |
}
|
|
413 |
memcpy(aEntry->DIR_Name,iShortName,sizeof(aEntry->DIR_Name));
|
|
414 |
aEntry->DIR_Attr = iAttrs;
|
|
415 |
aEntry->DIR_NTRes = 0 ;
|
|
416 |
aEntry->DIR_CrtTimeTenth = 0 ;
|
|
417 |
memcpy(aEntry->DIR_CrtTime,&iCrtTime,sizeof(aEntry->DIR_CrtTime));
|
|
418 |
memcpy(aEntry->DIR_CrtDate,&iCrtDate,sizeof(aEntry->DIR_CrtDate));
|
|
419 |
memcpy(aEntry->DIR_LstAccDate,&iLstAccDate,sizeof(aEntry->DIR_LstAccDate));
|
|
420 |
memset(aEntry->DIR_FstClusHI,0,sizeof(aEntry->DIR_FstClusHI));
|
|
421 |
memcpy(aEntry->DIR_WrtTime,&iWrtTime,sizeof(aEntry->DIR_WrtTime));
|
|
422 |
memcpy(aEntry->DIR_WrtDate,&iWrtDate,sizeof(aEntry->DIR_WrtDate));
|
|
423 |
memset(aEntry->DIR_FstClusLO,0,sizeof(aEntry->DIR_FstClusLO));
|
|
424 |
memcpy(aEntry->DIR_FileSize,&iFileSize,sizeof(aEntry->DIR_FileSize));
|
|
425 |
}
|
|
426 |
TUint8 FATChkSum(const char* pFcbName) {
|
|
427 |
short fcbNameLen ;
|
|
428 |
TUint8 sum = 0 ;
|
|
429 |
for(fcbNameLen = 11 ; fcbNameLen != 0 ; fcbNameLen --) {
|
|
430 |
sum = ((sum & 1) ? 0x80 : 0 ) + (sum >> 1 ) + *pFcbName++ ;
|
|
431 |
}
|
|
432 |
return sum ;
|
|
433 |
}
|
|
434 |
/** GetLongEntries : Make a series of long directory entries (FAT16/32 conception)
|
|
435 |
* aEntries : the start addr of the long directory entries buffer
|
|
436 |
*
|
|
437 |
* return value : actual entris count.
|
|
438 |
*/
|
|
439 |
int TFSNode::GetLongEntries(TLongDirEntry* aEntries) {
|
|
440 |
|
|
441 |
if(!aEntries) return 0;
|
|
442 |
int packs = (GetWideNameLength() + KBytesPerEntry) / KBytesPerEntry ;
|
|
443 |
|
|
444 |
TUint buflen = packs * KBytesPerEntry;
|
|
445 |
TUint16* buffer = new(std::nothrow) TUint16[buflen];
|
|
446 |
if(!buffer)
|
|
447 |
return 0 ;
|
|
448 |
memset(buffer,0xff,(buflen << 1));
|
|
449 |
if(iWideName) {
|
|
450 |
memcpy(buffer,iWideName->c_str(),iWideName->bytes());
|
|
451 |
buffer[iWideName->length()] = 0;
|
|
452 |
}
|
|
453 |
TUint8 chkSum = FATChkSum(iShortName);;
|
|
454 |
|
|
455 |
TUint16* ptr = buffer ;
|
|
456 |
TLongDirEntry* entry = aEntries +(packs - 1);
|
|
457 |
for(int i = 1 ; i <= packs ; i++, entry--) {
|
|
458 |
entry->LDIR_Ord = i ;
|
|
459 |
entry->LDIR_Chksum = chkSum ;
|
|
460 |
entry->LDIR_Attr = (TUint8)ATTR_LONG_NAME;
|
|
461 |
*((TUint16*)(entry->LDIR_FstClusLO)) = 0;
|
|
462 |
entry->LDIR_Type = 0;
|
|
463 |
memcpy(entry->LDIR_Name1,ptr,10);
|
|
464 |
memcpy(entry->LDIR_Name2,&ptr[5],12);
|
|
465 |
memcpy(entry->LDIR_Name3,&ptr[11],4);
|
|
466 |
ptr += 13;
|
|
467 |
}
|
|
468 |
aEntries->LDIR_Ord |= 0x40 ;
|
|
469 |
|
|
470 |
delete []buffer ;
|
|
471 |
return packs ;
|
|
472 |
}
|
|
473 |
/** Make a unique name for a new child which has not been added.
|
|
474 |
* to avoid same short names under a directory
|
|
475 |
* rShortName : [in,out] , The new short name to be checked and changed.
|
|
476 |
* baseNameLength: [in], the length of the base part of the short name
|
|
477 |
* not including the "~n"
|
|
478 |
* for example,
|
|
479 |
* "ABC.LOG" => baseNameLength == 3 ("ABC")
|
|
480 |
* "AB~1.TXT" => baseNameLength == 2 ("AB")
|
|
481 |
*
|
|
482 |
*
|
|
483 |
*The Numeric-Tail Generation Algorithm
|
|
484 |
|
|
485 |
* If (a "lossy conversion" was not flagged)
|
|
486 |
* and (the long name fits within the 8.3 naming conventions)
|
|
487 |
* and (the basis-name does not collide with any existing short name)
|
|
488 |
* {
|
|
489 |
* The short name is only the basis-name without the numeric tail.
|
|
490 |
* }
|
|
491 |
* else {
|
|
492 |
* Insert a numeric-tail "~n" to the end of the primary name such that the value of
|
|
493 |
* the "~n" is chosen so that the name thus formed does not collide with
|
|
494 |
* any existing short name and that the primary name does not exceed eight
|
|
495 |
* characters in length.
|
|
496 |
* }
|
|
497 |
* The "~n" string can range from "~1" to "~999999".
|
|
498 |
*
|
|
499 |
*/
|
|
500 |
|
|
501 |
void TFSNode::MakeUniqueShortName(char rShortName[12],TUint baseNameLength) const {
|
|
502 |
bool dup ;
|
|
503 |
char nstring[10];
|
|
504 |
int n = 0 ;
|
|
505 |
do {
|
|
506 |
TFSNode* child = iFirstChild ;
|
|
507 |
dup = false ;
|
|
508 |
while(child){
|
|
509 |
if(0 == memcmp(rShortName,child->iShortName,11)) {
|
|
510 |
dup = true ;
|
|
511 |
break ;
|
|
512 |
}
|
|
513 |
child = child->iSibling ;
|
|
514 |
}
|
|
515 |
if(dup){ //duplex , increase the index , make a new name
|
|
516 |
int nlen = sprintf(nstring,"~%u",++n);
|
|
517 |
while((baseNameLength + nlen > 8) && baseNameLength > 1)
|
|
518 |
baseNameLength -- ;
|
|
519 |
memcpy(&rShortName[baseNameLength],nstring,nlen);
|
|
520 |
|
|
521 |
}
|
|
522 |
}while(dup) ;
|
|
523 |
|
|
524 |
}
|
|
525 |
|