filesize fix

This commit is contained in:
Ben Harris 2016-10-25 16:51:57 -04:00
parent fd33a1383a
commit c4e0caa964
2 changed files with 14 additions and 28 deletions

View File

@ -7,17 +7,9 @@
#include <unordered_map> #include <unordered_map>
using namespace std; using namespace std;
// class Hashtable {
// unordered_map<ino_t, bool> 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; int file_cnt = 0, link_cnt = 0, dir_cnt = 0;
unsigned long space_used = 0; unsigned long space_used = 0;
struct stat buf; struct stat buf;
// Hashtable ht;
unordered_map<ino_t, bool> ht; unordered_map<ino_t, bool> ht;
void listdir (const char *name) { void listdir (const char *name) {
@ -30,8 +22,6 @@ void listdir (const char *name) {
do { do {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
// if (ht.get(&entry->d_ino)) continue;
// 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);
@ -45,11 +35,11 @@ void listdir (const char *name) {
link_cnt++; link_cnt++;
printf("[l] %s/%s\n", name, entry->d_name); printf("[l] %s/%s\n", name, entry->d_name);
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;
stat(path, &buf); // stat(path, &buf);
space_used += buf.st_blocks; // space_used += buf.st_blocks;
} }
else{ else{
file_cnt++; file_cnt++;
@ -70,7 +60,8 @@ void listdir (const char *name) {
} }
int main (int argc, char** argv) { 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); 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; return 0;
} }

View File

@ -5,13 +5,6 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
// class Hashtable {
// unordered_map<ino_t, bool> 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; int dircnt = 0, filecnt = 0, lnkcnt = 0;
unsigned long space_used; unsigned long space_used;
struct stat buf; struct stat buf;
@ -37,10 +30,14 @@ bool listFileAndType(const string &dir) {
} }
else { else {
if (ht[dp->d_ino]) continue; if (ht[dp->d_ino]) continue;
string statpath;
switch(dp->d_type) { switch(dp->d_type) {
case DT_REG: case DT_REG:
filecnt++; filecnt++;
cout << "[f] "; cout << "[f] ";
statpath = dir + "/" + file;
stat(statpath.c_str(), &buf);
space_used += buf.st_blocks;
break; break;
case DT_LNK: case DT_LNK:
lnkcnt++; lnkcnt++;
@ -50,10 +47,7 @@ bool listFileAndType(const string &dir) {
cout << "[none] "; cout << "[none] ";
break; break;
} }
string statpath = dir + "/" + file;
ht[dp->d_ino] = true; ht[dp->d_ino] = true;
stat(statpath.c_str(), &buf);
space_used += buf.st_blocks * 512;
cout << statpath << endl; cout << statpath << endl;
} }
@ -70,13 +64,14 @@ bool listFileAndType(const string &dir) {
int main( int argc, char **argv ) { int main( int argc, char **argv ) {
const string dir = (argc > 1 ? argv[1] : "foo"); const string dir = (argc > 1 ? argv[1] : ".");
if (!listFileAndType(dir)) { if (!listFileAndType(dir)) {
cout << "Error: Cannot open directory '" << dir << "'" << endl; cout << "Error: Cannot open directory '" << dir << "'" << endl;
} }
cout << endl << "totals" << endl; cout << endl << "totals" << endl;
cout << "file count: " << filecnt << "\tdir cnt: " << dircnt << "\tlink cnt: " << lnkcnt << 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; return 0;
} }