ewmscp  ..
Typedefs | Functions | Variables
The dirCount command

tool to count the objects in a directory tree More...

Typedefs

typedef MyWaitQueues::simple< std::string > dirQueueType
 

Functions

void handleDir (const std::string &path, counterType &counter)
 
int main (int argc, const char *argv[])
 

Variables

static options::single< bool > ignoreAccess ('i', "ignoreAccess", "ignore subdirs we may not access")
 
static options::single< bool > quiet ('q', "quiet", "be quiet, no prefix to output lines")
 
static options::single< bool > reportUnknowns ('U',"reportUnknowns", "print a list of items with type DT_UNKNOWN")
 
static options::single< unsigned > nThreads ('n', "nThreads", "number of threads", 0)
 
static std::forward_list< std::string > unknownList
 
static std::mutex unknownListMutex
 
static unsigned int runnerType::nWorkers
 
static std::mutex runnerType::listMutex
 
static std::condition_variable runnerType::cv
 
static std::atomic< unsigned int > runnerType::nActiveWorkers
 

Detailed Description

tool to count the objects in a directory tree

Typedef Documentation

◆ dirQueueType

typedef MyWaitQueues::simple<std::string> dirQueueType

Definition at line 142 of file dirCount.cpp.

Function Documentation

◆ handleDir()

void handleDir ( const std::string &  path,
counterType counter 
)

Definition at line 259 of file dirCount.cpp.

259  {
260  static timer::anchor a("opendir");
262  auto result = opendir(path.c_str());
263  i.stop();
264  if (result == nullptr) {
265  if (errno == EACCES && ignoreAccess) {
266  counter.count(counterType::specials::denied);
267  return;
268  } else if (errno == ENOENT) {
269  counter.count(counterType::specials::noent);
270  return;
271  } else if (errno == ENOTDIR) {
272  counter.count(counterType::specials::notdir);
273  return;
274  }
275  throwcall::badval(result, nullptr, "can't open directory ", path);
276  }
277  scoped::Dir dir(path, result);
278  std::forward_list<std::string> subdirs;
279  static timer::anchor b("readdir");
281  while (auto entry = readdir(dir)) {
282  I.stop();
283  if (entry->d_name[entry->d_name[0] != '.' ? 0 : entry->d_name[1] != '.' ? 1 : 2] == '\0') {
284  I.restart();
285  continue; // skip . .. and empty strings
286  }
287  counter.count(entry->d_type);
288  if (entry->d_type == DT_DIR) {
289  subdirs.emplace_front(entry->d_name);
290  } else if (entry->d_type == DT_UNKNOWN && reportUnknowns) {
291  std::string item(path);
292  item += "/";
293  item += entry->d_name;
294  unknownList.emplace_front(item);
295  }
296  I.restart();
297  }
298  for (const auto& subdir : subdirs) {
299  handleDir(path + "/" + subdir, counter);
300  }
301 
302 }

References throwcall::badval(), counterType::count(), ignoreAccess, reportUnknowns, timer::instanceUnscoped::restart(), timer::instanceUnscoped::stop(), and unknownList.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 304 of file dirCount.cpp.

304  {
305  options::parser parser("dirCount", "", {});
307  "paths to consider");
308  options::single<bool> timing('t', "timing", "show timing info");
310  parser.fParse(argc, argv);
311 
312  counterType counter;
313  if (nThreads == 1) {
314  for (auto path : paths) {
315  handleDir(path, counter);
316  }
317  } else {
318  if (nThreads == 0) {
319  nThreads = sysconf(_SC_NPROCESSORS_ONLN);
320  }
321  dirQueueType dirQueue;
322  for (const auto& path : paths) {
323  dirQueue.emplace(path);
324  }
325  for (unsigned int i = 0; i < paths.size(); i++) {
326  runnerType::newWorker(dirQueue);
327  }
328  runnerType::finish(counter, timing);
329  }
330  counter.print();
331  if (timing) {
332  timer::anchor::print(std::cerr, "");
333  }
334  if (reportUnknowns) {
335  std::cout << "List of items of type DT_UNKNOWN:\n";
336  for (const auto& item: unknownList) {
337  std::cout << item << "\n";
338  }
339  }
340  return 0;
341 }

