Skip to content

Commit

Permalink
Fixed issues while creating the filelist.txt
Browse files Browse the repository at this point in the history
-some code cleanup in the file replacer
  • Loading branch information
Maschell committed Apr 30, 2016
1 parent 0675732 commit 88f5051
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 59 deletions.
9 changes: 3 additions & 6 deletions src/utils/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

extern "C" {
// Get declaration for f(int i, char c, float x)
#include "patcher/fs_logger.h"
#include "utils/logger.h"
#include "common/common.h"
}

Directory::Directory(int socket_,std::string name_){
Directory::Directory(std::string name_){
name = name_;
socket = socket_;
}

Directory::~Directory(){
Expand All @@ -34,7 +33,6 @@ int Directory::getSize()
char log_size[100];
size += dir_size +file_size + folder_size;
sprintf(log_size,"Size of %s: %d Bytes | Dirsize: %d | Folder %d(%d Bytes) Files: %d (%d Bytes)",getFolderName().c_str(),size,dir_size,folder.size(),folder_size,files.size(),file_size);
//fs_log_string(socket, log_size, BYTE_LOG_STR);
return size;
}

Expand Down Expand Up @@ -109,8 +107,7 @@ void Directory::printFolderRecursive(std::string base){
}

for(unsigned int i = 0; i < files.size(); i++){
std::string logstring = base + std::string("/") + files[i];
fs_log_string(socket, logstring.c_str(), BYTE_LOG_STR); ;
log_printf("%s/%s",base.c_str(),files[i].c_str()); ;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/utils/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Directory
{
public:
Directory(int socket_,std::string name);
Directory(std::string name);
~Directory();
void addFile(std::string name_);
void addFolder(Directory * dir);
Expand All @@ -21,6 +21,5 @@ class Directory
std::string name;
std::vector<std::string> files;
std::vector<Directory*> folder;
int socket;
Directory * parent = NULL;
};
80 changes: 30 additions & 50 deletions src/utils/FileReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@

FileReplacer::FileReplacer(std::string path,std::string content,std::string filename,void * pClient,void * pCmd){
bool result = false;
dir_all = new Directory(socket,"content");
socket = 0;
fs_logger_connect(&socket);
fs_log_string(socket, "Read from file for replacement", BYTE_LOG_STR);
dir_all = new Directory("content");
std::string filepath = path + std::string("/") + filename;
fs_log_string(socket, filepath.c_str(), BYTE_LOG_STR);
log_printf("Read from file for replacement: %s\n",filename.c_str());
result = this->readFromFile(pClient,pCmd,filepath,dir_all);

if(!result){
fs_log_string(socket, "Error!", BYTE_LOG_STR);
log_print("Error!\n");
}else{
//dir_all->printFolderRecursive("");
}
Expand All @@ -30,17 +26,15 @@ FileReplacer::FileReplacer(std::string filepath,std::string content,std::string
int entries = 0;
progressWindow.setTitle("Creating filelist.txt:");

dir_all = new Directory(socket,"content");
dir_all = new Directory("content");
read_dir(filepath + std::string("/") + content, dir_all, &entries, progressWindow);
log_printf("Found %d entries\n",entries);
//log_printf("Total size: %d",log_size);
}

std::string FileReplacer::getFileListAsString(){
return dir_all->getFileList();
}


int FileReplacer::getSize()
{
int size = sizeof(FileReplacer);
Expand All @@ -60,15 +54,15 @@ bool FileReplacer::readFromFile(void *pClient, void *pCmd, const std::string & p
if(FSGetStat(pClient, pCmd, path.c_str(),&stats,FS_RET_ALL_ERROR) == FS_STATUS_OK){
char * file = (char *) malloc((sizeof(char)*stats.size)+1);
if(!file){
fs_log_string(socket, "Failed to allocate space for reading the file", BYTE_LOG_STR);
log_print("Failed to allocate space for reading the file\n");
return false;
}
file[stats.size] = '\0';
if ((ret = FSOpenFile(pClient, pCmd, path.c_str(),"r", &handle, FS_RET_ALL_ERROR)) == FS_STATUS_OK){
int total_read = 0;
int ret2 = 0;
while ((ret2 = FSReadFile(pClient, pCmd, file+total_read, 1, stats.size-total_read, handle, 0, FS_RET_ALL_ERROR)) > 0){
fs_log_string(socket, "Reading filelist.txt", BYTE_LOG_STR);
log_print("Reading filelist.txt\n");
total_read += ret2;
}
}
Expand All @@ -78,38 +72,25 @@ bool FileReplacer::readFromFile(void *pClient, void *pCmd, const std::string & p
ptr = strtok (file,delimiter);
while (ptr != NULL)
{
/*std::vector<std::string> lines = stringSplit(file, "\n");
fs_log_string(socket, "Spltting done", BYTE_LOG_STR);
if(!lines.empty()){
for(unsigned int i = 0; i < lines.size(); i++){*/

std::string dirname(ptr);
//fs_log_string(socket, dirname.c_str(), BYTE_LOG_STR);

if(dirname.compare(PARENT_DIR_IDENTIFY) == 0){ // back dir
//fs_log_string(socket, "PARENT DIR", BYTE_LOG_STR);
dir_cur = dir_cur->getParent();
if(dir_cur == NULL){
fs_log_string(socket, "Something went wrong. Try to delete the filelist.txt", BYTE_LOG_STR);
free(file);
return false;
}
}else if(dirname.substr(0,1).compare(DIR_IDENTIFY) == 0){ //DIR
//fs_log_string(socket, "DIR", BYTE_LOG_STR);
dirname = dirname.substr(1);
Directory * dir_new = new Directory(socket,dirname);
dir_cur->addFolder(dir_new);
dir_new->setParent(dir_cur);
dir_cur = dir_new;
}else {
//fs_log_string(socket, "ADD FILE", BYTE_LOG_STR);
dir_cur->addFile(dirname);
}
//}
//}
std::string dirname(ptr);
if(dirname.compare(PARENT_DIR_IDENTIFY) == 0){ // back dir
dir_cur = dir_cur->getParent();
if(dir_cur == NULL){
log_print("Something went wrong. Try to delete the filelist.txt\n");
free(file);
return false;
}
}else if(dirname.substr(0,1).compare(DIR_IDENTIFY) == 0){ //DIR
dirname = dirname.substr(1);
Directory * dir_new = new Directory(dirname);
dir_cur->addFolder(dir_new);
dir_new->setParent(dir_cur);
dir_cur = dir_new;
}else {
dir_cur->addFile(dirname);
}
ptr = strtok(NULL, delimiter);
}
fs_log_string(socket, "Done!", BYTE_LOG_STR);
}
free(file);
return true;
}else{
Expand All @@ -126,7 +107,6 @@ int FileReplacer::read_dir(const std::string & path , Directory* dir, int * entr
dir_ = opendir(path.c_str());
if (dir_ == NULL)
return -1;
//fs_log_string(socket, path.c_str(), BYTE_LOG_STR);
while ((dirent = readdir(dir_)) != 0)
{
(*entries)++;
Expand All @@ -135,29 +115,29 @@ int FileReplacer::read_dir(const std::string & path , Directory* dir, int * entr
if(*entries %25 == 0){
progressWindow.setTitle(strfmt("Creating filelist.txt: %d entries found", *entries));
}
//fs_log_string(socket, filename, BYTE_LOG_STR);
//log_printf("%s\n",dir_entry->name);
if(isDir){
//log_printf("DIR\n",path.c_str());

dirlist.push_back(new std::string(filename));
}else{
//log_printf("FILE\n",path.c_str());
dir->addFile(filename);
}
}
closedir(dir_);

for(unsigned int i = 0; i < dirlist.size(); i++){
Directory * dir_new = new Directory(socket,*dirlist[i]);
Directory * dir_new = new Directory(*dirlist[i]);
dir->addFolder(dir_new);
read_dir((path + "/"+ *dirlist[i]),dir_new,entries,progressWindow);
}

for(unsigned int i = 0; i < dirlist.size(); i++){
delete dirlist[i];
}

return 0;
}

int FileReplacer::isFileExisting(std::string param){
//fs_log_string(socket, param.c_str(), BYTE_LOG_STR);
if(dir_all != 0){
if(dir_all->isInFolder(param)){
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/utils/FileReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ class FileReplacer
int getSize();
std::string getFileListAsString();
private:
int socket;
Directory * dir_all;
};

0 comments on commit 88f5051

Please sign in to comment.