//Get the directory level given a string. //For the string only contains the file system identifier and only one file name, //which resides in root directory,the level is 0. BOOL GetFullNameLevel(CHAR* pFullName,DWORD* pdwLevel) { int i = 0; DWORD dwLevel = 0; if((NULL == pFullName) || (NULL == pdwLevel)) //Invalid parameters. { return FALSE; } if(!NameIsValid(pFullName)) //Is not a valid file name. { return FALSE; } while(pFullName[i]) { if(pFullName[i] == '\\') { dwLevel += 1; } i ++; } *pdwLevel = dwLevel - 1; //Substract the splitter between FS identifier and name. return TRUE; }
//Segment one full file name into directory part and file name part. //pDir and pFileName must be long enough to contain the result. //If the full name's level is zero,that no subdirectory in the full name, //the pDir[0] will contain the file system identifier to indicate this. //If the full name only contain a directory,i.e,the last character of //the full name is a slash character,then the pFileName[0] will be set to 0. BOOL GetPathName(CHAR* pFullName,CHAR* pDir,CHAR* pFileName) { BYTE DirName[MAX_FILE_NAME_LEN]; BYTE FileName[13]; BYTE tmp; int i = 0; int j = 0; if((NULL == pFullName) || (NULL == pDir) || (NULL == pFileName)) //Invalid parameters. { return FALSE; } if(!NameIsValid(pFullName)) //Not a valid file name. { return FALSE; } //strcpy((CHAR*)&DirName[0],(CHAR*)pFullName); StrCpy((CHAR*)pFullName,(CHAR*)&DirName); i = StrLen((CHAR*)pFullName); i -= 1; if(pFullName[i] == '\\') //The last character is a splitter,means only dir name present. { //DirName[i] = 0; //Eliminate the slash. //strcpy((CHAR*)pDir,(CHAR*)&DirName[0]); StrCpy((CHAR*)&DirName[0],(CHAR*)pDir); pFileName[0] = 0; return TRUE; } j = 0; while(pFullName[i] != '\\') //Get the file name part. { FileName[j ++] = pFullName[i --]; } DirName[i + 1] = 0; //Keep the slash. FileName[j] = 0; //Now reverse the file name string. for(i = 0;i < (j / 2);i ++) { tmp = FileName[i]; FileName[i] = FileName[j - i - 1]; FileName[j - i - 1] = tmp; } //strcpy((CHAR*)pDir,(CHAR*)&DirName[0]); StrCpy((CHAR*)&DirName[0],(CHAR*)pDir); //strcpy((CHAR*)pFileName,(CHAR*)&FileName[0]); StrCpy((CHAR*)&FileName[0],(CHAR*)pFileName); return TRUE; }
void FriendAdd(LPSESS sess, const char *username) { const char *pztail; sqlite3_stmt *pstmt; LPFRIEND pfriend; char buf[128]; int rc; if (!strcasecmp(sess->username, username)) { Send0x0F(sess, EID_ERROR, 0, "", RESPONSE_CANTADDSELF); return; } if (!NameIsValid(username)) { Send0x0F(sess, EID_ERROR, 0, "", RESPONSE_INVALUSER); return; } if (sess->friends && sess->friends->numelem >= 25) { Send0x0F(sess, EID_ERROR, 0, "", RESPONSE_TOOMANYFRND); return; } pfriend = malloc(sizeof(FRIEND)); strncpy(pfriend->name, username, sizeof(pfriend->name)); pfriend->name[sizeof(pfriend->name) - 1] = 0; pfriend->mutual = FriendUpdateMutual(sess, pfriend->name, 1); sess->friends = VectorAdd(sess->friends, pfriend); rc = sqlite3_prepare_v2(db_accounts, "INSERT INTO friends(user, pos, mutual, friend) VALUES(?, ?, ?, ?)", -1, &pstmt, &pztail); if (rc != SQLITE_OK) { printf("sqlite3_prepare_v2() failed in FriendAdd(): %s\n", sqlite3_errmsg(db_accounts)); return; } sqlite3_bind_text(pstmt, 1, sess->username, -1, SQLITE_STATIC); sqlite3_bind_int(pstmt, 2, sess->friends->numelem); sqlite3_bind_int(pstmt, 3, pfriend->mutual); sqlite3_bind_text(pstmt, 4, pfriend->name, -1, SQLITE_STATIC); sqlite3_step(pstmt); sqlite3_finalize(pstmt); sprintf(buf, "Added %s to your friends list.", pfriend->name); Send0x0F(sess, EID_INFO, 0, "", buf); }
//Get the directory entry of a full file name. //The difference between this routine and GetShortEntry is,the last one only //search the directory designated by start cluster. //But this one will search the whole file system tree to seek the target. BOOL GetDirEntry(__FAT32_FS* pFat32Fs,CHAR* pFullName, __FAT32_SHORTENTRY* pfse,DWORD* pDirClus,DWORD* pDirOffset) { __FAT32_SHORTENTRY ShortEntry = {0}; CHAR SubDir[MAX_FILE_NAME_LEN]; CHAR buffer[MAX_FILE_NAME_LEN]; // BOOL bResult = FALSE; DWORD dwLevel = 0; BYTE* pBuffer = 0; DWORD dwStartClus = 0; //Start cluster of current directory to search. DWORD dwSector = 0; int i; if((NULL == pFat32Fs) || (NULL == pFullName) || (NULL == pfse)) { goto __TERMINAL; } if(!NameIsValid(pFullName)) //Is not a valid full file name. { goto __TERMINAL; } if(!GetFullNameLevel(pFullName,&dwLevel)) { goto __TERMINAL; } i = 1; dwStartClus = pFat32Fs->dwRootDirClusStart; //Initialize the short entry as root directory. ShortEntry.FileAttributes = FILE_ATTR_DIRECTORY; ShortEntry.wFirstClusHi = (WORD)(dwStartClus >> 16); ShortEntry.wFirstClusLow = (WORD)dwStartClus; while(dwLevel) { if(!GetSubDirectory(pFullName,i,SubDir)) //Get the sub-directory. { //PrintLine(" In GetDirEntry: GetSubDirectory failed."); goto __TERMINAL; } if(!GetShortEntry(pFat32Fs,dwStartClus,SubDir,&ShortEntry,pDirClus,pDirOffset)) { goto __TERMINAL; } dwStartClus = MAKELONG(ShortEntry.wFirstClusLow,ShortEntry.wFirstClusHi); //dwStartClus = (DWORD)ShortEntry.wFirstClusHi; //dwStartClus = dwStartClus << 16; //dwStartClus = dwStartClus + (DWORD)ShortEntry.wFirstClusLow; dwLevel --; i ++; } if(!GetPathName(pFullName,SubDir,buffer)) { // PrintLine(" In GetDirEntry: GetPathName failed."); goto __TERMINAL; } if(0 == buffer[0]) //The target is a directory. { memcpy((char*)pfse,(const char*)&ShortEntry,sizeof(__FAT32_SHORTENTRY)); bResult = TRUE; goto __TERMINAL; } if(!GetShortEntry(pFat32Fs,dwStartClus,buffer,&ShortEntry,pDirClus,pDirOffset)) { /* PrintLine(" In GetDirEntry: GetShortEntry failed,next one."); sprintf(Buffer," Parameters: start clus = %d,SubDir = %s",dwStartClus,SubDir); PrintLine(Buffer);*/ goto __TERMINAL; } memcpy((char*)pfse,(const char*)&ShortEntry,sizeof(__FAT32_SHORTENTRY)); bResult = TRUE; __TERMINAL: if(pBuffer) { free(pBuffer); } return bResult; }