1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Group parameters in a struct when calling filesearcher's callback.

This commit is contained in:
King_DuckZ 2015-12-01 16:34:22 +00:00
parent 26e62df73f
commit f1c3deea63
6 changed files with 69 additions and 17 deletions

View file

@ -39,21 +39,28 @@ namespace fastf {
__thread SearchOptions g_searchOptions;
bool print_def_callback (const char* parPath, int parLevel, bool parDir, bool parSymlink) {
bool print_def_callback (const char* parPath, const FileStats& parStats) {
std::cout << parPath;
if (parDir) {
if (parStats.is_dir) {
std::cout << '/';
}
if (parSymlink) {
if (parStats.is_symlink) {
std::cout << " (symlink)";
}
std::cout << " (" << parLevel << ')';
std::cout << " (" << parStats.level << ')';
std::cout << '\n';
return true;
}
int HandleDir (const char* parPath, const struct stat*, FTW* parBuff, bool parCanBeRead, bool parSymlink) {
if (not (*g_searchOptions.callback)(parPath, parBuff->level, true, parSymlink))
int HandleDir (const char* parPath, const struct stat* parStat, FTW* parBuff, bool parCanBeRead, bool parSymlink) {
FileStats st;
st.level = parBuff->level;
st.is_dir = true;
st.is_symlink = parSymlink;
st.atime = parStat->st_atime;
st.mtime = parStat->st_mtime;
if (not (*g_searchOptions.callback)(parPath, st))
return FTW_STOP;
if (!parCanBeRead)
return FTW_SKIP_SUBTREE;
@ -66,10 +73,17 @@ namespace fastf {
return FTW_CONTINUE;
}
int HandleFile (const char* parPath, const struct stat*, FTW* parBuff, bool parSymlink) {
int HandleFile (const char* parPath, const struct stat* parStat, FTW* parBuff, bool parSymlink) {
const FileSearcher::ConstCharVecType& extensions = *g_searchOptions.extensions;
FileStats st;
st.level = parBuff->level;
st.is_dir = false;
st.is_symlink = parSymlink;
st.atime = parStat->st_atime;
st.mtime = parStat->st_mtime;
if (extensions.empty()) {
if (not (*g_searchOptions.callback)(parPath, parBuff->level, false, parSymlink)) {
if (not (*g_searchOptions.callback)(parPath, st)) {
return FTW_STOP;
}
}
@ -78,7 +92,7 @@ namespace fastf {
const int& extLen = extensions[z].len;
const int pathLen = parBuff->base + std::strlen(parPath + parBuff->base);
if (std::strncmp(extensions[z].str, parPath + pathLen - extLen, extensions[z].len) == 0) {
if (not (*g_searchOptions.callback)(parPath, parBuff->level, false, parSymlink)) {
if (not (*g_searchOptions.callback)(parPath, st)) {
return FTW_STOP;
}
break;

View file

@ -18,6 +18,7 @@
#ifndef idB6D385B7779240308449D6081CB790F1
#define idB6D385B7779240308449D6081CB790F1
#include "filestats.hpp"
#include <vector>
#include <functional>
#include <boost/utility/string_ref.hpp>
@ -37,7 +38,7 @@ namespace fastf {
class FileSearcher {
public:
typedef std::vector<StringWithLength> ConstCharVecType;
typedef std::function<bool(const char*, int, bool, bool)> CallbackType;
typedef std::function<bool(const char*, const FileStats&)> CallbackType;
explicit FileSearcher ( boost::string_ref parBaseDir );
~FileSearcher ( void ) noexcept;

33
src/filestats.hpp Normal file
View file

@ -0,0 +1,33 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id4A7D7AB671954418939FC0BDA19C5B3F
#define id4A7D7AB671954418939FC0BDA19C5B3F
#include <ctime>
namespace fastf {
struct FileStats {
int level;
std::time_t atime;
std::time_t mtime;
bool is_dir;
bool is_symlink;
};
} //namespace fastf
#endif

View file

@ -20,6 +20,7 @@
#include "tiger.hpp"
#include "dbbackend.hpp"
#include "settings.hpp"
#include "filestats.hpp"
#include <algorithm>
#include <functional>
#include <vector>
@ -311,9 +312,10 @@ namespace din {
return true;
}
bool Indexer::add_path (const char* parPath, int parLevel, bool parIsDir, bool parIsSymLink) {
m_local_data->paths.push_back(FileEntry(parPath, parLevel, parIsDir, parIsSymLink));
if (not parIsDir) {
bool Indexer::add_path (const char* parPath, const fastf::FileStats& parStats) {
m_local_data->paths.push_back(
FileEntry(parPath, parStats));
if (not parStats.is_dir) {
++m_local_data->file_count;
}
return true;

View file

@ -31,6 +31,10 @@ namespace std {
} //namespace std
#endif
namespace fastf {
struct FileStats;
} //namespace fastf
namespace din {
struct DinDBSettings;
@ -41,7 +45,7 @@ namespace din {
Indexer ( const Indexer& ) = delete;
~Indexer ( void ) noexcept;
bool add_path ( const char* parPath, int parLevel, bool parIsDir, bool parIsSymlink );
bool add_path ( const char* parPath, const fastf::FileStats& parStats );
#if defined(INDEXER_VERBOSE)
void dump ( void ) const;
#endif

View file

@ -45,8 +45,6 @@ namespace {
int main (int parArgc, char* parArgv[]) {
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
using std::placeholders::_4;
using boost::program_options::variables_map;
variables_map vm;
@ -79,7 +77,7 @@ int main (int parArgc, char* parArgv[]) {
fastf::FileSearcher searcher(search_path);
fastf::FileSearcher::ConstCharVecType ext, ignore;
searcher.SetFollowSymlinks(true);
searcher.SetCallback(fastf::FileSearcher::CallbackType(std::bind(&din::Indexer::add_path, &indexer, _1, _2, _3, _4)));
searcher.SetCallback(fastf::FileSearcher::CallbackType(std::bind(&din::Indexer::add_path, &indexer, _1, _2)));
searcher.Search(ext, ignore);
if (verbose) {
std::cout << "Fetching items list...\n";