2008-08-09 17:03:24 +00:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: ftdbase.cc
|
|
|
|
*
|
|
|
|
* File Transfer for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ft/ftdbase.h"
|
|
|
|
#include "util/rsdir.h"
|
|
|
|
|
2008-10-29 20:58:23 +00:00
|
|
|
#define DB_DEBUG 1
|
2008-08-09 17:03:24 +00:00
|
|
|
|
|
|
|
ftFiStore::ftFiStore(CacheStrapper *cs, CacheTransfer *cft, NotifyBase *cb_in,
|
|
|
|
RsPeerId ownid, std::string cachedir)
|
|
|
|
:FileIndexStore(cs, cft, cb_in, ownid, cachedir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftFiStore::search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const
|
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
/* could use hintflags to specify which bits of fileinfo to use additionally.
|
|
|
|
eg. hintflags & FT_SEARCH_PEER_ID, then only return matching peers + hash.
|
|
|
|
eg. hintflags & FT_SEARCH_NAME, then only return matching name + hash.
|
|
|
|
*
|
|
|
|
* Still to see if concept is worthwhle
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftFiStore::search(" << hash << "," << size << "," << hintflags;
|
|
|
|
std::cerr << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 17:03:24 +00:00
|
|
|
std::list<FileDetail> results;
|
|
|
|
std::list<FileDetail>::iterator it;
|
|
|
|
|
|
|
|
if (SearchHash(hash, results))
|
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
bool first = true;
|
2008-08-09 17:03:24 +00:00
|
|
|
for(it = results.begin(); it != results.end(); it++)
|
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftFiStore::search() found: ";
|
|
|
|
std::cerr << it->name << " (" << it->size;
|
|
|
|
std::cerr << ") @ " << it->id << " = " << hash;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
bool fullmatch = true;
|
|
|
|
|
|
|
|
if (it->size != size)
|
|
|
|
fullmatch = false;
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (hintflags & FT_SEARCH_PEER_ID)
|
2008-08-09 17:03:24 +00:00
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
pit = std::find(info.srcIds.begin(),
|
|
|
|
info.srcId.end(). it->id);
|
|
|
|
if (pit == info.srcIds.end())
|
|
|
|
{
|
|
|
|
fullmatch = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 17:03:24 +00:00
|
|
|
|
2008-10-29 20:58:23 +00:00
|
|
|
if (fullmatch)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
info.fname = it->name;
|
|
|
|
info.size = it->size;
|
|
|
|
info.hash = it->hash;
|
2008-11-02 11:38:11 +00:00
|
|
|
|
2008-10-29 20:58:23 +00:00
|
|
|
}
|
2008-11-02 11:38:11 +00:00
|
|
|
|
|
|
|
TransferInfo ti;
|
|
|
|
ti.peerId = it->id;
|
|
|
|
ti.name = it->name;
|
|
|
|
ti.tfRate = 0;
|
|
|
|
info.peers.push_back(ti);
|
2008-08-09 17:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-29 20:58:23 +00:00
|
|
|
|
2008-11-02 11:38:11 +00:00
|
|
|
/**** DEPENDS ON SOURCES!
|
|
|
|
info.downloadStatus = FT_STATE_COMPLETE:
|
|
|
|
****/
|
|
|
|
|
2008-10-29 20:58:23 +00:00
|
|
|
/* if the first flag is cleared, we've definitely
|
|
|
|
* had a full match!.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
return true;
|
2008-08-09 17:03:24 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ftFiMonitor::ftFiMonitor(CacheStrapper *cs, std::string cachedir, std::string pid)
|
|
|
|
:FileIndexMonitor(cs, cachedir, pid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftFiMonitor::search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const
|
|
|
|
{
|
|
|
|
uint64_t fsize;
|
|
|
|
std::string path;
|
|
|
|
|
2008-10-29 20:58:23 +00:00
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftFiMonitor::search(" << hash << "," << size << "," << hintflags;
|
|
|
|
std::cerr << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 17:03:24 +00:00
|
|
|
if (findLocalFile(hash, path, fsize))
|
|
|
|
{
|
|
|
|
/* fill in details */
|
2008-10-29 20:58:23 +00:00
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftFiMonitor::search() found: ";
|
|
|
|
std::cerr << path;
|
|
|
|
std::cerr << " = " << hash << "," << fsize;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-09 17:03:24 +00:00
|
|
|
|
|
|
|
info.size = fsize;
|
|
|
|
info.fname = RsDirUtil::getTopDir(path);
|
|
|
|
info.path = path;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ftCacheStrapper::ftCacheStrapper(p3AuthMgr *am, p3ConnectMgr *cm)
|
|
|
|
:CacheStrapper(am, cm)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overloaded search function */
|
|
|
|
bool ftCacheStrapper::search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const
|
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftCacheStrapper::search(" << hash << "," << size << "," << hintflags;
|
|
|
|
std::cerr << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 17:03:24 +00:00
|
|
|
CacheData data;
|
|
|
|
if (findCache(hash, data))
|
|
|
|
{
|
2008-10-29 20:58:23 +00:00
|
|
|
#ifdef DB_DEBUG
|
|
|
|
std::cerr << "ftCacheStrapper::search() found: ";
|
|
|
|
std::cerr << data.path << "/" << data.name;
|
|
|
|
std::cerr << " = " << data.hash << "," << data.size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 17:03:24 +00:00
|
|
|
/* ... */
|
|
|
|
info.size = data.size;
|
|
|
|
info.fname = data.name;
|
|
|
|
info.path = data.path + "/" + data.name;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|