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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/Options.h>
#include <IceUtil/StringUtil.h>
#include <IceUtil/FileUtil.h>
#include <IcePatch2Lib/Util.h>
#include <iterator>
using namespace std;
using namespace Ice;
using namespace IcePatch2;
using namespace IcePatch2Internal;
struct FileInfoPathLess: public binary_function<const LargeFileInfo&, const LargeFileInfo&, bool>
{
bool
operator()(const LargeFileInfo& lhs, const LargeFileInfo& rhs)
{
return lhs.path < rhs.path;
}
};
struct IFileInfoPathEqual: public binary_function<const LargeFileInfo&, const LargeFileInfo&, bool>
{
bool
operator()(const LargeFileInfo& lhs, const LargeFileInfo& rhs)
{
if(lhs.path.size() != rhs.path.size())
{
return false;
}
for(string::size_type i = 0; i < lhs.path.size(); ++i)
{
if(::tolower(static_cast<unsigned char>(lhs.path[i])) != ::tolower(static_cast<unsigned char>(rhs.path[i])))
{
return false;
}
}
return true;
}
};
struct IFileInfoPathLess: public binary_function<const LargeFileInfo&, const LargeFileInfo&, bool>
{
bool
operator()(const LargeFileInfo& lhs, const LargeFileInfo& rhs)
{
for(string::size_type i = 0; i < lhs.path.size() && i < rhs.path.size(); ++i)
{
if(::tolower(static_cast<unsigned char>(lhs.path[i])) < ::tolower(static_cast<unsigned char>(rhs.path[i])))
{
return true;
}
else if(::tolower(static_cast<unsigned char>(lhs.path[i])) >
::tolower(static_cast<unsigned char>(rhs.path[i])))
{
return false;
}
}
return lhs.path.size() < rhs.path.size();
}
};
class CalcCB : public GetFileInfoSeqCB
{
public:
virtual bool
remove(const string& path)
{
cout << "removing: " << path << endl;
return true;
}
virtual bool
checksum(const string& path)
{
cout << "checksum: " << path << endl;
return true;
}
virtual bool
compress(const string& path)
{
cout << "compress: " << path << endl;
return true;
}
};
void
usage(const string& appName)
{
cerr << "Usage: " << appName << " [options] DIR [FILES...]\n";
cerr <<
"Options:\n"
"-h, --help Show this message.\n"
"-v, --version Display the Ice version.\n"
"-z, --compress Always compress files.\n"
"-Z, --no-compress Never compress files.\n"
"-i, --case-insensitive Files must not differ in case only.\n"
"-V, --verbose Verbose mode.\n"
;
}
#ifdef _WIN32
int
wmain(int argc, wchar_t* argv[])
#else
int
main(int argc, char* argv[])
#endif
{
Ice::StringSeq originalArgs = Ice::argsToStringSeq(argc, argv);
assert(originalArgs.size() > 0);
const string appName = originalArgs[0];
string dataDir;
StringSeq fileSeq;
int compress = 1;
bool verbose;
bool caseInsensitive;
IceUtilInternal::Options opts;
opts.addOpt("h", "help");
opts.addOpt("v", "version");
opts.addOpt("z", "compress");
opts.addOpt("Z", "no-compress");
opts.addOpt("V", "verbose");
opts.addOpt("i", "case-insensitive");
vector<string> args;
try
{
args = opts.parse(originalArgs);
}
catch(const IceUtilInternal::BadOptException& e)
{
cerr << e.reason << endl;
usage(appName);
return EXIT_FAILURE;
}
if(opts.isSet("help"))
{
usage(appName);
return EXIT_SUCCESS;
}
if(opts.isSet("version"))
{
cout << ICE_STRING_VERSION << endl;
return EXIT_SUCCESS;
}
bool doCompress = opts.isSet("compress");
bool dontCompress = opts.isSet("no-compress");
if(doCompress && dontCompress)
{
cerr << appName << ": only one of -z and -Z are mutually exclusive" << endl;
usage(appName);
return EXIT_FAILURE;
}
if(doCompress)
{
compress = 2;
}
else if(dontCompress)
{
compress = 0;
}
verbose = opts.isSet("verbose");
caseInsensitive = opts.isSet("case-insensitive");
if(args.empty())
{
cerr << appName << ": no data directory specified" << endl;
usage(appName);
return EXIT_FAILURE;
}
dataDir = simplify(args[0]);
for(vector<string>::size_type i = 1; i < args.size(); ++i)
{
fileSeq.push_back(simplify(args[i]));
}
try
{
string absDataDir = dataDir;
string cwd;
if(IceUtilInternal::getcwd(cwd) != 0)
{
throw "cannot get the current directory:\n" + IceUtilInternal::lastErrorToString();
}
if(!IceUtilInternal::isAbsolutePath(absDataDir))
{
absDataDir = simplify(cwd + '/' + absDataDir);
}
for(StringSeq::iterator p = fileSeq.begin(); p != fileSeq.end(); ++p)
{
if(!IceUtilInternal::isAbsolutePath(*p))
{
*p = cwd + '/' + *p;
}
}
//
// We must call simplify() here: under Cygwin, any path starting with
// a double slash simply doesn't work. But, if dataDir is "/", we end
// up with paths that start with "//" unless we call simplify().
//
string absDataDirWithSlash = simplify(absDataDir + '/');
for(StringSeq::iterator p = fileSeq.begin(); p != fileSeq.end(); ++p)
{
if(p->compare(0, absDataDirWithSlash.size(), absDataDirWithSlash) != 0)
{
throw "`" + *p + "' is not a path in `" + dataDir + "'";
}
p->erase(0, absDataDirWithSlash.size());
}
LargeFileInfoSeq infoSeq;
if(fileSeq.empty())
{
CalcCB calcCB;
if(!getFileInfoSeq(absDataDir, compress, verbose ? &calcCB : 0, infoSeq))
{
return EXIT_FAILURE;
}
}
else
{
loadFileInfoSeq(absDataDir, infoSeq);
for(StringSeq::iterator p = fileSeq.begin(); p != fileSeq.end(); ++p)
{
LargeFileInfoSeq partialInfoSeq;
CalcCB calcCB;
if(!getFileInfoSeqSubDir(absDataDir, *p, compress, verbose ? &calcCB : 0, partialInfoSeq))
{
return EXIT_FAILURE;
}
LargeFileInfoSeq newInfoSeq;
newInfoSeq.reserve(infoSeq.size());
set_difference(infoSeq.begin(),
infoSeq.end(),
partialInfoSeq.begin(),
partialInfoSeq.end(),
back_inserter(newInfoSeq),
FileInfoPathLess());
infoSeq.swap(newInfoSeq);
newInfoSeq.clear();
newInfoSeq.reserve(infoSeq.size() + partialInfoSeq.size());
set_union(infoSeq.begin(),
infoSeq.end(),
partialInfoSeq.begin(),
partialInfoSeq.end(),
back_inserter(newInfoSeq),
FileInfoPathLess());
infoSeq.swap(newInfoSeq);
}
}
if(caseInsensitive)
{
LargeFileInfoSeq newInfoSeq = infoSeq;
sort(newInfoSeq.begin(), newInfoSeq.end(), IFileInfoPathLess());
string ex;
LargeFileInfoSeq::iterator p = newInfoSeq.begin();
while((p = adjacent_find(p, newInfoSeq.end(), IFileInfoPathEqual())) != newInfoSeq.end())
{
do
{
ex += '\n' + dataDir + '/' + p->path;
++p;
}
while(p < newInfoSeq.end() && IFileInfoPathEqual()(*(p - 1), *p));
}
if(!ex.empty())
{
ex = "duplicate files:" + ex;
throw ex;
}
}
saveFileInfoSeq(absDataDir, infoSeq);
}
catch(const string& ex)
{
cerr << appName << ": " << ex << endl;
return EXIT_FAILURE;
}
catch(const char* ex)
{
cerr << appName << ": " << ex << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|