Child: [eaf0ad] (diff)

Download this file

vdir.cxx    354 lines (314 with data), 8.8 kB

  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
/* Copyright (C) 2006-2016 J.F.Dockes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "libupnpp/config.h"
#include "vdir.hxx"
#include <string.h>
#include <upnp/ixml.h>
#include <upnp/upnp.h>
#include <iostream>
#include <utility>
#include <unordered_map>
#include "libupnpp/log.hxx"
#include "libupnpp/upnpp_p.hxx"
using namespace std;
using namespace UPnPP;
#if UPNP_VERSION_MAJOR > 1 || (UPNP_VERSION_MAJOR==1 && UPNP_VERSION_MINOR >= 8)
#define V18VDIR 1
#else
#undef V18VDIR
typedef struct File_Info UpnpFileInfo;
#endif
namespace UPnPProvider {
static VirtualDir *theDir;
class FileEnt {
public:
time_t mtime;
std::string mimetype;
std::string content;
};
struct DirEnt {
public:
DirEnt(bool vd = false) : isvd(vd) {}
bool isvd;
unordered_map<string, FileEnt> files;
VirtualDir::FileOps ops;
};
static unordered_map<string, DirEnt> m_dirs;
static void pathcatslash(string& path)
{
if (path.empty() || path[path.size() - 1] != '/') {
path += '/';
}
}
// Look up entry for pathname
static FileEnt *vdgetentry(const char *pathname, DirEnt **de)
{
//LOGDEB("vdgetentry: [" << pathname << "]" << endl);
*de = nullptr;
VirtualDir *thedir = VirtualDir::getVirtualDir();
if (thedir == 0) {
return 0;
}
string path = path_getfather(pathname);
string name= path_getsimple(pathname);
pathcatslash(path);
auto dir = m_dirs.find(path);
if (dir == m_dirs.end()) {
LOGERR("VirtualDir::vdgetentry: no dir: " << path << endl);
return 0;
}
*de = &dir->second;
if (dir->second.isvd) {
return 0;
} else {
auto f = dir->second.files.find(name);
if (f == dir->second.files.end()) {
LOGERR("VirtualDir::vdgetentry: no file: " << path << endl);
return 0;
}
return &(f->second);
}
}
bool VirtualDir::addFile(const string& _path, const string& name,
const string& content, const string& mimetype)
{
string path(_path);
pathcatslash(path);
//LOGDEB("VirtualDir::addFile: path " << path << " name " << name << endl);
if (m_dirs.find(path) == m_dirs.end()) {
m_dirs[path] = DirEnt();
UpnpAddVirtualDir(path.c_str()
#ifdef V18VDIR
, 0, 0
#endif
);
}
FileEnt entry;
entry.mtime = time(0);
entry.mimetype = mimetype;
entry.content = content;
m_dirs[path].files[name] = entry;
// LOGDEB("VirtualDir::addFile: added entry for dir " <<
// path << " name " << name << endl);
return true;
}
bool VirtualDir::addVDir(const std::string& _path, FileOps fops)
{
string path(_path);
pathcatslash(path);
if (m_dirs.find(path) == m_dirs.end()) {
m_dirs[path] = DirEnt(true);
UpnpAddVirtualDir(path.c_str()
#ifdef V18VDIR
, 0, 0
#endif
);
}
m_dirs[path].ops = fops;
return true;
}
//////////
// Methods for the libupnp interface:
struct Handle {
Handle(FileEnt *e, DirEnt *d = nullptr, void *vh = 0)
: entry(e), dir(d), vhandle(vh), offset(0) {
}
FileEnt *entry;
DirEnt *dir;
void *vhandle;
size_t offset;
};
static int vdclose(UpnpWebFileHandle fileHnd
#if V18VDIR
, const void*
#endif
)
{
Handle *h = (Handle*)fileHnd;
if (h->vhandle) {
h->dir->ops.close(h->vhandle);
}
delete h;
return 0;
}
static int vdgetinfo(const char *fn, UpnpFileInfo* info
#if V18VDIR
, const void*
#endif
)
{
//LOGDEB("vdgetinfo: [" << fn << "] off_t " << sizeof(off_t) <<
// " time_t " << sizeof(time_t) << endl);
DirEnt *dir;
FileEnt *entry = vdgetentry(fn, &dir);
if (dir && dir->isvd) {
VirtualDir::FileInfo inf;
int ret = dir->ops.getinfo(fn, &inf);
if (ret >= 0) {
#if V18VDIR
UpnpFileInfo_set_FileLength(info, inf.file_length);
UpnpFileInfo_set_LastModified(info, inf.last_modified);
UpnpFileInfo_set_IsDirectory(info, inf.is_directory);
UpnpFileInfo_set_IsReadable(info, inf.is_readable);
UpnpFileInfo_set_ContentType(info,
ixmlCloneDOMString(inf.mime.c_str()));
#else
info->file_length = inf.file_length;
info->last_modified = inf.last_modified;
info->is_directory = inf.is_directory;
info->is_readable = inf.is_readable;
info->content_type = ixmlCloneDOMString(inf.mime.c_str());
#endif
}
return ret;
}
if (entry == 0) {
LOGERR("vdgetinfo: no entry for " << fn << endl);
return -1;
}
#if V18VDIR
UpnpFileInfo_set_FileLength(info, entry->content.size());
UpnpFileInfo_set_LastModified(info, entry->mtime);
UpnpFileInfo_set_IsDirectory(info, 0);
UpnpFileInfo_set_IsReadable(info, 1);
UpnpFileInfo_set_ContentType(info,
ixmlCloneDOMString(entry->mimetype.c_str()));
#else
info->file_length = entry->content.size();
info->last_modified = entry->mtime;
info->is_directory = 0;
info->is_readable = 1;
info->content_type = ixmlCloneDOMString(entry->mimetype.c_str());
#endif
return 0;
}
static UpnpWebFileHandle vdopen(const char* fn, enum UpnpOpenFileMode
#if V18VDIR
, const void*
#endif
)
{
//LOGDEB("vdopen: " << fn << endl);
DirEnt *dir;
FileEnt *entry = vdgetentry(fn, &dir);
if (dir && dir->isvd) {
void *vh = dir->ops.open(fn);
if (vh) {
return new Handle(nullptr, dir, vh);
} else {
return NULL;
}
}
if (entry == 0) {
LOGERR("vdopen: no entry for " << fn << endl);
return NULL;
}
return new Handle(entry);
}
static int vdread(UpnpWebFileHandle fileHnd, char* buf, size_t buflen
#if V18VDIR
, const void*
#endif
)
{
// LOGDEB("vdread: " << endl);
if (buflen == 0) {
return 0;
}
Handle *h = (Handle *)fileHnd;
if (h->vhandle) {
return h->dir->ops.read(h->vhandle, buf, buflen);
}
if (h->offset >= h->entry->content.size()) {
return 0;
}
size_t toread = buflen > h->entry->content.size() - h->offset ?
h->entry->content.size() - h->offset : buflen;
memcpy(buf, h->entry->content.c_str() + h->offset, toread);
h->offset += toread;
return toread;
}
static int vdseek(UpnpWebFileHandle fileHnd, off_t offset, int origin
#if V18VDIR
, const void*
#endif
)
{
// LOGDEB("vdseek: " << endl);
Handle *h = (Handle *)fileHnd;
if (h->vhandle) {
return h->dir->ops.seek(h->vhandle, offset, origin);
}
if (origin == 0) {
h->offset = offset;
} else if (origin == 1) {
h->offset += offset;
} else if (origin == 2) {
h->offset = h->entry->content.size() + offset;
} else {
return -1;
}
return offset;
}
static int vdwrite(UpnpWebFileHandle, char*, size_t
#if V18VDIR
, const void*
#endif
)
{
LOGERR("vdwrite" << endl);
return -1;
}
#if V18VDIR
VirtualDir *VirtualDir::getVirtualDir()
{
if (theDir == 0) {
theDir = new VirtualDir();
if (UpnpVirtualDir_set_GetInfoCallback(vdgetinfo) ||
UpnpVirtualDir_set_OpenCallback(vdopen) ||
UpnpVirtualDir_set_ReadCallback(vdread) ||
UpnpVirtualDir_set_WriteCallback(vdwrite) ||
UpnpVirtualDir_set_SeekCallback(vdseek) ||
UpnpVirtualDir_set_CloseCallback(vdclose)) {
LOGERR("SetVirtualDirCallbacks failed" << endl);
delete theDir;
theDir = 0;
return 0;
}
}
return theDir;
}
#else
static struct UpnpVirtualDirCallbacks myvdcalls = {
vdgetinfo, vdopen, vdread, vdwrite, vdseek, vdclose
};
VirtualDir *VirtualDir::getVirtualDir()
{
if (theDir == 0) {
theDir = new VirtualDir();
if (UpnpSetVirtualDirCallbacks(&myvdcalls) != UPNP_E_SUCCESS) {
LOGERR("SetVirtualDirCallbacks failed" << endl);
delete theDir;
theDir = 0;
return 0;
}
}
return theDir;
}
#endif
}