This commit is contained in:
Ben Harris 2016-10-25 00:46:49 -04:00
parent 89e6dd1e4a
commit 7497603c72
4 changed files with 89 additions and 8 deletions

BIN
dc2 Executable file

Binary file not shown.

BIN
dircount Executable file

Binary file not shown.

View File

@ -10,12 +10,8 @@ using namespace std;
class Hashtable {
unordered_map<const ino_t*, bool> htmap;
public:
void put(const ino_t* key, bool value) {
htmap[key] = value;
}
const bool get(const ino_t* key) {
return htmap[key];
}
void put(const ino_t* key, bool value) { htmap[key] = value; }
const bool get(const ino_t* key) { return htmap[key]; }
};
int file_cnt = 0, link_cnt = 0, dir_cnt = 0;
@ -40,10 +36,10 @@ void listdir (const char *name) {
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
printf("[l] %s/%s\n", name, entry->d_name);
listdir(path);
//listdir(path);
}
else if (entry->d_type == DT_DIR) {
//if (ht.get(&entry->d_ino)) continue;
if (ht.get(&entry->d_ino)) continue;
//ht.put(&entry->d_ino, true);
dir_cnt++;
char path[4096];

85
dircount_v2.cc Normal file
View File

@ -0,0 +1,85 @@
#include <dirent.h>
#include <string>
#include <sys/stat.h>
#include <unordered_map>
#include <iostream>
using namespace std;
class Hashtable {
unordered_map<const ino_t*, bool> ht;
public:
void put(const ino_t* key, bool value) { ht[key] = value; }
const bool get(const ino_t* key) { return ht[key]; }
};
int dircnt = 0, filecnt = 0, lnkcnt = 0;
unsigned long space_used;
struct stat buf;
Hashtable 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 == ".." ) // skip these
continue;
if ( dp->d_type == DT_DIR ) {
string dirPath = dir + "/" + file;
cout << "[d] " << file << endl;
dircnt++;
// recurse into directory
listFileAndType( dirPath );
}
else {
//filecnt++;
if (ht.get(&dp->d_ino)) continue;
switch( dp->d_type ) {
case DT_REG:
filecnt++;
cout << "[f] ";
break;
case DT_LNK:
lnkcnt++;
cout << "[l] ";
break;
default:
cout << "[none] ";
break;
}
string statpath = dir + "/" + file;
ht.put(&dp->d_ino, true);
stat(statpath.c_str(), &buf);
space_used += buf.st_blocks * 512;
cout << dir << "/" << file << ": ";
cout << endl;
}
}
closedir( dirp );
return true;
}
else {
return false;
}
}
int main( int argc, char **argv ) {
const string dir = (argc > 1 ? argv[1] : "foo");
if (!listFileAndType(dir)) {
cout << "Error: Cannot open directory '" << dir << "'" << endl;
}
cout << endl << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl;
cout << "space used: " << space_used << endl;
return 0;
}