|
a/libupnpp/vdir.cxx |
|
b/libupnpp/vdir.cxx |
|
... |
|
... |
20 |
#include <iostream>
|
20 |
#include <iostream>
|
21 |
|
21 |
|
22 |
#include "upnpplib.hxx"
|
22 |
#include "upnpplib.hxx"
|
23 |
#include "upnpp_p.hxx"
|
23 |
#include "upnpp_p.hxx"
|
24 |
#include "vdir.hxx"
|
24 |
#include "vdir.hxx"
|
|
|
25 |
#include "log.hxx"
|
25 |
|
26 |
|
26 |
using namespace std;
|
27 |
using namespace std;
|
27 |
|
28 |
|
28 |
static VirtualDir *theDir;
|
29 |
static VirtualDir *theDir;
|
29 |
|
30 |
|
|
... |
|
... |
54 |
return theDir->getFile(dir, fn);
|
55 |
return theDir->getFile(dir, fn);
|
55 |
}
|
56 |
}
|
56 |
|
57 |
|
57 |
static int vdgetinfo(const char *fn, struct File_Info* info )
|
58 |
static int vdgetinfo(const char *fn, struct File_Info* info )
|
58 |
{
|
59 |
{
|
59 |
//cerr << "vdgetinfo: " << fn << endl;
|
60 |
//LOGDEB("vdgetinfo: " << fn << endl);
|
60 |
VirtualDir::FileEnt *entry = vdgetentry(fn);
|
61 |
VirtualDir::FileEnt *entry = vdgetentry(fn);
|
61 |
if (entry == 0) {
|
62 |
if (entry == 0) {
|
62 |
cerr << "vdgetinfo: no entry for " << fn << endl;
|
63 |
LOGERR("vdgetinfo: no entry for " << fn << endl);
|
63 |
return -1;
|
64 |
return -1;
|
64 |
}
|
65 |
}
|
65 |
info->file_length = entry->content.size();
|
66 |
info->file_length = entry->content.size();
|
66 |
info->last_modified = entry->mtime;
|
67 |
info->last_modified = entry->mtime;
|
67 |
info->is_directory = 0;
|
68 |
info->is_directory = 0;
|
68 |
info->is_readable = 1;
|
69 |
info->is_readable = 1;
|
69 |
info->content_type = ixmlCloneDOMString(entry->mimetype.c_str());
|
70 |
info->content_type = ixmlCloneDOMString(entry->mimetype.c_str());
|
70 |
return 0;
|
71 |
return 0;
|
71 |
}
|
72 |
}
|
|
|
73 |
|
72 |
static UpnpWebFileHandle vdopen(const char* fn, enum UpnpOpenFileMode Mode)
|
74 |
static UpnpWebFileHandle vdopen(const char* fn, enum UpnpOpenFileMode Mode)
|
73 |
{
|
75 |
{
|
74 |
//cerr << "vdopen: " << fn << endl;
|
76 |
//LOGDEB("vdopen: " << fn << endl);
|
75 |
VirtualDir::FileEnt *entry = vdgetentry(fn);
|
77 |
VirtualDir::FileEnt *entry = vdgetentry(fn);
|
76 |
if (entry == 0) {
|
78 |
if (entry == 0) {
|
77 |
cerr << "vdopen: no entry for " << fn << endl;
|
79 |
LOGERR("vdopen: no entry for " << fn << endl);
|
78 |
return NULL;
|
80 |
return NULL;
|
79 |
}
|
81 |
}
|
80 |
return new Handle(entry);
|
82 |
return new Handle(entry);
|
81 |
}
|
83 |
}
|
82 |
|
84 |
|
83 |
static int vdread(UpnpWebFileHandle fileHnd, char* buf, size_t buflen)
|
85 |
static int vdread(UpnpWebFileHandle fileHnd, char* buf, size_t buflen)
|
84 |
{
|
86 |
{
|
85 |
// cerr << "vdread: " << endl;
|
87 |
// LOGDEB("vdread: " << endl);
|
86 |
if (buflen == 0)
|
88 |
if (buflen == 0)
|
87 |
return 0;
|
89 |
return 0;
|
88 |
Handle *h = (Handle *)fileHnd;
|
90 |
Handle *h = (Handle *)fileHnd;
|
89 |
if (h->offset >= h->entry->content.size())
|
91 |
if (h->offset >= h->entry->content.size())
|
90 |
return 0;
|
92 |
return 0;
|
|
... |
|
... |
95 |
return toread;
|
97 |
return toread;
|
96 |
}
|
98 |
}
|
97 |
|
99 |
|
98 |
static int vdseek(UpnpWebFileHandle fileHnd, long offset, int origin)
|
100 |
static int vdseek(UpnpWebFileHandle fileHnd, long offset, int origin)
|
99 |
{
|
101 |
{
|
100 |
// cerr << "vdseek: " << endl;
|
102 |
// LOGDEB("vdseek: " << endl);
|
101 |
Handle *h = (Handle *)fileHnd;
|
103 |
Handle *h = (Handle *)fileHnd;
|
102 |
if (origin == 0)
|
104 |
if (origin == 0)
|
103 |
h->offset = offset;
|
105 |
h->offset = offset;
|
104 |
else if (origin == 1)
|
106 |
else if (origin == 1)
|
105 |
h->offset += offset;
|
107 |
h->offset += offset;
|
|
... |
|
... |
107 |
h->offset = h->entry->content.size() + offset;
|
109 |
h->offset = h->entry->content.size() + offset;
|
108 |
else
|
110 |
else
|
109 |
return -1;
|
111 |
return -1;
|
110 |
return offset;
|
112 |
return offset;
|
111 |
}
|
113 |
}
|
|
|
114 |
|
112 |
static int vdwrite(UpnpWebFileHandle fileHnd, char* buf, size_t buflen)
|
115 |
static int vdwrite(UpnpWebFileHandle fileHnd, char* buf, size_t buflen)
|
113 |
{
|
116 |
{
|
114 |
// cerr << "vdwrite" << endl;
|
117 |
LOGERR("vdwrite" << endl);
|
115 |
return -1;
|
118 |
return -1;
|
116 |
}
|
119 |
}
|
117 |
|
120 |
|
118 |
static struct UpnpVirtualDirCallbacks myvdcalls = {
|
121 |
static struct UpnpVirtualDirCallbacks myvdcalls = {
|
119 |
vdgetinfo, vdopen, vdread, vdwrite, vdseek, vdclose
|
122 |
vdgetinfo, vdopen, vdread, vdwrite, vdseek, vdclose
|
|
... |
|
... |
122 |
VirtualDir *VirtualDir::getVirtualDir()
|
125 |
VirtualDir *VirtualDir::getVirtualDir()
|
123 |
{
|
126 |
{
|
124 |
if (theDir == 0) {
|
127 |
if (theDir == 0) {
|
125 |
theDir = new VirtualDir();
|
128 |
theDir = new VirtualDir();
|
126 |
if (UpnpSetVirtualDirCallbacks(&myvdcalls) != UPNP_E_SUCCESS) {
|
129 |
if (UpnpSetVirtualDirCallbacks(&myvdcalls) != UPNP_E_SUCCESS) {
|
127 |
cerr<< "SetVirtualDirCallbacks failed" << endl;
|
130 |
LOGERR("SetVirtualDirCallbacks failed" << endl);
|
128 |
delete theDir;
|
131 |
delete theDir;
|
129 |
theDir = 0;
|
132 |
theDir = 0;
|
130 |
return 0;
|
133 |
return 0;
|
131 |
}
|
134 |
}
|
132 |
}
|
135 |
}
|
|
... |
|
... |
139 |
string path(_path);
|
142 |
string path(_path);
|
140 |
if (path.empty() || path[path.size()-1] != '/') {
|
143 |
if (path.empty() || path[path.size()-1] != '/') {
|
141 |
path += '/';
|
144 |
path += '/';
|
142 |
}
|
145 |
}
|
143 |
if (m_dirs.find(path) == m_dirs.end()) {
|
146 |
if (m_dirs.find(path) == m_dirs.end()) {
|
144 |
m_dirs[path] = map<string, VirtualDir::FileEnt>();
|
147 |
m_dirs[path] = unordered_map<string, VirtualDir::FileEnt>();
|
145 |
UpnpAddVirtualDir(path.c_str());
|
148 |
UpnpAddVirtualDir(path.c_str());
|
146 |
}
|
149 |
}
|
147 |
VirtualDir::FileEnt entry;
|
150 |
VirtualDir::FileEnt entry;
|
148 |
entry.mtime = time(0);
|
151 |
entry.mtime = time(0);
|
149 |
entry.mimetype = mimetype;
|
152 |
entry.mimetype = mimetype;
|
150 |
entry.content = content;
|
153 |
entry.content = content;
|
151 |
m_dirs[path][name] = entry;
|
154 |
m_dirs[path][name] = entry;
|
152 |
// cerr << "VirtualDir::addFile: added entry for dir " <<
|
155 |
// LOGDEB("VirtualDir::addFile: added entry for dir " <<
|
153 |
// path << " name " << name << endl;
|
156 |
// path << " name " << name << endl);
|
154 |
}
|
157 |
}
|
155 |
|
158 |
|
156 |
VirtualDir::FileEnt *VirtualDir::getFile(const string& _path,
|
159 |
VirtualDir::FileEnt *VirtualDir::getFile(const string& _path,
|
157 |
const string& name)
|
160 |
const string& name)
|
158 |
{
|
161 |
{
|
159 |
string path(_path);
|
162 |
string path(_path);
|
160 |
if (path.empty() || path[path.size()-1] != '/') {
|
163 |
if (path.empty() || path[path.size()-1] != '/') {
|
161 |
path += '/';
|
164 |
path += '/';
|
162 |
}
|
165 |
}
|
163 |
|
166 |
|
164 |
// cerr << "VirtualDir::getFile: path " << path << " name " << name << endl;
|
167 |
// LOGDEB("VirtualDir::getFile: path " << path << " name " << name << endl);
|
165 |
|
168 |
|
166 |
map<string, map<string,VirtualDir::FileEnt> >::iterator dir =
|
169 |
unordered_map<string, unordered_map<string,VirtualDir::FileEnt> >::iterator dir =
|
167 |
m_dirs.find(path);
|
170 |
m_dirs.find(path);
|
168 |
if (dir == m_dirs.end()) {
|
171 |
if (dir == m_dirs.end()) {
|
169 |
cerr << "VirtualDir::getFile: no dir: " << path << endl;
|
172 |
LOGERR("VirtualDir::getFile: no dir: " << path << endl);
|
170 |
return 0;
|
173 |
return 0;
|
171 |
}
|
174 |
}
|
172 |
map<string, FileEnt>::iterator f = dir->second.find(name);
|
175 |
unordered_map<string, FileEnt>::iterator f = dir->second.find(name);
|
173 |
if (f == dir->second.end()) {
|
176 |
if (f == dir->second.end()) {
|
174 |
cerr << "VirtualDir::getFile: no file: " << path << endl;
|
177 |
LOGERR("VirtualDir::getFile: no file: " << path << endl);
|
175 |
return 0;
|
178 |
return 0;
|
176 |
}
|
179 |
}
|
177 |
|
180 |
|
178 |
return &(f->second);
|
181 |
return &(f->second);
|
179 |
}
|
182 |
}
|