/usr/include/eiskaltdcpp/dcpp/HashManager.h is in libeiskaltdcpp-dev 2.2.9-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | /*
* Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "Singleton.h"
#include "MerkleTree.h"
#include "Thread.h"
#include "CriticalSection.h"
#include "Semaphore.h"
#include "TimerManager.h"
#include "Util.h"
#include "FastAlloc.h"
#include "Text.h"
#include "Streams.h"
#include "HashManagerListener.h"
#ifdef USE_XATTR
#include "attr/attributes.h"
#else
#define ATTR_MAX_VALUELEN 128
#endif
namespace dcpp {
STANDARD_EXCEPTION(HashException);
class File;
class HashLoader;
class FileException;
class HashManager : public Singleton<HashManager>, public Speaker<HashManagerListener>,
private TimerManagerListener
{
public:
/** We don't keep leaves for blocks smaller than this... */
static const int64_t MIN_BLOCK_SIZE;
HashManager() {
TimerManager::getInstance()->addListener(this);
}
virtual ~HashManager() noexcept {
TimerManager::getInstance()->removeListener(this);
hasher.join();
}
/**
* Check if the TTH tree associated with the filename is current.
*/
bool checkTTH(const string& aFileName, int64_t aSize, uint32_t aTimeStamp);
void stopHashing(const string& baseDir) { hasher.stopHashing(baseDir); }
void setPriority(Thread::Priority p) { hasher.setThreadPriority(p); }
/** @return TTH root */
TTHValue getTTH(const string& aFileName, int64_t aSize);
/** eiskaltdc++ **/
const TTHValue* getFileTTHif(const string& aFileName);
bool getTree(const TTHValue& root, TigerTree& tt);
/** Return block size of the tree associated with root, or 0 if no such tree is in the store */
size_t getBlockSize(const TTHValue& root);
void addTree(const string& aFileName, uint32_t aTimeStamp, const TigerTree& tt) {
hashDone(aFileName, aTimeStamp, tt, -1, -1);
}
void addTree(const TigerTree& tree) { Lock l(cs); store.addTree(tree); }
void getStats(string& curFile, int64_t& bytesLeft, size_t& filesLeft) {
hasher.getStats(curFile, bytesLeft, filesLeft);
}
/**
* Rebuild hash data file
*/
void rebuild() { hasher.scheduleRebuild(); }
void startup() { hasher.start(); store.load(); }
void shutdown() {
hasher.shutdown();
hasher.join();
Lock l(cs);
store.save();
}
struct HashPauser {
HashPauser();
~HashPauser();
private:
bool resume;
};
/// @return whether hashing was already paused
bool pauseHashing();
void resumeHashing();
bool isHashingPaused() const;
private:
class Hasher : public Thread {
public:
Hasher() : stop(false), running(false), paused(0), rebuild(false), currentSize(0) { }
void hashFile(const string& fileName, int64_t size);
/// @return whether hashing was already paused
bool pause();
void resume();
bool isPaused() const;
void stopHashing(const string& baseDir);
virtual int run();
bool fastHash(const string& fname, uint8_t* buf, TigerTree& tth, int64_t size, CRC32Filter* xcrc32);
void getStats(string& curFile, int64_t& bytesLeft, size_t& filesLeft);
void shutdown() { stop = true; if(paused){ s.signal(); resume();} s.signal(); }
void scheduleRebuild() { rebuild = true; if(paused) s.signal(); s.signal(); }
private:
// Case-sensitive (faster), it is rather unlikely that case changes, and if it does it's harmless.
// map because it's sorted (to avoid random hash order that would create quite strange shares while hashing)
typedef map<string, int64_t> WorkMap;
typedef WorkMap::iterator WorkIter;
WorkMap w;
mutable CriticalSection cs;
Semaphore s;
bool stop;
bool running;
unsigned paused;
bool rebuild;
string currentFile;
int64_t currentSize;
void instantPause();
};
friend class Hasher;
class HashStore {
public:
HashStore();
void addFile(const string& aFileName, uint32_t aTimeStamp, const TigerTree& tth, bool aUsed);
void load();
void save();
void rebuild();
bool checkTTH(const string& aFileName, int64_t aSize, uint32_t aTimeStamp);
void addTree(const TigerTree& tt) noexcept;
const TTHValue* getTTH(const string& aFileName);
bool getTree(const TTHValue& root, TigerTree& tth);
size_t getBlockSize(const TTHValue& root) const;
bool isDirty() { return dirty; }
private:
/** Root -> tree mapping info, we assume there's only one tree for each root (a collision would mean we've broken tiger...) */
struct TreeInfo {
TreeInfo() : size(0), index(0), blockSize(0) { }
TreeInfo(int64_t aSize, int64_t aIndex, int64_t aBlockSize) : size(aSize), index(aIndex), blockSize(aBlockSize) { }
TreeInfo(const TreeInfo& rhs) : size(rhs.size), index(rhs.index), blockSize(rhs.blockSize) { }
TreeInfo& operator=(const TreeInfo& rhs) { size = rhs.size; index = rhs.index; blockSize = rhs.blockSize; return *this; }
GETSET(int64_t, size, Size);
GETSET(int64_t, index, Index);
GETSET(int64_t, blockSize, BlockSize);
};
/** File -> root mapping info */
struct FileInfo {
public:
FileInfo(const string& aFileName, const TTHValue& aRoot, uint32_t aTimeStamp, bool aUsed) :
fileName(aFileName), root(aRoot), timeStamp(aTimeStamp), used(aUsed) { }
bool operator==(const string& name) { return name == fileName; }
GETSET(string, fileName, FileName);
GETSET(TTHValue, root, Root);
GETSET(uint32_t, timeStamp, TimeStamp);
GETSET(bool, used, Used);
};
typedef vector<FileInfo> FileInfoList;
typedef FileInfoList::iterator FileInfoIter;
typedef unordered_map<string, FileInfoList> DirMap;
typedef DirMap::iterator DirIter;
typedef unordered_map<TTHValue, TreeInfo> TreeMap;
typedef TreeMap::iterator TreeIter;
friend class HashLoader;
DirMap fileIndex;
TreeMap treeIndex;
bool dirty;
void createDataFile(const string& name);
bool loadTree(File& dataFile, const TreeInfo& ti, const TTHValue& root, TigerTree& tt);
int64_t saveTree(File& dataFile, const TigerTree& tt);
string getIndexFile() { return Util::getPath(Util::PATH_USER_CONFIG) + "HashIndex.xml"; }
string getDataFile() { return Util::getPath(Util::PATH_USER_CONFIG) + "HashData.dat"; }
};
friend class HashLoader;
Hasher hasher;
HashStore store;
class StreamStore
{
public:
struct TTHStreamHeader
{
uint32_t magic;
uint32_t checksum;
uint64_t fileSize;
uint64_t timeStamp;
uint64_t blockSize;
TTHValue root;
};
bool loadTree(const string& p_filePath, TigerTree &tree, int64_t p_aFileSize = -1);
bool saveTree(const string& p_filePath, const TigerTree& p_Tree);
void deleteStream(const string& p_filePath);
private:
static const uint32_t g_MAGIC = 0x2b2b6c67;
static const string g_streamName;
void setCheckSum(TTHStreamHeader& p_header);
bool validateCheckSum(const TTHStreamHeader& p_header);
};
StreamStore m_streamstore;
mutable CriticalSection cs;
/** Single node tree where node = root, no storage in HashData.dat */
static const int64_t SMALL_TREE = -1;
void hashDone(const string& aFileName, uint32_t aTimeStamp, const TigerTree& tth, int64_t speed, int64_t size);
void doRebuild() {
Lock l(cs);
store.rebuild();
}
virtual void on(TimerManagerListener::Minute, uint64_t) noexcept {
Lock l(cs);
store.save();
}
void on(TimerManagerListener::Second, uint64_t) noexcept;
};
} // namespace dcpp
|