dircount/dircount_v2.cc

78 lines
1.8 KiB
C++
Raw Normal View History

2016-10-25 20:08:21 +00:00
#include <dirent.h>
#include <string>
#include <sys/stat.h>
#include <unordered_map>
#include <iostream>
using namespace std;
int dircnt = 0, filecnt = 0, lnkcnt = 0;
unsigned long space_used;
struct stat buf;
unordered_map<ino_t, bool> ht;
bool listFileAndType(const string &dir) {
DIR *dirp = opendir(dir.c_str());
if (dirp) {
struct dirent *dp = 0;
while ((dp = readdir(dirp)) != 0 ) {
string file( dp->d_name );
if (file == "." || file == "..") continue;
if (dp->d_type == DT_DIR) {
string dirPath = dir + "/" + file;
cout << "[d] " << dirPath << endl;
dircnt++;
// recurse into directory
listFileAndType( dirPath );
}
else {
if (ht[dp->d_ino]) continue;
2016-10-25 20:51:57 +00:00
string statpath;
2016-10-25 20:08:21 +00:00
switch(dp->d_type) {
case DT_REG:
filecnt++;
cout << "[f] ";
2016-10-25 20:51:57 +00:00
statpath = dir + "/" + file;
stat(statpath.c_str(), &buf);
space_used += buf.st_blocks;
2016-10-25 20:08:21 +00:00
break;
case DT_LNK:
lnkcnt++;
cout << "[l] ";
break;
default:
cout << "[none] ";
break;
}
ht[dp->d_ino] = true;
cout << statpath << endl;
}
}
closedir( dirp );
return true;
}
else {
return false;
}
}
int main( int argc, char **argv ) {
2016-10-25 20:51:57 +00:00
const string dir = (argc > 1 ? argv[1] : ".");
2016-10-25 20:08:21 +00:00
if (!listFileAndType(dir)) {
cout << "Error: Cannot open directory '" << dir << "'" << endl;
}
cout << endl << "totals" << endl;
cout << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl;
2016-10-25 20:51:57 +00:00
cout << "space used: " << space_used << " blocks" << endl;
cout << "\t" << space_used*512 << " bytes" << endl;
2016-10-25 20:08:21 +00:00
return 0;
}