trunk 20080912
[gitenigma.git] / src / enigma_dyn_mount.cpp
blob36f6ae14598ff46548730d4a6ff49add26a20690
1 /*
2 * $Id: enigma_dyn_mount.cpp,v 1.30 2005/10/12 20:46:27 digi_casi Exp $
4 * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifdef ENABLE_EXPERT_WEBIF
23 #include <map>
24 #include <time.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <dirent.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <net/if.h>
35 #include <arpa/inet.h>
36 #include <linux/if_ether.h>
37 #include <iomanip>
38 #include <iostream>
39 #include <fstream>
40 #include <lib/system/http_dyn.h>
41 #include <lib/system/econfig.h>
42 #include <enigma_dyn.h>
43 #include <enigma_dyn_utils.h>
44 #include <enigma_dyn_mount.h>
45 #include <enigma_main.h>
46 #include <enigma_mount.h>
48 using namespace std;
50 eString getConfigMountMgr(void)
52 eString result = readFile(TEMPLATE_DIR + "mountPoints.tmp");
53 result.strReplace("#ADDMOUNTPOINTBUTTON#", button(100, "Add", GREEN, "javascript:addMountPoint()", "#FFFFFF"));
54 eString skelleton = readFile(TEMPLATE_DIR + "mountPoint.tmp");
55 result.strReplace("#BODY#", eMountMgr::getInstance()->listMountPoints(skelleton));
56 return result;
59 static eString addChangeMountPoint(eString request, eString dirpath, eString opts, eHTTPConnection *content)
61 t_mount mp;
63 std::map<eString,eString> opt = getRequestOptions(opts, '&');
64 eString action = opt["action"];
65 eString id = opt["id"];
67 mp.localDir = opt["localdir"];
68 mp.fstype = atoi(opt["fstype"].c_str());
69 mp.password = opt["password"];
70 mp.userName = opt["username"];
71 mp.mountDir = opt["mountdir"];
72 mp.automount = (opt["automount"] == "on") ? 1 : 0;
73 eString options = opt["options"];
74 mp.ip[0] = atoi(opt["ip0"].c_str());
75 mp.ip[1] = atoi(opt["ip1"].c_str());
76 mp.ip[2] = atoi(opt["ip2"].c_str());
77 mp.ip[3] = atoi(opt["ip3"].c_str());
78 mp.mounted = false;
79 eString async = opt["async"];
80 eString sync = opt["sync"];
81 eString atime = opt["atime"];
82 eString autom = opt["autom"];
83 eString execm = opt["execm"];
84 eString noexec = opt["noexec"];
85 eString ro = opt["ro"];
86 eString rw = opt["rw"];
87 eString users = opt["users"];
88 eString nolock = opt["nolock"];
89 eString intr = opt["intr"];
90 eString soft = opt["soft"];
91 eString udp = opt["udp"];
92 mp.description = opt["description"];
94 if (async == "on")
95 mp.options += "async,";
96 if (sync == "on")
97 mp.options += "sync,";
98 if (atime == "on")
99 mp.options += "atime,";
100 if (autom == "on")
101 mp.options += "autom,";
102 if (execm == "on")
103 mp.options += "execm,";
104 if (noexec == "on")
105 mp.options += "noexec,";
106 if (ro == "on")
107 mp.options += "ro,";
108 if (rw == "on")
109 mp.options += "rw,";
110 if (users == "on")
111 mp.options += "users,";
112 if (nolock == "on")
113 mp.options += "nolock,";
114 if (intr == "on")
115 mp.options += "intr,";
116 if (soft == "on")
117 mp.options += "soft,";
118 if (udp == "on")
119 mp.options += "udp,";
120 if (mp.options.length() > 0)
121 mp.options = mp.options.substr(0, mp.options.length() - 1); //remove last comma
122 mp.options += (options) ? ("," + options) : "";
124 eString result;
125 if (action == "change")
127 eMountMgr::getInstance()->changeMountPoint(atoi(id.c_str()), mp);
128 result = "<html><body onUnload=\"parent.window.opener.location.reload(true)\"><script>window.close();</script></body></html>";
130 else
132 eMountMgr::getInstance()->addMountPoint(mp);
133 result = "<html><body onUnload=\"parent.window.opener.location.reload(true)\"><script>window.close();</script></body></html>";
136 content->local_header["Content-Type"]="text/html; charset=utf-8";
137 return result;
140 static eString removeMountPoint(eString request, eString dirpath, eString opts, eHTTPConnection *content)
142 std::map<eString,eString> opt = getRequestOptions(opts, '&');
143 eString id = opt["id"];
145 content->local_header["Content-Type"]="text/html; charset=utf-8";
146 eMountMgr::getInstance()->removeMountPoint(atoi(id.c_str()));
148 return "<html><body onUnload=\"parent.window.opener.location.reload(true)\">Mount point deleted successfully.</body></html>";
151 static eString mountPointWindow(eString request, eString dirpath, eString opts, eHTTPConnection *content)
153 eString async, sync, atime, autom, execm, noexec, ro, rw, users, nolock, intr, soft, udp;
154 t_mount mp;
155 eString cmd;
157 std::map<eString,eString> opt = getRequestOptions(opts, '&');
158 eString action = opt["action"];
159 eString id = opt["id"];
161 eString result = readFile(TEMPLATE_DIR + "mountPointWindow.tmp");
162 if (action == "change")
164 result.strReplace("#TITLE#", "Change Mount Point");
165 mp = eMountMgr::getInstance()->getMountPointData(atoi(id.c_str()));
166 cmd = "change";
168 else
170 result.strReplace("#TITLE#", "Add Mount Point");
171 mp.options = "nolock,intr,soft,udp,rsize=4096,wsize=4096";
172 mp.ip[0] = 0;
173 mp.ip[1] = 0;
174 mp.ip[2] = 0;
175 mp.ip[3] = 0;
176 mp.fstype = 0;
177 mp.automount = 0;
178 cmd = "add";
181 result.strReplace("#ACTION#", "/control/addChangeMountPoint");
182 result.strReplace("#CMD#", cmd);
184 unsigned int pos = 0;
185 eString options, option;
186 while (mp.options.length() > 0)
188 if ((pos = mp.options.find(",")) != eString::npos)
190 option = mp.options.substr(0, pos);
191 mp.options = mp.options.substr(pos + 1);
193 else
195 option = mp.options;
196 mp.options = "";
199 if (option == "async")
200 async = "checked";
201 else
202 if (option == "sync")
203 sync = "checked";
204 else
205 if (option == "atime")
206 atime = "checked";
207 else
208 if (option == "autom")
209 autom = "checked";
210 else
211 if (option == "execm")
212 execm = "checked";
213 else
214 if (option == "noexec")
215 noexec = "checked";
216 else
217 if (option == "ro")
218 ro = "checked";
219 else
220 if (option == "rw")
221 rw = "checked";
222 else
223 if (option == "users")
224 users = "checked";
225 else
226 if (option == "nolock")
227 nolock = "checked";
228 else
229 if (option == "intr")
230 intr = "checked";
231 else
232 if (option == "soft")
233 soft = "checked";
234 else
235 if (option == "udp")
236 udp = "checked";
237 else
238 options += (options) ? ("," + option) : option;
241 result.strReplace("#ID#", eString().sprintf("%d", mp.id));
242 result.strReplace("#LDIR#", mp.localDir);
243 result.strReplace("#FSTYPE#", eString().sprintf("%d", mp.fstype));
244 result.strReplace("#PW#", mp.password);
245 result.strReplace("#USER#", mp.userName);
246 result.strReplace("#MDIR#", mp.mountDir);
247 result.strReplace("#AUTO#", (mp.automount == 1) ? "checked" : "");
248 result.strReplace("#OPTIONS#", options);
249 result.strReplace("#IP0#", eString().sprintf("%d", mp.ip[0]));
250 result.strReplace("#IP1#", eString().sprintf("%d", mp.ip[1]));
251 result.strReplace("#IP2#", eString().sprintf("%d", mp.ip[2]));
252 result.strReplace("#IP3#", eString().sprintf("%d", mp.ip[3]));
253 result.strReplace("#ASYNC#", async);
254 result.strReplace("#SYNC#", sync);
255 result.strReplace("#ATIME#", atime);
256 result.strReplace("#AUTOM#", autom);
257 result.strReplace("#EXECM#", execm);
258 result.strReplace("#NOEXEC#", noexec);
259 result.strReplace("#RO#", ro);
260 result.strReplace("#RW#", rw);
261 result.strReplace("#USERS#", users);
262 result.strReplace("#NOLOCK#", nolock);
263 result.strReplace("#INTR#", intr);
264 result.strReplace("#SOFT#", soft);
265 result.strReplace("#UDP#", udp);
266 result.strReplace("#DESCRIPTION#", mp.description);
268 content->local_header["Content-Type"]="text/html; charset=utf-8";
270 return result;
273 static eString mountMountPoint(eString request, eString dirpath, eString opts, eHTTPConnection *content)
275 eString result;
277 std::map<eString,eString> opt = getRequestOptions(opts, '&');
278 eString id = opt["id"];
280 content->local_header["Content-Type"]="text/html; charset=utf-8";
281 int rc = eMountMgr::getInstance()->mountMountPoint(atoi(id.c_str()));
283 switch(rc)
285 case -1:
286 result = "Mountpoint is already mounted.";
287 break;
288 case -2:
289 result = "Local directory is already used as mount point.";
290 break;
291 case -3:
292 result = "CIFS is not supported.";
293 break;
294 case -4:
295 result = "NFS is not supported.";
296 break;
297 case -5:
298 result = "Mount failed (timeout).";
299 break;
300 case -10:
301 result = "Unable to create mount directory.";
302 break;
303 default:
304 result = "Mount point mounted successfully.";
305 break;
307 eDebug("[ENIGMA_DYN_MOUNT] mount: rc = %d (%s)", rc, result.c_str());
309 return closeWindow(content, "", 500);
312 static eString unmountMountPoint(eString request, eString dirpath, eString opts, eHTTPConnection *content)
314 eString result;
316 std::map<eString,eString> opt = getRequestOptions(opts, '&');
317 eString id = opt["id"];
319 content->local_header["Content-Type"]="text/html; charset=utf-8";
320 int rc = eMountMgr::getInstance()->unmountMountPoint(atoi(id.c_str()));
322 if (rc == 0)
323 result = "Mount point unmounted successfully.";
324 else
325 result = "Mount point unmount failed.";
327 eDebug("[ENIGMA_DYN_MOUNT] unmount: rc = %d (%s)", rc, result.c_str());
329 return closeWindow(content, "", 500);
332 extern bool rec_movies();
334 static eString selectMovieSource(eString request, eString dirpath, eString opts, eHTTPConnection *content)
336 eString result;
338 std::map<eString,eString> opt = getRequestOptions(opts, '&');
339 eString id = opt["id"];
341 content->local_header["Content-Type"]="text/html; charset=utf-8";
343 eZapMain::getInstance()->saveRecordings();
344 eMountMgr::getInstance()->selectMovieSource(atoi(id.c_str()));
345 if (access("/hdd/movie/recordings.epl", R_OK) == 0)
347 eDebug("[ENIGMA_DYN_MOUNT] recordings.epl available");
348 eZapMain::getInstance()->loadRecordings();
350 else
352 eDebug("[ENIGMA_DYN_MOUNT] recordings.epl not available, recovering...");
353 rec_movies();
356 eDebug("[ENIGMA_DYN_MOUNT] selectMovieSource.");
358 return closeWindow(content, "", 500);
361 void ezapMountInitializeDyn(eHTTPDynPathResolver *dyn_resolver, bool lockWeb)
363 dyn_resolver->addDyn("GET", "/control/addChangeMountPoint", addChangeMountPoint, lockWeb);
364 dyn_resolver->addDyn("GET", "/control/removeMountPoint", removeMountPoint, lockWeb);
365 dyn_resolver->addDyn("GET", "/control/mountPointWindow", mountPointWindow, lockWeb);
366 dyn_resolver->addDyn("GET", "/control/mountMountPoint", mountMountPoint, lockWeb);
367 dyn_resolver->addDyn("GET", "/control/unmountMountPoint", unmountMountPoint, lockWeb);
368 dyn_resolver->addDyn("GET", "/control/selectMovieSource", selectMovieSource, lockWeb);
371 #endif