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>
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;
unsigned long space_used = 0;
struct stat buf;
// Hashtable ht;
unordered_map<ino_t, bool> 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;
}

View File

@ -5,13 +5,6 @@
#include <iostream>
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;
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;
}