Skip to content

Commit

Permalink
Merge pull request winnfsd#53 from Toilal/fix-getpath-leak
Browse files Browse the repository at this point in the history
Fix CFileTree::GetNodeFullPath memory leak
  • Loading branch information
marcharding authored Nov 13, 2017
2 parents 4e412a7 + 633a1c0 commit b75b2ed
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 182 deletions.
44 changes: 23 additions & 21 deletions src/FileTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CFileTable::~CFileTable()
}
}

unsigned long CFileTable::GetIDByPath(char *path)
unsigned long CFileTable::GetIDByPath(const char *path)
{
unsigned char *handle;

Expand All @@ -61,7 +61,7 @@ unsigned long CFileTable::GetIDByPath(char *path)
return *(unsigned long *)handle;
}

unsigned char *CFileTable::GetHandleByPath(char *path)
unsigned char *CFileTable::GetHandleByPath(const char *path)
{
tree_node_<FILE_ITEM> *node;

Expand All @@ -83,23 +83,23 @@ unsigned char *CFileTable::GetHandleByPath(char *path)
return node->data.handle;
}

char *CFileTable::GetPathByHandle(unsigned char *handle)
bool CFileTable::GetPathByHandle(unsigned char *handle, std::string &path)
{
unsigned int id;
tree_node_<FILE_ITEM>* node;

id = *(unsigned int *)handle;
// TODO : Not found item

node = GetItemByID(id);
if (node != NULL) {
return g_FileTree.GetNodeFullPath(node);
g_FileTree.GetNodeFullPath(node, path);
return true;
} else {
return NULL;
return false;
}
//return pItem == NULL ? NULL : pItem->path;
}

