ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Spectre/Mounter.cpp
Revision: 173
Committed: 2003-06-28T21:35:25-07:00 (22 years ago) by douglas
File size: 3854 byte(s)
Log Message:
Fixed hidden mounting.

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 173 string path = config.root + "/" + host + "/" + (folder[folder.length() -
128     1] == '$' ? "." + folder.substr(0, folder.length() - 1) : folder);
129 douglas 172
130     if (mounted.find(path) != mounted.end()) return;
131    
132     ostringstream options;
133    
134     struct passwd* item = getpwnam(user.c_str());
135    
136     if (item == NULL)
137     {
138     cerr << program << ": " << user << ": Unknown user\n";
139     return;
140     }
141    
142     #if defined _Linux_
143     char credentials[17];
144    
145     sprintf(credentials, "%s", "/tmp/spectre.XXXX");
146     mkstemp(credentials);
147    
148     ofstream fout(credentials);
149    
150     fout << "username = " << owner << "\n"
151     << "password = " << password(owner) << "\n";
152    
153     fout.close();
154    
155     options << "credentials=" << credentials << ","
156     << "uid=" << item->pw_uid << ","
157     << "gid=" << item->pw_gid;
158    
159     string smb = "//" + host + "/" + folder;
160     #elif defined _FreeBSD_
161     char* credentials = "~/.nsmbrc";
162    
163     int dot = open(credentials, O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
164     close(dot);
165    
166     ofstream fout(credentials);
167    
168     fout << "[default]\n"
169     << "password=" << password(owner) << "\n";
170    
171     fout.close();
172    
173     options << "-N,"
174     << "-u=" << item->pw_uid << ","
175     << "-g=" << item->pw_gid;
176    
177     string smb = "//" + owner + "@" + host + "/" + folder;
178     #endif
179    
180     vector<string> args;
181    
182     args.push_back("spectre");
183     #ifdef _FreeBSD_
184     args.push_back("-o");
185     args.push_back(options.str());
186     #endif
187     args.push_back("-t");
188     args.push_back("smbfs");
189     #ifdef _Linux_
190     args.push_back("-o");
191     args.push_back(options.str());
192     #endif
193     args.push_back(smb);
194     args.push_back(path);
195    
196     ipstream mount(config.mount, args, pstreambuf::pstderr);
197    
198     while (mount.good())
199     {
200     string line;
201    
202     getline(mount, line);
203    
204     if (line != "") cerr << line << "\n";
205     }
206    
207     mount.close();
208    
209     unlink(credentials);
210 douglas 171 }