-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.cpp
206 lines (180 loc) · 5.4 KB
/
rm.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
/***************************************************************************
The Base Framework
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework 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.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/string/FormatOutputStream.h>
#include <base/filesystem/FileSystem.h>
#include <base/filesystem/FolderInfo.h>
using namespace com::azure::dev::base;
class RemoveApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 1;
enum Command {
COMMAND_HELP,
COMMAND_VERSION,
COMMAND_REMOVE
};
Command command = COMMAND_REMOVE;
bool silent = false;
bool recursive = false;
bool force = false;
Array<String> paths;
public:
RemoveApplication()
: Application("rm")
{
}
void parseArguments() {
Array<String> arguments = getArguments();
Array<String>::ReadEnumerator enu = arguments.getReadEnumerator();
while (enu.hasNext()) {
String argument = enu.next();
if (argument == "--help") {
command = COMMAND_HELP;
return;
} else if (argument == "--recursive") {
recursive = true;
} else if (argument == "--force") {
force = true;
} else if (argument == "--silent") {
silent = true;
} else if (argument == "--version") {
command = COMMAND_VERSION;
return;
} else {
paths.append(argument);
}
}
}
void version()
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
}
void help()
{
version();
fout << "Usage: " << getFormalName() << " [options] path(s)" << EOL
<< EOL
<< "--help this message" << EOL
<< "--version dump the version" << EOL
<< EOL
<< "--recursive enable recursive mode" << EOL
<< "--force continue on error" << EOL
<< "--silent disable output" << EOL
<< ENDL;
}
void recursiveRemove(const String& folderPath) {
// String originalFolder = FileSystem::getCurrentFolder();
// FileSystem::setCurrentFolder(path);
FolderInfo folder(folderPath);
Array<String> entries = folder.getEntries();
Array<String>::ReadEnumerator enu = entries.getReadEnumerator();
while (enu.hasNext()) {
const String entry = enu.next();
if ((entry == ".") || (entry == "..")) {
continue;
}
try {
unsigned int type = FileSystem::getType(entry);
if (type & FileSystem::REGULAR) {
if (!silent) {
fout << "removing: " << entry << ENDL;
}
FileSystem::removeFile(entry);
} else if (type & FileSystem::FOLDER) {
recursiveRemove(entry);
} else {
if (!silent) {
ferr << "Unable to remove entry: " << entry << ENDL;
}
if (!force) {
_throw FileSystemException("Unable to remove entry.", this);
}
}
} catch (FileSystemException& e) {
if (!force) {
ferr << entry << ": " << e << ENDL;
setExitCode(EXIT_CODE_ERROR);
_rethrow;
}
}
}
if (!silent) {
fout << "removing: " << folderPath << ENDL;
}
// FileSystem::setCurrentFolder(originalFolder);
FileSystem::removeFolder(folderPath);
}
void remove() {
Array<String>::ReadEnumerator enu = paths.getReadEnumerator();
while (enu.hasNext()) {
String path = enu.next();
if (!FileSystem::entryExists(path)) {
ferr << "Error: " << "Entry does not exist: " << path << ENDL;
if (!force) {
setExitCode(EXIT_CODE_ERROR);
return;
}
continue;
}
try {
unsigned int type = FileSystem::getType(path);
if (type & FileSystem::REGULAR) {
if (!silent) {
fout << "removing: " << path << ENDL;
}
FileSystem::removeFile(path);
} else if (type & FileSystem::FOLDER) {
if (recursive) {
if (type & FileSystem::LINK) {
FileSystem::removeFolder(path);
} else {
recursiveRemove(path);
}
} else {
if (!silent) {
fout << "removing: " << path << ENDL;
}
FileSystem::removeFolder(path);
}
} else {
ferr << "Error: " << "Unable to remove entry: " << path << ENDL;
if (!force) {
setExitCode(EXIT_CODE_ERROR);
return;
}
}
} catch (FileSystemException& e) {
ferr << "Error: " << e.getMessage() << ENDL;
setExitCode(EXIT_CODE_ERROR);
}
}
}
void main() {
parseArguments();
switch (command) {
case COMMAND_HELP:
help();
break;
case COMMAND_VERSION:
version();
break;
case COMMAND_REMOVE:
remove();
break;
}
}
~RemoveApplication() {
}
};
APPLICATION_STUB(RemoveApplication);