removed duplicate hashing function. Now the hash buffer is always 512 bytes.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4038 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2011-02-14 19:59:06 +00:00
parent acf536317f
commit 9de341953d
5 changed files with 13 additions and 80 deletions

View File

@ -846,7 +846,7 @@ void FileIndexMonitor::hashFiles(const std::vector<DirContentToHash>& to_hash)
//
if(useHashCache && hashCache.find(real_path,fe.size,fe.modtime,fe.hash))
fi.updateFileEntry(to_hash[i].dirpath,fe,stamp);
else if(RsDirUtil::hashFile(real_path, fe.hash)) // not found, then hash it.
else if(RsDirUtil::getFileHash(real_path, fe.hash,fe.size)) // not found, then hash it.
{
RsStackMutex stack(fiMutex); /**** LOCKED DIRS ****/

View File

@ -278,6 +278,8 @@ int ftFileCreator::locked_initializeFileAttrs()
}
ftFileCreator::~ftFileCreator()
{
std::cerr << "Deleting file creator for " << file_name << std::endl;
// Note: The file is actually closed in the parent, that is always a ftFileProvider.
//
/*
@ -495,6 +497,7 @@ bool ftFileCreator::finished()
bool ftFileCreator::hashReceivedData(std::string& hash)
{
std::cerr << "file creator " << hash << " asked for hashing received data " << file_name << std::endl;
// csoler: No mutex here please !
//
// This is a bit dangerous, but otherwise we might stuck the GUI for a
@ -502,9 +505,13 @@ bool ftFileCreator::hashReceivedData(std::string& hash)
// at a time file_name nor hash can be modified, which is easy.
//
if(!finished())
{
std::cerr << "Transfer not finished !! This should not happen" << std::endl;
return false ;
}
return RsDirUtil::hashFile(file_name,hash) ;
uint64_t tmpsize ;
return RsDirUtil::getFileHash(file_name,hash,tmpsize) ;
}
bool ftFileCreator::crossCheckChunkMap(const CRC32Map& ref,uint32_t& bad_chunks,uint32_t& incomplete_chunks)

View File

@ -26,6 +26,7 @@
/******
* #define FT_DEBUG 1
*****/
#define FT_DEBUG 1
#include "retroshare/rsturtle.h"
#include "fttransfermodule.h"

View File

@ -54,7 +54,6 @@
/****
* #define RSDIR_DEBUG 1
****/
#define RSDIR_DEBUG 1
std::string RsDirUtil::getTopDir(const std::string& dir)
{
@ -157,74 +156,6 @@ bool RsDirUtil::crc32File(FILE *fd, uint64_t file_size,uint32_t chunk_size, CRC3
return true ;
}
bool RsDirUtil::hashFile(const std::string& f_hash, std::string& hash)
{
FILE *fd;
int len;
SHA_CTX *sha_ctx = new SHA_CTX;
unsigned char sha_buf[SHA_DIGEST_LENGTH];
unsigned char *gblBuf = new unsigned char[1024*1024*10]; // 10MB buffer.
#ifdef FIM_DEBUG
std::cerr << "File to hash = " << f_hash << std::endl;
#endif
#ifdef WINDOWS_SYS
std::wstring wf_hash;
librs::util::ConvertUtf8ToUtf16(f_hash, wf_hash);
if (NULL == (fd = _wfopen(wf_hash.c_str(), L"rb")))
goto hashing_failed ;
#else
if (NULL == (fd = fopen64(f_hash.c_str(), "rb")))
goto hashing_failed ;
#endif
SHA1_Init(sha_ctx);
while((len = fread(gblBuf,1, 512, fd)) > 0)
{
SHA1_Update(sha_ctx, gblBuf, len);
}
/* reading failed for some reason */
if (ferror(fd))
{
#ifdef FIM_DEBUG
std::cerr << "read error !!" << std::endl;
#endif
goto hashing_failed ;
}
SHA1_Final(&sha_buf[0], sha_ctx);
/* TODO: Actually we should store the hash data as binary ...
* but then it shouldn't be put in a string.
*/
{
std::ostringstream tmpout;
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
tmpout << std::setw(2) << std::setfill('0') << std::hex << (unsigned int) (sha_buf[i]);
}
hash = tmpout.str();
}
delete sha_ctx;
delete[] gblBuf ;
fclose(fd);
return true;
hashing_failed:
delete sha_ctx;
delete[] gblBuf ;
if(fd != NULL)
fclose(fd) ;
return false ;
}
std::string RsDirUtil::removeTopDir(const std::string& dir)
{
std::string rest;
@ -595,8 +526,7 @@ bool RsDirUtil::hashFile(const std::string& filepath,
#include <iomanip>
/* Function to hash, and get details of a file */
bool RsDirUtil::getFileHash(const std::string& filepath,
std::string &hash, uint64_t &size)
bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint64_t &size)
{
FILE *fd;
int len;

View File

@ -42,8 +42,6 @@ std::string removeRootDir(const std::string& path);
std::string removeTopDir(const std::string& dir);
std::string removeRootDirs(const std::string& path, const std::string& root);
bool hashFile(const std::string& full_path,std::string& hash) ;
// Renames file from to file to. Files should be on the same file system.
// returns true if succeed, false otherwise.
bool renameFile(const std::string& from,const std::string& to) ;
@ -65,11 +63,8 @@ bool checkDirectory(const std::string& dir);
bool checkCreateDirectory(const std::string& dir);
bool cleanupDirectory(const std::string& dir, const std::list<std::string> &keepFiles);
bool hashFile(const std::string& filepath,
std::string &name, std::string &hash, uint64_t &size);
bool getFileHash(const std::string& filepath,
std::string &hash, uint64_t &size);
bool hashFile(const std::string& filepath, std::string &name, std::string &hash, uint64_t &size);
bool getFileHash(const std::string& filepath,std::string &hash, uint64_t &size);
std::wstring getWideTopDir(std::wstring);