ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/Spectre/trunk/Mounter.cpp
Revision: 172
Committed: 2003-06-28T21:08:05-07:00 (21 years, 11 months ago) by douglas
Original Path: trunk/Spectre/Mounter.cpp
File size: 3767 byte(s)
Log Message:
Did some more work on Mounter, almost finished.

File Contents

# User Rev Content
1 douglas 161 // Spectre Samba Mounter
2     //
3     // Douglas Thrift
4     //
5     // Mounter.cpp
6    
7 douglas 172 #ifdef _FreeBSD_
8     #include <fcntl.h>
9     #endif
10    
11 douglas 161 #include "Mounter.h"
12    
13     void Mounter::mount()
14     {
15 douglas 171 samba();
16    
17 douglas 172 if (debug)
18     {
19     cerr << "mounted = {\n";
20    
21     for (set<string>::iterator itor = mounted.begin();
22     itor != mounted.end(); itor++)
23     {
24     cerr << " " << *itor << "\n";
25     }
26    
27     cerr << "}\n";
28     }
29    
30 douglas 171 if (debug) cerr << "folders = {\n";
31    
32     for (map<string, Folder>::iterator itor = folders.begin(); itor !=
33     folders.end(); itor++)
34     {
35     if (debug) cerr << " " << itor->first << " = {\n"
36     << " local = " << itor->second.local << "\n"
37     << " remote = " << itor->second.remote << "\n"
38     << " }\n";
39    
40 douglas 172 mount(itor->first, itor->second.local, itor->second.remote);
41 douglas 171 }
42    
43     if (debug) cerr << "}\n";
44 douglas 161 }
45 douglas 171
46     void Mounter::samba()
47     {
48     string path = config.root + "/" + host;
49    
50     DIR* dir = opendir(path.c_str());
51    
52     dirent* file;
53    
54     while ((file = readdir(dir)) != NULL)
55     {
56     struct stat dir;
57     string folder = path + "/" + file->d_name;
58    
59     stat(folder.c_str(), &dir);
60    
61     if (S_ISDIR(dir.st_mode))
62     {
63     string folder = file->d_name;
64    
65     if (folder == "." || folder == "..") continue;
66    
67     string share = folder.find('.') == 0 ? folder.erase(0, 1) + "$" :
68     folder;
69    
70     if (folders.find(share) == folders.end())
71     {
72     folders.insert(pair<string, Folder>(share, regular));
73     }
74     }
75     }
76    
77     closedir(dir);
78 douglas 172
79     vector<string> args;
80    
81     args.push_back("spectre");
82     args.push_back("-t");
83     args.push_back("smbfs");
84    
85     ipstream mount(config.mount, args);
86    
87     if (debug) cerr << "mount = {\n";
88    
89     samba(mount);
90    
91     if (debug) cerr << "}\n";
92    
93     mount.close();
94 douglas 171 }
95    
96     void Mounter::samba(ipstream& pin)
97     {
98 douglas 172 while (pin.good())
99     {
100     string line;
101    
102     getline(pin, line);
103    
104     if (debug) cerr << line << "\n";
105    
106     if (line == "") continue;
107    
108     unsigned begin = line.find(" on ") + 4;
109     #if defined _Linux_
110     unsigned end = line.rfind(" type smbfs");
111     #elif defined _FreeBSD_
112     unsigned end = line.rfind(" (smbfs)");
113     #endif
114    
115     string path = line.substr(begin, end - begin);
116    
117     if (mounted.find(path) == mounted.end())
118     {
119     mounted.insert(path);
120     }
121     }
122 douglas 171 }
123    
124 douglas 172 void Mounter::mount(const string& folder, const string& user, const string&
125     owner)
126 douglas 171 {
127 douglas 172 string path = config.root + "/" + host + "/" + folder;
128    
129     if (mounted.find(path) != mounted.end()) return;
130    
131     ostringstream options;
132    
133     struct passwd* item = getpwnam(user.c_str());
134    
135     if (item == NULL)
136     {
137     cerr << program << ": " << user << ": Unknown user\n";
138     return;
139     }
140    
141     #if defined _Linux_
142     char credentials[17];
143    
144     sprintf(credentials, "%s", "/tmp/spectre.XXXX");
145     mkstemp(credentials);
146    
147     ofstream fout(credentials);
148    
149     fout << "username = " << owner << "\n"
150     << "password = " << password(owner) << "\n";
151    
152     fout.close();
153    
154     options << "credentials=" << credentials << ","
155     << "uid=" << item->pw_uid << ","
156     << "gid=" << item->pw_gid;
157    
158     string smb = "//" + host + "/" + folder;
159     #elif defined _FreeBSD_
160     char* credentials = "~/.nsmbrc";
161    
162     int dot = open(credentials, O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
163     close(dot);
164    
165     ofstream fout(credentials);
166    
167     fout << "[default]\n"
168     << "password=" << password(owner) << "\n";
169    
170     fout.close();
171    
172     options << "-N,"
173     << "-u=" << item->pw_uid << ","
174     << "-g=" << item->pw_gid;
175    
176     string smb = "//" + owner + "@" + host + "/" + folder;
177     #endif
178    
179     vector<string> args;
180    
181     args.push_back("spectre");
182     #ifdef _FreeBSD_
183     args.push_back("-o");
184     args.push_back(options.str());
185     #endif
186     args.push_back("-t");
187     args.push_back("smbfs");
188     #ifdef _Linux_
189     args.push_back("-o");
190     args.push_back(options.str());
191     #endif
192     args.push_back(smb);
193     args.push_back(path);
194    
195     ipstream mount(config.mount, args, pstreambuf::pstderr);
196    
197     while (mount.good())
198     {
199     string line;
200    
201     getline(mount, line);
202    
203     if (line != "") cerr << line << "\n";
204     }
205    
206     mount.close();
207    
208     unlink(credentials);
209 douglas 171 }