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 176 by douglas, 2003-07-05T17:36:35-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines