bool OnFile( OnFileStruct *onFileStruct) { printf("OnFile: %i. (100%%) %i/%i %s %ib / %ib\n", onFileStruct->setID, onFileStruct->fileIndex+1, onFileStruct->numberOfFilesInThisSet, onFileStruct->fileName, onFileStruct->byteLengthOfThisFile, onFileStruct->byteLengthOfThisSet); FILE *fp = fopen(fileCopy.C_String(), "wb"); fwrite(onFileStruct->fileData, onFileStruct->byteLengthOfThisFile, 1, fp); fclose(fp); // Make sure it worked unsigned int hash1 = SuperFastHashFile(file.C_String()); if (RakNet::BitStream::DoEndianSwap()) RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash1, sizeof(hash1)); unsigned int hash2 = SuperFastHashFile(fileCopy.C_String()); if (RakNet::BitStream::DoEndianSwap()) RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash2, sizeof(hash2)); RakAssert(hash1==hash2); // Return true to have RakNet delete the memory allocated to hold this file. // False if you hold onto the memory, and plan to delete it yourself later return true; }
void FileList::AddFilesFromDirectory(const char *applicationDirectory, const char *subDirectory, bool writeHash, bool writeData, bool recursive, FileListNodeContext context) { DataStructures::Queue<char*> dirList; char root[260]; char fullPath[520]; _finddata_t fileInfo; intptr_t dir; FILE *fp; char *dirSoFar, *fileData; dirSoFar=(char*) rakMalloc_Ex( 520, __FILE__, __LINE__ ); if (applicationDirectory) strcpy(root, applicationDirectory); else root[0]=0; int rootLen=(int)strlen(root); if (rootLen) { strcpy(dirSoFar, root); if (FixEndingSlash(dirSoFar)) rootLen++; } else dirSoFar[0]=0; if (subDirectory) { strcat(dirSoFar, subDirectory); FixEndingSlash(dirSoFar); } if (callback) callback->OnAddFilesFromDirectoryStarted(this, dirSoFar); // RAKNET_DEBUG_PRINTF("Adding files from directory %s\n",dirSoFar); dirList.Push(dirSoFar, __FILE__, __LINE__ ); while (dirList.Size()) { dirSoFar=dirList.Pop(); strcpy(fullPath, dirSoFar); // Changed from *.* to * for Linux compatibility strcat(fullPath, "*"); dir=_findfirst(fullPath, &fileInfo ); if (dir==-1) { _findclose(dir); rakFree_Ex(dirSoFar, __FILE__, __LINE__ ); unsigned i; for (i=0; i < dirList.Size(); i++) rakFree_Ex(dirList[i], __FILE__, __LINE__ ); return; } // RAKNET_DEBUG_PRINTF("Adding %s. %i remaining.\n", fullPath, dirList.Size()); if (callback) callback->OnDirectory(this, fullPath, dirList.Size()); do { // no guarantee these entries are first... if (strcmp("." , fileInfo.name) == 0 || strcmp("..", fileInfo.name) == 0) { continue; } if ((fileInfo.attrib & (_A_HIDDEN | _A_SUBDIR | _A_SYSTEM))==0) { strcpy(fullPath, dirSoFar); strcat(fullPath, fileInfo.name); fileData=0; if (callback) callback->OnFile(this, dirSoFar, fileInfo.name, fileInfo.size); if (writeData && writeHash) { fileData= (char*) rakMalloc_Ex( fileInfo.size+HASH_LENGTH, __FILE__, __LINE__ ); fp = fopen(fullPath, "rb"); fread(fileData+HASH_LENGTH, fileInfo.size, 1, fp); fclose(fp); unsigned int hash = SuperFastHash(fileData+HASH_LENGTH, fileInfo.size); if (RakNet::BitStream::DoEndianSwap()) RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash, sizeof(hash)); memcpy(fileData, &hash, HASH_LENGTH); // sha1.Reset(); // sha1.Update( ( unsigned char* ) fileData+HASH_LENGTH, fileInfo.size ); // sha1.Final(); // memcpy(fileData, sha1.GetHash(), HASH_LENGTH); // File data and hash AddFile((const char*)fullPath+rootLen, fullPath, fileData, fileInfo.size+HASH_LENGTH, fileInfo.size, context); } else if (writeHash) { // sha1.Reset(); // sha1.HashFile((char*)fullPath); // sha1.Final(); unsigned int hash = SuperFastHashFile(fullPath); if (RakNet::BitStream::DoEndianSwap()) RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash, sizeof(hash)); // Hash only // AddFile((const char*)fullPath+rootLen, (const char*)sha1.GetHash(), HASH_LENGTH, fileInfo.size, context); AddFile((const char*)fullPath+rootLen, fullPath, (const char*)&hash, HASH_LENGTH, fileInfo.size, context); } else if (writeData) { fileData= (char*) rakMalloc_Ex( fileInfo.size, __FILE__, __LINE__ ); fp = fopen(fullPath, "rb"); fread(fileData, fileInfo.size, 1, fp); fclose(fp); // File data only AddFile(fullPath+rootLen, fullPath, fileData, fileInfo.size, fileInfo.size, context); } else { // Just the filename AddFile(fullPath+rootLen, fullPath, 0, 0, fileInfo.size, context); } if (fileData) rakFree_Ex(fileData, __FILE__, __LINE__ ); } else if ((fileInfo.attrib & _A_SUBDIR) && (fileInfo.attrib & (_A_HIDDEN | _A_SYSTEM))==0 && recursive) { char *newDir=(char*) rakMalloc_Ex( 520, __FILE__, __LINE__ ); strcpy(newDir, dirSoFar); strcat(newDir, fileInfo.name); strcat(newDir, "/"); dirList.Push(newDir, __FILE__, __LINE__ ); } } while (_findnext(dir, &fileInfo ) != -1); _findclose(dir); rakFree_Ex(dirSoFar, __FILE__, __LINE__ ); } }