really done now

This commit is contained in:
Ben Harris 2016-10-25 02:02:08 -04:00
parent 7497603c72
commit 1899658f3e
8 changed files with 111 additions and 90 deletions

20
Makefile Executable file
View File

@ -0,0 +1,20 @@
CSW = -O3 -Wall -std=c++11
LSW = -std=c++11
all:
make dc dc2
dc: dc.o Makefile
g++ dc.o -o dc $(LSW)
dc.o: dircount.cc Makefile
g++ dircount.cc -c -o dc.o $(CSW)
dc2: dc2.o Makefile
g++ dc2.o -o dc2 $(LSW)
dc2.o: dircount_v2.cc Makefile
g++ dircount_v2.cc -c -o dc2.o $(CSW)
clean:
touch Makefile; make

BIN
dc Executable file

Binary file not shown.

BIN
dc.o Normal file

Binary file not shown.

BIN
dc2

Binary file not shown.

BIN
dc2.o Normal file

Binary file not shown.

BIN
dircount

Binary file not shown.

42
dircount.cc Normal file → Executable file
View File

@ -8,10 +8,10 @@
using namespace std; using namespace std;
class Hashtable { class Hashtable {
unordered_map<const ino_t*, bool> htmap; unordered_map<ino_t, bool> htmap;
public: public:
void put(const ino_t* key, bool value) { htmap[key] = value; } void put(ino_t key, bool value) { htmap[key] = value; }
const bool get(const ino_t* key) { return htmap[key]; } bool get(ino_t key) { return htmap[key]; }
}; };
int file_cnt = 0, link_cnt = 0, dir_cnt = 0; int file_cnt = 0, link_cnt = 0, dir_cnt = 0;
@ -27,31 +27,31 @@ void listdir (const char *name) {
if (!(entry = readdir(dir))) return; if (!(entry = readdir(dir))) return;
do { do {
if (entry->d_type == DT_LNK) {
if (ht.get(&entry->d_ino)) continue;
ht.put(&entry->d_ino, true);
link_cnt++;
char path[4096];
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
printf("[l] %s/%s\n", name, entry->d_name); if (entry->d_type == DT_DIR) {
//listdir(path); // if (ht.get(&entry->d_ino)) continue;
}
else if (entry->d_type == DT_DIR) {
if (ht.get(&entry->d_ino)) continue;
// ht.put(&entry->d_ino, true); // ht.put(&entry->d_ino, true);
dir_cnt++; dir_cnt++;
char path[4096]; char path[4096];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0; path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
printf("[d] %s/%s\n", name, entry->d_name); printf("[d] %s/%s\n", name, entry->d_name);
listdir(path); listdir(path);
} }
else { else {
if (ht.get(&entry->d_ino)) continue; if (ht.get(entry->d_ino)) continue;
ht.put(&entry->d_ino, true);
if (entry->d_type == DT_LNK) {
link_cnt++;
printf("[l] %s/%s\n", name, entry->d_name);
char path[4096];
int len = snprintf(path, sizeof(path) - 1, "%s/%s", name, entry->d_name);
path[len] = 0;
stat(path, &buf);
space_used += buf.st_blocks * 512;
}
else{
file_cnt++; file_cnt++;
printf("[f] %d %s/%s\n", (int)entry->d_ino, name, entry->d_name); printf("[f] %d %s/%s\n", (int)entry->d_ino, name, entry->d_name);
@ -61,7 +61,11 @@ void listdir (const char *name) {
stat(path, &buf); stat(path, &buf);
space_used += buf.st_blocks * 512; space_used += buf.st_blocks * 512;
} }
} while (entry = readdir(dir));
ht.put(entry->d_ino, true);
}
} while ((entry = readdir(dir)));
closedir(dir); closedir(dir);
} }

23
dircount_v2.cc Normal file → Executable file
View File

@ -6,10 +6,10 @@
using namespace std; using namespace std;
class Hashtable { class Hashtable {
unordered_map<const ino_t*, bool> ht; unordered_map<ino_t, bool> ht;
public: public:
void put(const ino_t* key, bool value) { ht[key] = value; } void put(ino_t key, bool value) { ht[key] = value; }
const bool get(const ino_t* key) { return ht[key]; } bool get(ino_t key) { return ht[key]; }
}; };
int dircnt = 0, filecnt = 0, lnkcnt = 0; int dircnt = 0, filecnt = 0, lnkcnt = 0;
@ -26,20 +26,17 @@ bool listFileAndType( const string &dir ) {
while ((dp = readdir(dirp)) != 0 ) { while ((dp = readdir(dirp)) != 0 ) {
string file( dp->d_name ); string file( dp->d_name );
if ( file == "." || file == ".." ) // skip these if (file == "." || file == "..") continue;
continue;
if (dp->d_type == DT_DIR) { if (dp->d_type == DT_DIR) {
string dirPath = dir + "/" + file; string dirPath = dir + "/" + file;
cout << "[d] " << dirPath << endl;
cout << "[d] " << file << endl;
dircnt++; dircnt++;
// recurse into directory // recurse into directory
listFileAndType( dirPath ); listFileAndType( dirPath );
} }
else { else {
//filecnt++; if (ht.get(dp->d_ino)) continue;
if (ht.get(&dp->d_ino)) continue;
switch(dp->d_type) { switch(dp->d_type) {
case DT_REG: case DT_REG:
filecnt++; filecnt++;
@ -54,12 +51,11 @@ bool listFileAndType( const string &dir ) {
break; break;
} }
string statpath = dir + "/" + file; string statpath = dir + "/" + file;
ht.put(&dp->d_ino, true); ht.put(dp->d_ino, true);
stat(statpath.c_str(), &buf); stat(statpath.c_str(), &buf);
space_used += buf.st_blocks * 512; space_used += buf.st_blocks * 512;
cout << dir << "/" << file << ": "; cout << statpath << endl;
cout << endl;
} }
} }
@ -79,7 +75,8 @@ int main( int argc, char **argv ) {
if (!listFileAndType(dir)) { if (!listFileAndType(dir)) {
cout << "Error: Cannot open directory '" << dir << "'" << endl; cout << "Error: Cannot open directory '" << dir << "'" << endl;
} }
cout << endl << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl; cout << endl << "totals" << endl;
cout << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << endl;
cout << "space used: " << space_used << endl; cout << "space used: " << space_used << endl;
return 0; return 0;
} }