-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.c
492 lines (430 loc) · 10.1 KB
/
data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Image plugin to VDR (C++)
*
* (C) 2004-2011 Andreas Brachold <vdr07 at deltab.de>
* (C) 2003 Kai Tobias Burwieck <kai at burwieck.net>
*
* based on MP3/MPlayer plugin to VDR (C++)
* (C) 2001,2002 Stefan Huelswitt <huels at iname.com>
*
* This code is distributed under the terms and conditions of the
* GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
*
*/
#include <ctype.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "data.h"
#include "data-image.h"
#include "list.h"
#include <vdr/tools.h>
// ----------------------------------------------------------------
const char *g_szMountScript = "mount.sh";
char *AddPath(const char *dir, const char *filename)
{
int l = strlen(dir);
char *name = MALLOC(char, l + strlen(filename) + 2);
if(name)
{
strcpy(name, dir);
name[l] = '/';
strcpy(name + l + 1, filename);
} else {
esyslog("imageplugin: ERROR: no memory for filename");
}
return name;
}
class cFileExt : public cListObject
{
char * m_szExt;
protected:
void clean() { if(Ext()) free(m_szExt);m_szExt=0;}
public:
cFileExt() { m_szExt = 0; }
virtual ~cFileExt() { clean(); }
cFileExt(cFileExt& x) //Copy-Ctor
{
if(x.Ext())
{
m_szExt = MALLOC(char, strlen(x.m_szExt)+1);
strcpy(m_szExt,x.m_szExt);
}
else m_szExt = 0;
}
const char* Ext() const { return m_szExt; }
const char* Set(const char* sz)
{
const char *f = sz;
for(;*sz != 0 && *sz == ' ';++sz); //Skip leading whitespace
for(f = sz;*f != 0 && *f != ' ';++f); //Find end of item \0 or ' '
clean(); //Prevebt double assign
if(f - sz > 0) // Store founded item
{
m_szExt = MALLOC(char, f - sz + 1);
strncpy(m_szExt,sz,f - sz);
*(m_szExt + (f - sz)) = '\0';
}
return f;
}
};
class cFileExtList : public cList<cFileExt>
{
public:
cFileExtList(const char* szExtList) {
const char* sz = szExtList;
while(*sz != 0)
{
cFileExt ext;
sz = ext.Set(sz);
if(ext.Ext())
Add(new cFileExt(ext)); // Add copy
};
}
virtual ~cFileExtList()
{
}
};
// -- cScanDir --------------------------------------------------------------
bool cScanDir::ScanDir(cFileSource * src, const char *subdir, eScanType type,
const char *spec, const char *excl, bool recursiv)
{
bool result = true;
char *cmd = 0, *dir = 0, *s = 0, *e = 0, tc;
switch (type)
{
default:
case stFile:
tc = 'f';
break;
case stDir:
tc = 'd';
break;
}
if(subdir)
asprintf(&dir, "%s/%s", src->BaseDir(), subdir);
else
asprintf(&dir, "%s", src->BaseDir());
// If is'nt set a filter use this a default
if(stFile == type && (0 == spec || 0>= strlen(spec)))
spec = "*.jpg *.jpeg *.jif *.jiff *.tif *.tiff *.gif *.bmp *.png *.pnm *.mps";
// Check if filter is set build complex include find ( -iname "*.jpg" -o -iname "*.jpeg" -o .. )
if(stFile == type && spec)
{
cFileExtList list(spec); //Splitt *.jpg *.jpeg
cFileExt *src = list.First();
while(src) //Loop throw all found item
{
char* sn;
if(!s) //open bracket
asprintf(&s, " \\( ");
asprintf(&sn, "%s-iname \"%s\" ",s,QuoteString(src->Ext()));
if(s) free(s);
s = sn;
src = list.Next(src);
if(src) //follow ext
{
char* sn;
asprintf(&sn, "%s-o ",s);
free(s);
s = sn;
}
}
if(s) // close bracket
{
char* sn;
asprintf(&sn, "%s\\)",s);
free(s);
s = sn;
}
}
// Check if filter is set build complex exclude find -not ( -iname "*.jpg" -o -iname "*.jpeg" -o .. )
if(stFile == type && excl) {
cFileExtList list(excl); //Splitt *.jpg *.jpeg
cFileExt *src = list.First();
while(src) //Loop throw all found item
{
char* en;
if(!e) //open bracket
asprintf(&e, "-not \\( ");
asprintf(&en, "%s-iname \"%s\" ",e,QuoteString(src->Ext()));
if(e) free(e);
e = en;
src = list.Next(src);
if(src) //follow ext
{
char* en;
asprintf(&en, "%s-o ",e);
free(e);
e = en;
}
}
if(e) // close bracket
{
char* en;
asprintf(&en, "%s\\)",e);
free(e);
e = en;
}
}
#if 0
asprintf(&cmd, "find \"%s\" -follow -type %c %s %s %s 2>/dev/null | sort -df",
QuoteString(dir), tc, s?s:"", e?e:"", recursiv?"":"-maxdepth 1");
#else
asprintf(&cmd, "find \"%s\" -follow -type %c %s %s %s 2>/dev/null | sort -df | grep -v \"/\\.\"",
QuoteString(dir), tc, s?s:"", e?e:"", recursiv?"":"-maxdepth 1");
#endif
//fprintf(stderr,"%s\n",cmd);
cReadLine l;
FILE *p = popen(cmd, "r");
if(p) {
int len = strlen(dir);
char *s;
while((s = l.Read(p)) != 0) {
char *ss = strstr(s, dir);
if(ss) {
s = ss + len;
if(*s == '/')
s++;
}
if(*s)
DoItem(src, subdir, s);
}
pclose(p);
}
else
result = false;
free(cmd);
free(dir);
free(s);
free(e);
return result;
}
char *cScanDir::QuoteString(const char *str)
{
static char *nstr = 0;
free(nstr);
nstr = MALLOC(char, strlen(str) * 2);
char *p = nstr;
while(*str)
{
switch (*str)
{
case '$': // dollar
case '\\': // backslash
case '\"': // double quote
case '`': // back tick
*p++ = '\\';
// fall through
default:
*p++ = *str++;
break;
}
}
*p = 0;
return nstr;
}
// -- cDirItem --------------------------------------------------------------
cDirItem::cDirItem(cFileSource * src, const char *subdir, const char *name,
const eItemType type)
{
Source = src;
Subdir = subdir ? strdup(subdir) : 0;
Name = name ? strdup(name) : 0;
Type = type;
}
cDirItem::~cDirItem()
{
free(Name);
free(Subdir);
}
char *cDirItem::Path(void)
{
char *path;
if(Subdir)
path = AddPath(Subdir, Name);
else
path = strdup(Name);
return path;
}
// -- cDirList --------------------------------------------------------------
bool cDirList::Load(cFileSource * src, const char *subdir)
{
bool res = false;
Clear();
if(subdir)
Add(new cDirItem(src, subdir, "..", itParent));
itype = itDir;
if(ScanDir(src, subdir, stDir, 0, 0, false)) {
itype = itFile;
if(ScanDir(src, subdir, stFile, src->Include(), 0, false))
res = true;
}
return res;
}
void cDirList::DoItem(cFileSource * src, const char *subdir, const char *name)
{
Add(new cDirItem(src, subdir, name, itype));
}
// -- cFileSource --------------------------------------------------------------
cFileSource::cFileSource(void)
{
browsedir = browseparent = 0;
basedir = description = include = 0;
useCount = 0;
needsmount = false;
}
cFileSource::cFileSource(const char *Basedir, const char *Description,
const bool NeedsMount, const char *Include)
{
browsedir = browseparent = 0;
basedir = description = include = 0;
useCount = 0;
Set(Basedir, Description, NeedsMount, Include);
}
cFileSource::~cFileSource()
{
ClearRemember();
free(basedir);
free(description);
free(include);
}
void cFileSource::Set(const char *Basedir, const char *Description,
const bool NeedsMount, const char *Include)
{
free(basedir);
basedir = strdup(Basedir);
free(description);
description = strdup(Description);
free(include);
include = Include ? strdup(Include) : 0;
needsmount = NeedsMount;
}
char *cFileSource::BuildName(const char *filename)
{
return AddPath(basedir, filename);
}
void cFileSource::SetRemember(const char *dir, const char *parent)
{
ClearRemember();
if(dir)
browsedir = strdup(dir);
if(parent)
browseparent = strdup(parent);
}
void cFileSource::ClearRemember(void)
{
free(browsedir);
browsedir = 0;
free(browseparent);
browseparent = 0;
}
bool cFileSource::GetRemember(char *&dir, char *&parent)
{
dir = parent = 0;
if(browsedir) {
if(browseparent)
parent = strdup(browseparent);
dir = strdup(browsedir);
return true;
}
return false;
}
bool cFileSource::Parse(char *s)
{
char base[256], des[256], incl[256];
int needsmount, n;
if((n =
sscanf(s, "%255[^;];%255[^;];%d;%255[^;]", base, des, &needsmount,
incl)) >= 3)
{
char *base2 = skipspace(stripspace(base));
Set(base2, skipspace(stripspace(des)), needsmount != 0,
n > 3 ? skipspace(stripspace(incl)) : 0);
// do some checking of the basedir and issue a warning if apropriate
if(access(base2, R_OK)) {
esyslog("imageplugin: WARNING: source base %s not found/permission denied",base2);
}
else {
struct stat64 ds;
if(lstat64(base2, &ds)) {
esyslog("imageplugin: WARNING: can't stat source base %s",base2);
}
else {
if(S_ISLNK(ds.st_mode)) {
esyslog("imageplugin: WARNING: source base %s is a symbolic link",base2);
}
else if(!S_ISDIR(ds.st_mode)) {
esyslog("imageplugin: WARNING: source base %s is not a directory",base2);
}
}
}
return true;
}
return false;
}
bool cFileSource::Action(eAction act)
{
static const char *str[] = { "mount", "unmount", "eject", "status" };
char *cmd = 0;
asprintf(&cmd, "%s %s %s", g_szMountScript, str[act], basedir);
bool res = (system(cmd) == 0);
free(cmd);
return res;
}
bool cFileSource::Mount(void)
{
bool res = false;
if(needsmount && (res = Action(acMount)))
ClearRemember();
return res;
}
bool cFileSource::Unmount(void)
{
bool res = false;
if(needsmount) {
theSlideShow.Remove(this);
if(!useCount && (res = Action(acUnmount)))
ClearRemember();
}
return res;
}
bool cFileSource::Eject(void)
{
bool res = false;
if(needsmount) {
theSlideShow.Remove(this);
if(!useCount && (res = Action(acEject)))
ClearRemember();
}
return res;
}
bool cFileSource::Status(void)
{
if(needsmount)
return Action(acStatus);
return true;
}
// -- cFileSources --------------------------------------------------------------
bool cFileSources::Load(const char *filename, bool dummy)
{
if(cConfig < cFileSource >::Load(filename, true)) {
SetSource(First());
return true;
}
return false;
}
cFileSource *cFileSources::FindSource(const char *filename)
{
cFileSource *src = First();
while(src) {
if(startswith(filename, src->BaseDir()))
return src;
src = Next(src);
}
return 0;
}