References MyWaitQueues::simple< T >::emplace(), runnerType::finish(), handleDir(), counterType::init(), runnerType::newWorker(), nThreads, timer::anchor::print(), counterType::print(), reportUnknowns, and unknownList.

Here is the call graph for this function:

Variable Documentation

◆ cv

std::condition_variable runnerType::cv
staticprivate

Definition at line 152 of file dirCount.cpp.

Referenced by runnerType::finish(), and runnerType::process().

◆ ignoreAccess

options::single<bool> ignoreAccess( 'i', "ignoreAccess", "ignore subdirs we may not access")
static

Referenced by handleDir(), and runnerType::process().

◆ listMutex

std::mutex runnerType::listMutex
staticprivate

Definition at line 151 of file dirCount.cpp.

Referenced by runnerType::finish(), runnerType::newWorker(), and runnerType::process().

◆ nActiveWorkers

std::atomic< unsigned int > runnerType::nActiveWorkers
staticprivate

Definition at line 153 of file dirCount.cpp.

Referenced by runnerType::finish(), runnerType::newWorker(), and runnerType::process().

◆ nThreads

options::single<unsigned> nThreads( 'n', "nThreads", "number of threads", 0)
static

◆ nWorkers

unsigned int runnerType::nWorkers
static

Definition at line 159 of file dirCount.cpp.

Referenced by runnerType::newWorker().

◆ quiet

options::single<bool> quiet( 'q', "quiet", "be quiet, no prefix to output lines")
static

◆ reportUnknowns

options::single<bool> reportUnknowns( 'U',"reportUnknowns", "print a list of items with type DT_UNKNOWN")
static

◆ unknownList

std::forward_list<std::string> unknownList
static

Definition at line 145 of file dirCount.cpp.

Referenced by handleDir(), main(), and runnerType::process().

◆ unknownListMutex

std::mutex unknownListMutex
static

Definition at line 146 of file dirCount.cpp.

Referenced by runnerType::process().

options::parser
class that contains the parser, i.e. does that option handling
Definition: Options.h:363
unknownList
static std::forward_list< std::string > unknownList
Definition: dirCount.cpp:145
options::single< bool >
class specialisation for options of type bool
Definition: Options.h:595
throwcall::badval
T badval(T call, t badvalue, const Args &... args)
template function to wrap system calls that return a special bad value on failure
Definition: throwcall.h:54
counterType::print
void print()
Definition: dirCount.cpp:124
handleDir
void handleDir(const std::string &path, counterType &counter)
Definition: dirCount.cpp:259
counterType::count
void count(unsigned char what)
Definition: dirCount.cpp:116
runnerType::newWorker
static void newWorker(dirQueueType &dirQueue)
Definition: dirCount.cpp:165
MyWaitQueues::simple
Definition: dirCount.cpp:28
timer::anchor::print
static void print(std::ostream &out, const std::string &prefix)
Definition: timer.cpp:5
nThreads
static options::single< unsigned > nThreads('n', "nThreads", "number of threads", 0)
timer::instanceUnscoped
Definition: timer.h:95
timer::anchor
Definition: timer.h:22
MyWaitQueues::simple::emplace
void emplace(Types ... args)
Definition: dirCount.cpp:37
counterType::init
static void init()
Definition: dirCount.cpp:113
ignoreAccess
static options::single< bool > ignoreAccess('i', "ignoreAccess", "ignore subdirs we may not access")
scoped::Dir
Definition: scoped.h:71
options::positional
Definition: Options.h:876
runnerType::finish
static void finish(counterType &sumCounter, bool verbose)
Definition: dirCount.cpp:173
reportUnknowns
static options::single< bool > reportUnknowns('U',"reportUnknowns", "print a list of items with type DT_UNKNOWN")
counterType
Definition: dirCount.cpp:70