summaryrefslogtreecommitdiff
path: root/shared/IFileOperation/fileOp.cpp
blob: 732fb9b58afc4f402f7491d73cb4aaf246a729a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// **************************************************************************
// * 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 "fileOp.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...
}


std::wstring numberToHexString(long number)
{
    wchar_t result[100];
    swprintf(result, 100, L"0x%08x", number);
    return std::wstring(result);
}


std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr)
{
    std::wstring output(input);
    output += L" (";
    output += numberToHexString(hr);
    output += L": ";
    output += _com_error(hr).ErrorMessage();
    output += L")";
    return output;
}


//IShellItem resource management
template <class T>
class ReleaseAtExit
{
public:
    ReleaseAtExit(T*& item) : item_(item) {}
    ~ReleaseAtExit()
    {
        if (item_ != NULL)
            item_->Release();
    }
private:
    T*& item_;
};


namespace FileOp
{
std::wstring lastErrorMessage;
}


bool FileOp::moveToRecycleBin(const wchar_t* fileNames[],
                              size_t         fileNo) //size of fileNames array
{
    HRESULT hr;

    // Create the IFileOperation interface
    IFileOperation* pfo = NULL;
    ReleaseAtExit<IFileOperation> dummy(pfo);
    hr = CoCreateInstance(CLSID_FileOperation,
                          NULL,
                          CLSCTX_ALL,
                          IID_PPV_ARGS(&pfo));
    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 = pfo->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
        IShellItem* psiFile = NULL;
        ReleaseAtExit<IShellItem> dummy2(psiFile);
        hr = SHCreateItemFromParsingName(fileNames[i],
                                         NULL,
                                         IID_PPV_ARGS(&psiFile));
        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 = pfo->DeleteItem(psiFile, NULL);
        if (FAILED(hr))
        {
            lastErrorMessage = generateErrorMsg(L"Error calling \"DeleteItem\".", hr);
            return false;
        }
    }

    //perform actual operations
    hr = pfo->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 = pfo->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)
{
    HRESULT hr;

    // Create the IFileOperation interface
    IFileOperation* pfo = NULL;
    ReleaseAtExit<IFileOperation> dummy(pfo);
    hr = CoCreateInstance(CLSID_FileOperation,
                          NULL,
                          CLSCTX_ALL,
                          IID_PPV_ARGS(&pfo));
    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 = pfo->SetOperationFlags(FOF_NOCONFIRMATION |
                                FOF_SILENT |
                                FOFX_EARLYFAILURE |
                                FOF_NOERRORUI);
    if (FAILED(hr))
    {
        lastErrorMessage = generateErrorMsg(L"Error calling \"SetOperationFlags\".", hr);
        return false;
    }

    //create source object
    IShellItem* psiSourceFile = NULL;
    ReleaseAtExit<IShellItem> dummy2(psiSourceFile);
    hr = SHCreateItemFromParsingName(sourceFile,
                                     NULL,
                                     IID_PPV_ARGS(&psiSourceFile));
    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
    IShellItem* psiTargetFolder = NULL;
    ReleaseAtExit<IShellItem> dummy3(psiTargetFolder);
	hr = SHCreateItemFromParsingName(targetFolder.c_str(),
                                     NULL,
                                     IID_PPV_ARGS(&psiTargetFolder));
    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 = pfo->CopyItem(psiSourceFile, psiTargetFolder, targetFileNameShort.c_str(), NULL);
    if (FAILED(hr))
    {
        lastErrorMessage = generateErrorMsg(L"Error calling \"CopyItem\".", hr);
        return false;
    }

    //perform actual operations
    hr = pfo->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 = pfo->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