summaryrefslogtreecommitdiff
path: root/lib/IFileOperation/file_op.cpp
blob: 8024ab91489e4bd79d558553db7e6b14b060b056 (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
// **************************************************************************
// * 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) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved    *
// **************************************************************************

#include "file_op.h"
#include <algorithm>
#include <string>
#define WIN32_LEAN_AND_MEAN
#include <zen/com_ptr.h>
#include <zen/com_error.h>

#include <shellapi.h> //shell constants such as FO_* values
#include <shobjidl.h>

using namespace zen;


namespace
{
void moveToRecycleBin(const wchar_t* fileNames[],    //throw ComError
                      size_t         fileNo) //size of fileNames array
{
    ComPtr<IFileOperation> fileOp;
    ZEN_CHECK_COM(::CoCreateInstance(CLSID_FileOperation, //throw ComError
                                     NULL,
                                     CLSCTX_ALL,
                                     IID_PPV_ARGS(fileOp.init())));

    // Set the operation flags.  Turn off  all UI
    // from being shown to the user during the
    // operation.  This includes error, confirmation
    // and progress dialogs.
    ZEN_CHECK_COM(fileOp->SetOperationFlags(FOF_ALLOWUNDO | //throw ComError
                                            FOF_NOCONFIRMATION |
                                            FOF_SILENT         |
                                            FOFX_EARLYFAILURE  |
                                            FOF_NOERRORUI));

    int operationCount = 0;

    for (size_t i = 0; i < fileNo; ++i)
    {
        //create file/folder item object
        ComPtr<IShellItem> psiFile;
        HRESULT hr = ::SHCreateItemFromParsingName(fileNames[i],
                                                   NULL,
                                                   IID_PPV_ARGS(psiFile.init()));
        if (FAILED(hr))
        {
            if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || //file not existing anymore
                hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
                continue;
            throw ComError(std::wstring(L"Error calling \"SHCreateItemFromParsingName\" for file:\n") + L"\"" + fileNames[i] + L"\".", hr);
        }

        ZEN_CHECK_COM(fileOp->DeleteItem(psiFile.get(), NULL));

        ++operationCount;
    }

    if (operationCount == 0) //calling PerformOperations() without anything to do would result in E_UNEXPECTED
        return;

    //perform actual operations
    ZEN_CHECK_COM(fileOp->PerformOperations());

    //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors!
    BOOL pfAnyOperationsAborted = FALSE;
    ZEN_CHECK_COM(fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted));

    if (pfAnyOperationsAborted == TRUE)
        throw ComError(L"Operation did not complete successfully.");
}


void copyFile(const wchar_t* sourceFile, //throw ComError
              const wchar_t* targetFile)
{
    ComPtr<IFileOperation> fileOp;
    ZEN_CHECK_COM(::CoCreateInstance(CLSID_FileOperation, //throw ComError
                                     NULL,
                                     CLSCTX_ALL,
                                     IID_PPV_ARGS(fileOp.init())));

    // Set the operation flags.  Turn off  all UI
    // from being shown to the user during the
    // operation.  This includes error, confirmation
    // and progress dialogs.
    ZEN_CHECK_COM(fileOp->SetOperationFlags(FOF_NOCONFIRMATION | //throw ComError
                                            FOF_SILENT         |
                                            FOFX_EARLYFAILURE  |
                                            FOF_NOERRORUI));
    //create source object
    ComPtr<IShellItem> psiSourceFile;
    {
        HRESULT hr = ::SHCreateItemFromParsingName(sourceFile,
                                                   NULL,
                                                   IID_PPV_ARGS(psiSourceFile.init()));
        if (FAILED(hr))
            throw ComError(std::wstring(L"Error calling \"SHCreateItemFromParsingName\" for file:\n") + L"\"" + sourceFile + L"\".", hr);
    }

    const size_t pos = std::wstring(targetFile).find_last_of(L'\\');
    if (pos == std::wstring::npos)
        throw ComError(L"Target filename does not contain a path separator.");

    const std::wstring targetFolder(targetFile, pos);
    const std::wstring targetFileNameShort = targetFile + pos + 1;

    //create target folder object
    ComPtr<IShellItem> psiTargetFolder;
    {
        HRESULT hr = ::SHCreateItemFromParsingName(targetFolder.c_str(),
                                                   NULL,
                                                   IID_PPV_ARGS(psiTargetFolder.init()));
        if (FAILED(hr))
            throw ComError(std::wstring(L"Error calling \"SHCreateItemFromParsingName\" for folder:\n") + L"\"" + targetFolder + L"\".", hr);
    }

    //schedule file copy operation
    ZEN_CHECK_COM(fileOp->CopyItem(psiSourceFile.get(), psiTargetFolder.get(), targetFileNameShort.c_str(), NULL));

    //perform actual operations
    ZEN_CHECK_COM(fileOp->PerformOperations());

    //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors!
    BOOL pfAnyOperationsAborted = FALSE;
    ZEN_CHECK_COM(fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted));

    if (pfAnyOperationsAborted == TRUE)
        throw ComError(L"Operation did not complete successfully.");
}


inline
void copyString(const std::wstring& input, wchar_t* buffer, size_t bufferSize)
{
    if (bufferSize > 0)
    {
        //size_t endPos = input.copy(buffer, bufferSize - 1);
        //buffer[endPos] = 0;
        const size_t maxSize = std::min(input.length(), bufferSize - 1);
        std::copy(input.begin(), input.begin() + maxSize, buffer);
        buffer[maxSize] = 0;
    }
}

std::wstring lastErrorMessage; //this should really be thread-local!!!
}


bool fileop::moveToRecycleBin(const wchar_t* fileNames[],
                              size_t         fileNo) //size of fileNames array
{
    try
    {
        ::moveToRecycleBin(fileNames, fileNo); //throw ComError
        return true;
    }
    catch (const zen::ComError& e)
    {
        lastErrorMessage = e.toString();
        return false;
    }
}


bool fileop::copyFile(const wchar_t* sourceFile,
                      const wchar_t* targetFile)
{
    try
    {
        ::copyFile(sourceFile, targetFile); //throw ComError
        return true;
    }
    catch (const zen::ComError& e)
    {
        lastErrorMessage = e.toString();
        return false;
    }
}


//if any of the functions above returns 'false', this message returns last error
void fileop::getLastError(wchar_t* errorMessage, size_t errorBufferLen)
{
    copyString(lastErrorMessage, errorMessage, errorBufferLen);
}
bgstack15