From c4e0caa964ce56f6ca440471149fbe2e46a5f39f Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 25 Oct 2016 16:51:57 -0400 Subject: [PATCH] filesize fix --- dircount.cc | 23 +++++++---------------- dircount_v2.cc | 19 +++++++------------ 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/dircount.cc b/dircount.cc index f8568b8..82e6adc 100755 --- a/dircount.cc +++ b/dircount.cc @@ -7,17 +7,9 @@ #include using namespace std; -// class Hashtable { -// unordered_map htmap; -// public: -// void put(ino_t key, bool value) { htmap[key] = value; } -// bool get(ino_t key) { return htmap[key]; } -// }; - int file_cnt = 0, link_cnt = 0, dir_cnt = 0; unsigned long space_used = 0; struct stat buf; -// Hashtable ht; unordered_map ht; void listdir (const char *name) { @@ -30,8 +22,6 @@ void listdir (const char *name) { do { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (entry->d_type == DT_DIR) { - // if (ht.get(&entry->d_ino)) continue; - // ht.put(&entry->d_ino, true); dir_cnt++; char path[4096]; int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); @@ -45,11 +35,11 @@ void listdir (const char *name) { 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; + // 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; } else{ file_cnt++; @@ -70,7 +60,8 @@ void listdir (const char *name) { } int main (int argc, char** argv) { - listdir(argv[1]); + const char* dirpath = argc > 1 ? argv[1] : "."; + listdir(dirpath); printf("\ntotals\nfile count: %d\tdir count: %d\tlink count: %d\nspace used: %lu blocks\n\t%lu bytes\n", file_cnt, dir_cnt, link_cnt, space_used, space_used*512); return 0; } diff --git a/dircount_v2.cc b/dircount_v2.cc index 62ba539..1516156 100755 --- a/dircount_v2.cc +++ b/dircount_v2.cc @@ -5,13 +5,6 @@ #include using namespace std; -// class Hashtable { -// unordered_map ht; -// public: -// void put(ino_t key, bool value) { ht[key] = value; } -// bool get(ino_t key) { return ht[key]; } -// }; - int dircnt = 0, filecnt = 0, lnkcnt = 0; unsigned long space_used; struct stat buf; @@ -37,10 +30,14 @@ bool listFileAndType(const string &dir) { } else { if (ht[dp->d_ino]) continue; + string statpath; switch(dp->d_type) { case DT_REG: filecnt++; cout << "[f] "; + statpath = dir + "/" + file; + stat(statpath.c_str(), &buf); + space_used += buf.st_blocks; break; case DT_LNK: lnkcnt++; @@ -50,10 +47,7 @@ bool listFileAndType(const string &dir) { cout << "[none] "; break; } - string statpath = dir + "/" + file; ht[dp->d_ino] = true; - stat(statpath.c_str(), &buf); - space_used += buf.st_blocks * 512; cout << statpath << endl; } @@ -70,13 +64,14 @@ bool listFileAndType(const string &dir) { int main( int argc, char **argv ) { - const string dir = (argc > 1 ? argv[1] : "foo"); + const string dir = (argc > 1 ? argv[1] : "."); 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; - cout << "space used: " << space_used << endl; + cout << "space used: " << space_used << " blocks" << endl; + cout << "\t" << space_used*512 << " bytes" << endl; return 0; }