picexif: fix buffer overflow
[enigma2.git] / lib / service / servicefs.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/object.h>
3 #include <string>
4 #include <errno.h>
5 #include <lib/service/servicefs.h>
6 #include <lib/service/service.h>
7 #include <lib/service/servicedvb.h>
8 #include <lib/base/init_num.h>
9 #include <lib/base/init.h>
10 #include <dirent.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14
15 class eStaticServiceFSInformation: public iStaticServiceInformation
16 {
17         DECLARE_REF(eStaticServiceFSInformation);
18 public:
19         RESULT getName(const eServiceReference &ref, std::string &name);
20         int getLength(const eServiceReference &ref) { return -1; }
21 };
22
23 DEFINE_REF(eStaticServiceFSInformation);
24
25 RESULT eStaticServiceFSInformation::getName(const eServiceReference &ref, std::string &name)
26 {
27         name = ref.path;
28         return 0;
29 }
30
31 // eServiceFactoryFS
32
33 eServiceFactoryFS::eServiceFactoryFS()
34 {
35         ePtr<eServiceCenter> sc;
36         
37         eServiceCenter::getPrivInstance(sc);
38         if (sc)
39         {
40                 std::list<std::string> extensions;
41                 sc->addServiceFactory(eServiceFactoryFS::id, this, extensions);
42         }
43         
44         m_service_information = new eStaticServiceFSInformation();
45 }
46
47 eServiceFactoryFS::~eServiceFactoryFS()
48 {
49         ePtr<eServiceCenter> sc;
50         
51         eServiceCenter::getPrivInstance(sc);
52         if (sc)
53                 sc->removeServiceFactory(eServiceFactoryFS::id);
54 }
55
56 DEFINE_REF(eServiceFactoryFS)
57
58         // iServiceHandler
59 RESULT eServiceFactoryFS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
60 {
61         ptr=0;
62         return -1;
63 }
64
65 RESULT eServiceFactoryFS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
66 {
67         ptr=0;
68         return -1;
69 }
70
71 RESULT eServiceFactoryFS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
72 {
73         ptr = new eServiceFS(ref.path.c_str(), ref.getName().length() ? ref.getName().c_str() : 0);
74         return 0;
75 }
76
77 RESULT eServiceFactoryFS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
78 {
79         ptr = m_service_information;
80         return 0;
81 }
82
83 RESULT eServiceFactoryFS::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
84 {
85         ptr = 0;
86         return -1;
87 }
88
89 // eServiceFS
90
91 DEFINE_REF(eServiceFS);
92
93 eServiceFS::eServiceFS(const char *path, const char *additional_extensions): path(path)
94 {
95         m_list_valid = 0;
96         if (additional_extensions)
97         {
98                 size_t slen=strlen(additional_extensions);
99                 char buf[slen+1];
100                 char *tmp=0, *cmds = buf;
101                 memcpy(buf, additional_extensions, slen+1);
102
103                 // strip spaces at beginning
104                 while(cmds[0] == ' ')
105                 {
106                         ++cmds;
107                         --slen;
108                 }
109
110                 // strip spaces at the end
111                 while(slen && cmds[slen-1] == ' ')
112                 {
113                         cmds[slen-1] = 0;
114                         --slen;
115                 }
116
117                 if (slen)
118                 {
119                         if (*cmds)
120                         {
121                                 int id;
122                                 char buf2[16];
123                                 while(1)
124                                 {
125                                         tmp = strchr(cmds, ' ');
126                                         if (tmp)
127                                                 *tmp = 0;
128                                         if (strstr(cmds, "0x"))
129                                         {
130                                                 if (sscanf(cmds, "0x%x:%s", &id, buf2) == 2)
131                                                         m_additional_extensions[id].push_back(buf2);
132                                                 else
133                                                         eDebug("parse additional_extension (%s) failed", cmds);
134                                         }
135                                         else
136                                         {
137                                                 if (sscanf(cmds, "%d:%s", &id, buf2) == 2)
138                                                         m_additional_extensions[id].push_back(buf2);
139                                                 else
140                                                         eDebug("parse additional_extension (%s) failed", cmds);
141                                         }
142                                         if (!tmp)
143                                                 break;
144                                         cmds = tmp+1;
145                                         while (*cmds && *cmds == ' ')
146                                                 ++cmds;
147                                 }
148                         }
149                 }
150         }
151 }
152
153 eServiceFS::~eServiceFS()
154 {
155 }
156
157 int lower(char c)
158 {
159         return std::tolower(static_cast<unsigned char>(c));
160 }
161
162 RESULT eServiceFS::getContent(std::list<eServiceReference> &list, bool sorted)
163 {
164         DIR *d=opendir(path.c_str());
165         if (!d)
166                 return -errno;
167         while (dirent *e=readdir(d))
168         {
169                 if (!(strcmp(e->d_name, ".") && strcmp(e->d_name, "..")))
170                         continue;
171                 
172                 std::string filename;
173                 
174                 filename = path;
175                 filename += e->d_name;
176                 
177                 struct stat s;
178                 if (::stat(filename.c_str(), &s) < 0)
179                         continue;
180                 
181                 if (S_ISDIR(s.st_mode))
182                         filename += "/";
183                 
184                 if (S_ISDIR(s.st_mode))
185                 {
186                         eServiceReference service(eServiceFactoryFS::id, 
187                                 eServiceReference::isDirectory|
188                                 eServiceReference::canDescent|eServiceReference::mustDescent|
189                                 eServiceReference::shouldSort|eServiceReference::sort1,
190                                 filename);
191                         service.data[0] = 1;
192                         list.push_back(service);
193                 } else
194                 {
195                         size_t e = filename.rfind('.');
196                         if (e != std::string::npos && e+1 < filename.length())
197                         {
198                                 std::string extension = filename.substr(e+1);
199                                 std::transform(extension.begin(), extension.end(), extension.begin(), lower);
200                                 int type = getServiceTypeForExtension(extension);
201
202                                 if (type == -1)
203                                 {
204                                         ePtr<eServiceCenter> sc;
205                                         eServiceCenter::getPrivInstance(sc);
206                                         type = sc->getServiceTypeForExtension(extension);
207                                 }
208                         
209                                 if (type != -1)
210                                 {
211                                         eServiceReference service(type,
212                                                 0,
213                                                 filename);
214                                         service.data[0] = 0;
215                                         list.push_back(service);
216                                 }
217                         }
218                 }
219         }
220         closedir(d);
221
222         if (sorted)
223                 list.sort(iListableServiceCompare(this));
224
225         return 0;
226 }
227
228 //   The first argument of this function is a format string to specify the order and
229 //   the content of the returned list
230 //   useable format options are
231 //   R = Service Reference (as swig object .. this is very slow)
232 //   S = Service Reference (as python string object .. same as ref.toString())
233 //   C = Service Reference (as python string object .. same as ref.toCompareString())
234 //   N = Service Name (as python string object)
235 //   when exactly one return value per service is selected in the format string,
236 //   then each value is directly a list entry
237 //   when more than one value is returned per service, then the list is a list of
238 //   python tuples
239 //   unknown format string chars are returned as python None values !
240 PyObject *eServiceFS::getContent(const char* format, bool sorted)
241 {
242         ePyObject ret;
243         std::list<eServiceReference> tmplist;
244         int retcount=1;
245
246         if (!format || !(retcount=strlen(format)))
247                 format = "R"; // just return service reference swig object ...
248
249         if (!getContent(tmplist, sorted))
250         {
251                 int services=tmplist.size();
252                 ePtr<iStaticServiceInformation> sptr;
253                 eServiceCenterPtr service_center;
254
255                 if (strchr(format, 'N'))
256                         eServiceCenter::getPrivInstance(service_center);
257
258                 ret = PyList_New(services);
259                 std::list<eServiceReference>::iterator it(tmplist.begin());
260
261                 for (int cnt=0; cnt < services; ++cnt)
262                 {
263                         eServiceReference &ref=*it++;
264                         ePyObject tuple = retcount > 1 ? PyTuple_New(retcount) : ePyObject();
265                         for (int i=0; i < retcount; ++i)
266                         {
267                                 ePyObject tmp;
268                                 switch(format[i])
269                                 {
270                                 case 'R':  // service reference (swig)object
271                                         tmp = NEW_eServiceReference(ref);
272                                         break;
273                                 case 'C':  // service reference compare string
274                                         tmp = PyString_FromString(ref.toCompareString().c_str());
275                                         break;
276                                 case 'S':  // service reference string
277                                         tmp = PyString_FromString(ref.toString().c_str());
278                                         break;
279                                 case 'N':  // service name
280                                         if (service_center)
281                                         {
282                                                 service_center->info(ref, sptr);
283                                                 if (sptr)
284                                                 {
285                                                         std::string name;
286                                                         sptr->getName(ref, name);
287                                                         if (name.length())
288                                                                 tmp = PyString_FromString(name.c_str());
289                                                 }
290                                         }
291                                         if (!tmp)
292                                                 tmp = PyString_FromString("<n/a>");
293                                         break;
294                                 default:
295                                         if (tuple)
296                                         {
297                                                 tmp = Py_None;
298                                                 Py_INCREF(Py_None);
299                                         }
300                                         break;
301                                 }
302                                 if (tmp)
303                                 {
304                                         if (tuple)
305                                                 PyTuple_SET_ITEM(tuple, i, tmp);
306                                         else
307                                                 PyList_SET_ITEM(ret, cnt, tmp);
308                                 }
309                         }
310                         if (tuple)
311                                 PyList_SET_ITEM(ret, cnt, tuple);
312                 }
313         }
314         return ret ? (PyObject*)ret : (PyObject*)PyList_New(0);
315 }
316
317 RESULT eServiceFS::getNext(eServiceReference &ptr)
318 {
319         if (!m_list_valid)
320         {
321                 m_list_valid = 1;
322                 int res = getContent(m_list);
323                 if (res)
324                         return res;
325         }
326         
327         if (!m_list.size())
328         {
329                 ptr = eServiceReference();
330                 return -ERANGE;
331         }
332         
333         ptr = m_list.front();
334         m_list.pop_front();
335         return 0;
336 }
337
338 int eServiceFS::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
339 {
340                 /* directories first */
341         if ((a.flags & ~b.flags) & eServiceReference::isDirectory)
342                 return 1;
343         else if ((~a.flags & b.flags) & eServiceReference::isDirectory)
344                 return 0;
345                 /* sort by filename */
346         else
347                 return a.path < b.path;
348 }
349
350 RESULT eServiceFS::startEdit(ePtr<iMutableServiceList> &res)
351 {
352         res = 0;
353         return -1;
354 }
355
356 int eServiceFS::getServiceTypeForExtension(const char *str)
357 {
358         for (std::map<int, std::list<std::string> >::iterator sit(m_additional_extensions.begin()); sit != m_additional_extensions.end(); ++sit)
359         {
360                 for (std::list<std::string>::iterator eit(sit->second.begin()); eit != sit->second.end(); ++eit)
361                 {
362                         if (*eit == str)
363                                 return sit->first;
364                 }
365         }
366         return -1;
367 }
368
369 int eServiceFS::getServiceTypeForExtension(const std::string &str)
370 {
371         return getServiceTypeForExtension(str.c_str());
372 }
373
374 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");