2 |
|
// |
3 |
|
// Douglas Thrift |
4 |
|
// |
5 |
< |
// Spectre.cpp |
5 |
> |
// $Id: Spectre.cpp,v 1.13 2003/07/31 04:14:12 douglas Exp $ |
6 |
|
|
7 |
|
#include "Spectre.h" |
8 |
|
#include "Maker.h" |
9 |
|
#include "Mounter.h" |
10 |
+ |
#include "Unmounter.h" |
11 |
|
|
12 |
|
string program; |
13 |
|
string programName = "Spectre Samba Mounter"; |
14 |
< |
string programVersion = "1.0beta"; |
14 |
> |
string programVersion = "1.0rc1"; |
15 |
|
bool debug = false; |
16 |
|
|
17 |
|
Config config; |
20 |
|
void automake(set<string>& makes); |
21 |
|
void automake(set<string>& makes, ipstream& pin); |
22 |
|
void automount(set<string>& mounts); |
23 |
+ |
void autounmount(set<string>& unmounts); |
24 |
|
void configure(); |
25 |
|
|
26 |
|
int main(int argc, char* argv[]) |
43 |
|
|
44 |
|
bool make = false; |
45 |
|
bool mount = false; |
46 |
+ |
bool unmount = false; |
47 |
|
bool automake = false; |
48 |
|
bool automount = false; |
49 |
+ |
bool autounmount = false; |
50 |
|
|
51 |
|
set<string> makes; |
52 |
|
set<string> mounts; |
53 |
+ |
set<string> unmounts; |
54 |
|
|
55 |
|
for (int index = 1; index < args.size(); index++) |
56 |
|
{ |
94 |
|
return 1; |
95 |
|
} |
96 |
|
} |
97 |
+ |
else if (args[index] == "-unmount") |
98 |
+ |
{ |
99 |
+ |
if (!unmount) unmount = true; |
100 |
+ |
|
101 |
+ |
if (++index < args.size()) |
102 |
+ |
{ |
103 |
+ |
unmounts.insert(args[index]); |
104 |
+ |
} |
105 |
+ |
else |
106 |
+ |
{ |
107 |
+ |
cerr << program << ": Bad arguments\n"; |
108 |
+ |
usage(); |
109 |
+ |
return 1; |
110 |
+ |
} |
111 |
+ |
} |
112 |
|
else if (args[index] == "-automake") |
113 |
|
{ |
114 |
|
if (!automake) automake = true; |
117 |
|
{ |
118 |
|
if (!automount) automount = true; |
119 |
|
} |
120 |
+ |
else if (args[index] == "-autounmount") |
121 |
+ |
{ |
122 |
+ |
if (!autounmount) autounmount = true; |
123 |
+ |
} |
124 |
|
else if (args[index] == "-D") |
125 |
|
{ |
126 |
|
if (!debug) |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
< |
if (!make && !mount && !automake && !automount) |
134 |
> |
if (!make && !mount && !unmount && !automake && !automount && !autounmount) |
135 |
|
{ |
136 |
|
usage(); |
137 |
|
return 0; |
159 |
|
} |
160 |
|
|
161 |
|
cerr << (mount ? "}\n" : "") |
162 |
+ |
<< "unmount = " << unmount << "\n" |
163 |
+ |
<< (unmount ? "unmounts = {\n" : ""); |
164 |
+ |
|
165 |
+ |
for (set<string>::iterator itor = unmounts.begin(); itor != |
166 |
+ |
unmounts.end(); itor++) |
167 |
+ |
{ |
168 |
+ |
cerr << " " << *itor << "\n"; |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
cerr << (unmount ? "}\n" : "") |
172 |
|
<< "automake = " << automake << "\n" |
173 |
|
<< "automount = " << automount << "\n" |
174 |
+ |
<< "autounmount = " << autounmount << "\n" |
175 |
|
<< "config.install = " << config.install << "\n"; |
176 |
|
} |
177 |
|
|
182 |
|
cerr << "config.findsmb = " << config.findsmb << "\n" |
183 |
|
<< "config.smbclient = " << config.smbclient << "\n" |
184 |
|
<< "config.mount = " << config.mount << "\n" |
185 |
+ |
<< "config.umount = " << config.umount << "\n" |
186 |
|
<< "config.root = " << config.root << "\n" |
187 |
|
<< (!config.hosts.empty() ? "config.hosts = {\n" : ""); |
188 |
|
|
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
+ |
if (autounmount) ::autounmount(unmounts); |
225 |
+ |
|
226 |
+ |
if (unmount || autounmount) |
227 |
+ |
{ |
228 |
+ |
for (set<string>::iterator itor = unmounts.begin(); itor != |
229 |
+ |
unmounts.end(); itor++) |
230 |
+ |
{ |
231 |
+ |
Unmounter unmounter(*itor); |
232 |
+ |
|
233 |
+ |
unmounter.unmount(); |
234 |
+ |
} |
235 |
+ |
} |
236 |
+ |
|
237 |
|
return 0; |
238 |
|
} |
239 |
|
|
257 |
|
{ |
258 |
|
string tab(8 + program.length(), ' '); |
259 |
|
|
260 |
< |
cout << "Usage: " << program << " [-make host ...] [-mount host ...]\n" |
261 |
< |
<< tab << "[-automake] [-automount]\n" |
260 |
> |
cout << "Usage: " << program << " [-make host ...] [-mount host ...] [-unm" |
261 |
> |
<< "ount host ...]\n" |
262 |
> |
<< tab << "[-automake] [-automount] [-autounmount]\n" |
263 |
|
<< tab << "[-D] [-version] [-help]\n" |
264 |
|
<< "Options:\n" |
265 |
|
<< " -make host Make the mount tree for host\n" |
266 |
|
<< " -mount host Mount the shares on host to its tree\n" |
267 |
+ |
<< " -unmount host Unmount the shares on host from its tree\n" |
268 |
|
<< " -automake Automagically make the mount tree\n" |
269 |
|
<< " -automount Automagically mount shares to the tree\n" |
270 |
+ |
<< " -autounmount Automagically unmount dead shares from the tre" |
271 |
+ |
<< "e\n" |
272 |
|
<< " -D Display debug information\n" |
273 |
|
<< " -version Display version information and exit\n" |
274 |
|
<< " -help Display this message and exit\n"; |
292 |
|
|
293 |
|
if (debug) cerr << "findsmb = {\n"; |
294 |
|
|
295 |
< |
for (unsigned index = 0; index < 5; index++) |
295 |
> |
do |
296 |
|
{ |
297 |
+ |
if (isdigit(findsmb.peek())) break; |
298 |
+ |
|
299 |
|
string line; |
300 |
|
|
301 |
|
getline(findsmb, line); |
302 |
|
|
303 |
|
if (debug) cerr << line << "\n"; |
304 |
|
} |
305 |
+ |
while (findsmb.good()); |
306 |
|
|
307 |
|
automake(makes, findsmb); |
308 |
|
|
331 |
|
if (!isdigit(pin.peek())) break; |
332 |
|
|
333 |
|
char ip[17]; |
334 |
< |
char host[16]; |
334 |
> |
string tab; |
335 |
> |
char host[15]; |
336 |
|
string info; |
337 |
|
|
338 |
|
pin.get(ip, 17); |
339 |
< |
pin.ignore(); |
340 |
< |
pin.get(host, 16); |
339 |
> |
|
340 |
> |
while (isspace(pin.peek())) |
341 |
> |
{ |
342 |
> |
tab += pin.get(); |
343 |
> |
} |
344 |
> |
|
345 |
> |
pin.get(host, 15); |
346 |
|
getline(pin, info); |
347 |
|
|
348 |
< |
if (debug) cerr << ip << "\t" << host << info << "\n"; |
348 |
> |
if (debug) cerr << ip << tab << host << info << "\n"; |
349 |
|
|
350 |
|
strip(host); |
351 |
|
|
366 |
|
|
367 |
|
void automount(set<string>& mounts) |
368 |
|
{ |
369 |
+ |
vector<string> args; |
370 |
+ |
|
371 |
+ |
args.push_back("spectre"); |
372 |
+ |
|
373 |
+ |
ipstream findsmb(config.findsmb, args); |
374 |
+ |
|
375 |
+ |
if (debug) cerr << "findsmb = {\n"; |
376 |
+ |
|
377 |
+ |
do |
378 |
+ |
{ |
379 |
+ |
if (isdigit(findsmb.peek())) break; |
380 |
+ |
|
381 |
+ |
string line; |
382 |
+ |
|
383 |
+ |
getline(findsmb, line); |
384 |
+ |
|
385 |
+ |
if (debug) cerr << line << "\n"; |
386 |
+ |
} |
387 |
+ |
while (findsmb.good()); |
388 |
+ |
|
389 |
+ |
set<string> hosts; |
390 |
+ |
|
391 |
+ |
automake(hosts, findsmb); |
392 |
+ |
|
393 |
+ |
if (debug) cerr << "}\n"; |
394 |
+ |
|
395 |
+ |
findsmb.close(); |
396 |
+ |
|
397 |
|
DIR* dir = opendir(config.root.c_str()); |
398 |
|
dirent* file; |
399 |
|
|
408 |
|
{ |
409 |
|
string folder = file->d_name; |
410 |
|
|
411 |
< |
if (folder == "." || folder == "..") continue; |
411 |
> |
if (folder == "." || folder == ".." || hosts.find(folder) == |
412 |
> |
hosts.end()) continue; |
413 |
|
|
414 |
|
mounts.insert(folder); |
415 |
|
} |
431 |
|
} |
432 |
|
} |
433 |
|
|
434 |
+ |
void autounmount(set<string>& unmounts) |
435 |
+ |
{ |
436 |
+ |
vector<string> args; |
437 |
+ |
|
438 |
+ |
args.push_back("spectre"); |
439 |
+ |
|
440 |
+ |
ipstream findsmb(config.findsmb, args); |
441 |
+ |
|
442 |
+ |
if (debug) cerr << "findsmb = {\n"; |
443 |
+ |
|
444 |
+ |
do |
445 |
+ |
{ |
446 |
+ |
if (isdigit(findsmb.peek())) break; |
447 |
+ |
|
448 |
+ |
string line; |
449 |
+ |
|
450 |
+ |
getline(findsmb, line); |
451 |
+ |
|
452 |
+ |
if (debug) cerr << line << "\n"; |
453 |
+ |
} |
454 |
+ |
while (findsmb.good()); |
455 |
+ |
|
456 |
+ |
set<string> hosts; |
457 |
+ |
|
458 |
+ |
automake(hosts, findsmb); |
459 |
+ |
|
460 |
+ |
if (debug) cerr << "}\n"; |
461 |
+ |
|
462 |
+ |
findsmb.close(); |
463 |
+ |
|
464 |
+ |
for (multimap<string, string>::iterator itor = config.hosts.begin(); itor |
465 |
+ |
!= config.hosts.end(); itor = config.hosts.upper_bound(itor->first)) |
466 |
+ |
{ |
467 |
+ |
string host = itor->first; |
468 |
+ |
|
469 |
+ |
if (hosts.find(host) != hosts.end()) continue; |
470 |
+ |
|
471 |
+ |
unmounts.insert(host); |
472 |
+ |
} |
473 |
+ |
|
474 |
+ |
if (debug) |
475 |
+ |
{ |
476 |
+ |
cerr << "unmounts = {\n"; |
477 |
+ |
|
478 |
+ |
for (set<string>::iterator itor = unmounts.begin(); itor != |
479 |
+ |
unmounts.end(); itor++) |
480 |
+ |
{ |
481 |
+ |
cerr << " " << *itor << "\n"; |
482 |
+ |
} |
483 |
+ |
|
484 |
+ |
cerr << "}\n"; |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
|
488 |
|
void configure() |
489 |
|
{ |
490 |
|
string conf = config.install + "/conf/spectre.conf"; |
519 |
|
{ |
520 |
|
config.mount = line.substr(6); |
521 |
|
} |
522 |
+ |
else if (line.find("umount=") == 0) |
523 |
+ |
{ |
524 |
+ |
config.umount = line.substr(7); |
525 |
+ |
} |
526 |
|
else if (line.find("root=") == 0) |
527 |
|
{ |
528 |
|
config.root = line.substr(5); |