ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Spectre/Mounter.cpp
(Generate patch)

Comparing trunk/Spectre/Mounter.cpp (file contents):
Revision 148 by douglas, 2003-06-08T16:55:49-07:00 vs.
Revision 228 by douglas, 2003-07-30T21:14:12-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines