Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added TskFileManager::copyDirectory() method to save contents of a di…

…rectory.
  • Loading branch information...
commit 9a4fc7f12e725cdd6121f2ab2e23283954f9c44c 1 parent 2d7c1af
@esaunders esaunders authored
View
27 framework/File/TskFileManager.h
@@ -83,6 +83,33 @@ class TSK_FRAMEWORK_API TskFileManager
}
/**
+ * Copy the contents of a directory to the given fully qualifed file path.
+ * Directories along the path will be created if they do not exist.
+ * If the destination directory exists it will be replaced.
+ * Defaults to a non-recursive copy.
+ * @param directoryToCopy The TskFile object representing the directory to copy.
+ * @param destinationPath The path to save directory contents to, including the directory name.
+ * @param bRecurse Whether to recursively copy directory contents.
+ * @throws various exceptions on errors
+ */
+ virtual void copyDirectory(TskFile* directoryToCopy, const std::wstring& destinationPath, const bool bRecurse = false) = 0;
+
+ /**
+ * Copy the contents of a directory to the given fully qualifed file path.
+ * Directories along the path will be created if they do not exist.
+ * If the destination directory exists it will be replaced.
+ * Defaults to a non-recursive copy.
+ * @param directoryIdToCopy The id representing the directory to copy.
+ * @param destinationPath The path to save directory contents to, including the directory name.
+ * @param bRecurse Whether to recursively copy directory contents.
+ * @throws various exceptions on errors
+ */
+ virtual void copyDirectory(uint64_t directoryIdToCopy, const std::wstring& destinationPath, const bool bRecurse = false)
+ {
+ copyDirectory(getFile(directoryIdToCopy), destinationPath, bRecurse);
+ }
+
+ /**
* Add a file to the system using the given file id and input stream.
* This method saves a local copy of the content contained in the input stream.
* @param fileId ID of the new file.
View
106 framework/File/TskFileManagerImpl.cpp
@@ -28,6 +28,7 @@
#include "Poco/Path.h"
#include "Poco/FileStream.h"
#include "Poco/StreamCopier.h"
+#include "Poco/NumberFormatter.h"
TskFileManagerImpl * TskFileManagerImpl::m_pInstance = NULL;
@@ -119,10 +120,14 @@ void TskFileManagerImpl::copyFile(TskFile* fileToSave, const std::wstring& fileP
{
if (fileToSave == NULL)
{
- LOGERROR(L"TskFileManagerImpl::saveFile - Passed NULL file pointer.");
- throw TskNullPointerException();
+ throw TskException("TskFile pointer is NULL.");
}
+ if (fileToSave->isDirectory())
+ {
+ throw TskException("Attempt to copy directory where file is expected.");
+ }
+
Poco::Path destPath(TskUtilities::toUTF8(filePath));
// Create directories that may be missing along the path.
@@ -184,37 +189,100 @@ void TskFileManagerImpl::copyFile(TskFile* fileToSave, const std::wstring& fileP
}
catch (TskFileException& tskEx)
{
- std::wstringstream errorMsg;
- errorMsg << L"TskFileManagerImpl::saveFile - " << tskEx.message().c_str() << std::endl;
- LOGERROR(errorMsg.str());
-
// Rethrow the exception up to our caller
- throw;
+ throw tskEx;
}
catch (Poco::PathNotFoundException& ex)
{
- std::wstringstream errorMsg;
- errorMsg << L"TskFileManagerImpl::saveFile - Path not found : "
- << ex.message().c_str() << std::endl;
- LOGERROR(errorMsg.str());
-
- throw TskFileException("Path not found : " + fileToSave->getPath());
+ throw TskException("Path not found : " + fileToSave->getPath());
}
catch (std::exception & ex)
{
- std::wstringstream errorMsg;
- errorMsg << L"TskFileManagerImpl::saveFile - Exception : "
- << ex.what() << std::endl;
- LOGERROR(errorMsg.str());
- throw;
+ throw ex;
}
}
+void TskFileManagerImpl::copyDirectory(TskFile* directoryToCopy, const std::wstring& destinationPath, const bool bRecurse)
+{
+ if (directoryToCopy == NULL)
+ {
+ throw TskException("Directory pointer is NULL.");
+ }
+
+ if (!directoryToCopy->isDirectory())
+ {
+ throw TskException("File object to copy is not a directory.");
+ }
+
+ try
+ {
+ Poco::File destDir(TskUtilities::toUTF8(destinationPath));
+
+ // If the destination directory exists it is replaced.
+ if (destDir.exists())
+ {
+ destDir.remove(true);
+ }
+
+ // Create directories that may be missing along the path.
+ destDir.createDirectories();
+
+ // If the source directory exists we simply copy it to the destination.
+ if (directoryToCopy->exists())
+ {
+ Poco::File sourceFile(directoryToCopy->getPath());
+ sourceFile.copyTo(destDir.path());
+ }
+ else
+ {
+ // Find all files contained in this directory.
+ std::stringstream condition;
+ condition << "WHERE par_file_id = " << directoryToCopy->getId();
+
+ std::vector<uint64_t> fileIds = TskServices::Instance().getImgDB().getFileIds(condition.str());
+
+ for (std::vector<uint64_t>::const_iterator it = fileIds.begin(); it != fileIds.end(); ++it)
+ {
+ TskFile * pFile = getFile(*it);
+
+ if (pFile == NULL)
+ {
+ throw TskException("Failed to create file object for file id " + Poco::NumberFormatter::format(*it));
+ }
+
+ if (pFile->isDirectory() && bRecurse)
+ {
+ Poco::Path subDirPath = Poco::Path::forDirectory(destDir.path());
+ subDirPath.pushDirectory(pFile->getName());
+ copyDirectory(pFile, TskUtilities::toUTF16(subDirPath.toString()), bRecurse);
+ }
+
+ if (!pFile->isDirectory())
+ {
+ Poco::Path filePath(destDir.path());
+ filePath.append(pFile->getName());
+ copyFile(pFile, TskUtilities::toUTF16(filePath.toString()));
+ }
+ }
+ }
+ }
+ catch (TskException& tskEx)
+ {
+ // Rethrow the exception up to our caller
+ throw tskEx;
+ }
+ catch (std::exception & ex)
+ {
+ throw ex;
+ }
+}
void TskFileManagerImpl::saveFile(TskFile* fileToSave)
{
// Determine what the path should be based on TskFile.id()
- // and call saveFile(fileToSave, path)
+ // and call copyFile(fileToSave, path)
+ // Note that all saveFile() methods ultimately resolve
+ // to a call to copyFile().
copyFile(fileToSave, getPath(fileToSave->getId()));
}
View
5 framework/File/TskFileManagerImpl.h
@@ -51,7 +51,10 @@ class TSK_FRAMEWORK_API TskFileManagerImpl : public TskFileManager
virtual void copyFile(TskFile* fileToSave, const std::wstring& filePath);
virtual void copyFile(const uint64_t fileId, const std::wstring& filePath);
- // Save the contents of the input stream to a file with the given fileId
+ // Copy the contents of a directory to the specified path.
+ virtual void copyDirectory(TskFile* directoryToCopy, const std::wstring& destinationPath, const bool bRecurse = false);
+
+ // Save the contents of the input stream to a file with the given fileId
virtual void addFile(const uint64_t fileId, std::istream& istr);
virtual void addFile(const uint64_t fileId, std::wstring& filePath);
Please sign in to comment.
Something went wrong with that request. Please try again.