2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* RetroShare FileCache Module: findex.h
|
|
|
|
*
|
|
|
|
* Copyright 2004-2007 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FILE_INDEX_H
|
|
|
|
#define FILE_INDEX_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2009-08-09 12:32:31 -04:00
|
|
|
#include <set>
|
2011-04-15 16:21:52 -04:00
|
|
|
#include <tr1/unordered_set>
|
2007-11-14 22:18:48 -05:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2009-05-11 10:30:53 -04:00
|
|
|
#include <stdint.h>
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rstypes.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* The Key Data Types for the File Index:
|
|
|
|
|
|
|
|
FileEntry : Information about a single file.
|
|
|
|
DirEntry : Information about a directory and its children
|
|
|
|
PersonEntry : Information the root of a FileIndex.
|
|
|
|
|
|
|
|
FileIndex : A Collection of root directories, with a set of functions to manipulate them.
|
|
|
|
In terms of retroshare, There will be a single 'Local' FileIndex used to store
|
|
|
|
the shared files, and a set of 'Remote' FileIndices which are used to store
|
|
|
|
the available files of all the peers.
|
|
|
|
|
|
|
|
******************************************************************************************/
|
|
|
|
/******************************************************************************************
|
|
|
|
STILL TODO:
|
|
|
|
|
|
|
|
(1) Load/Store a FileIndex to file...
|
|
|
|
int FileIndex::loadIndex(FILE *input);
|
|
|
|
int FileIndex::saveIndex(FILE *input);
|
|
|
|
|
|
|
|
This can be done in a recursive manner, or handled completely within FileIndex.
|
|
|
|
|
|
|
|
(2) Search Functions for Partial File Names and Hashes.
|
|
|
|
|
|
|
|
int FileIndex::searchHash(std::string hash, std::list<FileEntry> &results);
|
|
|
|
int FileIndex::searchTerms(std::list<string> terms, std::list<FileEntry> &results);
|
|
|
|
|
|
|
|
This is probably best done in a recursive manner.
|
|
|
|
|
|
|
|
The search could also be extended to handle complex Boolean searches such as :
|
|
|
|
match (size > 100K) && (name contains 'Blue') .... if anyone is interested.
|
|
|
|
But this can get quite complicated, and can be left to a later date.
|
|
|
|
|
|
|
|
******************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* FileEntry
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
2011-04-03 16:10:26 -04:00
|
|
|
#include <util/smallobject.h>
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
class DirEntry;
|
|
|
|
|
2011-04-03 16:10:26 -04:00
|
|
|
class FileEntry: public RsMemoryManagement::SmallObject
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
public:
|
2010-10-06 15:06:12 -04:00
|
|
|
FileEntry()
|
2010-10-27 16:01:31 -04:00
|
|
|
: size(0), modtime(0), pop(0), updtime(0), parent(NULL), row(0)
|
2010-10-06 15:06:12 -04:00
|
|
|
{ return; }
|
|
|
|
|
2011-04-03 15:59:12 -04:00
|
|
|
virtual ~FileEntry() { return; }
|
|
|
|
virtual uint32_t type() const { return DIR_TYPE_FILE ; }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-16 13:47:53 -04:00
|
|
|
virtual int print(std::string &out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* Data */
|
|
|
|
std::string name;
|
|
|
|
std::string hash;
|
2007-12-11 20:08:31 -05:00
|
|
|
uint64_t size; /* file size */
|
|
|
|
time_t modtime; /* modification time - most recent mod time for a sub entry for dirs */
|
2007-11-14 22:18:48 -05:00
|
|
|
int pop; /* popularity rating */
|
|
|
|
|
2007-12-11 20:08:31 -05:00
|
|
|
time_t updtime; /* last updated */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* References for easy manipulation */
|
|
|
|
DirEntry *parent;
|
|
|
|
int row;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* DirEntry
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
|
|
|
class DirEntry: public FileEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-10-06 15:06:12 -04:00
|
|
|
DirEntry() : most_recent_time(0) {}
|
2007-11-14 22:18:48 -05:00
|
|
|
/* cleanup */
|
|
|
|
virtual ~DirEntry();
|
|
|
|
|
|
|
|
/* update local entries */
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry * updateDir(const FileEntry& fe, time_t updtime);
|
|
|
|
FileEntry * updateFile(const FileEntry& fe, time_t updtime);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2011-04-03 15:59:12 -04:00
|
|
|
virtual uint32_t type() const { return DIR_TYPE_DIR ; }
|
2007-11-14 22:18:48 -05:00
|
|
|
int checkParentPointers();
|
|
|
|
int updateChildRows();
|
|
|
|
|
|
|
|
/* remove local entries */
|
2010-12-15 17:51:41 -05:00
|
|
|
int removeFile(const std::string& name);
|
|
|
|
int removeDir(const std::string& name);
|
|
|
|
int removeOldDir(const std::string& name, time_t old); /* checks ts first */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* recursive cleanup */
|
|
|
|
int removeOldEntries(time_t old, bool recursive);
|
|
|
|
|
|
|
|
/* recursive searches */
|
|
|
|
DirEntry * findOldDirectory(time_t old);
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry * findDirectory(const std::string& path);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* recursive update directory mod/pop values */
|
2010-12-15 17:51:41 -05:00
|
|
|
int updateDirectories(const std::string& path, int pop, int modtime);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* output */
|
2012-04-16 13:47:53 -04:00
|
|
|
int print(std::string &out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-16 13:47:53 -04:00
|
|
|
int saveEntry(std::string &out);
|
|
|
|
void writeDirInfo(std::string&);
|
|
|
|
void writeFileInfo(std::string&);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* Data */
|
|
|
|
std::string path; /* full path (includes name) */
|
|
|
|
std::map<std::string, DirEntry *> subdirs;
|
|
|
|
std::map<std::string, FileEntry *> files;
|
|
|
|
|
2010-02-08 17:32:00 -05:00
|
|
|
time_t most_recent_time; /* last updated */
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/* Inherited members from FileEntry:
|
|
|
|
int size - count for dirs
|
|
|
|
std::string name; - directory name
|
|
|
|
std::string hash; - not used
|
|
|
|
int size; - not used
|
|
|
|
int modtime; - most recent modication time of any child file (recursive)
|
|
|
|
int pop; - most popular child file (recursive)
|
|
|
|
int updtime; - last updated
|
|
|
|
*/
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* PersonEntry
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
|
|
|
class PersonEntry: public DirEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/* cleanup */
|
2010-12-15 17:51:41 -05:00
|
|
|
PersonEntry(const std::string& pid) : id(pid) { return; }
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual ~PersonEntry() { return; }
|
|
|
|
|
|
|
|
DirEntry &operator=(DirEntry &src)
|
|
|
|
{
|
|
|
|
DirEntry *pdest = this;
|
|
|
|
(*pdest) = src;
|
2010-08-30 05:56:11 -04:00
|
|
|
return *this;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-04-03 15:59:12 -04:00
|
|
|
virtual uint32_t type() const { return DIR_TYPE_PERSON ; }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* Data */
|
|
|
|
std::string id;
|
|
|
|
|
|
|
|
/* Inherited members from FileEntry:
|
|
|
|
int size - count for dirs
|
|
|
|
std::string name; - directory name
|
|
|
|
std::string hash; - not used
|
|
|
|
int size; - not used
|
|
|
|
int modtime; - most recent modication time of any child file (recursive)
|
|
|
|
int pop; - most popular child file (recursive)
|
|
|
|
int updtime; - last updated
|
|
|
|
*/
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* FileIndex
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
|
|
|
class Expression;
|
|
|
|
|
|
|
|
class FileIndex
|
|
|
|
{
|
|
|
|
public:
|
2010-12-15 17:51:41 -05:00
|
|
|
FileIndex(const std::string& pid);
|
2009-08-09 12:32:31 -04:00
|
|
|
~FileIndex();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
/* control root entries */
|
2010-09-17 15:21:14 -04:00
|
|
|
int setRootDirectories(const std::list<std::string> &inlist, time_t utime);
|
2009-08-09 12:32:31 -04:00
|
|
|
int getRootDirectories(std::list<std::string> &outlist);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
/* update (index building) */
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry * updateDirEntry(const std::string& path, const FileEntry& fe, time_t utime);
|
|
|
|
FileEntry * updateFileEntry(const std::string& path, const FileEntry& fe, time_t utime);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
DirEntry * findOldDirectory(time_t old); /* finds directories older than old */
|
2010-12-15 17:51:41 -05:00
|
|
|
int removeOldDirectory(const std::string& fpath, const std::string& name, time_t old);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
int cleanOldEntries(time_t old); /* removes entries older than old */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
/* debug */
|
2012-04-16 13:47:53 -04:00
|
|
|
int printFileIndex(std::string &out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
/* load/save to file */
|
2010-12-15 17:51:41 -05:00
|
|
|
int loadIndex(const std::string& filename, const std::string& expectedHash, uint64_t size);
|
|
|
|
int saveIndex(const std::string& filename, std::string &fileHash, uint64_t &size, const std::set<std::string>& forbidden_roots);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
/* search through this index */
|
2010-12-15 17:51:41 -05:00
|
|
|
int searchTerms(const std::list<std::string>& terms, std::list<FileEntry *> &results) const;
|
|
|
|
int searchHash(const std::string& hash, std::list<FileEntry *> &results) const;
|
2009-08-09 12:32:31 -04:00
|
|
|
int searchBoolExp(Expression * exp, std::list<FileEntry *> &results) const;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-02-08 17:32:00 -05:00
|
|
|
/// Recursively compute the maximum modification time of children.
|
|
|
|
/// Used to easily retrieve mose recent files.
|
|
|
|
//
|
|
|
|
void updateMaxModTime() ;
|
|
|
|
void RecursUpdateMaxModTime(DirEntry *) ;
|
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
PersonEntry *root;
|
2009-08-12 18:06:11 -04:00
|
|
|
|
2011-04-15 16:21:52 -04:00
|
|
|
static std::tr1::unordered_set<void*> _pointers ;
|
2009-08-12 18:06:11 -04:00
|
|
|
static void registerEntry(void*p) ;
|
|
|
|
static void unregisterEntry(void*p) ;
|
|
|
|
static bool isValid(void*p) ;
|
2010-01-30 15:47:34 -05:00
|
|
|
|
|
|
|
/// Fills up details from the data contained in ref.
|
|
|
|
//
|
|
|
|
static bool extractData(void *ref,DirDetails& details) ;
|
2011-04-03 15:59:12 -04:00
|
|
|
static uint32_t getType(void *ref) ;
|
2012-01-17 15:38:25 -05:00
|
|
|
|
|
|
|
void *findRef(const std::string& path) const ;
|
|
|
|
bool extractData(const std::string& path,DirDetails& details) const ;
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|