1
0

Directory traversal complete

This commit is contained in:
Scott E. Graves
2017-03-13 00:35:22 -05:00
parent 170a79d0fc
commit f09f8f1e93
2 changed files with 60 additions and 46 deletions

View File

@@ -86,10 +86,16 @@ std::vector<String> CSiaApi::_CSiaFileTree::QueryDirectories(String rootFolder)
if ((dir.length() > rootFolder.length()) && (dir.substr(0, rootFolder.length()) == rootFolder)) if ((dir.length() > rootFolder.length()) && (dir.substr(0, rootFolder.length()) == rootFolder))
{ {
String subFolder = dir.substr(rootFolder.length()); String subFolder = dir.substr(rootFolder.length());
int f = subFolder.find_first_of('\\'); int idx = subFolder.find_first_of('\\');
if (f > 0) if (idx == 0)
{ {
subFolder = subFolder.substr(0, f); subFolder = subFolder.substr(1);
idx = subFolder.find_first_of('\\');
}
if (idx > 0)
{
subFolder = subFolder.substr(0, idx);
} }
auto it = std::find(ret.begin(), ret.end(), subFolder); auto it = std::find(ret.begin(), ret.end(), subFolder);

View File

@@ -158,7 +158,7 @@ private:
::PathCombine(&cacheFilePath[0], GetCacheLocation().c_str(), &FileName[1]); ::PathCombine(&cacheFilePath[0], GetCacheLocation().c_str(), &FileName[1]);
DWORD fileAttr = ::GetFileAttributes(cacheFilePath.c_str()); DWORD fileAttr = ::GetFileAttributes(cacheFilePath.c_str());
if (fileAttr != INVALID_FILE_ATTRIBUTES && if ((fileAttr != INVALID_FILE_ATTRIBUTES) &&
(fileAttr & FILE_ATTRIBUTE_DIRECTORY) && (fileAttr & FILE_ATTRIBUTE_DIRECTORY) &&
!(CreateOptions & FILE_NON_DIRECTORY_FILE)) !(CreateOptions & FILE_NON_DIRECTORY_FILE))
{ {
@@ -170,7 +170,20 @@ private:
} }
} }
if (!DokanFileInfo->IsDirectory) // Folder (cache operation only)
if (DokanFileInfo->IsDirectory)
{
HANDLE handle = ::CreateFile(cacheFilePath.c_str(), genericDesiredAccess, ShareAccess, &securityAttrib, creationDisposition, fileAttributesAndFlags | FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
ret = DokanNtStatusFromWin32(GetLastError());
}
else
{
DokanFileInfo->Context = reinterpret_cast<ULONG64>(handle); // save the file handle in Context
}
}
else // File (cache and/or Sia operation)
{ {
// Formulate Sia path and cache path // Formulate Sia path and cache path
String siaPath = CSiaApi::FormatToSiaPath(PathSkipRoot(FileName)); // Strip drive letter to get Sia path String siaPath = CSiaApi::FormatToSiaPath(PathSkipRoot(FileName)); // Strip drive letter to get Sia path
@@ -334,18 +347,6 @@ private:
ret = STATUS_OBJECT_NAME_INVALID; ret = STATUS_OBJECT_NAME_INVALID;
} }
} }
else // Folder Operation (cache operation only)
{
HANDLE handle = ::CreateFile(cacheFilePath.c_str(), genericDesiredAccess, ShareAccess, &securityAttrib, creationDisposition, fileAttributesAndFlags | FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
ret = DokanNtStatusFromWin32(GetLastError());
}
else
{
DokanFileInfo->Context = reinterpret_cast<ULONG64>(handle); // save the file handle in Context
}
}
} }
return ret; return ret;
@@ -357,16 +358,30 @@ private:
auto siaFileTree = _siaFileTree; auto siaFileTree = _siaFileTree;
if (siaFileTree) if (siaFileTree)
{ {
String siaQuery = CSiaApi::FormatToSiaPath(PathSkipRoot(FileName)); String siaQuery = CSiaApi::FormatToSiaPath(::PathSkipRoot(FileName));
String rootPath = FileName;
if (::PathIsDirectory(FileName)) String cachePath;
String rootPath = siaQuery;
if (wcscmp(FileName, L"\\") == 0)
{
cachePath = GetCacheLocation().c_str();
siaQuery += L"/*.*";
}
else
{
cachePath.resize(MAX_PATH + 1);
::PathCombine(&cachePath[0], GetCacheLocation().c_str(), &FileName[1]);
cachePath = cachePath.c_str();
if (::GetFileAttributes(&cachePath[0]) & FILE_ATTRIBUTE_DIRECTORY)
{ {
siaQuery += L"/*.*"; siaQuery += L"/*.*";
} }
else else
{ {
rootPath = cachePath;
::PathRemoveFileSpec(&rootPath[0]); ::PathRemoveFileSpec(&rootPath[0]);
rootPath = rootPath.c_str(); rootPath = CSiaApi::FormatToSiaPath(rootPath);
}
} }
auto dirList = siaFileTree->QueryDirectories(rootPath); auto dirList = siaFileTree->QueryDirectories(rootPath);
@@ -374,22 +389,15 @@ private:
{ {
WIN32_FIND_DATA fd = { 0 }; WIN32_FIND_DATA fd = { 0 };
wcscpy_s(fd.cFileName, dir.c_str()); wcscpy_s(fd.cFileName, dir.c_str());
fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
String cachePath;
cachePath.resize(MAX_PATH + 1);
if (rootPath == L"\\")
{
::PathCombine(&cachePath[0], GetCacheLocation().c_str(), dir.c_str());
}
else
{
::PathCombine(&cachePath[0], GetCacheLocation().c_str(), rootPath.c_str());
::PathAppend(&cachePath[0], dir.c_str());
}
cachePath = cachePath.c_str();
::CreateDirectory(cachePath.c_str(), nullptr);
FillFindData(&fd, DokanFileInfo); FillFindData(&fd, DokanFileInfo);
// Create cache sub-folder
String subCachePath = cachePath.c_str();
subCachePath.resize(MAX_PATH + 1);
::PathAppend(&subCachePath[0], dir.c_str());
subCachePath = subCachePath.c_str();
::CreateDirectory(subCachePath.c_str(), nullptr);
} }
auto fileList = siaFileTree->Query(siaQuery); auto fileList = siaFileTree->Query(siaQuery);