10 |
|
|
11 |
|
string program; |
12 |
|
string programName = "Spectre Samba Mounter"; |
13 |
< |
string programVersion = "1.0alpha"; |
13 |
> |
string programVersion = "1.0rc1"; |
14 |
|
bool debug = false; |
15 |
|
|
16 |
|
Config config; |
17 |
|
|
18 |
+ |
int spectre(vector<string>& args); |
19 |
+ |
void automake(set<string>& makes); |
20 |
+ |
void automake(set<string>& makes, ipstream& pin); |
21 |
+ |
void automount(set<string>& mounts); |
22 |
+ |
void configure(); |
23 |
+ |
|
24 |
|
int main(int argc, char* argv[]) |
25 |
|
{ |
26 |
< |
program = argv[0]; |
26 |
> |
vector<string> args(argc); |
27 |
> |
|
28 |
> |
for (int index = 0; index < argc; index++) |
29 |
> |
{ |
30 |
> |
args[index] = argv[index]; |
31 |
> |
} |
32 |
> |
|
33 |
> |
return spectre(args); |
34 |
> |
} |
35 |
> |
|
36 |
> |
int spectre(vector<string>& args) |
37 |
> |
{ |
38 |
> |
program = args[0]; |
39 |
|
|
40 |
|
#include "configure.h" |
41 |
|
|
42 |
|
bool make = false; |
43 |
|
bool mount = false; |
44 |
+ |
bool automake = false; |
45 |
+ |
bool automount = false; |
46 |
|
|
47 |
|
set<string> makes; |
48 |
|
set<string> mounts; |
49 |
|
|
50 |
< |
for (int index = 1; index < argc; index++) |
50 |
> |
for (int index = 1; index < args.size(); index++) |
51 |
|
{ |
52 |
< |
string arg = argv[index]; |
33 |
< |
|
34 |
< |
if (arg == "-help") |
52 |
> |
if (args[index] == "-help") |
53 |
|
{ |
54 |
|
usage(); |
55 |
|
return 0; |
56 |
|
} |
57 |
< |
else if (arg == "-version") |
57 |
> |
else if (args[index] == "-version") |
58 |
|
{ |
59 |
|
version(); |
60 |
|
return 0; |
61 |
|
} |
62 |
< |
else if (arg == "-make") |
62 |
> |
else if (args[index] == "-make") |
63 |
|
{ |
64 |
|
if (!make) make = true; |
65 |
|
|
66 |
< |
if (++index < argc) |
66 |
> |
if (++index < args.size()) |
67 |
|
{ |
68 |
< |
makes.insert(argv[index]); |
68 |
> |
makes.insert(args[index]); |
69 |
|
} |
70 |
|
else |
71 |
|
{ |
74 |
|
return 1; |
75 |
|
} |
76 |
|
} |
77 |
< |
else if (arg == "-mount") |
77 |
> |
else if (args[index] == "-mount") |
78 |
|
{ |
79 |
|
if (!mount) mount = true; |
80 |
|
|
81 |
< |
if (++index < argc) |
81 |
> |
if (++index < args.size()) |
82 |
|
{ |
83 |
< |
mounts.insert(argv[index]); |
83 |
> |
mounts.insert(args[index]); |
84 |
|
} |
85 |
|
else |
86 |
|
{ |
89 |
|
return 1; |
90 |
|
} |
91 |
|
} |
92 |
< |
else if (arg == "-D") |
92 |
> |
else if (args[index] == "-automake") |
93 |
> |
{ |
94 |
> |
if (!automake) automake = true; |
95 |
> |
} |
96 |
> |
else if (args[index] == "-automount") |
97 |
> |
{ |
98 |
> |
if (!automount) automount = true; |
99 |
> |
} |
100 |
> |
else if (args[index] == "-D") |
101 |
|
{ |
102 |
|
if (!debug) |
103 |
|
{ |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
< |
if (!make && !mount) |
110 |
> |
if (!make && !mount && !automake && !automount) |
111 |
|
{ |
112 |
|
usage(); |
113 |
|
return 0; |
135 |
|
} |
136 |
|
|
137 |
|
cerr << (mount ? "}\n" : "") |
138 |
+ |
<< "automake = " << automake << "\n" |
139 |
+ |
<< "automount = " << automount << "\n" |
140 |
|
<< "config.install = " << config.install << "\n"; |
141 |
|
} |
142 |
|
|
143 |
< |
// configure(); |
143 |
> |
configure(); |
144 |
|
|
145 |
|
if (debug) |
146 |
|
{ |
147 |
< |
cerr << "config.smbclient = " << config.smbclient << "\n"; |
148 |
< |
cerr << "config.mount_smbfs = " << config.mount_smbfs << "\n" |
147 |
> |
cerr << "config.findsmb = " << config.findsmb << "\n" |
148 |
> |
<< "config.smbclient = " << config.smbclient << "\n" |
149 |
> |
<< "config.mount = " << config.mount << "\n" |
150 |
> |
<< "config.root = " << config.root << "\n" |
151 |
|
<< (!config.hosts.empty() ? "config.hosts = {\n" : ""); |
152 |
|
|
153 |
|
for (multimap<string, string>::iterator itor = config.hosts.begin(); |
159 |
|
cerr << (!config.hosts.empty() ? "}\n" : ""); |
160 |
|
} |
161 |
|
|
162 |
+ |
if (automake) ::automake(makes); |
163 |
+ |
|
164 |
+ |
if (make || automake) |
165 |
+ |
{ |
166 |
+ |
for (set<string>::iterator itor = makes.begin(); itor != makes.end(); |
167 |
+ |
itor++) |
168 |
+ |
{ |
169 |
+ |
Maker maker(*itor); |
170 |
+ |
|
171 |
+ |
maker.make(); |
172 |
+ |
} |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
if (automount) ::automount(mounts); |
176 |
+ |
|
177 |
+ |
if (mount || automount) |
178 |
+ |
{ |
179 |
+ |
for (set<string>::iterator itor = mounts.begin(); itor != mounts.end(); |
180 |
+ |
itor++) |
181 |
+ |
{ |
182 |
+ |
Mounter mounter(*itor); |
183 |
+ |
|
184 |
+ |
mounter.mount(); |
185 |
+ |
} |
186 |
+ |
} |
187 |
+ |
|
188 |
|
return 0; |
189 |
|
} |
190 |
|
|
206 |
|
|
207 |
|
void usage() |
208 |
|
{ |
209 |
< |
cout << "Usage: " << program << " [-make host ...] [-mount host ...] [-D] " |
210 |
< |
<< "[-version] [-help]\n" |
209 |
> |
string tab(8 + program.length(), ' '); |
210 |
> |
|
211 |
> |
cout << "Usage: " << program << " [-make host ...] [-mount host ...]\n" |
212 |
> |
<< tab << "[-automake] [-automount]\n" |
213 |
> |
<< tab << "[-D] [-version] [-help]\n" |
214 |
|
<< "Options:\n" |
215 |
|
<< " -make host Make the mount tree for host\n" |
216 |
|
<< " -mount host Mount the shares on host to its tree\n" |
217 |
+ |
<< " -automake Automagically make the mount tree\n" |
218 |
+ |
<< " -automount Automagically mount shares to the tree\n" |
219 |
|
<< " -D Display debug information\n" |
220 |
|
<< " -version Display version information and exit\n" |
221 |
|
<< " -help Display this message and exit\n"; |
228 |
|
<< " This product includes software developed by Douglas Thrift\n" |
229 |
|
<< " (http://computers.douglasthrift.net/).\n"; |
230 |
|
} |
231 |
+ |
|
232 |
+ |
void automake(set<string>& makes) |
233 |
+ |
{ |
234 |
+ |
vector<string> args; |
235 |
+ |
|
236 |
+ |
args.push_back("spectre"); |
237 |
+ |
|
238 |
+ |
ipstream findsmb(config.findsmb, args); |
239 |
+ |
|
240 |
+ |
if (debug) cerr << "findsmb = {\n"; |
241 |
+ |
|
242 |
+ |
do |
243 |
+ |
{ |
244 |
+ |
if (isdigit(findsmb.peek())) break; |
245 |
+ |
|
246 |
+ |
string line; |
247 |
+ |
|
248 |
+ |
getline(findsmb, line); |
249 |
+ |
|
250 |
+ |
if (debug) cerr << line << "\n"; |
251 |
+ |
} |
252 |
+ |
while (findsmb.good()); |
253 |
+ |
|
254 |
+ |
automake(makes, findsmb); |
255 |
+ |
|
256 |
+ |
if (debug) cerr << "}\n"; |
257 |
+ |
|
258 |
+ |
findsmb.close(); |
259 |
+ |
|
260 |
+ |
if (debug) |
261 |
+ |
{ |
262 |
+ |
cerr << "makes = {\n"; |
263 |
+ |
|
264 |
+ |
for (set<string>::iterator itor = makes.begin(); itor != makes.end(); |
265 |
+ |
itor++) |
266 |
+ |
{ |
267 |
+ |
cerr << " " << *itor << "\n"; |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
cerr << "}\n"; |
271 |
+ |
} |
272 |
+ |
} |
273 |
+ |
|
274 |
+ |
void automake(set<string>& makes, ipstream& pin) |
275 |
+ |
{ |
276 |
+ |
while (pin.good()) |
277 |
+ |
{ |
278 |
+ |
if (!isdigit(pin.peek())) break; |
279 |
+ |
|
280 |
+ |
char ip[17]; |
281 |
+ |
string tab; |
282 |
+ |
char host[15]; |
283 |
+ |
string info; |
284 |
+ |
|
285 |
+ |
pin.get(ip, 17); |
286 |
+ |
|
287 |
+ |
while (isspace(pin.peek())) |
288 |
+ |
{ |
289 |
+ |
tab += pin.get(); |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
pin.get(host, 15); |
293 |
+ |
getline(pin, info); |
294 |
+ |
|
295 |
+ |
if (debug) cerr << ip << tab << host << info << "\n"; |
296 |
+ |
|
297 |
+ |
strip(host); |
298 |
+ |
|
299 |
+ |
makes.insert(tolower(host)); |
300 |
+ |
} |
301 |
+ |
|
302 |
+ |
string line; |
303 |
+ |
|
304 |
+ |
getline(pin, line); |
305 |
+ |
|
306 |
+ |
if (debug) cerr << line << "\n"; |
307 |
+ |
|
308 |
+ |
if (line != "") |
309 |
+ |
{ |
310 |
+ |
cerr << program << ": Unknown error\n"; |
311 |
+ |
} |
312 |
+ |
} |
313 |
+ |
|
314 |
+ |
void automount(set<string>& mounts) |
315 |
+ |
{ |
316 |
+ |
vector<string> args; |
317 |
+ |
|
318 |
+ |
args.push_back("spectre"); |
319 |
+ |
|
320 |
+ |
ipstream findsmb(config.findsmb, args); |
321 |
+ |
|
322 |
+ |
if (debug) cerr << "findsmb = {\n"; |
323 |
+ |
|
324 |
+ |
do |
325 |
+ |
{ |
326 |
+ |
if (isdigit(findsmb.peek())) break; |
327 |
+ |
|
328 |
+ |
string line; |
329 |
+ |
|
330 |
+ |
getline(findsmb, line); |
331 |
+ |
|
332 |
+ |
if (debug) cerr << line << "\n"; |
333 |
+ |
} |
334 |
+ |
while (findsmb.good()); |
335 |
+ |
|
336 |
+ |
set<string> hosts; |
337 |
+ |
|
338 |
+ |
automake(hosts, findsmb); |
339 |
+ |
|
340 |
+ |
if (debug) cerr << "}\n"; |
341 |
+ |
|
342 |
+ |
findsmb.close(); |
343 |
+ |
|
344 |
+ |
DIR* dir = opendir(config.root.c_str()); |
345 |
+ |
dirent* file; |
346 |
+ |
|
347 |
+ |
while((file = readdir(dir)) != NULL) |
348 |
+ |
{ |
349 |
+ |
struct stat dir; |
350 |
+ |
string folder = config.root + "/" + file->d_name; |
351 |
+ |
|
352 |
+ |
stat(folder.c_str(), &dir); |
353 |
+ |
|
354 |
+ |
if (S_ISDIR(dir.st_mode)) |
355 |
+ |
{ |
356 |
+ |
string folder = file->d_name; |
357 |
+ |
|
358 |
+ |
if (folder == "." || folder == ".." || hosts.find(folder) == |
359 |
+ |
hosts.end()) continue; |
360 |
+ |
|
361 |
+ |
mounts.insert(folder); |
362 |
+ |
} |
363 |
+ |
} |
364 |
+ |
|
365 |
+ |
closedir(dir); |
366 |
+ |
|
367 |
+ |
if (debug) |
368 |
+ |
{ |
369 |
+ |
cerr << "mounts = {\n"; |
370 |
+ |
|
371 |
+ |
for (set<string>::iterator itor = mounts.begin(); itor != mounts.end(); |
372 |
+ |
itor++) |
373 |
+ |
{ |
374 |
+ |
cerr << " " << *itor << "\n"; |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
cerr << "}\n"; |
378 |
+ |
} |
379 |
+ |
} |
380 |
+ |
|
381 |
+ |
void configure() |
382 |
+ |
{ |
383 |
+ |
string conf = config.install + "/conf/spectre.conf"; |
384 |
+ |
|
385 |
+ |
ifstream fin(conf.c_str()); |
386 |
+ |
|
387 |
+ |
if (!fin.is_open()) |
388 |
+ |
{ |
389 |
+ |
cerr << program << ": Could not open " << conf << "\n"; |
390 |
+ |
exit(1); |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
do |
394 |
+ |
{ |
395 |
+ |
string line; |
396 |
+ |
|
397 |
+ |
getline(fin, line); |
398 |
+ |
|
399 |
+ |
if (line.find('#') == 0 || line == "") |
400 |
+ |
{ |
401 |
+ |
// ignore |
402 |
+ |
} |
403 |
+ |
else if (line.find("findsmb=") == 0) |
404 |
+ |
{ |
405 |
+ |
config.findsmb = line.substr(8); |
406 |
+ |
} |
407 |
+ |
else if (line.find("smbclient=") == 0) |
408 |
+ |
{ |
409 |
+ |
config.smbclient = line.substr(10); |
410 |
+ |
} |
411 |
+ |
else if (line.find("mount=") == 0) |
412 |
+ |
{ |
413 |
+ |
config.mount = line.substr(6); |
414 |
+ |
} |
415 |
+ |
else if (line.find("root=") == 0) |
416 |
+ |
{ |
417 |
+ |
config.root = line.substr(5); |
418 |
+ |
} |
419 |
+ |
else if (line.find("host=") == 0) |
420 |
+ |
{ |
421 |
+ |
string host = line.substr(5); |
422 |
+ |
|
423 |
+ |
getline(fin, line); |
424 |
+ |
|
425 |
+ |
if (line != "{") |
426 |
+ |
{ |
427 |
+ |
cerr << program << ": Munged configuration for " << host |
428 |
+ |
<< "\n"; |
429 |
+ |
exit(1); |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
do |
433 |
+ |
{ |
434 |
+ |
getline(fin, line); |
435 |
+ |
|
436 |
+ |
if (line.find("\t#") == 0) |
437 |
+ |
{ |
438 |
+ |
// ignore |
439 |
+ |
} |
440 |
+ |
else if (line.find('\t') == 0) |
441 |
+ |
{ |
442 |
+ |
config.hosts.insert(pair<string, string>(host, |
443 |
+ |
line.substr(1))); |
444 |
+ |
} |
445 |
+ |
else if (line == "}") |
446 |
+ |
{ |
447 |
+ |
break; |
448 |
+ |
} |
449 |
+ |
} |
450 |
+ |
while (fin.good()); |
451 |
+ |
} |
452 |
+ |
} |
453 |
+ |
while (fin.good()); |
454 |
+ |
|
455 |
+ |
fin.close(); |
456 |
+ |
} |