ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Spectre/Spectre.cpp
Revision: 202
Committed: 2003-07-15T17:25:42-07:00 (21 years, 11 months ago) by douglas
File size: 7928 byte(s)
Log Message:
Fixed automount.

File Contents

# User Rev Content
1 douglas 152 // Spectre Samba Mounter
2     //
3     // Douglas Thrift
4     //
5     // Spectre.cpp
6    
7     #include "Spectre.h"
8     #include "Maker.h"
9     #include "Mounter.h"
10    
11 douglas 153 string program;
12     string programName = "Spectre Samba Mounter";
13 douglas 176 string programVersion = "1.0beta";
14 douglas 153 bool debug = false;
15    
16 douglas 154 Config config;
17 douglas 153
18 douglas 176 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 douglas 152 int main(int argc, char* argv[])
25     {
26 douglas 176 vector<string> args(argc);
27 douglas 154
28 douglas 176 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 douglas 154 #include "configure.h"
41 douglas 155
42     bool make = false;
43     bool mount = false;
44 douglas 176 bool automake = false;
45     bool automount = false;
46 douglas 155
47     set<string> makes;
48     set<string> mounts;
49 douglas 153
50 douglas 176 for (int index = 1; index < args.size(); index++)
51 douglas 153 {
52 douglas 176 if (args[index] == "-help")
53 douglas 153 {
54 douglas 155 usage();
55 douglas 153 return 0;
56     }
57 douglas 176 else if (args[index] == "-version")
58 douglas 153 {
59 douglas 155 version();
60 douglas 153 return 0;
61     }
62 douglas 176 else if (args[index] == "-make")
63 douglas 153 {
64 douglas 155 if (!make) make = true;
65    
66 douglas 176 if (++index < args.size())
67 douglas 155 {
68 douglas 176 makes.insert(args[index]);
69 douglas 155 }
70     else
71     {
72     cerr << program << ": Bad arguments\n";
73     usage();
74     return 1;
75     }
76 douglas 153 }
77 douglas 176 else if (args[index] == "-mount")
78 douglas 153 {
79 douglas 155 if (!mount) mount = true;
80    
81 douglas 176 if (++index < args.size())
82 douglas 155 {
83 douglas 176 mounts.insert(args[index]);
84 douglas 155 }
85     else
86     {
87     cerr << program << ": Bad arguments\n";
88     usage();
89     return 1;
90     }
91 douglas 153 }
92 douglas 176 else if (args[index] == "-automake")
93 douglas 153 {
94 douglas 176 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 douglas 155 if (!debug)
103     {
104     debug = true;
105     cerr.setf(ios_base::boolalpha);
106     }
107 douglas 153 }
108     }
109    
110 douglas 176 if (!make && !mount && !automake && !automount)
111 douglas 155 {
112     usage();
113     return 0;
114     }
115 douglas 152
116 douglas 155 if (debug)
117     {
118     cerr << "make = " << make << "\n"
119     << (make ? "makes = {\n" : "");
120    
121     for (set<string>::iterator itor = makes.begin(); itor != makes.end();
122     itor++)
123     {
124     cerr << " " << *itor << "\n";
125     }
126    
127     cerr << (make ? "}\n" : "")
128     << "mount = " << mount << "\n"
129     << (mount ? "mounts = {\n" : "");
130    
131     for (set<string>::iterator itor = mounts.begin(); itor != mounts.end();
132     itor++)
133     {
134     cerr << " " << *itor << "\n";
135     }
136    
137     cerr << (mount ? "}\n" : "")
138 douglas 176 << "automake = " << automake << "\n"
139     << "automount = " << automount << "\n"
140 douglas 155 << "config.install = " << config.install << "\n";
141     }
142    
143 douglas 159 configure();
144 douglas 155
145     if (debug)
146     {
147 douglas 176 cerr << "config.findsmb = " << config.findsmb << "\n"
148     << "config.smbclient = " << config.smbclient << "\n"
149 douglas 159 << "config.mount = " << config.mount << "\n"
150     << "config.root = " << config.root << "\n"
151 douglas 155 << (!config.hosts.empty() ? "config.hosts = {\n" : "");
152    
153     for (multimap<string, string>::iterator itor = config.hosts.begin();
154     itor != config.hosts.end(); itor++)
155     {
156     cerr << " " << itor->first << " = " << itor->second << "\n";
157     }
158    
159     cerr << (!config.hosts.empty() ? "}\n" : "");
160     }
161    
162 douglas 176 if (automake) ::automake(makes);
163    
164     if (make || automake)
165 douglas 160 {
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 douglas 176 if (automount) ::automount(mounts);
176    
177     if (mount || automount)
178 douglas 160 {
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 douglas 152 return 0;
189     }
190 douglas 155
191     string platform()
192     {
193     utsname* computer = new utsname;
194     uname(computer);
195    
196     string os = computer->sysname;
197     string version = computer->release;
198     string architecture = computer->machine;
199    
200     delete computer;
201    
202     string platform = "(" + os + " " + version + " " + architecture + ")";
203    
204     return platform;
205     }
206    
207     void usage()
208     {
209 douglas 176 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 douglas 155 << "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 douglas 176 << " -automake Automagically make the mount tree\n"
218     << " -automount Automagically mount shares to the tree\n"
219 douglas 155 << " -D Display debug information\n"
220     << " -version Display version information and exit\n"
221     << " -help Display this message and exit\n";
222     }
223    
224     void version()
225     {
226     cout << programName << " " << programVersion << " "<< platform() << "\n\n"
227     << " Copyright (C) 2003, Douglas Thrift. All Rights Reserved.\n\n"
228     << " This product includes software developed by Douglas Thrift\n"
229     << " (http://computers.douglasthrift.net/).\n";
230     }
231 douglas 159
232 douglas 176 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     for (unsigned index = 0; index < 5; index++)
243     {
244     string line;
245    
246     getline(findsmb, line);
247    
248     if (debug) cerr << line << "\n";
249     }
250    
251     automake(makes, findsmb);
252    
253     if (debug) cerr << "}\n";
254    
255     findsmb.close();
256    
257     if (debug)
258     {
259     cerr << "makes = {\n";
260    
261     for (set<string>::iterator itor = makes.begin(); itor != makes.end();
262     itor++)
263     {
264     cerr << " " << *itor << "\n";
265     }
266    
267     cerr << "}\n";
268     }
269     }
270    
271     void automake(set<string>& makes, ipstream& pin)
272     {
273     while (pin.good())
274     {
275     if (!isdigit(pin.peek())) break;
276    
277     char ip[17];
278     char host[16];
279     string info;
280    
281     pin.get(ip, 17);
282     pin.ignore();
283     pin.get(host, 16);
284     getline(pin, info);
285    
286     if (debug) cerr << ip << "\t" << host << info << "\n";
287    
288     strip(host);
289    
290     makes.insert(tolower(host));
291     }
292    
293     string line;
294    
295     getline(pin, line);
296    
297     if (debug) cerr << line << "\n";
298    
299     if (line != "")
300     {
301     cerr << program << ": Unknown error\n";
302     }
303     }
304    
305     void automount(set<string>& mounts)
306     {
307 douglas 202 vector<string> args;
308    
309     args.push_back("spectre");
310    
311     ipstream findsmb(config.findsmb, args);
312    
313     if (debug) cerr << "findsmb = {\n";
314    
315     for (unsigned index = 0; index < 5; index++)
316     {
317     string line;
318    
319     getline(findsmb, line);
320    
321     if (debug) cerr << line << "\n";
322     }
323    
324     set<string> hosts;
325    
326     automake(hosts, findsmb);
327    
328     if (debug) cerr << "}\n";
329    
330     findsmb.close();
331    
332 douglas 177 DIR* dir = opendir(config.root.c_str());
333     dirent* file;
334    
335     while((file = readdir(dir)) != NULL)
336     {
337     struct stat dir;
338     string folder = config.root + "/" + file->d_name;
339    
340     stat(folder.c_str(), &dir);
341    
342     if (S_ISDIR(dir.st_mode))
343     {
344     string folder = file->d_name;
345    
346 douglas 202 if (folder == "." || folder == ".." || hosts.find(folder) ==
347     hosts.end()) continue;
348 douglas 177
349     mounts.insert(folder);
350     }
351     }
352    
353     closedir(dir);
354    
355     if (debug)
356     {
357     cerr << "mounts = {\n";
358    
359     for (set<string>::iterator itor = mounts.begin(); itor != mounts.end();
360     itor++)
361     {
362     cerr << " " << *itor << "\n";
363     }
364    
365     cerr << "}\n";
366     }
367 douglas 176 }
368    
369 douglas 159 void configure()
370     {
371     string conf = config.install + "/conf/spectre.conf";
372    
373     ifstream fin(conf.c_str());
374    
375     if (!fin.is_open())
376     {
377     cerr << program << ": Could not open " << conf << "\n";
378     exit(1);
379     }
380    
381     do
382     {
383 douglas 160 string line;
384    
385     getline(fin, line);
386    
387     if (line.find('#') == 0 || line == "")
388     {
389     // ignore
390     }
391 douglas 176 else if (line.find("findsmb=") == 0)
392     {
393     config.findsmb = line.substr(8);
394     }
395 douglas 160 else if (line.find("smbclient=") == 0)
396     {
397     config.smbclient = line.substr(10);
398     }
399     else if (line.find("mount=") == 0)
400     {
401     config.mount = line.substr(6);
402     }
403     else if (line.find("root=") == 0)
404     {
405     config.root = line.substr(5);
406     }
407     else if (line.find("host=") == 0)
408     {
409     string host = line.substr(5);
410    
411     getline(fin, line);
412    
413     if (line != "{")
414     {
415     cerr << program << ": Munged configuration for " << host
416     << "\n";
417     exit(1);
418     }
419    
420     do
421     {
422     getline(fin, line);
423    
424     if (line.find("\t#") == 0)
425     {
426     // ignore
427     }
428     else if (line.find('\t') == 0)
429     {
430     config.hosts.insert(pair<string, string>(host,
431     line.substr(1)));
432     }
433     else if (line == "}")
434     {
435     break;
436     }
437     }
438     while (fin.good());
439     }
440 douglas 159 }
441     while (fin.good());
442    
443     fin.close();
444     }