tree_node_<FILE_ITEM>* CFileTable::FindItemByPath(char *path)
tree_node_<FILE_ITEM>* CFileTable::FindItemByPath(const char *path)
{
tree_node_<FILE_ITEM>* node= NULL;/*
CACHE_LIST *pCurr;
Expand Down Expand Up @@ -156,7 +156,7 @@ tree_node_<FILE_ITEM>* CFileTable::FindItemByPath(char *path)
return node;
}

tree_node_<FILE_ITEM>* CFileTable::AddItem(char *path)
tree_node_<FILE_ITEM>* CFileTable::AddItem(const char *path)
{
FILE_ITEM item;
tree_node_<FILE_ITEM>* node;
Expand Down Expand Up @@ -256,7 +256,7 @@ void CFileTable::PutItemInCache(FILE_ITEM *pItem)
}
}

bool CFileTable::RemoveItem(char *path) {
bool CFileTable::RemoveItem(const char *path) {
/* CACHE_LIST *pCurr;
FILE_ITEM *pItem;
unsigned int i, j, nPathLen;
Expand Down Expand Up @@ -317,7 +317,9 @@ bool CFileTable::RemoveItem(char *path) {
}
// Remove from table end

g_FileTree.RemoveItem(g_FileTree.GetNodeFullPath(foundDeletedItem));
std::string path;
g_FileTree.GetNodeFullPath(foundDeletedItem, path);
g_FileTree.RemoveItem(path.c_str());
return true;
}
else {
Expand All @@ -326,7 +328,7 @@ bool CFileTable::RemoveItem(char *path) {
return false;
}

void CFileTable::RenameFile(char *pathFrom, char* pathTo)
void CFileTable::RenameFile(const char *pathFrom, const char* pathTo)
{
/*FILE_ITEM *pItem;
Expand All @@ -343,7 +345,7 @@ void CFileTable::RenameFile(char *pathFrom, char* pathTo)
g_FileTree.RenameItem(pathFrom, pathTo);
}

bool FileExists(char *path)
bool FileExists(const char *path)
{
int handle;
struct _finddata_t fileinfo;
Expand All @@ -354,22 +356,22 @@ bool FileExists(char *path)
return handle == -1 ? false : strcmp(fileinfo.name, strrchr(path, '\\') + 1) == 0; //filename must match case
}

unsigned long GetFileID(char *path)
unsigned long GetFileID(const char *path)
{
return g_FileTable.GetIDByPath(path);
}

unsigned char *GetFileHandle(char *path)
unsigned char *GetFileHandle(const char *path)
{
return g_FileTable.GetHandleByPath(path);
}

char *GetFilePath(unsigned char *handle)
bool GetFilePath(unsigned char *handle, std::string &filePath)
{
return g_FileTable.GetPathByHandle(handle);
return g_FileTable.GetPathByHandle(handle, filePath);
}

int RenameFile(char *pathFrom, char *pathTo)
int RenameFile(const char *pathFrom, const char *pathTo)
{
tree_node_<FILE_ITEM>* node;
FILE_ITEM *pItem;
Expand All @@ -392,7 +394,7 @@ int RenameFile(char *pathFrom, char *pathTo)
}
}

int RenameDirectory(char *pathFrom, char *pathTo)
int RenameDirectory(const char *pathFrom, const char *pathTo)
{
errno_t errorNumber = RenameFile(pathFrom, pathTo);

Expand Down Expand Up @@ -426,7 +428,7 @@ int RenameDirectory(char *pathFrom, char *pathTo)

}

bool RemoveFile(char *path)
bool RemoveFile(const char *path)
{
int nMode = 0;
nMode |= S_IREAD;
Expand All @@ -440,7 +442,7 @@ bool RemoveFile(char *path)
return false;
}

int RemoveFolder(char *path)
int RemoveFolder(const char *path)
{
int nMode = 0;
unsigned long errorCode = 0;
Expand Down
30 changes: 15 additions & 15 deletions src/FileTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class CFileTable
public:
CFileTable();
~CFileTable();
unsigned long GetIDByPath(char *path);
unsigned char *GetHandleByPath(char *path);
char *GetPathByHandle(unsigned char *handle);
tree_node_<FILE_ITEM>* FindItemByPath(char *path);
bool RemoveItem(char *path);
void RenameFile(char *pathFrom, char *pathTo);
unsigned long GetIDByPath(const char *path);
unsigned char *GetHandleByPath(const char *path);
bool GetPathByHandle(unsigned char *handle, std::string &path);
tree_node_<FILE_ITEM>* FindItemByPath(const char *path);
bool RemoveItem(const char *path);
void RenameFile(const char *pathFrom, const char *pathTo);

protected:
tree_node_<FILE_ITEM>* AddItem(char *path);
tree_node_<FILE_ITEM>* AddItem(const char *path);

private:
FILE_TABLE *m_pFirstTable, *m_pLastTable;
Expand All @@ -50,12 +50,12 @@ class CFileTable

};

extern bool FileExists(char *path);
extern unsigned long GetFileID(char *path);
extern unsigned char *GetFileHandle(char *path);
extern char *GetFilePath(unsigned char *handle);
extern int RenameFile(char *pathFrom, char *pathTo);
extern int RenameDirectory(char *pathFrom, char *pathTo);
extern int RemoveFolder(char *path);
extern bool RemoveFile(char *path);
extern bool FileExists(const char *path);
extern unsigned long GetFileID(const char *path);
extern unsigned char *GetFileHandle(const char *path);
extern bool GetFilePath(unsigned char *handle, std::string &filePath);
extern int RenameFile(const char *pathFrom, const char *pathTo);
extern int RenameDirectory(const char *pathFrom, const char *pathTo);
extern int RemoveFolder(const char *path);
extern bool RemoveFile(const char *path);
#endif
30 changes: 12 additions & 18 deletions src/FileTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ std::string _basename_932(std::string path) {
return result;
}

FILE_ITEM CFileTree::AddItem(char *absolutePath, unsigned char* handle)
FILE_ITEM CFileTree::AddItem(const char *absolutePath, unsigned char* handle)
{
FILE_ITEM item;
item.handle = handle;
Expand Down Expand Up @@ -125,7 +125,7 @@ FILE_ITEM CFileTree::AddItem(char *absolutePath, unsigned char* handle)
return item;
}

void CFileTree::RemoveItem(char *absolutePath)
void CFileTree::RemoveItem(const char *absolutePath)
{
tree_node_<FILE_ITEM>* node = findNodeFromRootWithPath(absolutePath);
if (node != NULL) {
Expand All @@ -138,7 +138,7 @@ void CFileTree::RemoveItem(char *absolutePath)
DisplayTree(topNode.node, 0);
}

void CFileTree::RenameItem(char *absolutePathFrom, char *absolutePathTo)
void CFileTree::RenameItem(const char *absolutePathFrom, const char *absolutePathTo)
{
tree_node_<FILE_ITEM>* node = findNodeFromRootWithPath(absolutePathFrom);
tree_node_<FILE_ITEM>* parentNode = findParentNodeFromRootForPath(absolutePathTo);
Expand All @@ -162,7 +162,7 @@ void CFileTree::RenameItem(char *absolutePathFrom, char *absolutePathTo)
DisplayTree(topNode.node, 0);
}

tree_node_<FILE_ITEM>* CFileTree::FindFileItemForPath(char *absolutePath)
tree_node_<FILE_ITEM>* CFileTree::FindFileItemForPath(const char *absolutePath)
{
tree_node_<FILE_ITEM>* node = findNodeFromRootWithPath(absolutePath);
if (node == NULL) {
Expand All @@ -171,7 +171,7 @@ tree_node_<FILE_ITEM>* CFileTree::FindFileItemForPath(char *absolutePath)
return node;
}

tree_node_<FILE_ITEM>* CFileTree::findNodeFromRootWithPath(char *path)
tree_node_<FILE_ITEM>* CFileTree::findNodeFromRootWithPath(const char *path)
{
// No topNode - bail out.
if (topNode.node == NULL){
Expand All @@ -190,7 +190,7 @@ tree_node_<FILE_ITEM>* CFileTree::findNodeFromRootWithPath(char *path)
if (sPath.find(nPath) != std::string::npos) {
// printf("Found %s is part of %s \n", sPath.c_str(), topNode->path);
std::string splittedString = sPath.substr(strlen(topNode->path) + 1);
return findNodeWithPathFromNode(splittedString, topNode.node);
return findNodeWithPathFromNode(splittedString.c_str(), topNode.node);
}
else {
// If the current topNode isn't related to the requested path
Expand All @@ -214,15 +214,15 @@ tree_node_<FILE_ITEM>* CFileTree::findNodeFromRootWithPath(char *path)
// printf("Found root node %s \n", it.node->data.path);
topNode = it;
std::string splittedString = sPath.substr(itPath.length() + 1);
return findNodeWithPathFromNode(splittedString, it.node);
return findNodeWithPathFromNode(splittedString.c_str(), it.node);
}
}
}
// Nothing found return NULL.
return NULL;
}

tree_node_<FILE_ITEM>* CFileTree::findNodeWithPathFromNode(std::string path, tree_node_<FILE_ITEM>* node)
tree_node_<FILE_ITEM>* CFileTree::findNodeWithPathFromNode(const char *path, tree_node_<FILE_ITEM>* node)
{
tree<FILE_ITEM>::sibling_iterator sib = filesTree.begin(node);
tree<FILE_ITEM>::sibling_iterator end = filesTree.end(node);
Expand All @@ -241,15 +241,15 @@ tree_node_<FILE_ITEM>* CFileTree::findNodeWithPathFromNode(std::string path, tre
return sib.node;
}
else {
return findNodeWithPathFromNode(followingPath, sib.node);
return findNodeWithPathFromNode(followingPath.c_str(), sib.node);
}
}
++sib;
}
return NULL;
}

tree_node_<FILE_ITEM>* CFileTree::findParentNodeFromRootForPath(char *path) {
tree_node_<FILE_ITEM>* CFileTree::findParentNodeFromRootForPath(const char *path) {
std::string sPath(path);
std::string nPath(topNode->path);

Expand All @@ -265,13 +265,12 @@ tree_node_<FILE_ITEM>* CFileTree::findParentNodeFromRootForPath(char *path) {
if (followingPath.empty()) {
return topNode.node;
} else {
return findNodeWithPathFromNode(followingPath, topNode.node);
return findNodeWithPathFromNode(followingPath.c_str(), topNode.node);
}
}

char* CFileTree::GetNodeFullPath(tree_node_<FILE_ITEM>* node)
void CFileTree::GetNodeFullPath(tree_node_<FILE_ITEM>* node, std::string &path)
{
std::string path;
path.append(node->data.path);
tree_node_<FILE_ITEM>* parentNode = node->parent;
while (parentNode != NULL)
Expand All @@ -280,11 +279,6 @@ char* CFileTree::GetNodeFullPath(tree_node_<FILE_ITEM>* node)
path.insert(0, parentNode->data.path);
parentNode = parentNode->parent;
}

// TODO : Memory leak
char *cstr = new char[path.length() + 1];
strcpy_s(cstr, path.length() + 1, path.c_str());
return cstr;
}

void DisplayTree(tree_node_<FILE_ITEM>* node, int level)
Expand Down
16 changes: 8 additions & 8 deletions src/FileTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ class CFileTree
{
public:
bool static const debug = false;
FILE_ITEM AddItem(char *absolutePath, unsigned char *handle);
void RemoveItem(char *absolutePath);
void RenameItem(char *absolutePathFrom, char *absolutePathTo);
FILE_ITEM AddItem(const char *absolutePath, unsigned char *handle);
void RemoveItem(const char *absolutePath);
void RenameItem(const char *absolutePathFrom, const char *absolutePathTo);

tree_node_<FILE_ITEM>* FindFileItemForPath(char *absolutePath);
tree_node_<FILE_ITEM>* FindFileItemForPath(const char *absolutePath);

char * GetNodeFullPath(tree_node_<FILE_ITEM>* node);
void GetNodeFullPath(tree_node_<FILE_ITEM>* node, std::string &fullPath);

protected:
tree_node_<FILE_ITEM>* findNodeFromRootWithPath(char *path);
tree_node_<FILE_ITEM>* findNodeWithPathFromNode(std::string path, tree_node_<FILE_ITEM>* node);
tree_node_<FILE_ITEM>* findParentNodeFromRootForPath(char *path);
tree_node_<FILE_ITEM>* findNodeFromRootWithPath(const char *path);
tree_node_<FILE_ITEM>* findNodeWithPathFromNode(const char *path, tree_node_<FILE_ITEM>* node);
tree_node_<FILE_ITEM>* findParentNodeFromRootForPath(const char *path);
};
extern void DisplayTree(tree_node_<FILE_ITEM>* node, int level);

Expand Down
20 changes: 10 additions & 10 deletions src/MountProg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CMountProg::~CMountProg()

}

bool CMountProg::SetPathFile(char *file)
bool CMountProg::SetPathFile(const char *file)
{
char *formattedFile = FormatPath(file, FORMAT_PATH);

Expand All @@ -77,7 +77,7 @@ bool CMountProg::SetPathFile(char *file)
return false;
}

void CMountProg::Export(char *path, char *pathAlias)
void CMountProg::Export(const char *path, const char *pathAlias)
{
char *formattedPath = FormatPath(path, FORMAT_PATH);
pathAlias = FormatPath(pathAlias, FORMAT_PATHALIAS);
Expand Down Expand Up @@ -348,7 +348,7 @@ bool CMountProg::GetPath(char **returnPath)
}


bool CMountProg::ReadPathsFromFile(char* sFileName)
bool CMountProg::ReadPathsFromFile(const char* sFileName)
{
std::ifstream pathFile(sFileName);

Expand Down Expand Up @@ -396,7 +396,7 @@ bool CMountProg::ReadPathsFromFile(char* sFileName)
return true;
}

char *CMountProg::FormatPath(char *pPath, pathFormats format)
char *CMountProg::FormatPath(const char *pPath, pathFormats format)
{
size_t len = strlen(pPath);

Expand Down Expand Up @@ -484,15 +484,15 @@ char *CMountProg::FormatPath(char *pPath, pathFormats format)
}
} else if (format == FORMAT_PATHALIAS) {
if (pPath[1] == ':' && ((pPath[0] >= 'A' && pPath[0] <= 'Z') || (pPath[0] >= 'a' && pPath[0] <= 'z'))) {
strncpy_s(result, len + 1, pPath, len);
//transform Windows format to mount path d:\work => /d/work
pPath[1] = pPath[0];
pPath[0] = '/';
for (size_t i = 2; i < strlen(pPath); i++) {
if (pPath[i] == '\\') {
pPath[i] = '/';
result[1] = result[0];
result[0] = '/';
for (size_t i = 2; i < strlen(result); i++) {
if (result[i] == '\\') {
result[i] = '/';
}
}
strncpy_s(result, len + 1, pPath, len);
} else if (pPath[0] != '/') { //check path alias format
printf("Path alias format is incorrect.\n");
printf("Please use a path like /exports\n");
Expand Down
Loading

0 comments on commit b75b2ed

Please sign in to comment.