Skip to content

Commit

Permalink
Merge pull request #119 from utsaslab/memLeakFix
Browse files Browse the repository at this point in the history
Fixing memLeaks and cleaning up autochecker
  • Loading branch information
SoujanyaPonnapalli authored Oct 5, 2018
2 parents f65b3e6 + b444592 commit c394765
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 150 deletions.
208 changes: 95 additions & 113 deletions code/harness/DiskContents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,43 @@ using std::ofstream;
namespace fs_testing {

fileAttributes::fileAttributes() {
dir_attr = NULL;
stat_attr = NULL;
md5sum = "";
// Initialize dir_attr entries
dir_attr.d_ino = -1;
dir_attr.d_off = -1;
dir_attr.d_reclen = -1;
dir_attr.d_type = -1;
dir_attr.d_name[0] = '\0';
// Initialize stat_attr entried
stat_attr.st_ino == -1;
stat_attr.st_mode = -1;
stat_attr.st_nlink = -1;
stat_attr.st_uid = -1;
stat_attr.st_gid = -1;
stat_attr.st_size = -1;
stat_attr.st_blksize = -1;
stat_attr.st_blocks = -1;
}

fileAttributes::~fileAttributes() {
}

void fileAttributes::set_dir_attr(struct dirent* a) {
dir_attr = (struct dirent *) malloc(sizeof(struct dirent));
dir_attr->d_ino = a->d_ino;
dir_attr->d_off = a->d_off;
dir_attr->d_reclen = a->d_reclen;
dir_attr->d_type = a->d_type;
strncpy(dir_attr->d_name, a->d_name, sizeof(a->d_name));
dir_attr->d_name[sizeof(a->d_name) - 1] = '\0';
dir_attr.d_ino = a->d_ino;
dir_attr.d_off = a->d_off;
dir_attr.d_reclen = a->d_reclen;
dir_attr.d_type = a->d_type;
strncpy(dir_attr.d_name, a->d_name, sizeof(a->d_name));
dir_attr.d_name[sizeof(a->d_name) - 1] = '\0';
}

void fileAttributes::set_stat_attr(struct stat* a) {
stat_attr = (struct stat *) malloc(sizeof(struct stat));

stat_attr->st_dev = a->st_dev;
stat_attr->st_ino = a->st_ino;
stat_attr->st_mode = a->st_mode;
stat_attr->st_nlink = a->st_nlink;
stat_attr->st_uid = a->st_uid;
stat_attr->st_gid = a->st_gid;
stat_attr->st_rdev = a->st_rdev;
stat_attr->st_size = a->st_size;
stat_attr->st_atime = a->st_atime;
stat_attr->st_mtime = a->st_mtime;
stat_attr->st_ctime = a->st_ctime;
stat_attr->st_blksize = a->st_blksize;
stat_attr->st_blocks = a->st_blocks;
void fileAttributes::set_stat_attr(string path, bool islstat) {
if (islstat) {
lstat(path.c_str(), &stat_attr);
} else {
stat(path.c_str(), &stat_attr);
}
return;
}

void fileAttributes::set_md5sum(string file_path) {
Expand All @@ -51,104 +54,84 @@ void fileAttributes::set_md5sum(string file_path) {
fp = popen(command.c_str(), "r");
fscanf(fp, "%s", md5);
fclose(fp);
string md5_str(md5);
md5sum = md5_str;
md5sum = string(md5);
}

bool fileAttributes::compare_dir_attr(struct dirent* a) {
if (a == NULL && dir_attr == NULL) {
return true;
} else if (a == NULL || dir_attr == NULL) {
return false;
}
return ((dir_attr->d_ino == a->d_ino) &&
(dir_attr->d_off == a->d_off) &&
(dir_attr->d_reclen == a->d_reclen) &&
(dir_attr->d_type == a->d_type) &&
(strcmp(dir_attr->d_name, a->d_name) == 0));
}
bool fileAttributes::compare_dir_attr(struct dirent a) {

bool fileAttributes::compare_stat_attr(struct stat *a) {
if (a == NULL && stat_attr == NULL) {
return true;
} else if (a == NULL || stat_attr == NULL) {
return false;
}
return ((dir_attr.d_ino == a.d_ino) &&
(dir_attr.d_off == a.d_off) &&
(dir_attr.d_reclen == a.d_reclen) &&
(dir_attr.d_type == a.d_type) &&
(strcmp(dir_attr.d_name, a.d_name) == 0));
}

return ((stat_attr->st_ino == a->st_ino) &&
(stat_attr->st_mode == a->st_mode) &&
(stat_attr->st_nlink == a->st_nlink) &&
(stat_attr->st_uid == a->st_uid) &&
(stat_attr->st_gid == a->st_gid) &&
// (stat_attr->st_rdev == a->st_rdev) &&
// (stat_attr->st_dev == a->st_dev) &&
(stat_attr->st_size == a->st_size) &&
(stat_attr->st_blksize == a->st_blksize) &&
(stat_attr->st_blocks == a->st_blocks));
bool fileAttributes::compare_stat_attr(struct stat a) {

return ((stat_attr.st_ino == a.st_ino) &&
(stat_attr.st_mode == a.st_mode) &&
(stat_attr.st_nlink == a.st_nlink) &&
(stat_attr.st_uid == a.st_uid) &&
(stat_attr.st_gid == a.st_gid) &&
// (stat_attr.st_rdev == a.st_rdev) &&
// (stat_attr.st_dev == a.st_dev) &&
(stat_attr.st_size == a.st_size) &&
(stat_attr.st_blksize == a.st_blksize) &&
(stat_attr.st_blocks == a.st_blocks));
}

bool fileAttributes::compare_md5sum(string a) {
return md5sum.compare(a);
}

bool fileAttributes::is_regular_file() {
return S_ISREG(stat_attr->st_mode);
return S_ISREG(stat_attr.st_mode);
}

ofstream& operator<< (ofstream& os, fileAttributes& a) {
// print dir_attr
if (a.dir_attr != NULL) {
os << "---Directory Atrributes---" << endl;
os << "Name : " << (a.dir_attr)->d_name << endl;
os << "Inode : " << (a.dir_attr)->d_ino << endl;
os << "Offset : " << (a.dir_attr)->d_off << endl;
os << "Length : " << (a.dir_attr)->d_reclen << endl;
os << "Type : " << (a.dir_attr)->d_type << endl;
}
os << "---Directory Atrributes---" << endl;
os << "Name : " << (a.dir_attr).d_name << endl;
os << "Inode : " << (a.dir_attr).d_ino << endl;
os << "Offset : " << (a.dir_attr).d_off << endl;
os << "Length : " << (a.dir_attr).d_reclen << endl;
os << "Type : " << (a.dir_attr).d_type << endl;
// print stat_attr
if (a.stat_attr != NULL) {
os << "---File Stat Atrributes---" << endl;
os << "Inode : " << (a.stat_attr)->st_ino << endl;
os << "TotalSize : " << (a.stat_attr)->st_size << endl;
os << "BlockSize : " << (a.stat_attr)->st_blksize << endl;
os << "#Blocks : " << (a.stat_attr)->st_blocks << endl;
os << "#HardLinks: " << (a.stat_attr)->st_nlink << endl;
os << "Mode : " << (a.stat_attr)->st_mode << endl;
os << "User ID : " << (a.stat_attr)->st_uid << endl;
os << "Group ID : " << (a.stat_attr)->st_gid << endl;
os << "Device ID : " << (a.stat_attr)->st_rdev << endl;
os << "RootDev ID: " << (a.stat_attr)->st_dev << endl;
}
os << "---File Stat Atrributes---" << endl;
os << "Inode : " << (a.stat_attr).st_ino << endl;
os << "TotalSize : " << (a.stat_attr).st_size << endl;
os << "BlockSize : " << (a.stat_attr).st_blksize << endl;
os << "#Blocks : " << (a.stat_attr).st_blocks << endl;
os << "#HardLinks: " << (a.stat_attr).st_nlink << endl;
os << "Mode : " << (a.stat_attr).st_mode << endl;
os << "User ID : " << (a.stat_attr).st_uid << endl;
os << "Group ID : " << (a.stat_attr).st_gid << endl;
os << "Device ID : " << (a.stat_attr).st_rdev << endl;
os << "RootDev ID: " << (a.stat_attr).st_dev << endl;
}

DiskContents::DiskContents(char* path, const char* type) {
disk_path = (char *) malloc(sizeof(char)*30);
mount_point = (char *) malloc(sizeof(char)*40);
fs_type = (char *) malloc(sizeof(char)*10);
strcpy(disk_path, path);
strcpy(fs_type, type);
DiskContents::DiskContents(string path, string type) {
disk_path = path;
fs_type = type;
device_mounted = false;
}

DiskContents::~DiskContents() {
// free(disk_path);
// free(mount_point);
// free(fs_type);
}

int DiskContents::mount_disk() {
// Construct and set mount_point
strcpy(mount_point, "/mnt/");
strcat(mount_point, (disk_path + 5));
mount_point = "/mnt/";
mount_point += disk_path.substr(5);
// Create the mount directory with read/write/search permissions for owner and group,
// and with read/search permissions for others.
int ret = mkdir(mount_point, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
int ret = mkdir(mount_point.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (ret == -1 && errno != EEXIST) {
cout << "creating mountpoint failed" << endl;
return -1;
}
// Mount the disk
if (mount(disk_path, mount_point, fs_type, MS_RDONLY, NULL) < 0) {
if (mount(disk_path.c_str(), mount_point.c_str(), fs_type.c_str(), MS_RDONLY, NULL) < 0) {
return -1;
}
// sleep after mount
Expand All @@ -167,25 +150,24 @@ int DiskContents::unmount_and_delete_mount_point() {
int err;
string command = "umount ";
command += mount_point;
system(command.c_str());
do {
umount_res = umount(mount_point);
umount_res = system(command.c_str());
if (umount_res < 0) {
err = errno;
usleep(500);
}
} while (umount_res < 0 && err == EBUSY);

// Delete the mount directory
if (unlink(mount_point) != 0) {
if (unlink(mount_point.c_str()) != 0) {
return -1;
}
device_mounted = false;
return 0;
}

void DiskContents::set_mount_point(string path) {
strcpy(mount_point, path.c_str());
mount_point = path;
}

void DiskContents::get_contents(const char* path) {
Expand All @@ -205,7 +187,7 @@ void DiskContents::get_contents(const char* path) {
string filename(dir_entry->d_name);
string current_path = parent_path + "/" + filename;
string relative_path = current_path;
relative_path.erase(0, strlen(mount_point));
relative_path.erase(0, mount_point.length());
struct stat statbuf;
fileAttributes fa;
if (stat(current_path.c_str(), &statbuf) == -1) {
Expand All @@ -216,7 +198,7 @@ void DiskContents::get_contents(const char* path) {
continue;
}
fa.set_dir_attr(dir_entry);
fa.set_stat_attr(&statbuf);
fa.set_stat_attr(current_path, false);
contents[relative_path] = fa;
// If the entry is a directory and not . or .. make a recursive call
get_contents(current_path.c_str());
Expand All @@ -226,28 +208,28 @@ void DiskContents::get_contents(const char* path) {
if (lstat(current_path.c_str(), &lstatbuf) == -1) {
continue;
}
fa.set_stat_attr(&lstatbuf);
fa.set_stat_attr(current_path, true);
contents[relative_path] = fa;
} else if (dir_entry->d_type == DT_REG) {
fa.set_md5sum(current_path);
fa.set_stat_attr(&statbuf);
fa.set_stat_attr(current_path, false);
contents[relative_path] = fa;
} else {
fa.set_stat_attr(&statbuf);
fa.set_stat_attr(current_path, false);
contents[relative_path] = fa;
}
} while (dir_entry = readdir(directory));
closedir(directory);
}

const char* DiskContents::get_mount_point() {
string DiskContents::get_mount_point() {
return mount_point;
}

bool DiskContents::compare_disk_contents(DiskContents &compare_disk, ofstream &diff_file) {
bool retValue = true;

if (strcmp(disk_path, compare_disk.disk_path) == 0) {
if (disk_path.compare(compare_disk.disk_path) == 0) {
return retValue;
}

Expand All @@ -258,29 +240,29 @@ bool DiskContents::compare_disk_contents(DiskContents &compare_disk, ofstream &d
cout << "Mounting " << compare_disk.disk_path << " failed" << endl;
}

compare_disk.get_contents(compare_disk.get_mount_point());
compare_disk.get_contents(compare_disk.get_mount_point().c_str());

// Compare the size of contents
if (contents.size() != compare_disk.contents.size()) {
diff_file << "DIFF: Mismatch" << endl;
diff_file << "Unequal #entries in " << disk_path << ", " << compare_disk.disk_path;
diff_file << endl << endl;
diff_file << disk_path << " contains:" << endl;
for (auto i : contents) {
for (auto &i : contents) {
diff_file << i.first << endl;
}
diff_file << endl;

diff_file << compare_disk.disk_path << " contains:" << endl;
for (auto i : compare_disk.contents) {
for (auto &i : compare_disk.contents) {
diff_file << i.first << endl;
}
diff_file << endl;
retValue = false;
}

// entry-wise comparision
for (auto i : contents) {
for (auto &i : contents) {
fileAttributes i_fa = i.second;
if (compare_disk.contents.find((i.first)) == compare_disk.contents.end()) {
diff_file << "DIFF: Missing " << i.first << endl;
Expand Down Expand Up @@ -312,17 +294,16 @@ bool DiskContents::compare_disk_contents(DiskContents &compare_disk, ofstream &d
}
}
}
// TODO(P.S.) Fix the unmount issue and uncomment the function below.
compare_disk.unmount_and_delete_mount_point();
return retValue;
}

// TODO(P.S.) Cleanup the code and pull out redundant code into separate functions
bool DiskContents::compare_entries_at_path(DiskContents &compare_disk,
string path, ofstream &diff_file) {
string &path, ofstream &diff_file) {
bool retValue = true;

if (strcmp(disk_path, compare_disk.disk_path) == 0) {
if (disk_path.compare(compare_disk.disk_path) == 0) {
return retValue;
}

Expand Down Expand Up @@ -352,8 +333,8 @@ bool DiskContents::compare_entries_at_path(DiskContents &compare_disk,
return false;
}

base_fa.set_stat_attr(&base_statbuf);
compare_fa.set_stat_attr(&compare_statbuf);
base_fa.set_stat_attr(base_path, false);
compare_fa.set_stat_attr(compare_path, false);
if (!(base_fa.compare_stat_attr(compare_fa.stat_attr))) {
diff_file << "DIFF: Content Mismatch " << path << endl << endl;
diff_file << base_path << ":" << endl;
Expand All @@ -377,15 +358,16 @@ bool DiskContents::compare_entries_at_path(DiskContents &compare_disk,
}
}

// TODO(P.S.) Fix the unmount issue and uncomment the function below.
compare_disk.unmount_and_delete_mount_point();
return retValue;
}

// TODO[P.S]: Compare fixed sized segments of files,
// to support comparing very large files.
bool DiskContents::compare_file_contents(DiskContents &compare_disk, string path,
int offset, int length, ofstream &diff_file) {
bool retValue = true;
if (strcmp(disk_path, compare_disk.disk_path) == 0) {
if (disk_path.compare(compare_disk.disk_path) == 0) {
return retValue;
}

Expand Down Expand Up @@ -539,7 +521,7 @@ bool DiskContents::deleteFiles(string path, ofstream &diff_file) {
bool DiskContents::makeFiles(string base_path, ofstream &diff_file) {
get_contents(base_path.c_str());
for (auto &i : contents) {
if (S_ISDIR((i.second).stat_attr->st_mode)) {
if (S_ISDIR((i.second).stat_attr.st_mode)) {
string filepath = base_path + i.first + "/" + "_dummy";
int fd = open(filepath.c_str(), O_CREAT|O_RDWR);
if (fd < 0) {
Expand Down
Loading

0 comments on commit c394765

Please sign in to comment.