2008-07-02 09:19:59 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: ftextralist.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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
#include "ft/ftextralist.h"
|
2008-11-15 15:00:29 -05:00
|
|
|
#include "serialiser/rsconfigitems.h"
|
2008-07-23 18:01:59 -04:00
|
|
|
#include "util/rsdir.h"
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-11-18 16:22:58 -05:00
|
|
|
/******
|
|
|
|
* #define DEBUG_ELIST 1
|
|
|
|
*****/
|
2008-08-03 08:45:53 -04:00
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
ftExtraList::ftExtraList()
|
2008-11-15 15:00:29 -05:00
|
|
|
:p3Config(CONFIG_TYPE_FT_EXTRA_LIST)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ftExtraList::run()
|
|
|
|
{
|
|
|
|
bool todo = false;
|
|
|
|
time_t cleanup = 0;
|
|
|
|
time_t now = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
2008-11-02 06:38:11 -05:00
|
|
|
//std::cerr << "ftExtraList::run() Iteration";
|
|
|
|
//std::cerr << std::endl;
|
2008-08-03 08:45:53 -04:00
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
todo = (mToHash.size() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (todo)
|
|
|
|
{
|
|
|
|
/* Hash a file */
|
|
|
|
hashAFile();
|
|
|
|
|
2008-08-25 16:03:39 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(1);
|
|
|
|
#else
|
2008-07-02 09:19:59 -04:00
|
|
|
/* microsleep */
|
|
|
|
usleep(10);
|
2008-08-25 16:03:39 -04:00
|
|
|
#endif
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* cleanup */
|
|
|
|
if (cleanup < now)
|
|
|
|
{
|
|
|
|
cleanupOldFiles();
|
|
|
|
cleanup = now + CLEANUP_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sleep */
|
2008-08-25 16:03:39 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(1000);
|
|
|
|
#else
|
2008-07-02 09:19:59 -04:00
|
|
|
sleep(1);
|
2008-08-25 16:03:39 -04:00
|
|
|
#endif
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ftExtraList::hashAFile()
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::hashAFile()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* extract entry from the queue */
|
2008-07-23 18:01:59 -04:00
|
|
|
FileDetails details;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(extMutex);
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
if (mToHash.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
details = mToHash.front();
|
2008-07-02 09:19:59 -04:00
|
|
|
mToHash.pop_front();
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "Hashing: " << details.info.path;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* hash it! */
|
2008-07-23 18:01:59 -04:00
|
|
|
std::string name, hash;
|
2008-08-09 13:03:24 -04:00
|
|
|
//uint64_t size;
|
2008-07-23 18:01:59 -04:00
|
|
|
if (RsDirUtil::hashFile(details.info.path, details.info.fname,
|
|
|
|
details.info.hash, details.info.size))
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-07-23 18:01:59 -04:00
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* stick it in the available queue */
|
2008-07-23 18:01:59 -04:00
|
|
|
mFiles[details.info.hash] = details;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
/* add to the path->hash map */
|
2008-07-23 18:01:59 -04:00
|
|
|
mHashedList[details.info.path] = details.info.hash;
|
2008-11-15 15:00:29 -05:00
|
|
|
|
|
|
|
IndicateConfigChanged();
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* If the File is alreay Hashed, then just add it in.
|
|
|
|
**/
|
|
|
|
|
|
|
|
bool ftExtraList::addExtraFile(std::string path, std::string hash,
|
|
|
|
uint64_t size, uint32_t period, uint32_t flags)
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::addExtraFile() path: " << path;
|
|
|
|
std::cerr << " hash: " << hash;
|
|
|
|
std::cerr << " size: " << size;
|
|
|
|
std::cerr << " period: " << period;
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
FileDetails details;
|
|
|
|
|
|
|
|
details.info.path = path;
|
|
|
|
details.info.fname = RsDirUtil::getTopDir(path);
|
|
|
|
details.info.hash = hash;
|
|
|
|
details.info.size = size;
|
2008-11-15 15:00:29 -05:00
|
|
|
details.info.age = time(NULL) + period; /* if time > this... cleanup */
|
2008-07-23 18:01:59 -04:00
|
|
|
details.flags = flags;
|
|
|
|
|
|
|
|
/* stick it in the available queue */
|
|
|
|
mFiles[details.info.hash] = details;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
IndicateConfigChanged();
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
return true;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftExtraList::removeExtraFile(std::string hash, uint32_t flags)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::removeExtraFile()";
|
|
|
|
std::cerr << " hash: " << hash;
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
std::map<std::string, FileDetails>::iterator it;
|
|
|
|
it = mFiles.find(hash);
|
|
|
|
if (it == mFiles.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFiles.erase(it);
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
IndicateConfigChanged();
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
bool ftExtraList::cleanupOldFiles()
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::cleanupOldFiles()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
std::list<std::string> toRemove;
|
|
|
|
std::list<std::string>::iterator rit;
|
|
|
|
|
|
|
|
std::map<std::string, FileDetails>::iterator it;
|
|
|
|
for(it = mFiles.begin(); it != mFiles.end(); it++)
|
|
|
|
{
|
|
|
|
/* check timestamps */
|
2008-11-15 15:00:29 -05:00
|
|
|
if (it->second.info.age < (unsigned) now)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
toRemove.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toRemove.size() > 0)
|
|
|
|
{
|
|
|
|
/* remove items */
|
|
|
|
for(rit = toRemove.begin(); rit != toRemove.end(); rit++)
|
|
|
|
{
|
|
|
|
if (mFiles.end() != (it = mFiles.find(*rit)))
|
|
|
|
{
|
2008-11-15 15:00:29 -05:00
|
|
|
cleanupEntry(it->second.info.path, it->second.flags);
|
2008-07-02 09:19:59 -04:00
|
|
|
mFiles.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2008-11-15 15:00:29 -05:00
|
|
|
IndicateConfigChanged();
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
2008-08-03 08:45:53 -04:00
|
|
|
return true;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
bool ftExtraList::cleanupEntry(std::string path, uint32_t flags)
|
|
|
|
{
|
|
|
|
if (flags & RS_FILE_CONFIG_CLEANUP_DELETE)
|
|
|
|
{
|
|
|
|
/* Delete the file? - not yet! */
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/***
|
|
|
|
* Hash file, and add to the files,
|
|
|
|
* file is removed after period.
|
|
|
|
**/
|
|
|
|
|
|
|
|
bool ftExtraList::hashExtraFile(std::string path, uint32_t period, uint32_t flags)
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::hashExtraFile() path: " << path;
|
|
|
|
std::cerr << " period: " << period;
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* add into queue */
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
FileDetails details(path, period, flags);
|
2008-11-15 15:00:29 -05:00
|
|
|
details.info.age = time(NULL) + period;
|
2008-07-02 09:19:59 -04:00
|
|
|
mToHash.push_back(details);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
bool ftExtraList::hashExtraFileDone(std::string path, FileInfo &info)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::hashExtraFileDone()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
std::string hash;
|
|
|
|
{
|
|
|
|
/* Find in the path->hash map */
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
std::map<std::string, std::string>::iterator it;
|
|
|
|
if (mHashedList.end() == (it = mHashedList.find(path)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hash = it->second;
|
|
|
|
}
|
2008-08-03 08:45:53 -04:00
|
|
|
return search(hash, 0, 0, info);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Search Function - used by File Transfer
|
|
|
|
*
|
|
|
|
**/
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftExtraList::search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::search()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* find hash */
|
2008-08-03 08:45:53 -04:00
|
|
|
std::map<std::string, FileDetails>::const_iterator fit;
|
2008-07-02 09:19:59 -04:00
|
|
|
if (mFiles.end() == (fit = mFiles.find(hash)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
info = fit->second.info;
|
2008-07-02 09:19:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Configuration - store extra files.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
RsSerialiser *ftExtraList::setupSerialiser()
|
|
|
|
{
|
2008-11-15 15:00:29 -05:00
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
|
|
|
|
/* add in the types we need! */
|
|
|
|
rss->addSerialType(new RsFileConfigSerialiser());
|
|
|
|
return rss;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::list<RsItem *> ftExtraList::saveList(bool &cleanup)
|
|
|
|
{
|
|
|
|
std::list<RsItem *> sList;
|
2008-11-15 15:00:29 -05:00
|
|
|
|
|
|
|
cleanup = true;
|
|
|
|
|
|
|
|
/* called after each item is added */
|
|
|
|
|
|
|
|
/* create a list of fileitems with
|
|
|
|
* age used to specify its timeout.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::saveList()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, FileDetails>::const_iterator it;
|
|
|
|
for(it = mFiles.begin(); it != mFiles.end(); it++)
|
|
|
|
{
|
|
|
|
RsFileConfigItem *fi = new RsFileConfigItem();
|
|
|
|
fi->file.path = (it->second).info.path;
|
|
|
|
fi->file.name = (it->second).info.fname;
|
|
|
|
fi->file.hash = (it->second).info.hash;
|
|
|
|
fi->file.filesize = (it->second).info.size;
|
|
|
|
fi->file.age = (it->second).info.age;
|
|
|
|
fi->flags = (it->second).flags;
|
|
|
|
|
|
|
|
sList.push_back(fi);
|
|
|
|
}
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
return sList;
|
|
|
|
}
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
bool ftExtraList::loadList(std::list<RsItem *> load)
|
|
|
|
{
|
2008-11-15 15:00:29 -05:00
|
|
|
/* for each item, check it exists ....
|
|
|
|
* - remove any that are dead (or flag?)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_ELIST
|
|
|
|
std::cerr << "ftExtraList::loadList()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_t ts = time(NULL);
|
|
|
|
|
|
|
|
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
for(it = load.begin(); it != load.end(); it++)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsFileConfigItem *fi = dynamic_cast<RsFileConfigItem *>(*it);
|
|
|
|
if (!fi)
|
|
|
|
{
|
|
|
|
delete (*it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open file */
|
|
|
|
FILE *fd = fopen(fi->file.path.c_str(), "rb");
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
delete (*it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
if (ts > fi->file.age)
|
|
|
|
{
|
|
|
|
/* to old */
|
|
|
|
cleanupEntry(fi->file.path, fi->flags);
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add into system */
|
|
|
|
FileDetails file;
|
|
|
|
|
|
|
|
RsStackMutex stack(extMutex);
|
|
|
|
|
|
|
|
FileDetails details;
|
|
|
|
|
|
|
|
details.info.path = fi->file.path;
|
|
|
|
details.info.fname = fi->file.name;
|
|
|
|
details.info.hash = fi->file.hash;
|
|
|
|
details.info.size = fi->file.filesize;
|
|
|
|
details.info.age = fi->file.age; /* time that we remove it. */
|
|
|
|
details.flags = fi->flags;
|
|
|
|
|
|
|
|
/* stick it in the available queue */
|
|
|
|
mFiles[details.info.hash] = details;
|
|
|
|
delete (*it);
|
|
|
|
|
|
|
|
/* short sleep */
|
|
|
|
usleep(1000); /* 1000 per second */
|
|
|
|
}
|
2008-07-02 09:19:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|