-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalFilesDeleteOperation.cpp
155 lines (123 loc) · 4.52 KB
/
LocalFilesDeleteOperation.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
// 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 "LocalFilesDeleteOperation.h"
#include "file-lib/File.h"
LocalFilesDeleteOperation::LocalFilesDeleteOperation(LogWriter *logWriter,
const FileInfo *filesToDelete,
UINT32 filesCount,
const TCHAR *pathToTargetRoot)
: FileTransferOperation(logWriter)
{
m_filesToDelete.resize(filesCount);
for (UINT32 i = 0; i < filesCount; i++) {
m_filesToDelete[i] = filesToDelete[i];
}
m_pathToTargetRoot.setString(pathToTargetRoot);
}
LocalFilesDeleteOperation::~LocalFilesDeleteOperation()
{
}
void LocalFilesDeleteOperation::start()
{
// Notify listeners that operation have started
notifyStart();
// Start new thread
Thread::resume();
}
void LocalFilesDeleteOperation::terminate()
{
FileTransferOperation::terminate();
Thread::terminate();
}
//
// REMARK: This method is executed in separate thread
//
void LocalFilesDeleteOperation::execute()
{
for (UINT32 i = 0; i < m_filesToDelete.size(); i++) {
// Create path to file from root folder and filename
StringStorage pathToTargetFile(m_pathToTargetRoot);
if (!pathToTargetFile.endsWith(_T('\\'))) {
pathToTargetFile.appendString(_T("\\"));
}
pathToTargetFile.appendString(m_filesToDelete[i].getFileName());
File file(pathToTargetFile.getString());
//
// Logging
//
StringStorage message;
message.format(_T("Deleting local '%s' %s"), pathToTargetFile.getString(),
file.isDirectory() ? _T("folder") : _T("file"));
notifyInformation(message.getString());
// Delete file
deleteFile(&file);
} // for all files in file list
// Notify listeners that operation have ended
notifyFinish();
}
bool LocalFilesDeleteOperation::deleteFile(File *file)
{
if (Thread::isTerminating()) {
return false;
}
// Full path to local file
StringStorage pathToTargetFile;
file->getPath(&pathToTargetFile);
// Remove files in directory first
if (file->isDirectory()) {
UINT32 filesCount;
StringStorage *files = NULL;
if (file->list(NULL, &filesCount)) {
files = new StringStorage[filesCount];
file->list(files, NULL);
for (UINT32 i = 0; i < filesCount; i++) {
if (Thread::isTerminating()) {
return false;
}
// Create path to subfile
StringStorage pathToTargetSubFile(pathToTargetFile.getString());
pathToTargetSubFile.appendString(_T("\\"));
pathToTargetSubFile.appendString(files[i].getString());
File subFile(pathToTargetSubFile.getString());
// Delete file
deleteFile(&subFile);
} // for all subfiles
delete[] files;
} else {
StringStorage message;
message.format(_T("Error: failed to get file list in local folder '%s'"),
pathToTargetFile.getString());
notifyError(message.getString());
}
} // if file to delete is directory
bool returnVal = file->remove();
if (!returnVal) {
StringStorage message;
message.format(_T("Error: failed to remove local '%s' %s"),
pathToTargetFile.getString(),
file->isDirectory() ? _T("folder") : _T("file"));
notifyError(message.getString());
} // if failed to remove
return returnVal;
}