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.

62
dircount.cc Normal file → Executable file
View File

@ -8,10 +8,10 @@
using namespace std;
class Hashtable {
unordered_map<const ino_t*, bool> htmap;
unordered_map<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(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;
@ -27,41 +27,45 @@ void listdir (const char *name) {
if (!(entry = readdir(dir))) return;
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;
printf("[l] %s/%s\n", name, entry->d_name);
//listdir(path);
}
else if (entry->d_type == DT_DIR) {
if (ht.get(&entry->d_ino)) continue;
//ht.put(&entry->d_ino, true);
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);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
printf("[d] %s/%s\n", name, entry->d_name);
listdir(path);
}
else{
if (ht.get(&entry->d_ino)) continue;
ht.put(&entry->d_ino, true);
file_cnt++;
printf("[f] %d %s/%s\n", (int)entry->d_ino, name, entry->d_name);
else {
if (ht.get(entry->d_ino)) continue;
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++;
printf("[f] %d %s/%s\n", (int)entry->d_ino, 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;
}
ht.put(entry->d_ino, true);
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;
}
} while (entry = readdir(dir));
} while ((entry = readdir(dir)));
closedir(dir);
}

119
dircount_v2.cc Normal file → Executable file
View File

@ -1,15 +1,15 @@
#include <dirent.h>
#include <string>
#include <dirent.h>
#include <string>
#include <sys/stat.h>
#include <unordered_map>
#include <iostream>
#include <iostream>
using namespace std;
class Hashtable {
unordered_map<const ino_t*, bool> ht;
unordered_map<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]; }
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;
@ -17,69 +17,66 @@ unsigned long space_used;
struct stat buf;
Hashtable ht;
bool listFileAndType( const string &dir ) {
DIR *dirp = opendir( dir.c_str() );
bool listFileAndType(const string &dir) {
DIR *dirp = opendir(dir.c_str());
if ( dirp ) {
struct dirent *dp = 0;
if (dirp) {
struct dirent *dp = 0;
while ( (dp = readdir( dirp )) != 0 ) {
string file( dp->d_name );
while ((dp = readdir(dirp)) != 0 ) {
string file( dp->d_name );
if ( file == "." || file == ".." ) // skip these
continue;
if (file == "." || file == "..") continue;
if ( dp->d_type == DT_DIR ) {
string dirPath = dir + "/" + file;
if (dp->d_type == DT_DIR) {
string dirPath = dir + "/" + file;
cout << "[d] " << dirPath << endl;
dircnt++;
// recurse into directory
listFileAndType( dirPath );
}
else {
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 << "[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 << statpath << endl;
}
}
cout << dir << "/" << file << ": ";
cout << endl;
}
}
closedir( dirp );
closedir( dirp );
return true;
}
else {
return false;
}
}
return true;
}
else {
return false;
}
}
int main( int argc, char **argv ) {
const string dir = (argc > 1 ? argv[1] : "foo");
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;
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;
return 0;
}
return 0;
}