From d4d3566421585b19e2bbeb9066978601d18b3f89 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 23 Aug 2017 11:00:13 +0100 Subject: [PATCH] Move SingleFileTask into a file of its own so it can be used elsewhere. --- .../scantask/singlefile.hpp | 41 +++++++++++ src/locate/hash.cpp | 45 +----------- src/machinery/CMakeLists.txt | 1 + src/machinery/scantask/singlefile.cpp | 68 +++++++++++++++++++ 4 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 include/dindexer-machinery/scantask/singlefile.hpp create mode 100644 src/machinery/scantask/singlefile.cpp diff --git a/include/dindexer-machinery/scantask/singlefile.hpp b/include/dindexer-machinery/scantask/singlefile.hpp new file mode 100644 index 0000000..e56fe2f --- /dev/null +++ b/include/dindexer-machinery/scantask/singlefile.hpp @@ -0,0 +1,41 @@ +/* Copyright 2015, 2016, 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 . + */ + +#include "dindexer-machinery/scantask/dirtree.hpp" +#include + +struct stat; + +namespace mchlib { + namespace scantask { + class SingleFileTask : public Base> { + public: + typedef std::vector PathList; + + explicit SingleFileTask ( std::string parPath ); + explicit SingleFileTask ( std::string parPath, const struct stat* parStat ); + virtual ~SingleFileTask ( void ) noexcept = default; + + private: + virtual void on_data_destroy ( PathList& parData ) override; + virtual void on_data_create ( PathList& parData ) override; + + std::string m_path; + const struct stat* m_stat; + }; + } //namespace scantask +} //namespace mchlib diff --git a/src/locate/hash.cpp b/src/locate/hash.cpp index 43f1708..52d1b56 100644 --- a/src/locate/hash.cpp +++ b/src/locate/hash.cpp @@ -17,7 +17,7 @@ #include "hash.hpp" #include "dindexer-machinery/scantask/hashing.hpp" -#include "dindexer-machinery/scantask/dirtree.hpp" +#include "dindexer-machinery/scantask/singlefile.hpp" #include "dindexer-machinery/recorddata.hpp" #include "dindexer-machinery/make_filerecord_tree.hpp" #include @@ -31,48 +31,6 @@ namespace stask = mchlib::scantask; namespace din { namespace { - class SingleFileTask : public stask::Base> { - public: - typedef std::vector PathList; - - SingleFileTask ( std::string parPath, const struct stat* parStat ); - virtual ~SingleFileTask ( void ) noexcept = default; - - private: - virtual void on_data_destroy ( PathList& parData ) override; - virtual void on_data_create ( PathList& parData ) override; - - std::string m_path; - const struct stat* m_stat; - }; - - SingleFileTask::SingleFileTask (std::string parPath, const struct stat* parStat) : - m_path(std::move(parPath)), - m_stat(parStat) - { - assert(not m_path.empty()); - assert(m_stat); - } - - void SingleFileTask::on_data_destroy (PathList& parData) { - assert(not parData.empty()); - parData.clear(); - } - - void SingleFileTask::on_data_create (PathList& parData) { - assert(parData.empty()); - parData.reserve(1); - parData.push_back(mchlib::FileRecordData( - std::string(m_path), - 0, - m_stat->st_atime, - m_stat->st_mtime, - 0, - false, - false - )); - } - void fill_hash_nodes ( const std::vector& parRefData, const std::vector& parNodesIn, @@ -99,6 +57,7 @@ namespace din { std::vector hash (const std::string& parPath) { using mchlib::FileRecordData; using HashingTaskPtr = std::shared_ptr; + using stask::SingleFileTask; struct stat path_stat; { diff --git a/src/machinery/CMakeLists.txt b/src/machinery/CMakeLists.txt index 2c8ac3e..dc9a8fd 100644 --- a/src/machinery/CMakeLists.txt +++ b/src/machinery/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(${PROJECT_NAME} SHARED scantask/contenttype.cpp scantask/mime.cpp scantask/setbasic.cpp + scantask/singlefile.cpp make_filerecord_tree.cpp ) diff --git a/src/machinery/scantask/singlefile.cpp b/src/machinery/scantask/singlefile.cpp new file mode 100644 index 0000000..e74b835 --- /dev/null +++ b/src/machinery/scantask/singlefile.cpp @@ -0,0 +1,68 @@ +/* Copyright 2015, 2016, 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 . + */ + +#include "dindexer-machinery/scantask/singlefile.hpp" +#include "dindexer-machinery/recorddata.hpp" +#include +#include +#include + +namespace mchlib { + namespace scantask { + SingleFileTask::SingleFileTask (std::string parPath) : + SingleFileTask(parPath, nullptr) + { + } + + SingleFileTask::SingleFileTask (std::string parPath, const struct stat* parStat) : + m_path(std::move(parPath)), + m_stat(parStat) + { + assert(not m_path.empty()); + } + + void SingleFileTask::on_data_destroy (PathList& parData) { + assert(not parData.empty()); + parData.clear(); + } + + void SingleFileTask::on_data_create (PathList& parData) { + struct stat path_stat; + const struct stat* stat_to_use = m_stat; + + if (not stat_to_use) { + const int retval = stat(m_path.c_str(), &path_stat); + if (retval) { + throw std::runtime_error("Can't access file \"" + m_path + "\""); + } + stat_to_use = &path_stat; + } + + assert(parData.empty()); + parData.reserve(1); + parData.push_back(mchlib::FileRecordData( + std::string(m_path), + 0, + stat_to_use->st_atime, + stat_to_use->st_mtime, + 0, + false, + false + )); + } + } //namespace scantask +} //namespace mchlib