ewmscp  ..
moveRequestProvider.cpp
Go to the documentation of this file.
1 #include "moveRequestProvider.h"
2 #include "throwcall.h"
3 #include "escapism.h"
4 #include <errMsgQueue.h>
5 
7 
8 void moveRequestProvider::processSource(const std::string& source) {
9  auto sourceStat = InputHandler->getStat(source, dereferenceCmdLine);
10  if (!sourceStat) {
11  if (ignoreMissing) {
12  if (! quiet) {
14  source, "stat", "not found");
15  }
16  return;
17  }
18  throw std::runtime_error("can't stat " + source);
19  }
20  if (sourceStat->isDir()) {
21  if (recursive) {
22  std::string dstPath;
23  auto mapEntry = getDstPath(source, dstPath, true);
24  addDirContent(source, dstPath, mapEntry, *sourceStat);
25  } else {
26  throw std::runtime_error(source + " is a directory");
27  }
28  } else {
29  std::string dst;
30  auto mapEntry = getDstPath(source, dst, true);
31  requestList.push_front(new copyRequest::base(InputHandler, source, dst,
32  source, source, mapEntry,
33  copyRequest::clock_type::now()));
34  }
35 }
36 
37 void moveRequestProvider::processSources(const std::vector<std::string>& sources) {
38 
39  for (auto& source : sources) {
40  static auto escaper = escapism::newEscaper(sourceDecodeMethod);
41  if (escaper) {
42  std::string srcbuf;
43  escaper->deEscape(source, srcbuf);
44  processSource(srcbuf);
45  } else {
46  processSource(source);
47  }
48  }
49  for (auto request : requestList) {
50  std::unique_ptr<copyRequest::base> item(request);
51  requests.enqueue(item);
52  }
53  requestList.clear();
54 }
55 
56 void moveRequestProvider::addDirContent(const std::string& srcpath,
57  const std::string& dstpath,
58  const singleMap &mapEntry,
59  const genericStat& dirStat) {
60  // with the subdir list we have only one open dir at a time
61  class subdirType {
62  public:
63  std::string srcPath;
64  std::string dstPath;
65  std::unique_ptr<const genericStat> dirStat;
66  subdirType(const std::string& aSrcPath,
67  const std::string& aDstPath,
68  std::unique_ptr<const genericStat>& aDirStat) :
69  srcPath(aSrcPath),
70  dstPath(aDstPath),
71  dirStat(std::move(aDirStat)) {};
72  };
73  std::list<subdirType> subdirs;
74  {
75  auto dir = InputHandler->getDirectory(srcpath);
76  while (auto entry = dir->getNextEntry(ignoreMissing)) {
77  std::string subpath(srcpath);
78  subpath += "/";
79  subpath += entry->getName();
80  std::string destpath(dstpath);
81  destpath += "/";
82  destpath += entry->getName();
83  auto entryStat = entry->getStat();
84 
85  if (entryStat->isDir() &&
86  entryStat->device == dirStat.device) {
87  subdirs.emplace_back(subpath, destpath, entryStat);
88  } else if (entryStat->isRegularFile() || entryStat->isLink()) {
89  requestList.push_front(new copyRequest::base(InputHandler, subpath, destpath,
90  subpath, subpath, mapEntry,
91  copyRequest::clock_type::now()));
92  }
93  }
94  }
95 
96  for (auto& subdir : subdirs) {
97  addDirContent(subdir.srcPath, subdir.dstPath, mapEntry, *(subdir.dirStat));
98  }
99 }
100 
noFollowRequestProvider::ignoreMissing
static options::single< bool > ignoreMissing
Definition: noFollowRequestProvider.h:11
errMsgQueue.h
errMsg::level::warning
@ warning
errMsg::location
class for defining the location of a error message in the source code.
Definition: errMsgQueue.h:14
moveRequestProvider.h
inputHandler::base::getDirectory
virtual std::unique_ptr< Directory > getDirectory(const std::string &path)=0
noFollowRequestProvider::sourceDecodeMethod
static options::single< std::string > & sourceDecodeMethod
Definition: noFollowRequestProvider.h:10
genericStat
generic stat abstraction class Used to abstract the variants of the stat structure.
Definition: genericStat.h:12
cmdLineRequestProvider::getDstPath
const singleMap & getDstPath(const std::string &source, std::string &destination, bool baseNameOnly=false) override
get detstination papth for a given source path
Definition: cmdLineRequestProvider.cpp:18
cmdLineRequestProvider::recursive
static options::single< bool > recursive
Definition: cmdLineRequestProvider.h:13
copyRequest::base
class for copy requests.
Definition: copyRequest.h:99
moveRequestProvider::requestList
std::list< copyRequest::base * > requestList
Definition: moveRequestProvider.h:9
escapism.h
throwcall.h
genericStat::device
dev_t device
Definition: genericStat.h:15
waitQueues::simple::enqueue
void enqueue(std::unique_ptr< T > &item)
Definition: waitQueues.h:37
moveRequestProvider::factory
static factoryTemplate< moveRequestProvider > factory
Definition: moveRequestProvider.h:7
requestProvider::requests
copyRequest::simpleQueue & requests
Definition: requestProvider.h:31
escapism::newEscaper
static const escapism * newEscaper(const std::string &name)
Definition: escapism.cpp:25
moveRequestProvider::processSources
void processSources(const std::vector< std::string > &sources) override
Definition: moveRequestProvider.cpp:37
pathHandler::getStat
virtual std::unique_ptr< const genericStat > getStat(const std::string &path, bool followLink=true)=0
errMsg::emit
void emit(level aLogLevel, const location &loc, const std::string &aObject, const std::string &aAction, const Args &... args)
function to create and enqueue a message, this is the only way that messages should be created!
Definition: errMsgQueue.h:148
singleMap
std::pair< std::string, std::string > singleMap
Definition: copyRequest.h:52
requestProvider::InputHandler
inputHandler::base * InputHandler
Definition: requestProvider.h:34
moveRequestProvider::addDirContent
void addDirContent(const std::string &srcpath, const std::string &dstpath, const singleMap &mapEntry, const genericStat &dirStat) override
Definition: moveRequestProvider.cpp:56
moveRequestProvider::processSource
void processSource(const std::string &source) override
Definition: moveRequestProvider.cpp:8
quiet
static options::single< bool > quiet('q', "quiet", "be quiet, no prefix to output lines")
cmdLineRequestProvider::dereferenceCmdLine
static options::single< bool > dereferenceCmdLine
Definition: cmdLineRequestProvider.h:14
defineStatic
#define defineStatic(var,...)
defines a static variable and instatitates the constructor with the variable number of arguments.
Definition: ewmscp.h:42