From 09d285d94d844311e2d691ddb2f9a4b512eec72b Mon Sep 17 00:00:00 2001 From: mrbesen Date: Tue, 8 Nov 2022 21:51:29 +0100 Subject: [PATCH] more options --- inc/dedup.h | 2 ++ inc/fileindexer.h | 4 ++++ src/dedup.cpp | 18 ++++++++++++------ src/fileindexer.cpp | 22 ++++++++++++++++------ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/inc/dedup.h b/inc/dedup.h index 184a831..ae6a9c1 100644 --- a/inc/dedup.h +++ b/inc/dedup.h @@ -35,6 +35,8 @@ private: bool hardlink = true; bool ignoredotfiles = false; bool dryrun = true; + bool deleteDuplicates = true; + bool ignoreInodeID = true; uint64_t deduplicatedBytes = 0; // amount of bytes deduplicated uint64_t deduplicatedFiles = 0; }; diff --git a/inc/fileindexer.h b/inc/fileindexer.h index 0ac040e..6ccf03c 100644 --- a/inc/fileindexer.h +++ b/inc/fileindexer.h @@ -16,10 +16,14 @@ public: using map_t = std::multimap>; map_t index(const SearchFolder& sf); + + void setIgnoreDotFiles(bool b = true); + void setIgnoreInodeIDs(bool b = true); private: void handleFolderContent(dirent* dircont, const SearchFolder& sf, std::list& newfolders, map_t& newfiles); void handleNewFile(ino_t inode, const std::string& path, map_t& newfiles); std::set knownInodes; // file inodes, that are known and should not be indexed again bool ignoredotfiles = false; + bool ignoreInodeID = false; }; diff --git a/src/dedup.cpp b/src/dedup.cpp index 653051c..dfcae59 100644 --- a/src/dedup.cpp +++ b/src/dedup.cpp @@ -38,6 +38,8 @@ Files& getFiles(uint64_t fs, std::map& m) { void Dedup::start() { FileIndexer::map_t foundfiles; + indexer.setIgnoreDotFiles(ignoredotfiles); + indexer.setIgnoreInodeIDs(ignoreInodeID); for(const SearchFolder& sf : folders) { foundfiles.merge(indexer.index(sf)); } @@ -192,7 +194,9 @@ bool Dedup::relinkFile(const std::string& linkbase, const std::string& replacedf if(dryrun || !hardlink) { Log::note << "delete " << replacedfile; - Log::note << "link " << linkbase << " -> " << replacedfile; + if(!deleteDuplicates) { + Log::note << "link " << linkbase << " -> " << replacedfile; + } } else { int res = ::unlink(replacedfile.c_str()); if(res != 0) { @@ -200,11 +204,13 @@ bool Dedup::relinkFile(const std::string& linkbase, const std::string& replacedf return false; } - res = ::link(linkbase.c_str(), replacedfile.c_str()); - if(res != 0) { - Log::error << "link(" << linkbase << ", " << replacedfile << ") failed: " << strerror(errno) << " (" << errno << ')'; - // TODO try to symlink? - return false; + if(!deleteDuplicates) { + res = ::link(linkbase.c_str(), replacedfile.c_str()); + if(res != 0) { + Log::error << "link(" << linkbase << ", " << replacedfile << ") failed: " << strerror(errno) << " (" << errno << ')'; + // TODO try to symlink? + return false; + } } } return true; diff --git a/src/fileindexer.cpp b/src/fileindexer.cpp index cce6092..a740662 100644 --- a/src/fileindexer.cpp +++ b/src/fileindexer.cpp @@ -35,6 +35,13 @@ std::multimap> FileIndexer::index(const SearchFo return out; } +void FileIndexer::setIgnoreDotFiles(bool b) { + ignoredotfiles = b; +} + +void FileIndexer::setIgnoreInodeIDs(bool b) { + ignoreInodeID = b; +} void FileIndexer::handleFolderContent(dirent* dircont, const SearchFolder& sf, std::list& newfolders, map_t& newfiles) { std::string name(dircont->d_name); @@ -62,10 +69,12 @@ void FileIndexer::handleFolderContent(dirent* dircont, const SearchFolder& sf, s void FileIndexer::handleNewFile(ino_t inode, const std::string& path, map_t& newfiles) { // check for already scanned inodes - auto it = knownInodes.find(inode); - if(it != knownInodes.end()) { - Log::note << "found already detected file: " << inode << ' ' << path; - return; + if(!ignoreInodeID) { + auto it = knownInodes.find(inode); + if(it != knownInodes.end()) { + Log::note << "found already detected file: " << inode << ' ' << path; + return; + } } struct stat statbuf; @@ -78,8 +87,9 @@ void FileIndexer::handleNewFile(ino_t inode, const std::string& path, map_t& new loff_t fileSize = statbuf.st_size; nlink_t linkCount = statbuf.st_nlink; - - knownInodes.insert(inode); + if(!ignoreInodeID) { + knownInodes.insert(inode); + } newfiles.insert({fileSize, std::make_shared(fileSize, inode, linkCount, path)}); }