summaryrefslogtreecommitdiff
path: root/shared/IFileOperation/file_op.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
commitfbe76102e941b9f1edaf236788e42678f05fdf9a (patch)
treef5f538316019fa89be8dc478103490c3a826f3ac /shared/IFileOperation/file_op.cpp
parent3.8 (diff)
downloadFreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.gz
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.bz2
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.zip
3.9
Diffstat (limited to 'shared/IFileOperation/file_op.cpp')
-rw-r--r--shared/IFileOperation/file_op.cpp229
1 files changed, 229 insertions, 0 deletions
diff --git a/shared/IFileOperation/file_op.cpp b/shared/IFileOperation/file_op.cpp
new file mode 100644
index 00000000..f37a2e66
--- /dev/null
+++ b/shared/IFileOperation/file_op.cpp
@@ -0,0 +1,229 @@
+// **************************************************************************
+// * This file is part of the FreeFileSync project. It is distributed under *
+// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
+// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
+// **************************************************************************
+//
+#include "file_op.h"
+#include "../com_ptr.h"
+#include "../com_error.h"
+
+#define WIN32_LEAN_AND_MEAN
+#include "windows.h"
+#include <Shellapi.h> // Included for shell constants such as FO_* values
+#include <shobjidl.h> // Required for necessary shell dependencies
+
+#include <algorithm>
+#include <string>
+#include <cstdio>
+#include <comdef.h>
+
+
+void writeString(const std::wstring& input, wchar_t* output, size_t outputBufferLen)
+{
+ const size_t newSize = min(input.length() + 1, outputBufferLen); //including null-termination
+ memcpy(output, input.c_str(), newSize * sizeof(wchar_t));
+ output[newSize-1] = 0; //if output buffer is too small...
+}
+
+
+namespace FileOp
+{
+std::wstring lastErrorMessage;
+}
+
+
+bool FileOp::moveToRecycleBin(const wchar_t* fileNames[],
+ size_t fileNo) //size of fileNames array
+{
+ using Util::ComPtr;
+ using Util::generateErrorMsg;
+ HRESULT hr;
+
+ // Create the IFileOperation interface
+ ComPtr<IFileOperation> fileOp;
+ hr = CoCreateInstance(CLSID_FileOperation,
+ NULL,
+ CLSCTX_ALL,
+ IID_PPV_ARGS(fileOp.init()));
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr);
+ return false;
+ }
+
+ // Set the operation flags. Turn off all UI
+ // from being shown to the user during the
+ // operation. This includes error, confirmation
+ // and progress dialogs.
+ hr = fileOp->SetOperationFlags(FOF_ALLOWUNDO |
+ FOF_NOCONFIRMATION |
+ FOF_SILENT |
+ FOF_NOERRORUI);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"SetOperationFlags\".", hr);
+ return false;
+ }
+
+ for (size_t i = 0; i < fileNo; ++i)
+ {
+ //create file/folder item object
+ ComPtr<IShellItem> psiFile;
+ hr = SHCreateItemFromParsingName(fileNames[i],
+ NULL,
+ IID_PPV_ARGS(psiFile.init()));
+ if (FAILED(hr))
+ {
+ std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for file:\n");
+ message += std::wstring(L"\"") + fileNames[i] + L"\".";
+
+ lastErrorMessage = generateErrorMsg(message, hr);
+ return false;
+ }
+
+ hr = fileOp->DeleteItem(psiFile.get(), NULL);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"DeleteItem\".", hr);
+ return false;
+ }
+ }
+
+ //perform actual operations
+ hr = fileOp->PerformOperations();
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"PerformOperations\".", hr);
+ return false;
+ }
+
+ //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors!
+ BOOL pfAnyOperationsAborted = FALSE;
+ hr = fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"GetAnyOperationsAborted\".", hr);
+ return false;
+ }
+
+ if (pfAnyOperationsAborted == TRUE)
+ {
+ lastErrorMessage = L"Operation did not complete successfully.";
+ return false;
+ }
+
+ return true;
+}
+
+
+bool FileOp::copyFile(const wchar_t* sourceFile,
+ const wchar_t* targetFile)
+{
+ using Util::ComPtr;
+ using Util::generateErrorMsg;
+
+ HRESULT hr;
+
+ // Create the IFileOperation interface
+ ComPtr<IFileOperation> fileOp;
+ hr = CoCreateInstance(CLSID_FileOperation,
+ NULL,
+ CLSCTX_ALL,
+ IID_PPV_ARGS(fileOp.init()));
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr);
+ return false;
+ }
+
+ // Set the operation flags. Turn off all UI
+ // from being shown to the user during the
+ // operation. This includes error, confirmation
+ // and progress dialogs.
+ hr = fileOp->SetOperationFlags(FOF_NOCONFIRMATION |
+ FOF_SILENT |
+ FOFX_EARLYFAILURE |
+ FOF_NOERRORUI);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"SetOperationFlags\".", hr);
+ return false;
+ }
+
+ //create source object
+ ComPtr<IShellItem> psiSourceFile;
+ hr = SHCreateItemFromParsingName(sourceFile,
+ NULL,
+ IID_PPV_ARGS(psiSourceFile.init()));
+ if (FAILED(hr))
+ {
+ std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for file:\n");
+ message += std::wstring(L"\"") + sourceFile + L"\".";
+ lastErrorMessage = generateErrorMsg(message, hr);
+ return false;
+ }
+
+ const size_t pos = std::wstring(targetFile).find_last_of(L'\\');
+ if (pos == std::wstring::npos)
+ {
+ lastErrorMessage = L"Target filename does not contain a path separator.";
+ return false;
+ }
+
+ const std::wstring targetFolder(targetFile, pos);
+ const std::wstring targetFileNameShort = targetFile + pos + 1;
+
+ //create target folder object
+ ComPtr<IShellItem> psiTargetFolder;
+ hr = SHCreateItemFromParsingName(targetFolder.c_str(),
+ NULL,
+ IID_PPV_ARGS(psiTargetFolder.init()));
+ if (FAILED(hr))
+ {
+ std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for folder:\n");
+ message += std::wstring(L"\"") + targetFolder + L"\".";
+ lastErrorMessage = generateErrorMsg(message, hr);
+ return false;
+ }
+
+ //schedule file copy operation
+ hr = fileOp->CopyItem(psiSourceFile.get(), psiTargetFolder.get(), targetFileNameShort.c_str(), NULL);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"CopyItem\".", hr);
+ return false;
+ }
+
+ //perform actual operations
+ hr = fileOp->PerformOperations();
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"PerformOperations\".", hr);
+ return false;
+ }
+
+ //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors!
+ BOOL pfAnyOperationsAborted = FALSE;
+ hr = fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted);
+ if (FAILED(hr))
+ {
+ lastErrorMessage = generateErrorMsg(L"Error calling \"GetAnyOperationsAborted\".", hr);
+ return false;
+ }
+
+ if (pfAnyOperationsAborted == TRUE)
+ {
+ lastErrorMessage = L"Operation did not complete successfully.";
+ return false;
+ }
+
+ return true;
+}
+
+
+//if any of the functions above returns 'false', this message returns last error
+void FileOp::getLastError(wchar_t* errorMessage, size_t errorBufferLen)
+{
+ writeString(lastErrorMessage, errorMessage, errorBufferLen);
+}
bgstack15