2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* RetroShare FileCache Module: findex.cc
|
|
|
|
*
|
|
|
|
* Copyright 2004-2007 by Robert Fernie, Kefei Zhou.
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-05-19 07:37:17 -04:00
|
|
|
#include <util/rswin.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
#include "dbase/findex.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rsexpr.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
#include "util/rsdir.h"
|
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
#include <assert.h>
|
2008-11-02 07:59:21 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <algorithm>
|
2007-11-14 22:18:48 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
2010-05-18 17:15:22 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
2009-08-12 18:06:11 -04:00
|
|
|
#include <util/rsthreads.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-09-08 15:47:50 -04:00
|
|
|
// This char is used to separate fields in the file list cache. It is supposed to be
|
|
|
|
// sufficiently safe on all systems.
|
|
|
|
//
|
|
|
|
static const char FILE_CACHE_SEPARATOR_CHAR = '|' ;
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
/****
|
2009-08-11 13:01:23 -04:00
|
|
|
#define FI_DEBUG 1
|
2008-11-13 18:03:46 -05:00
|
|
|
* #define FI_DEBUG_ALL 1
|
2008-07-09 05:55:09 -04:00
|
|
|
****/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-12 18:06:11 -04:00
|
|
|
static RsMutex FIndexPtrMtx ;
|
|
|
|
std::set<void*> FileIndex::_pointers ;
|
|
|
|
|
2009-08-25 16:49:50 -04:00
|
|
|
void FileIndex::registerEntry(void*p)
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
RsStackMutex m(FIndexPtrMtx) ;
|
2009-08-25 16:49:50 -04:00
|
|
|
_pointers.insert(p) ;
|
2009-08-12 18:06:11 -04:00
|
|
|
}
|
2009-08-25 16:49:50 -04:00
|
|
|
void FileIndex::unregisterEntry(void*p)
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
RsStackMutex m(FIndexPtrMtx) ;
|
2009-08-25 16:49:50 -04:00
|
|
|
_pointers.erase(p) ;
|
2009-08-11 15:43:51 -04:00
|
|
|
}
|
2009-08-25 16:49:50 -04:00
|
|
|
bool FileIndex::isValid(void*p)
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
RsStackMutex m(FIndexPtrMtx) ;
|
2009-08-25 16:49:50 -04:00
|
|
|
return _pointers.find(p) != _pointers.end() ;
|
2009-08-12 18:06:11 -04:00
|
|
|
}
|
2009-08-11 15:43:51 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
DirEntry::~DirEntry()
|
|
|
|
{
|
|
|
|
/* cleanup */
|
|
|
|
std::map<std::string, DirEntry *>::iterator dit;
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
|
|
|
|
for(dit = subdirs.begin(); dit != subdirs.end(); dit++)
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)dit->second) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete (dit->second);
|
|
|
|
}
|
|
|
|
subdirs.clear();
|
|
|
|
|
|
|
|
for(fit = files.begin(); fit != files.end(); fit++)
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)fit->second) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete (fit->second);
|
|
|
|
}
|
|
|
|
files.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DirEntry::checkParentPointers()
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
updateChildRows();
|
|
|
|
std::map<std::string, DirEntry *>::iterator dit;
|
|
|
|
for(dit = subdirs.begin(); dit != subdirs.end(); dit++)
|
|
|
|
{
|
|
|
|
/* debug check */
|
|
|
|
(dit->second)->checkParentPointers();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DirEntry::updateChildRows()
|
|
|
|
{
|
|
|
|
/* iterate through children and set row (parent should be good) */
|
|
|
|
std::map<std::string, DirEntry *>::iterator dit;
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
int i = 0;
|
|
|
|
for(dit = subdirs.begin(); dit != subdirs.end(); dit++)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
/* debug check */
|
|
|
|
if ((dit->second)->parent != this)
|
|
|
|
{
|
|
|
|
std::cerr << "DirEntry::updateChildRows()";
|
|
|
|
std::cerr << "****WARNING subdir Parent pointer invalid!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
(dit->second)->parent = this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
(dit->second)->row = i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(fit = files.begin(); fit != files.end(); fit++)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
/* debug check */
|
|
|
|
if ((fit->second)->parent != this)
|
|
|
|
{
|
|
|
|
std::cerr << "DirEntry::updateChildRows()";
|
|
|
|
std::cerr << "****WARNING file Parent pointer invalid!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
(fit->second)->parent = this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
(fit->second)->row = i++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int DirEntry::removeDir(const std::string& name)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if it doesn't exist - add */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
if (subdirs.end() != (it = subdirs.find(name)))
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::removeDir() Cleaning up dir: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
ndir = (it->second);
|
|
|
|
|
|
|
|
subdirs.erase(it);
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)ndir) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete ndir;
|
|
|
|
/* update row counters */
|
|
|
|
updateChildRows();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "DirEntry::removeDir() missing Entry: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int DirEntry::removeFile(const std::string& name)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if it doesn't exist - add */
|
|
|
|
std::map<std::string, FileEntry *>::iterator it;
|
|
|
|
FileEntry *nfile = NULL;
|
|
|
|
|
|
|
|
if (files.end() != (it = files.find(name)))
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::removeFile() removing File: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
nfile = (it->second);
|
|
|
|
|
|
|
|
files.erase(it);
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)nfile) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete nfile;
|
|
|
|
/* update row counters */
|
|
|
|
updateChildRows();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "DirEntry::removeFile() missing Entry: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int DirEntry::removeOldDir(const std::string& name, time_t old)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
if (subdirs.end() != (it = subdirs.find(name)))
|
|
|
|
{
|
|
|
|
ndir = (it->second);
|
|
|
|
if (ndir->updtime < old)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::removeOldDir() Removing Old dir: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
subdirs.erase(it);
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)ndir) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete ndir;
|
|
|
|
|
|
|
|
/* update row counters */
|
|
|
|
updateChildRows();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::removeOldDir() Keeping UptoDate dir: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "DirEntry::removeDir() missing Entry: " << name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DirEntry::removeOldEntries(time_t old, bool recursive)
|
|
|
|
{
|
|
|
|
/* remove old dirs from our lists -> then do children and files */
|
|
|
|
|
|
|
|
/* get all dirs with old time */
|
|
|
|
std::list<DirEntry *> removeList;
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
if ((it->second)->updtime < old)
|
|
|
|
{
|
|
|
|
removeList.push_back(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now remove the old entries */
|
|
|
|
std::list<DirEntry *>::iterator rit;
|
|
|
|
for(rit = removeList.begin(); rit != removeList.end(); rit++)
|
|
|
|
{
|
|
|
|
removeDir((*rit)->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
/* now handle children */
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->removeOldEntries(old, recursive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now handle files similarly */
|
|
|
|
std::list<FileEntry *> removeFileList;
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
for(fit = files.begin(); fit != files.end(); fit++)
|
|
|
|
{
|
|
|
|
if ((fit->second)->updtime < old)
|
|
|
|
{
|
|
|
|
removeFileList.push_back(fit->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now remove the old entries */
|
|
|
|
std::list<FileEntry *>::iterator rfit;
|
|
|
|
for(rfit = removeFileList.begin(); rfit != removeFileList.end(); rfit++)
|
|
|
|
{
|
|
|
|
removeFile((*rfit)->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirEntry *DirEntry::findOldDirectory(time_t old)
|
|
|
|
{
|
2009-08-25 16:49:50 -04:00
|
|
|
/* check if one of our directories is old ...
|
2007-11-14 22:18:48 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* get all dirs with old time */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
if ((it->second)->updtime < old)
|
|
|
|
{
|
|
|
|
return (it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* else check chlidren.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
DirEntry *olddir = (it->second)->findOldDirectory(old);
|
|
|
|
if (olddir)
|
|
|
|
{
|
|
|
|
return olddir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry *DirEntry::findDirectory(const std::string& fpath)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
std::string nextdir = RsDirUtil::getRootDir(fpath);
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
if (subdirs.end() == (it = subdirs.find(nextdir)))
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "DirEntry::findDirectory() Missing subdir:";
|
|
|
|
std::cerr << "\"" << nextdir << "\"";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rempath = RsDirUtil::removeRootDir(fpath);
|
|
|
|
if (rempath == "")
|
|
|
|
{
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (it->second)->findDirectory(rempath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int DirEntry::updateDirectories(const std::string& fpath, int new_pop, int new_modtime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
if (path != "") /* if not there -> continue down tree */
|
|
|
|
{
|
|
|
|
std::string nextdir = RsDirUtil::getRootDir(fpath);
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
if (subdirs.end() == (it = subdirs.find(nextdir)))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rempath = RsDirUtil::removeRootDir(fpath);
|
|
|
|
ret = (it->second)->updateDirectories(rempath, new_pop, new_modtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) /* if full path is okay -> update and return ok */
|
|
|
|
{
|
|
|
|
/* this is assumes that pop always increases! */
|
|
|
|
if (new_pop > pop)
|
|
|
|
{
|
|
|
|
pop = new_pop;
|
|
|
|
}
|
|
|
|
if (new_modtime > modtime)
|
|
|
|
{
|
|
|
|
modtime = new_modtime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry *DirEntry::updateDir(const FileEntry& fe, time_t utime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if it doesn't exist - add */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
if (subdirs.end() == (it = subdirs.find(fe.name)))
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::updateDir() Adding Entry";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
ndir = new DirEntry();
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::registerEntry((void*)ndir) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
ndir -> parent = this;
|
|
|
|
ndir -> path = path + "/" + fe.name;
|
|
|
|
ndir -> name = fe.name;
|
|
|
|
ndir -> pop = 0;
|
|
|
|
ndir -> modtime = 0;
|
|
|
|
ndir -> updtime = utime;
|
|
|
|
|
|
|
|
subdirs[fe.name] = ndir;
|
|
|
|
|
|
|
|
/* update row counters */
|
|
|
|
updateChildRows();
|
|
|
|
return ndir;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::updateDir() Updating Entry";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* update utime */
|
|
|
|
ndir = (it->second);
|
|
|
|
ndir->updtime = utime;
|
|
|
|
|
|
|
|
return ndir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
FileEntry *DirEntry::updateFile(const FileEntry& fe, time_t utime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if it doesn't exist - add */
|
|
|
|
std::map<std::string, FileEntry *>::iterator it;
|
|
|
|
FileEntry *nfile = NULL;
|
|
|
|
if (files.end() == (it = files.find(fe.name)))
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::updateFile() Adding Entry";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nfile = new FileEntry();
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::registerEntry((void*)nfile) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
nfile -> parent = this;
|
|
|
|
nfile -> name = fe.name;
|
|
|
|
nfile -> hash = fe.hash;
|
|
|
|
nfile -> size = fe.size;
|
|
|
|
nfile -> pop = 0;
|
|
|
|
nfile -> modtime = fe.modtime;
|
|
|
|
nfile -> updtime = utime;
|
|
|
|
|
|
|
|
files[fe.name] = nfile;
|
|
|
|
|
|
|
|
/* update row counters */
|
|
|
|
updateChildRows();
|
|
|
|
return nfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "DirEntry::updateFile() Updating Entry";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* update utime */
|
|
|
|
nfile = (it->second);
|
|
|
|
nfile -> parent = this;
|
|
|
|
nfile -> name = fe.name;
|
|
|
|
nfile -> hash = fe.hash;
|
|
|
|
nfile -> size = fe.size;
|
|
|
|
nfile -> modtime = fe.modtime;
|
|
|
|
nfile -> updtime = utime;
|
|
|
|
//nfile -> pop = 0; // not handled here.
|
|
|
|
|
|
|
|
return nfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FileEntry::print(std::ostream &out)
|
|
|
|
{
|
|
|
|
/* print this dir, then subdirs, then files */
|
|
|
|
|
|
|
|
out << "file ";
|
|
|
|
out << std::setw(3) << std::setfill('0') << row;
|
|
|
|
out << " [" << updtime << "/" << modtime << "] : ";
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
out << parent->path;
|
|
|
|
else
|
|
|
|
out << "[MISSING PARENT]";
|
|
|
|
|
2009-08-25 16:49:50 -04:00
|
|
|
out << " " << name;
|
2007-11-14 22:18:48 -05:00
|
|
|
out << " [ s: " << size << " ] ==> ";
|
|
|
|
out << " [ " << hash << " ]";
|
|
|
|
out << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DirEntry::print(std::ostream &out)
|
|
|
|
{
|
|
|
|
/* print this dir, then subdirs, then files */
|
|
|
|
out << "dir ";
|
|
|
|
out << std::setw(3) << std::setfill('0') << row;
|
|
|
|
out << " [" << updtime << "] : " << path;
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->print(out);
|
|
|
|
}
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
for(fit = files.begin(); fit != files.end(); fit++)
|
|
|
|
{
|
|
|
|
(fit->second)->print(out);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
FileIndex::FileIndex(const std::string& pid)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
root = new PersonEntry(pid);
|
2009-08-11 15:43:51 -04:00
|
|
|
registerEntry(root) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FileIndex::~FileIndex()
|
|
|
|
{
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)root) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete root;
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:21:14 -04:00
|
|
|
int FileIndex::setRootDirectories(const std::list<std::string> &inlist, time_t updtime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* set update time to zero */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = root->subdirs.begin(); it != root->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->updtime = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-17 15:21:14 -04:00
|
|
|
std::list<std::string>::const_iterator ait;
|
2007-11-14 22:18:48 -05:00
|
|
|
FileEntry fe;
|
|
|
|
time_t utime = 1;
|
|
|
|
for(ait = inlist.begin(); ait != inlist.end(); ait++)
|
|
|
|
{
|
|
|
|
fe.name = (*ait);
|
|
|
|
|
|
|
|
/* see if it exists */
|
|
|
|
root->updateDir(fe, utime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove all dirs with zero time (non recursive) */
|
|
|
|
root->removeOldEntries(utime, false);
|
|
|
|
|
|
|
|
/* now flag remaining directories with correct update time */
|
|
|
|
for(it = root->subdirs.begin(); it != root->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->updtime = updtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-08 17:32:00 -05:00
|
|
|
void FileIndex::updateMaxModTime()
|
|
|
|
{
|
|
|
|
RecursUpdateMaxModTime(root) ;
|
|
|
|
}
|
|
|
|
void FileIndex::RecursUpdateMaxModTime(DirEntry *dir)
|
|
|
|
{
|
|
|
|
time_t max_mod_t = 0 ;
|
|
|
|
|
|
|
|
for(std::map<std::string,DirEntry*>::iterator it(dir->subdirs.begin());it!=dir->subdirs.end();++it)
|
|
|
|
{
|
|
|
|
RecursUpdateMaxModTime(it->second) ;
|
|
|
|
max_mod_t = std::max(max_mod_t, it->second->most_recent_time) ;
|
|
|
|
}
|
|
|
|
for(std::map<std::string,FileEntry*>::iterator it(dir->files.begin());it!=dir->files.end();++it)
|
|
|
|
max_mod_t = std::max(max_mod_t, it->second->modtime) ;
|
|
|
|
|
|
|
|
dir->most_recent_time = max_mod_t ;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
int FileIndex::getRootDirectories(std::list<std::string> &outlist)
|
|
|
|
{
|
|
|
|
/* set update time to zero */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = root->subdirs.begin(); it != root->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
outlist.push_back(it->first);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-08 17:32:00 -05:00
|
|
|
/* update (index building) */
|
2010-12-15 17:51:41 -05:00
|
|
|
DirEntry *FileIndex::updateDirEntry(const std::string& fpath, const FileEntry& fe, time_t utime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* path is to parent */
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "FileIndex::updateDirEntry() Path: \"";
|
|
|
|
std::cerr << fpath << "\"" << " + \"" << fe.name << "\"";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
DirEntry *parent = NULL;
|
|
|
|
if (fpath == "")
|
|
|
|
{
|
|
|
|
parent = root;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent = root->findDirectory(fpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::updateDirEntry() NULL parent";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return parent -> updateDir(fe, utime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
FileEntry *FileIndex::updateFileEntry(const std::string& fpath, const FileEntry& fe, time_t utime)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* path is to parent */
|
|
|
|
#ifdef FI_DEBUG_ALL
|
|
|
|
std::cerr << "FileIndex::updateFileEntry() Path: \"";
|
|
|
|
std::cerr << fpath << "\"" << " + \"" << fe.name << "\"";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
DirEntry *parent = root->findDirectory(fpath);
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::updateFileEntry() NULL parent";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return parent -> updateFile(fe, utime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirEntry *FileIndex::findOldDirectory(time_t old) /* finds directories older than old */
|
|
|
|
{
|
|
|
|
DirEntry *olddir = root->findOldDirectory(old);
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
|
|
|
|
std::cerr << "FileIndex::findOldDirectory(" << old << ") -> ";
|
|
|
|
if (olddir)
|
|
|
|
{
|
|
|
|
std::cerr << olddir->path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "NONE";
|
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return olddir;
|
|
|
|
}
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int FileIndex::removeOldDirectory(const std::string& fpath, const std::string& name, time_t old)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* path is to parent */
|
|
|
|
#ifdef FI_DEBUG_ALL
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << "FileIndex::removeOldDirectory() Path: \"";
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << fpath << "\"" << " + \"" << name << "\"";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2009-08-25 16:49:50 -04:00
|
|
|
/* because of this find - we cannot get a child of
|
2007-11-14 22:18:48 -05:00
|
|
|
* root (which is what we want!)
|
|
|
|
*/
|
2008-11-02 06:38:11 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
DirEntry *parent = root->findDirectory(fpath);
|
2008-11-02 06:38:11 -05:00
|
|
|
/* for root directory case ... no subdir. */
|
|
|
|
if (fpath == "")
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::removeOldDirectory() removing a root dir";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
parent = root;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
#ifdef FI_DEBUG
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << "FileIndex::removeOldDirectory() NULL parent";
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return parent -> removeOldDir(name, old);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FileIndex::cleanOldEntries(time_t old) /* removes entries older than old */
|
|
|
|
{
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = root->subdirs.begin(); it != root->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->removeOldEntries(old, true);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int FileIndex::printFileIndex(std::ostream &out)
|
|
|
|
{
|
|
|
|
out << "FileIndex::printFileIndex()" << std::endl;
|
|
|
|
root->print(out);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int FileIndex::loadIndex(const std::string& filename, const std::string& expectedHash, uint64_t size)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
std::ifstream file (filename.c_str(), std::ifstream::binary);
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::loadIndex error opening file: " << filename;
|
2009-08-25 16:49:50 -04:00
|
|
|
std::cerr << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load file into memory, close file */
|
|
|
|
char ibuf[512];
|
|
|
|
std::stringstream ss;
|
|
|
|
while(!file.eof())
|
|
|
|
{
|
|
|
|
file.read(ibuf, 512);
|
|
|
|
ss.write(ibuf, file.gcount());
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
/* calculate hash */
|
|
|
|
unsigned char sha_buf[SHA_DIGEST_LENGTH];
|
2009-08-25 16:49:50 -04:00
|
|
|
SHA_CTX *sha_ctx = new SHA_CTX;
|
2007-11-14 22:18:48 -05:00
|
|
|
SHA1_Init(sha_ctx);
|
|
|
|
SHA1_Update(sha_ctx, ss.str().c_str(), ss.str().length());
|
2009-08-25 16:49:50 -04:00
|
|
|
SHA1_Final(&sha_buf[0], sha_ctx);
|
2007-11-14 22:18:48 -05:00
|
|
|
delete sha_ctx;
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2009-09-28 16:31:09 -04:00
|
|
|
if (expectedHash != "" && expectedHash != tmpout.str())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
2009-08-25 16:49:50 -04:00
|
|
|
std::cerr << "FileIndex::loadIndex expected hash does not match" << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
std::cerr << "Expected hash: " << expectedHash << std::endl;
|
|
|
|
std::cerr << "Hash found: " << tmpout.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
FileEntry *nfile = NULL;
|
|
|
|
std::list<DirEntry *> dirlist;
|
|
|
|
std::string word;
|
|
|
|
char ch;
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
while(ss.get(ch))
|
|
|
|
{
|
|
|
|
if (ch == '-')
|
|
|
|
{
|
|
|
|
ss.ignore(256, '\n');
|
|
|
|
switch(dirlist.size())
|
|
|
|
{
|
|
|
|
/* parse error: out of directory */
|
2009-08-25 16:49:50 -04:00
|
|
|
case 0:
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "loadIndex error parsing saved file: " << filename;
|
|
|
|
std::cerr << " Ran out of dirs";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* finished parse, last dir is root */
|
2009-08-25 16:49:50 -04:00
|
|
|
case 1:
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2009-08-25 16:49:50 -04:00
|
|
|
std::string pid = root -> id;
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)root) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete root; /* to clean up old entries */
|
|
|
|
root = new PersonEntry(pid);
|
2009-08-11 15:43:51 -04:00
|
|
|
registerEntry((void*)root) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* shallow copy of all except id */
|
2009-08-25 16:49:50 -04:00
|
|
|
ndir = dirlist.back();
|
2007-11-14 22:18:48 -05:00
|
|
|
dirlist.pop_back(); /* empty list */
|
|
|
|
(*root) = (*ndir);
|
|
|
|
|
|
|
|
/* now cleanup (can't call standard delete) */
|
|
|
|
ndir->subdirs.clear();
|
|
|
|
ndir->files.clear();
|
2009-08-12 18:06:11 -04:00
|
|
|
FileIndex::unregisterEntry((void*)ndir) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
delete ndir;
|
|
|
|
ndir = NULL;
|
|
|
|
|
|
|
|
/* must reset parent pointers now */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
2009-08-25 16:49:50 -04:00
|
|
|
for(it = root->subdirs.begin();
|
2007-11-14 22:18:48 -05:00
|
|
|
it != root->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->parent = root;
|
|
|
|
}
|
|
|
|
|
2009-08-25 16:49:50 -04:00
|
|
|
break;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
/* pop stack */
|
|
|
|
default: dirlist.pop_back(); ndir = dirlist.back();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore comments
|
|
|
|
else if (ch == '#')
|
|
|
|
{
|
|
|
|
ss.ignore(256, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
/* parse line */
|
|
|
|
while(1)
|
|
|
|
{
|
2009-09-08 15:47:50 -04:00
|
|
|
getline(ss, word, FILE_CACHE_SEPARATOR_CHAR);
|
2007-11-14 22:18:48 -05:00
|
|
|
if (ss.eof())
|
|
|
|
goto error;
|
|
|
|
tokens.push_back(word);
|
|
|
|
if (ss.peek() == '\n')
|
|
|
|
{
|
|
|
|
ss.ignore(256, '\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/* create new file and add it to last directory*/
|
|
|
|
if (ch == 'f')
|
|
|
|
{
|
|
|
|
if (tokens.size() != 6)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "loadIndex error parsing saved file: " << filename;
|
|
|
|
std::cerr << " File token count wrong: " << tokens.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
for(unsigned int i = 0; i < tokens.size(); i++)
|
|
|
|
{
|
|
|
|
std::cerr << "\tToken[" << i << "]:" << tokens[i];
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
nfile = new FileEntry();
|
2009-08-11 15:43:51 -04:00
|
|
|
registerEntry((void*)nfile) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
nfile->name = tokens[0];
|
|
|
|
nfile->hash = tokens[1];
|
2007-12-11 20:08:31 -05:00
|
|
|
nfile->size = atoll(tokens[2].c_str());
|
2007-11-14 22:18:48 -05:00
|
|
|
nfile->modtime = atoi(tokens[3].c_str());
|
|
|
|
nfile->pop = atoi(tokens[4].c_str());
|
|
|
|
nfile->updtime = atoi(tokens[5].c_str());
|
|
|
|
nfile->parent = ndir;
|
|
|
|
nfile->row = ndir->subdirs.size() + ndir->files.size();
|
|
|
|
ndir->files[nfile->name] = nfile;
|
|
|
|
|
|
|
|
}
|
|
|
|
/* create new dir and add to stack */
|
|
|
|
else if (ch == 'd')
|
|
|
|
{
|
|
|
|
if (tokens.size() != 6)
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "loadIndex error parsing saved file: " << filename;
|
|
|
|
std::cerr << " Dir token count wrong: " << tokens.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ndir = new DirEntry();
|
2009-08-11 15:43:51 -04:00
|
|
|
registerEntry((void*)ndir) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
ndir->name = tokens[0];
|
|
|
|
ndir->path = tokens[1];
|
|
|
|
ndir->size = atoi(tokens[2].c_str());
|
|
|
|
ndir->modtime = atoi(tokens[3].c_str());
|
|
|
|
ndir->pop = atoi(tokens[4].c_str());
|
|
|
|
ndir->updtime = atoi(tokens[5].c_str());
|
|
|
|
if (!dirlist.empty())
|
|
|
|
{
|
|
|
|
ndir->parent = (dirlist.back());
|
|
|
|
ndir->row = dirlist.back()->subdirs.size();
|
|
|
|
dirlist.back()->subdirs[ndir->name] = ndir;
|
|
|
|
}
|
|
|
|
dirlist.push_back(ndir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* parse error encountered */
|
|
|
|
error:
|
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "loadIndex error parsing saved file: " << filename;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int FileIndex::saveIndex(const std::string& filename, std::string &fileHash, uint64_t &size,const std::set<std::string>& forbidden_dirs)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
unsigned char sha_buf[SHA_DIGEST_LENGTH];
|
2009-09-08 15:47:50 -04:00
|
|
|
std::string filenametmp = filename + ".tmp" ;
|
2007-11-14 22:18:48 -05:00
|
|
|
std::ostringstream oss;
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2010-02-10 15:24:30 -05:00
|
|
|
size = 0 ;
|
|
|
|
fileHash = "" ;
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/* print version and header */
|
|
|
|
oss << "# FileIndex version 0.1" << std::endl;
|
|
|
|
oss << "# Dir: d name, path, parent, size, modtime, pop, updtime;" << std::endl;
|
|
|
|
oss << "# File: f name, hash, size, modtime, pop, updtime;" << std::endl;
|
|
|
|
oss << "#" << std::endl;
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/* begin recusion */
|
2009-08-09 12:32:31 -04:00
|
|
|
root->writeDirInfo(oss) ;
|
|
|
|
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = root->subdirs.begin(); it != root->subdirs.end(); it++)
|
|
|
|
{
|
2010-03-28 16:46:45 -04:00
|
|
|
#ifdef FI_DEBUG
|
2009-08-09 12:32:31 -04:00
|
|
|
std::cout << "writting root directory: name=" << it->second->name << ", path=" << it->second->path << std::endl ;
|
2010-03-28 16:46:45 -04:00
|
|
|
#endif
|
2009-08-09 12:32:31 -04:00
|
|
|
if(forbidden_dirs.find(it->second->name) != forbidden_dirs.end())
|
2010-03-28 16:46:45 -04:00
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
2009-08-09 12:32:31 -04:00
|
|
|
std::cerr << " will be suppressed." << std::endl ;
|
2010-03-28 16:46:45 -04:00
|
|
|
#endif
|
|
|
|
}
|
2009-08-09 12:32:31 -04:00
|
|
|
else
|
|
|
|
{
|
2010-03-28 16:46:45 -04:00
|
|
|
#ifdef FI_DEBUG
|
2009-08-09 12:32:31 -04:00
|
|
|
std::cerr << " will be saved." << std::endl ;
|
2010-03-28 16:46:45 -04:00
|
|
|
#endif
|
2009-08-09 12:32:31 -04:00
|
|
|
(it->second)->saveEntry(oss);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root->writeFileInfo(oss) ; // this should never do anything
|
|
|
|
|
|
|
|
/* signal to pop directory from stack in loadIndex() */
|
|
|
|
oss << "-" << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* calculate sha1 hash */
|
2009-08-25 16:49:50 -04:00
|
|
|
SHA_CTX *sha_ctx = new SHA_CTX;
|
2007-11-14 22:18:48 -05:00
|
|
|
SHA1_Init(sha_ctx);
|
|
|
|
SHA1_Update(sha_ctx, oss.str().c_str(), oss.str().length());
|
2009-08-25 16:49:50 -04:00
|
|
|
SHA1_Final(&sha_buf[0], sha_ctx);
|
2007-11-14 22:18:48 -05:00
|
|
|
delete sha_ctx;
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
fileHash = tmpout.str();
|
|
|
|
|
|
|
|
/* finally, save to file */
|
|
|
|
|
2010-05-18 17:15:22 -04:00
|
|
|
FILE *file = fopen(filenametmp.c_str(), "wb");
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
std::cerr << "FileIndex::saveIndex error opening file for writting: " << filename << ". Giving up." << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fprintf(file,"%s",oss.str().c_str()) ;
|
|
|
|
|
|
|
|
fclose(file);
|
2009-09-08 15:47:50 -04:00
|
|
|
|
|
|
|
// Use a temp file name so that the file is never half saved.
|
|
|
|
//
|
|
|
|
if(!RsDirUtil::renameFile(filenametmp,filename))
|
|
|
|
return false ;
|
|
|
|
|
2010-05-18 17:15:22 -04:00
|
|
|
/* get the size out */
|
|
|
|
struct stat64 buf;
|
|
|
|
|
|
|
|
if(-1 == stat64(filename.c_str(), &buf))
|
|
|
|
{
|
|
|
|
std::cerr << "Can't determine size of file " << filename << ": errno = " << errno << std::endl ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
size=buf.st_size;
|
|
|
|
|
2009-09-08 15:47:50 -04:00
|
|
|
return true;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
std::string FixName(const std::string& _in)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* replace any , with _ */
|
2010-12-15 17:51:41 -05:00
|
|
|
std::string in(_in) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(unsigned int i = 0; i < in.length(); i++)
|
|
|
|
{
|
2009-09-08 15:47:50 -04:00
|
|
|
if (in[i] == FILE_CACHE_SEPARATOR_CHAR)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
in[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
void DirEntry::writeDirInfo(std::ostringstream& oss)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* print node info */
|
|
|
|
oss << "d";
|
2009-09-08 15:47:50 -04:00
|
|
|
oss << FixName(name) << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << FixName(path) << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << size << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << modtime << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << pop << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << updtime << FILE_CACHE_SEPARATOR_CHAR << std::endl;
|
2009-08-09 12:32:31 -04:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
void DirEntry::writeFileInfo(std::ostringstream& oss)
|
|
|
|
{
|
2007-11-14 22:18:48 -05:00
|
|
|
/* print file info */
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
for(fit = files.begin(); fit != files.end(); fit++)
|
|
|
|
{
|
|
|
|
oss << "f";
|
2009-09-08 15:47:50 -04:00
|
|
|
oss << FixName((fit->second)->name) << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << (fit->second)->hash << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << (fit->second)->size << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << (fit->second)->modtime << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << (fit->second)->pop << FILE_CACHE_SEPARATOR_CHAR ;
|
|
|
|
oss << (fit->second)->updtime << FILE_CACHE_SEPARATOR_CHAR << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2009-08-09 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* recusive function for traversing the dir tree in preorder */
|
|
|
|
int DirEntry::saveEntry(std::ostringstream &oss)
|
|
|
|
{
|
|
|
|
writeDirInfo(oss) ;
|
|
|
|
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second)->saveEntry(oss);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFileInfo(oss) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* signal to pop directory from stack in loadIndex() */
|
|
|
|
oss << "-" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int FileIndex::searchHash(const std::string& hash, std::list<FileEntry *> &results) const
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::searchHash(" << hash << ")";
|
2009-08-25 16:49:50 -04:00
|
|
|
std::cerr << std::endl;
|
2008-11-04 18:12:53 -05:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
std::list<DirEntry *> dirlist;
|
|
|
|
dirlist.push_back(root);
|
|
|
|
|
|
|
|
while(!dirlist.empty())
|
|
|
|
{
|
|
|
|
ndir = dirlist.back();
|
|
|
|
dirlist.pop_back();
|
|
|
|
/* add subdirs to stack */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
for(it = ndir->subdirs.begin(); it != ndir->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
dirlist.push_back(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
/* search in current dir */
|
|
|
|
for(fit = ndir->files.begin(); fit != ndir->files.end(); fit++)
|
|
|
|
{
|
|
|
|
if (hash == (fit->second)->hash)
|
|
|
|
{
|
|
|
|
results.push_back(fit->second);
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef FI_DEBUG
|
|
|
|
std::cerr << "FileIndex::searchHash(" << hash << ")";
|
|
|
|
std::cerr << " found: " << fit->second->name;
|
2009-08-25 16:49:50 -04:00
|
|
|
std::cerr << std::endl;
|
2008-11-04 18:12:53 -05:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 17:51:41 -05:00
|
|
|
int FileIndex::searchTerms(const std::list<std::string>& terms, std::list<FileEntry *> &results) const
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
std::list<DirEntry *> dirlist;
|
|
|
|
dirlist.push_back(root);
|
|
|
|
|
|
|
|
/* iterators */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
std::list<std::string>::const_iterator iter;
|
|
|
|
|
|
|
|
while(!dirlist.empty())
|
|
|
|
{
|
|
|
|
ndir = dirlist.back();
|
|
|
|
dirlist.pop_back();
|
|
|
|
for(it = ndir->subdirs.begin(); it != ndir->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
dirlist.push_back(it->second);
|
|
|
|
}
|
|
|
|
|
2009-08-25 16:49:50 -04:00
|
|
|
for (iter = terms.begin(); iter != terms.end(); iter ++) {
|
|
|
|
std::string::const_iterator it2;
|
|
|
|
const std::string &str1 = ndir->name;
|
|
|
|
const std::string &str2 = *iter;
|
|
|
|
it2 = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), CompareCharIC());
|
|
|
|
if (it2 != str1.end()) {
|
|
|
|
/* first search to see if its parent is in the results list */
|
|
|
|
bool addDir = true;
|
|
|
|
for (std::list<FileEntry *>::iterator rit(results.begin()); rit != results.end() && addDir; rit ++) {
|
|
|
|
DirEntry *de = dynamic_cast<DirEntry *>(*rit);
|
|
|
|
if (de && (de == root))
|
|
|
|
continue;
|
|
|
|
if (de && (de == ndir->parent))
|
|
|
|
addDir = false;
|
|
|
|
}
|
|
|
|
if (addDir) {
|
|
|
|
results.push_back((FileEntry *) ndir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
for(fit = ndir->files.begin(); fit != ndir->files.end(); fit++)
|
|
|
|
{
|
|
|
|
/* cycle through terms */
|
|
|
|
for(iter = terms.begin(); iter != terms.end(); iter++)
|
|
|
|
{
|
|
|
|
/* always ignore case */
|
|
|
|
std::string::const_iterator it2 ;
|
|
|
|
const std::string &str1 = fit->second->name;
|
|
|
|
const std::string &str2 = (*iter);
|
|
|
|
|
|
|
|
it2 = std::search( str1.begin(), str1.end(),
|
|
|
|
str2.begin(), str2.end(), CompareCharIC() );
|
|
|
|
if (it2 != str1.end())
|
|
|
|
{
|
|
|
|
results.push_back(fit->second);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* original case specific term search ******
|
|
|
|
if (fit->second->name.find(*iter) != std::string::npos)
|
|
|
|
{
|
|
|
|
results.push_back(fit->second);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
************/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-09 12:06:01 -04:00
|
|
|
int FileIndex::searchBoolExp(Expression * exp, std::list<FileEntry *> &results) const
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
DirEntry *ndir = NULL;
|
|
|
|
std::list<DirEntry *> dirlist;
|
|
|
|
dirlist.push_back(root);
|
|
|
|
|
|
|
|
/* iterators */
|
|
|
|
std::map<std::string, DirEntry *>::iterator it;
|
|
|
|
std::map<std::string, FileEntry *>::iterator fit;
|
|
|
|
std::list<std::string>::const_iterator iter;
|
|
|
|
|
|
|
|
while(!dirlist.empty())
|
|
|
|
{
|
|
|
|
ndir = dirlist.back();
|
|
|
|
dirlist.pop_back();
|
|
|
|
for(it = ndir->subdirs.begin(); it != ndir->subdirs.end(); it++)
|
|
|
|
{
|
|
|
|
dirlist.push_back(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(fit = ndir->files.begin(); fit != ndir->files.end(); fit++)
|
|
|
|
{
|
|
|
|
/*Evaluate the boolean expression and add it to the results if its true*/
|
|
|
|
bool ret = exp->eval(fit->second);
|
|
|
|
if (ret == true){
|
2009-08-25 16:49:50 -04:00
|
|
|
results.push_back(fit->second);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
bool FileIndex::extractData(void *ref,DirDetails& details)
|
2009-08-09 16:00:25 -04:00
|
|
|
{
|
2010-01-30 15:47:34 -05:00
|
|
|
if(!isValid(ref))
|
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
2010-02-08 17:32:00 -05:00
|
|
|
std::cerr << "FileIndex::extractData() asked for an invalid pointer " << (void*)ref << std::endl;
|
2010-01-30 15:47:34 -05:00
|
|
|
#endif
|
2009-08-11 15:43:51 -04:00
|
|
|
return false ;
|
2010-01-30 15:47:34 -05:00
|
|
|
}
|
2009-08-11 15:43:51 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
FileEntry *file = static_cast<FileEntry *>(ref);
|
2009-08-09 16:00:25 -04:00
|
|
|
DirEntry *dir = dynamic_cast<DirEntry *>(file);
|
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
details.children = std::list<DirStub>() ;
|
2010-02-08 17:32:00 -05:00
|
|
|
time_t now = time(NULL) ;
|
2010-01-30 15:47:34 -05:00
|
|
|
|
|
|
|
if (dir!=NULL) /* has children --- fill */
|
2009-08-09 16:00:25 -04:00
|
|
|
{
|
|
|
|
#ifdef FI_DEBUG
|
2010-02-08 17:32:00 -05:00
|
|
|
std::cerr << "FileIndex::extractData() ref=dir" << std::endl;
|
2009-08-09 16:00:25 -04:00
|
|
|
#endif
|
2010-01-30 15:47:34 -05:00
|
|
|
/* extract all the entries */
|
|
|
|
for(std::map<std::string,DirEntry*>::const_iterator dit(dir->subdirs.begin()); dit != dir->subdirs.end(); ++dit)
|
2009-08-09 16:00:25 -04:00
|
|
|
{
|
2010-01-30 15:47:34 -05:00
|
|
|
DirStub stub;
|
|
|
|
stub.type = DIR_TYPE_DIR;
|
|
|
|
stub.name = (dit->second) -> name;
|
|
|
|
stub.ref = (dit->second);
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
details.children.push_back(stub);
|
|
|
|
}
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
for(std::map<std::string,FileEntry*>::const_iterator fit(dir->files.begin()); fit != dir->files.end(); ++fit)
|
|
|
|
{
|
|
|
|
DirStub stub;
|
|
|
|
stub.type = DIR_TYPE_FILE;
|
|
|
|
stub.name = (fit->second) -> name;
|
|
|
|
stub.ref = (fit->second);
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
details.children.push_back(stub);
|
2009-08-09 16:00:25 -04:00
|
|
|
}
|
2010-01-30 15:47:34 -05:00
|
|
|
|
|
|
|
if(dir->parent == NULL)
|
|
|
|
details.type = DIR_TYPE_PERSON ;
|
2009-08-09 16:00:25 -04:00
|
|
|
else
|
2010-01-30 15:47:34 -05:00
|
|
|
details.type = DIR_TYPE_DIR;
|
|
|
|
details.hash = "";
|
|
|
|
details.count = dir->subdirs.size() + dir->files.size();
|
2010-02-08 17:32:00 -05:00
|
|
|
details.min_age = now - dir->most_recent_time ;
|
2010-01-30 15:47:34 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-09 16:00:25 -04:00
|
|
|
#ifdef FI_DEBUG
|
2010-02-08 17:32:00 -05:00
|
|
|
std::cerr << "FileIndexStore::extractData() ref=file" << std::endl;
|
2009-08-09 16:00:25 -04:00
|
|
|
#endif
|
2010-01-30 15:47:34 -05:00
|
|
|
details.type = DIR_TYPE_FILE;
|
|
|
|
details.count = file->size;
|
2010-02-08 17:32:00 -05:00
|
|
|
details.min_age = now - file->modtime ;
|
2010-01-30 15:47:34 -05:00
|
|
|
}
|
2009-08-25 16:49:50 -04:00
|
|
|
|
2009-08-09 16:00:25 -04:00
|
|
|
#ifdef FI_DEBUG
|
2010-02-08 17:32:00 -05:00
|
|
|
std::cerr << "FileIndexStore::extractData() name: " << file->name << std::endl;
|
2009-08-09 16:00:25 -04:00
|
|
|
#endif
|
2010-01-30 15:47:34 -05:00
|
|
|
details.ref = file;
|
|
|
|
details.hash = file->hash;
|
2010-02-08 17:32:00 -05:00
|
|
|
details.age = now - file->modtime;
|
2010-01-30 15:47:34 -05:00
|
|
|
details.flags = 0;//file->pop;
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
/* find parent pointer, and row */
|
|
|
|
details.parent = file->parent ;
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
details.prow = (file->parent==NULL)?0:file->parent->row ;
|
2010-01-31 09:21:24 -05:00
|
|
|
|
|
|
|
if(details.type == DIR_TYPE_DIR)
|
|
|
|
{
|
|
|
|
details.name = file->name;
|
|
|
|
details.path = dir->path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
details.name = file->name;
|
|
|
|
details.path = (file->parent==NULL)?"":file->parent->path;
|
|
|
|
}
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
/* find peer id */
|
|
|
|
FileEntry *f ;
|
|
|
|
for(f=file;f->parent!=NULL;f=f->parent) ;
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
details.id = dynamic_cast<PersonEntry*>(f)->id;
|
2009-08-09 16:00:25 -04:00
|
|
|
|
2010-01-30 15:47:34 -05:00
|
|
|
#ifdef FI_DEBUG
|
|
|
|
assert(details.parent != details.ref) ;
|
|
|
|
std::cout << "details: ref=" << (void*)ref << ", prow=" << details.prow << ", parent=" << (void*)details.parent << ", children=" ;
|
|
|
|
for(std::list<DirStub>::iterator it(details.children.begin());it!=details.children.end();++it)
|
|
|
|
std::cout << " " << (void*)it->ref ;
|
|
|
|
std::cout << std::endl ;
|
|
|
|
#endif
|
2009-08-09 16:00:25 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|