Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsDirectoryCrawler.cpp
Go to the documentation of this file.
1 
10 #include "WsDirectoryCrawler.h"
11 #include <Include/WsGlobalConfig.h>
12 
13 using namespace boost::filesystem;
14 using namespace std;
15 
16 WsDirectoryCrawler::WsDirectoryCrawler(const string& p): m_isLoaded(false),
17  m_p(p)
18 {
19 }
20 
22 {
23 }
24 
26 {
27  path thePath(m_p);
28  m_isLoaded = false;
29  /* Avoid browsing current and hidden directories (avoid endless loops) */
30  if (thePath.filename().string().find_first_of(".") == 0 && thePath.filename().string() != "." ) {
31  LOG(ERROR) << "DirCrawler::start() : Root cannot be hidden (" << thePath << ")" << endl;
32  return ErrorCode::Failure;
33  }
34  /* Browse current path */
35  int r = this->browse(thePath);
36  if (r == ErrorCode::Success)
37  m_isLoaded = true;
38  return r;
39 }
40 
41 int WsDirectoryCrawler::browse(const path& p)
42 {
43  try {
44  /* If path exists */
45  if (exists(p)) {
46  if (is_regular_file(p)) {
47  /* it's a regular file, we call the virtual function applyFile */
48  return applyFile(p);
49  } else if (is_directory(p)) {
50  /* it's a directory, we call the virtual functuin applyDirectory */
51  int r = applyDirectory(p);
52  if (r == ErrorCode::Failure)
53  return ErrorCode::Failure;
54  directory_iterator endItr;
55  /* We iterate on all the entries of the directory */
56  for (directory_iterator dItr(p); dItr != endItr; ++dItr) {
57  /* We call this function to notify that we are accessing a child */
58  if (dItr->path().filename().string().find_first_of(".") == 0)
59  continue;
60  beginChild(dItr->path());
61  /* recursive call on the new level */
62  int r = browse(dItr->path());
63  /* Notify that crawling over the childs is done */
64  endChild(dItr->path());
65  if (r == ErrorCode::Failure)
66  return ErrorCode::Failure;
67  }
68  } else {
69  LOG(ERROR) << "DirCrawler::browse() : This is not a regular File nor a directory : " << p.string() << endl;
70  }
71  } else {
72  LOG(ERROR) << "DirCrawler::browse() : The file does not exist : " << p << endl;
73  if (p == m_p)
74  return ErrorCode::Failure;
75  }
76  } catch (const filesystem_error& ex) {
77  LOG(ERROR) << "DirCrawler :: " << ex.what() << endl;
78  }
79  return ErrorCode::Success;
80 }
81 
int browse(const boost::filesystem::path &p)
applies the required action if file or folder, and browse its children
Fetch a directory and all subdirs.
int start()
Start crawling over all the files and directories/subdirectories.
const int Failure
#define LOG
Definition: WsLogger.h:22
WsDirectoryCrawler(const std::string &path)
Class constructor, this class is Abstract and must therefore be inherited.
virtual void endChild(const boost::filesystem::path &p)=0
this method is called when finished browsing the subdir
virtual int applyFile(const boost::filesystem::path &p)=0
this method is called by browse when the boost::filesystem::path is a file
virtual void beginChild(const boost::filesystem::path &p)=0
this method is called before browsing a subdirectory
virtual int applyDirectory(const boost::filesystem::path &p)=0
this method is called by browse when the boost::filesystem::path is a folder
const int Success
virtual ~WsDirectoryCrawler()=0
Virtual destructor.
#define ERROR
Definition: WsLogger.h:42