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
|
/*
* File: FileSystemTest.cc
* Author: django
*
* Created on 21.07.2012, 12:40:48
*/
#include <stdlib.h>
#include <iostream>
#include <FileSystem.h>
#include <File.h>
#include <StringBuilder.h>
#include <LegacyVdrRecording.h>
#include <VdrRecording.h>
#include <DVDImage.h>
#include <Audio.h>
#include <Picture.h>
#include <Logging.h>
#include <TimeMs.h>
#include <string.h>
int visitor(void *opaque, cFile *Parent, const char *Name)
{
cFile *child = new cFile(*Parent, Name);
std::cout << Parent->Name() << " - has child-entry: " << Name << std::endl;
if (child->IsDirectory()) {
child->VisitFiles(visitor, opaque);
}
char *uri = child->toURI();
const char *path = child->AbsolutePath();
std::cout << "child does ";
if (!child->Exists()) std::cout << "NOT ";
std::cout << "exists ..." << std::endl;
std::cout << path << " - has URI: " << uri << std::endl << std::endl;
free(uri);
delete child;
return 0;
}
void test1()
{
std::cout << "FileSystemTest test 1" << std::endl;
cFile *f = new cFile("/media/xchange/");
const char *path = f->AbsolutePath();
std::cout << "got file: " << path << std::endl;
cFile *other = f->Parent();
other->SetVirtualRoot(true);
path = other->AbsolutePath();
std::cout << "parent is file: " << path << std::endl;
cFile *newOne = new cFile(*other, "/video/test/blah");
path = newOne->AbsolutePath();
std::cout << "assembled file: " << path << std::endl;
cFile *again = new cFile("/media/audio/Collection/Rock/Idol, Billy");
path = again->AbsolutePath();
std::cout << "re-assembled file: " << path << std::endl;
std::cout << path << " does ";
if (!again->Exists()) std::cout << "NOT ";
std::cout << "exists" << std::endl;
again->VisitFiles(visitor, NULL);
delete newOne;
delete other;
delete f;
delete again;
cFile::Cleanup();
}
int check4Media(void *opaque, cFile *Parent, const char *Name)
{
if (!opaque) return -1;
std::vector<cAbstractMedia *> *pool = (std::vector<cAbstractMedia *> *) opaque;
cFile *curFile = new cFile(*Parent, Name);
const char *mimeType = NULL;
cAbstractMedia *rv = NULL;
if (!curFile) {
esyslog("ERROR: out of memory!");
return -1;
}
if (!curFile->Exists()) {
delete curFile;
return -1;
}
if (curFile->IsDirectory()) {
static const char *keyFiles[] = { "001.vdr", "00001.ts", "VIDEO_TS/VIDEO_TS.IFO", NULL };
cFile *tmp;
const char *check;
int n=0;
for (const char **kf = keyFiles; kf && *kf; ++kf, ++n) {
tmp = new cFile(*curFile, *kf);
check = tmp ? tmp->AbsolutePath() : NULL;
if (tmp->Exists() && tmp->IsFile() && !tmp->IsDirectory()) {
switch (n) {
case 0: rv = new cLegacyVdrRecording(*curFile); break;
case 1: rv = new cVdrRecording(*curFile); break;
default: rv = new cDVDImage(*curFile); break;
}
}
delete tmp;
}
if (!rv) curFile->VisitFiles(check4Media, opaque);
}
else {
const char *extension = strrchr(Name, '.');
if (!extension) {
delete curFile;
return -1;
}
++extension;
mimeType = cMovie::ContentType(extension);
if (mimeType) rv = new cMovie(*curFile, mimeType);
else {
mimeType = cAudio::ContentType(extension);
if (mimeType) rv = new cAudio(*curFile, mimeType);
else {
mimeType = cPicture::ContentType(extension);
if (mimeType) rv = new cPicture(*curFile, mimeType);
}
}
}
delete curFile;
if (rv) {
pool->push_back(rv);
return 0;
}
else return -1;
}
void test2()
{
std::cout << "FileSystemTest test 2" << std::endl;
cFile mediaRoot("/media");
cFile &startScan = mediaRoot; //("/media/images");
if (!mediaRoot.Exists() || !mediaRoot.IsDirectory())
std::cout << "%TEST_FAILED% time=0 testname=test2 (FileSystemTest) message=error message sample" << std::endl;
if (!startScan.Exists() || !startScan.IsDirectory())
std::cout << "%TEST_FAILED% time=0 testname=test2 (FileSystemTest) message=error message sample" << std::endl;
mediaRoot.SetVirtualRoot();
std::vector<cAbstractMedia *> mediaPool;
uint64_t start = cTimeMs::Now();
startScan.VisitFiles(check4Media, &mediaPool);
uint64_t end = cTimeMs::Now();
for (size_t i=0; i < mediaPool.size(); ++i) {
std::cout << "pool entry: " << mediaPool[i]->Name() << std::endl;
}
//19: got 9998 media in 8081ms. (with valgrind)
// ==9779== total heap usage: 637,920 allocs, 637,918 frees, 172,023,271 bytes allocated
//19: got 9998 media in 3750ms. (with valgrind)
//19: got 9998 media in 122ms. (without valgrind)
// ==30004== total heap usage: 348,984 allocs, 348,982 frees, 103,182,434 bytes allocated
//01: got 5739 media in 88165ms. (with valgrind)
// ==2605== total heap usage: 924,253 allocs, 924,247 frees, 259,567,759 bytes allocated
//01: got 5739 media in 71959ms. (with valgrind)
//01: got 5739 media in 54619ms. (without valgrind - fresh reboot)
//01: got 5739 media in 205ms (without valgrind - repeated run)
// ==2465== total heap usage: 469,151 allocs, 469,145 frees, 155,163,997 bytes allocated
std::cout << "got " << mediaPool.size() << " media in " << (end - start) << "ms." << std::endl;
for (size_t i=0; i < mediaPool.size(); ++i) {
delete mediaPool[i];
}
cFile::Cleanup();
}
int main(int argc, char** argv)
{
uint64_t t0 = cTimeMs::Now();
std::cout << "%SUITE_STARTING% FileSystemTest" << std::endl;
std::cout << "%SUITE_STARTED%" << std::endl;
std::cout << "%TEST_STARTED% test1 (FileSystemTest)" << std::endl;
uint64_t start = cTimeMs::Now();
test1();
uint64_t end = cTimeMs::Now();
std::cout << "%TEST_FINISHED% time=" << (double)(end - start) / 1000 << " test1 (FileSystemTest)" << std::endl;
std::cout << "%TEST_STARTED% test2 (FileSystemTest)\n" << std::endl;
start = cTimeMs::Now();
test2();
end = cTimeMs::Now();
std::cout << "%TEST_FINISHED% time=" << (double)(end - start) / 1000 << " test2 (FileSystemTest)" << std::endl;
std::cout << "%SUITE_FINISHED% time=" << (double)(cTimeMs::Now() - t0) / 1000 << std::endl;
return (EXIT_SUCCESS);
}
|