-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtools.cpp
426 lines (385 loc) · 14.2 KB
/
tools.cpp
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
#include <iostream>
#include <fstream>
#include "tools.h"
#include "xxhash.h"
#include "setup.h"
#include "md5.h"
#include <tnt/ecpp.h>
#include <tnt/htmlescostream.h>
// STL headers need to be before VDR tools.h (included by <vdr/recording.h>)
#include <iomanip>
#include <vdr/plugin.h>
#include <vdr/recording.h>
#include <vdr/videodir.h>
using namespace tnt;
std::istream& operator>>( std::istream& is, tChannelID& ret )
{
std::string line;
if (!std::getline( is, line ) ) {
if (0 == is.gcount()) {
is.clear(is.rdstate() & ~std::ios::failbit);
return is;
}
if (!is.eof()) {
is.setstate( std::ios::badbit );
return is;
}
}
if ( !line.empty() && !( ret = tChannelID::FromString( line.c_str() ) ).Valid() )
is.setstate( std::ios::badbit );
return is;
}
namespace vdrlive {
std::string FormatDuration(char const* format, int duration)
{
cToSvConcat result;
AppendDuration(result, format, duration);
return std::string(result);
}
std::vector<std::string> StringSplit(cSv text, char delimiter )
{
std::vector<std::string> result;
size_t last = 0, pos;
while ( ( pos = text.find( delimiter, last ) ) != std::string::npos ) {
result.emplace_back( text.substr( last, pos - last ) );
last = pos + 1;
}
if ( last < text.length() )
result.emplace_back( text.substr( last ) );
return result;
}
cSv StringWordTruncate(cSv text, size_t maxLen, bool& truncated) {
// Return text truncted to maxLen characters. If such truncation is required,
// truncate at any char in the char list in find_last_of (see code below ...)
if (text.length() <= maxLen) {
truncated = false;
return text;
}
truncated = true;
cSv result = text.substr(0, maxLen);
size_t pos = result.find_last_of(" \t\n,;:.?!()[]{}*+-");
if (pos == std::string::npos) return result;
if (strchr("?!)]}", result[pos])) ++pos; // truncate after these characters
return StringTrim(result.substr(0, pos));
}
cSv StringTrim(cSv str)
{
size_t pos = str.find_last_not_of(' ');
if (pos == std::string::npos) return cSv();
cSv trailBlankRemoved = str.substr(0, pos+1);
pos = trailBlankRemoved.find_first_not_of(' ');
if (pos == std::string::npos) return cSv();
return trailBlankRemoved.substr(pos);
}
std::string MD5Hash(std::string const& str)
{
char* szInput = strdup(str.c_str());
if (!szInput) return "";
char* szRes = MD5String(szInput);
std::string res = szRes;
free(szRes);
free(szInput);
return res;
}
XXH64_hash_t parse_hex_64(cSv str) {
if (str.length() != 16) {
esyslog("live: ERROR in parse_hex_64, hex = \"%.*s\" is not 16 chars long", (int)str.length(), str.data());
return 0;
}
size_t parsed_chars;
XXH64_hash_t result = parse_hex<XXH64_hash_t>(str, &parsed_chars);
if (parsed_chars == 16) return result;
esyslog("live: ERROR in parse_hex_64, hex = \"%.*s\" contains invalid characters", (int)str.length(), str.data());
return 0;
}
XXH128_hash_t parse_hex_128(cSv str) {
XXH128_hash_t result;
if (str.length() != 32) {
esyslog("live: ERROR in parse_hex_128, hex = \"%.*s\" is not 32 chars long", (int)str.length(), str.data());
result.low64 = 0;
result.high64 = 0;
return result;
}
size_t parsed_chars_h, parsed_chars_l;
result.high64 = parse_hex<XXH64_hash_t>(str.substr(0, 16), &parsed_chars_h);
result.low64 = parse_hex<XXH64_hash_t>(str.substr(16), &parsed_chars_l);
if (parsed_chars_l == 16 && parsed_chars_h == 16) return result;
esyslog("live: ERROR in parse_hex_128, hex = \"%.*s\" contains invalid characters", (int)str.length(), str.data());
result.low64 = 0;
result.high64 = 0;
return result;
}
#define HOURS(x) ((x)/100)
#define MINUTES(x) ((x)%100)
std::string ExpandTimeString(std::string timestring)
{
size_t colonpos = timestring.find(":");
if (colonpos == std::string::npos)
{
if (timestring.size() == 1)
timestring = "0" + timestring + ":00";
else if (timestring.size() == 2)
timestring = timestring + ":00";
else if (timestring.size() == 3)
timestring = "0" + std::string(timestring.begin(), timestring.begin() + 1) + ":" + std::string(timestring.begin() + 1, timestring.end());
else
timestring = std::string(timestring.begin(), timestring.begin() + 2) + ":" + std::string(timestring.begin() + 2, timestring.end());
}
else
{
std::string hours = std::string(timestring.begin(), timestring.begin() + colonpos);
std::string mins = std::string(timestring.begin() + colonpos + 1, timestring.end());
hours = std::string(std::max(0,(int)(2 - hours.size())), '0') + hours;
mins = std::string(std::max(0,(int)(2 - mins.size())), '0') + mins;
timestring = hours + ":" + mins;
}
return timestring;
}
time_t GetTimeT(cSv timestring) // timestring in HH:MM
{
int iTime = parse_int<int>( cToSvReplace(timestring, ":", "") );
struct tm tm_r;
time_t t = time(NULL);
tm* tmnow = localtime_r(&t, &tm_r);
tmnow->tm_hour = HOURS(iTime);
tmnow->tm_min = MINUTES(iTime);
return mktime(tmnow);
}
// return the time value as time_t from <datestring> formatted with <format>
time_t GetDateFromDatePicker(cSv datestring, cSv format)
{
if (datestring.empty())
return 0;
int year = parse_int<int>(datestring.substr(format.find("yyyy"), 4));
int month = parse_int<int>(datestring.substr(format.find("mm"), 2));
int day = parse_int<int>(datestring.substr(format.find("dd"), 2));
struct tm tm_r;
tm_r.tm_year = year - 1900;
tm_r.tm_mon = month -1;
tm_r.tm_mday = day;
tm_r.tm_hour = tm_r.tm_min = tm_r.tm_sec = 0;
tm_r.tm_isdst = -1; // makes sure mktime() will determine the correct DST setting
return mktime(&tm_r);
}
// format is in datepicker format ('mm' for month, 'dd' for day, 'yyyy' for year)
std::string DatePickerToC(time_t date, cSv format)
{
if (date == 0) return "";
cToSvConcat cformat;
cformat.appendReplace(format, "mm", "%m");
cformat.replaceAll("dd", "%d");
cformat.replaceAll("yyyy", "%Y");
return std::string(cToSvDateTime(cformat, date));
}
int timeStringToInt(const char *t) {
// input: t in xx:xx format
// output: time in epgsearch format (int, 100*h + min)
int h = 0, min = 0;
sscanf(t, "%2d:%2d", &h, &min);
return 100*h + min;
}
int timeStringToInt(const std::string &t) {
return timeStringToInt(t.c_str() );
}
void intToTimeString(char *t, int tm) {
// sizeof (t) must be >= 6 "hh:mm", + ending 0
// see int timeStringToInt(const char *t) for formats
unsigned int h = tm / 100;
unsigned int min = tm % 100;
snprintf(t, 6, "%.2u:%.2u", h%100u, min);
}
std::string intToTimeString(int tm) {
// see int timeStringToInt(const char *t) for formats
char t[6];
intToTimeString(t, tm);
return t;
}
void EncodeDomId(char *toEncode_s, char *toEncode_e, char const * from, char const * to)
{
for (; *from && *to; from++, to++) {
std::replace(toEncode_s, toEncode_e, *from, *to);
}
}
std::string EncodeDomId(cSv toEncode, char const * from, char const * to)
{
std::string encoded(toEncode);
EncodeDomId(encoded.data(), encoded.data() + encoded.length(), from, to);
return encoded;
}
std::string FileSystemExchangeChars(cSv s, bool ToFileSystem)
{
if (s.empty()) return std::string();
char *str = reinterpret_cast<char*>(std::malloc(s.length() + 1)); // VDR ExchangeChars needs a pointer to data allocated with malloc
if (!str) {
esyslog("live, ERROR: out of memory in FileSystemExchangeChars");
return std::string(s);
}
std::memcpy(str, s.data(), s.length());
str[s.length()] = 0;
str = ExchangeChars(str, ToFileSystem);
std::string data = str;
std::free(str);
return data;
}
bool MoveDirectory(cSv sourceDir, cSv targetDir, bool copy)
{
const char* delim = "/";
std::string source(sourceDir);
std::string target(targetDir);
// add missing directory delimiters
if (source.compare(source.size() - 1, 1, delim) != 0) {
source += "/";
}
if (target.compare(target.size() - 1, 1, delim) != 0) {
target += "/";
}
if (source != target) {
// validate target directory
if (target.find(source) != std::string::npos) {
esyslog("live: cannot move under sub-directory\n");
return false;
}
if (!MakeDirs(target.c_str(), true)) {
esyslog("live: cannot create directory %s", target.c_str());
return false;
}
struct stat st1, st2;
if (stat(source.c_str(), &st1) != 0) {
esyslog("live: rename. Source %s does not exist", source.c_str());
return false;
}
stat(target.c_str(), &st2);
if (!copy && (st1.st_dev == st2.st_dev)) {
if (!cVideoDirectory::RenameVideoFile(source.c_str(), target.c_str())) {
esyslog("live: rename failed from %s to %s", source.c_str(), target.c_str());
return false;
}
}
else {
int required = DirSizeMB(source.c_str());
int available = FreeDiskSpaceMB(target.c_str());
// validate free space
if (required < available) {
cReadDir d(source.c_str());
struct dirent *e;
bool success = true;
// allocate copying buffer
const int len = 1024 * 1024;
char *buffer = MALLOC(char, len);
if (!buffer) {
esyslog("live: cannot allocate renaming buffer");
return false;
}
// loop through all files, but skip all subdirectories
while ((e = d.Next()) != NULL) {
// skip generic entries
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) {
std::string sourceFile = source + e->d_name;
std::string targetFile = target + e->d_name;
// copy only regular files
if (!stat(sourceFile.c_str(), &st1) && S_ISREG(st1.st_mode)) {
int r = -1, w = -1;
cUnbufferedFile *inputFile = cUnbufferedFile::Create(sourceFile.c_str(), O_RDONLY | O_LARGEFILE);
cUnbufferedFile *outputFile = cUnbufferedFile::Create(targetFile.c_str(), O_RDWR | O_CREAT | O_LARGEFILE);
// validate files
if (!inputFile || !outputFile) {
esyslog("live: cannot open file %s or %s", sourceFile.c_str(), targetFile.c_str());
success = false;
break;
}
// do actual copy
dsyslog("live: copying %s to %s", sourceFile.c_str(), targetFile.c_str());
do {
r = inputFile->Read(buffer, len);
if (r > 0)
w = outputFile->Write(buffer, r);
else
w = 0;
} while (r > 0 && w > 0);
DELETENULL(inputFile);
DELETENULL(outputFile);
// validate result
if (r < 0 || w < 0) {
success = false;
break;
}
}
}
}
// release allocated buffer
free(buffer);
// delete all created target files and directories
if (!success) {
size_t found = target.find_last_of(delim);
if (found != std::string::npos) {
target = target.substr(0, found);
}
if (!RemoveFileOrDir(target.c_str(), true)) {
esyslog("live: cannot remove target %s", target.c_str());
}
found = target.find_last_of(delim);
if (found != std::string::npos) {
target = target.substr(0, found);
}
if (!RemoveEmptyDirectories(target.c_str(), true)) {
esyslog("live: cannot remove target directory %s", target.c_str());
}
esyslog("live: copying failed");
return false;
}
else if (!copy && !RemoveFileOrDir(source.c_str(), true)) { // delete source files
esyslog("live: cannot remove source directory %s", source.c_str());
return false;
}
// delete all empty source directories
if (!copy) {
size_t found = source.find_last_of(delim);
if (found != std::string::npos) {
source = source.substr(0, found);
while (source != cVideoDirectory::Name()) {
found = source.find_last_of(delim);
if (found == std::string::npos)
break;
source = source.substr(0, found);
if (!RemoveEmptyDirectories(source.c_str(), true))
break;
}
}
}
}
else {
esyslog("live: %s requires %dMB - only %dMB available", copy ? "moving" : "copying", required, available);
// delete all created empty target directories
size_t found = target.find_last_of(delim);
if (found != std::string::npos) {
target = target.substr(0, found);
while (target != cVideoDirectory::Name()) {
found = target.find_last_of(delim);
if (found == std::string::npos)
break;
target = target.substr(0, found);
if (!RemoveEmptyDirectories(target.c_str(), true))
break;
}
}
return false;
}
}
}
return true;
}
cSv ScraperImagePath2Live(cSv path) {
int tvscraperImageDirLength = LiveSetup().GetTvscraperImageDir().length();
if (tvscraperImageDirLength == 0) return cSv();
if (path.compare(0, tvscraperImageDirLength, LiveSetup().GetTvscraperImageDir()) != 0) {
esyslog("live: ERROR, image path %.*s does not start with %s", (int)path.length(), path.data(), LiveSetup().GetTvscraperImageDir().c_str());
return cSv();
}
return path.substr(tvscraperImageDirLength);
}
bool ScraperCallService(const char *Id, void *Data) {
cPlugin *pScraper = LiveSetup().GetPluginScraper();
if (!pScraper) return false;
return pScraper->Service(Id, Data);
}
} // namespace vdrlive