-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteFilesDeleteOperation.cpp
221 lines (183 loc) · 6.34 KB
/
RemoteFilesDeleteOperation.cpp
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
// Copyright (C) 2009,2010,2011,2012 GlavSoft LLC.
// All rights reserved.
//
//-------------------------------------------------------------------------
// This file is part of the TightVNC software. Please visit our Web site:
//
// http://www.tightvnc.com/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//-------------------------------------------------------------------------
//
#include "RemoteFilesDeleteOperation.h"
RemoteFilesDeleteOperation::RemoteFilesDeleteOperation(LogWriter *logWriter,
const FileInfo *filesInfoToDelete,
size_t filesCount,
const TCHAR *pathToTargetRoot)
: FileTransferOperation(logWriter)
{
m_toDelete = new FileInfoList(filesInfoToDelete, filesCount);
m_pathToTargetRoot.setString(pathToTargetRoot);
}
RemoteFilesDeleteOperation::RemoteFilesDeleteOperation(LogWriter *logWriter,
FileInfo fileInfoToDelete,
const TCHAR *pathToTargetRoot)
: FileTransferOperation(logWriter)
{
m_toDelete = new FileInfoList(fileInfoToDelete);
m_pathToTargetRoot.setString(pathToTargetRoot);
}
RemoteFilesDeleteOperation::~RemoteFilesDeleteOperation()
{
if (m_toDelete != NULL) {
delete m_toDelete;
}
}
void RemoteFilesDeleteOperation::start()
{
// Notify listeners that operation have started
notifyStart();
// Remove first file in the list
remove(false);
}
void RemoteFilesDeleteOperation::onFileListReply(DataInputStream *input)
{
FileInfoList *current = m_toDelete;
//
// Current file is directory so, we can remove it only
// if it contain no files, and add these files to list
// and first remove them otherwise.
//
// Set child file list to current folder
m_toDelete->setChild(m_replyBuffer->getFilesInfo(),
m_replyBuffer->getFilesInfoCount());
// Get child file list
FileInfoList *child = m_toDelete->getChild();
//
// If it's not null than remove sub files.
// Forced remove folder otherwise
//
if (child != NULL) {
m_toDelete = child;
remove(false);
} else {
remove(true);
}
}
void RemoteFilesDeleteOperation::onRmReply(DataInputStream *input)
{
// Go to next file to delete it
gotoNext();
}
void RemoteFilesDeleteOperation::onLastRequestFailedReply(DataInputStream *input)
{
//
// Logging
//
StringStorage errorMessage;
m_replyBuffer->getLastErrorMessage(&errorMessage);
StringStorage remotePath;
getRemotePath(m_toDelete, m_pathToTargetRoot.getString(), &remotePath);
StringStorage message;
message.format(_T("Error: %s ('%s')"), errorMessage.getString(),
remotePath.getString());
notifyError(message.getString());
// Delete next file
gotoNext();
}
void RemoteFilesDeleteOperation::remove(bool removeIfFolder)
{
if (isTerminating()) {
killOp();
return ;
}
if (m_toDelete == NULL) {
return ;
}
FileInfo *fileInfo = m_toDelete->getFileInfo();
StringStorage remotePath;
getRemotePath(m_toDelete,
m_pathToTargetRoot.getString(),
&remotePath);
//
// Logging
//
if (((fileInfo->isDirectory() && !removeIfFolder) ||
(!fileInfo->isDirectory())) && (m_toDelete->getFirst()->getParent() == NULL)) {
StringStorage message;
message.format(_T("Deleting remote '%s' %s"), remotePath.getString(),
fileInfo->isDirectory() ? _T("folder") : _T("file"));
notifyInformation(message.getString());
}
//
// Try remove file if it's not folder or have order to forced
// file removal.
//
if (!fileInfo->isDirectory() || removeIfFolder) {
// Send request to server
m_sender->sendRmFileRequest(remotePath.getString());
//
// If file is folder and we have order to forced removal
// we must set folder child to NULL. If we dont do than
// program will try to remove already removed files next step.
//
if ((removeIfFolder) && (fileInfo->isDirectory())) {
m_toDelete->setChild(NULL, 0);
}
} else {
// Send file list request cause we must remove subfolders and files from
// folder before we can delete it
m_sender->sendFileListRequest(remotePath.getString(), m_replyBuffer->isCompressionSupported());
}
}
void RemoteFilesDeleteOperation::gotoNext()
{
FileInfoList *current = m_toDelete;
bool hasChild = current->getChild() != NULL;
bool hasNext = current->getNext() != NULL;
bool hasParent = current->getFirst()->getParent() != NULL;
if (hasChild) {
// If it has child, we must remove child file list first
m_toDelete = current->getChild();
remove(false);
} else if (hasNext) {
// If it has no child, but has next file, we must remove next file
m_toDelete = current->getNext();
remove(false);
} else {
//
// If it has no child and not next, but has parent file,
// we must go to parent file (folder i mean) and forced remove it
//
FileInfoList *first = current->getFirst();
if (hasParent) {
m_toDelete = first->getParent();
remove(true);
} else {
//
// If no files to delete, than clear memory and
// operation is finished
//
killOp();
} // if / else
} // if / else
} // void
void RemoteFilesDeleteOperation::killOp()
{
delete m_toDelete->getRoot();
m_toDelete = NULL;
// Notify listeners that operation have ended
notifyFinish();
}