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