-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdu.cpp
214 lines (187 loc) · 5.83 KB
/
du.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
/***************************************************************************
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>
#include <base/string/StringOutputStream.h>
using namespace com::azure::dev::base;
class SpaceApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 1;
enum Command {
COMMAND_HELP,
COMMAND_VERSION,
COMMAND_SUM
};
Command command = COMMAND_SUM;
bool showFiles = false;
bool showFolders = false;
bool summarize = false;
uint64 blockSize = 1;
Array<String> paths;
public:
SpaceApplication()
: Application("du")
{
}
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 == "--summarize") {
summarize = true;
} else if (argument == "--bytes") {
blockSize = 1;
} else if (argument == "--kilobytes") {
blockSize = 1024;
} else if (argument == "--megabytes") {
blockSize = 1024 * 1024;
} else if (argument == "--files") {
showFiles = true;
} else if (argument == "--folders") {
showFolders = 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
<< "--bytes selects output in bytes" << EOL
<< "--kilobytes selects output in kilobytes (kb)" << EOL
<< "--megabytes selects output in megabytes (Mb)" << EOL
<< "--files show total number of files" << EOL
<< "--folders show total number of folders" << EOL
<< "--summarize only produce summary" << EOL
<< ENDL;
}
struct Sum {
uint64 bytes;
unsigned int numberOfFiles;
unsigned int numberOfFolders;
unsigned int totalNumberOfFiles;
unsigned int totalNumberOfFolders;
};
void summary(Sum sum, const String& path) {
StringOutputStream streamFiles;
streamFiles << sum.numberOfFiles << '/' << sum.totalNumberOfFiles << FLUSH;
StringOutputStream streamFolders;
streamFolders << sum.numberOfFolders << '/' << sum.totalNumberOfFolders << FLUSH;
uint64 blockSum = sum.bytes/blockSize;
fout << setWidth(10) << blockSum << ' ';
if (showFiles) {
fout << setWidth(8) << RIGHT << streamFiles.getString() << ' ';
}
if (showFolders) {
fout << setWidth(8) << RIGHT << streamFolders.getString() << ' ';
}
fout << path << ENDL;
}
Sum traverse(const String& folderPath) {
FolderInfo folder(folderPath);
Array<String> entries = folder.getEntries();
Array<String>::ReadEnumerator enu = entries.getReadEnumerator();
Sum result;
result.bytes = 0;
result.numberOfFiles = 0;
result.numberOfFolders = 0;
result.totalNumberOfFiles = 0;
result.totalNumberOfFolders = 0;
while (enu.hasNext()) {
const String entry = enu.next();
if ((entry == ".") || (entry == "..")) {
continue;
}
String path = FileSystem::getPath(folderPath, entry);
try {
unsigned int type = FileSystem::getType(path);
if (type & FileSystem::FOLDER) {
Sum temp = traverse(path);
result.bytes += temp.bytes;
result.totalNumberOfFiles += temp.totalNumberOfFiles;
result.totalNumberOfFolders += temp.totalNumberOfFolders;
result.numberOfFolders++;
result.totalNumberOfFolders++;
} else if (type & FileSystem::REGULAR) {
result.bytes += FileSystem::getSize(path);
result.numberOfFiles++;
result.totalNumberOfFiles++;
} else {
// ignore
}
} catch (FileSystemException&) {
// remember problem
}
}
if (!summarize) {
summary(result, folderPath);
}
return result;
}
void sum() {
if (paths.getSize() == 0) {
paths.append(".");
}
Array<String>::ReadEnumerator enu = paths.getReadEnumerator();
while (enu.hasNext()) {
String path = enu.next();
if (!FileSystem::folderExists(path)) {
ferr << "Error: " << "Not a folder: " << path << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
try {
traverse(path);
} 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_SUM:
sum();
break;
}
}
~SpaceApplication() {
}
};
APPLICATION_STUB(SpaceApplication);