]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.cxx
utf8ToLatin1: return original instead of crashing on non-UTF-8 input
[simgear.git] / simgear / package / Catalog.cxx
1 // Copyright (C) 2013  James Turner - zakalawe@mac.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #include <simgear/package/Catalog.hxx>
19
20 #include <boost/foreach.hpp>
21 #include <algorithm>
22 #include <fstream>
23 #include <cstring>
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/props/props_io.hxx>
27 #include <simgear/io/HTTPRequest.hxx>
28 #include <simgear/io/HTTPClient.hxx>
29 #include <simgear/misc/sg_dir.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/package/Package.hxx>
32 #include <simgear/package/Root.hxx>
33 #include <simgear/package/Install.hxx>
34
35 namespace simgear {
36     
37 namespace pkg {
38
39 CatalogList static_catalogs;
40
41 //////////////////////////////////////////////////////////////////////////////
42
43 class Catalog::Downloader : public HTTP::Request
44 {
45 public:
46     Downloader(CatalogRef aOwner, const std::string& aUrl) :
47         HTTP::Request(aUrl),
48         m_owner(aOwner)
49     {        
50     }
51     
52 protected:
53     virtual void gotBodyData(const char* s, int n)
54     {
55         m_buffer += std::string(s, n);
56     }
57     
58     virtual void onDone()
59     {        
60         if (responseCode() != 200) {
61             SG_LOG(SG_GENERAL, SG_ALERT, "catalog download failure:" << m_owner->url());
62             m_owner->refreshComplete(Delegate::FAIL_DOWNLOAD);
63             return;
64         }
65         
66         SGPropertyNode* props = new SGPropertyNode;
67         
68         try {
69             readProperties(m_buffer.data(), m_buffer.size(), props);
70             m_owner->parseProps(props);
71         } catch (sg_exception& e) {
72             SG_LOG(SG_GENERAL, SG_ALERT, "catalog parse failure:" << m_owner->url());
73             m_owner->refreshComplete(Delegate::FAIL_EXTRACT);
74             return;
75         }
76         
77         std::string ver(m_owner->root()->catalogVersion());
78         if (!checkVersion(ver, props)) {
79             SG_LOG(SG_GENERAL, SG_WARN, "downloaded catalog " << m_owner->url() << ", version mismatch:\n\t"
80                    << props->getStringValue("version") << " vs required " << ver);
81             m_owner->refreshComplete(Delegate::FAIL_VERSION);
82             return;
83         }
84         
85         // cache the catalog data, now we have a valid install root
86         Dir d(m_owner->installRoot());
87         SGPath p = d.file("catalog.xml");
88
89         std::ofstream f(p.c_str(), std::ios::out | std::ios::trunc);
90         f.write(m_buffer.data(), m_buffer.size());
91         f.close();
92         
93         time(&m_owner->m_retrievedTime);
94         m_owner->writeTimestamp();
95         m_owner->refreshComplete(Delegate::FAIL_SUCCESS);
96     }
97     
98 private:
99     bool checkVersion(const std::string& aVersion, SGPropertyNode* aProps)
100     {
101         BOOST_FOREACH(SGPropertyNode* v, aProps->getChildren("version")) {
102             if (v->getStringValue() == aVersion) {
103                 return true;
104             }
105         }
106         return false;
107     }
108     
109     CatalogRef m_owner;
110     std::string m_buffer;
111 };
112
113 //////////////////////////////////////////////////////////////////////////////
114
115 CatalogList Catalog::allCatalogs()
116 {
117     return static_catalogs;
118 }
119
120 Catalog::Catalog(Root *aRoot) :
121     m_root(aRoot),
122     m_retrievedTime(0)
123 {
124     static_catalogs.push_back(this);
125 }
126
127 Catalog::~Catalog()
128 {
129     CatalogList::iterator it = std::find(static_catalogs.begin(), static_catalogs.end(), this);
130     static_catalogs.erase(it);
131 }
132
133 CatalogRef Catalog::createFromUrl(Root* aRoot, const std::string& aUrl)
134 {
135     CatalogRef c = new Catalog(aRoot);
136     c->m_url = aUrl;
137     Downloader* dl = new Downloader(c, aUrl);
138     aRoot->makeHTTPRequest(dl);
139     
140     return c;
141 }
142     
143 CatalogRef Catalog::createFromPath(Root* aRoot, const SGPath& aPath)
144 {
145     SGPath xml = aPath;
146     xml.append("catalog.xml");
147     if (!xml.exists()) {
148         return NULL;
149     }
150     
151     SGPropertyNode_ptr props;
152     try {
153         props = new SGPropertyNode;
154         readProperties(xml.str(), props);
155     } catch (sg_exception& e) {
156         return NULL;    
157     }
158     
159     if (props->getStringValue("version") != aRoot->catalogVersion()) {
160         SG_LOG(SG_GENERAL, SG_WARN, "skipping catalog at " << aPath << ", version mismatch:\n\t"
161                << props->getStringValue("version") << " vs required " << aRoot->catalogVersion());
162         return NULL;
163     }
164     
165     CatalogRef c = new Catalog(aRoot);
166     c->m_installRoot = aPath;
167     c->parseProps(props);
168     c->parseTimestamp();
169     
170     return c;
171 }
172
173 PackageList const&
174 Catalog::packages() const
175 {
176   return m_packages;
177 }
178
179 PackageList
180 Catalog::packagesMatching(const SGPropertyNode* aFilter) const
181 {
182     PackageList r;
183     BOOST_FOREACH(PackageRef p, m_packages) {
184         if (p->matches(aFilter)) {
185             r.push_back(p);
186         }
187     }
188     return r;
189 }
190
191 PackageList
192 Catalog::packagesNeedingUpdate() const
193 {
194     PackageList r;
195     BOOST_FOREACH(PackageRef p, m_packages) {
196         if (!p->isInstalled()) {
197             continue;
198         }
199         
200         if (p->install()->hasUpdate()) {
201             r.push_back(p);
202         }
203     }
204     return r;
205 }
206
207 PackageList
208 Catalog::installedPackages() const
209 {
210   PackageList r;
211   BOOST_FOREACH(PackageRef p, m_packages) {
212     if (p->isInstalled()) {
213       r.push_back(p);
214     }
215   }
216   return r;
217 }
218   
219 void Catalog::refresh()
220 {
221     Downloader* dl = new Downloader(this, url());
222     m_root->makeHTTPRequest(dl);
223     m_root->catalogRefreshBegin(this);
224 }
225
226 void Catalog::parseProps(const SGPropertyNode* aProps)
227 {
228     // copy everything except package children?
229     m_props = new SGPropertyNode;
230     
231     int nChildren = aProps->nChildren();
232     for (int i = 0; i < nChildren; i++) {
233         const SGPropertyNode* pkgProps = aProps->getChild(i);
234         if (strcmp(pkgProps->getName(), "package") == 0) {
235             PackageRef p = new Package(pkgProps, this);
236             m_packages.push_back(p);   
237         } else {
238             SGPropertyNode* c = m_props->getChild(pkgProps->getName(), pkgProps->getIndex(), true);
239             copyProperties(pkgProps, c);
240         }
241     } // of children iteration
242   
243     if (!m_url.empty()) {
244         if (m_url != m_props->getStringValue("url")) {
245             // this effectively allows packages to migrate to new locations,
246             // although if we're going to rely on that feature we should
247             // maybe formalise it!
248             SG_LOG(SG_GENERAL, SG_WARN, "package downloaded from:" << m_url
249                    << " is now at: " << m_props->getStringValue("url"));
250         }
251     }
252   
253     m_url = m_props->getStringValue("url");
254
255     if (m_installRoot.isNull()) {
256         m_installRoot = m_root->path();
257         m_installRoot.append(id());
258         
259         Dir d(m_installRoot);
260         d.create(0755);
261     }
262 }
263
264 PackageRef Catalog::getPackageById(const std::string& aId) const
265 {
266     BOOST_FOREACH(PackageRef p, m_packages) {
267         if (p->id() == aId) {
268             return p;
269         }
270     }
271     
272     return NULL; // not found
273 }
274
275 std::string Catalog::id() const
276 {
277     return m_props->getStringValue("id");
278 }
279
280 std::string Catalog::url() const
281 {
282     return m_url;
283 }
284
285 std::string Catalog::description() const
286 {
287     return getLocalisedString(m_props, "description");
288 }
289     
290 SGPropertyNode* Catalog::properties() const
291 {
292     return m_props.ptr();
293 }
294
295 void Catalog::parseTimestamp()
296 {
297     SGPath timestampFile = m_installRoot;
298     timestampFile.append(".timestamp");
299     std::ifstream f(timestampFile.c_str(), std::ios::in);
300     f >> m_retrievedTime;
301 }
302
303 void Catalog::writeTimestamp()
304 {
305     SGPath timestampFile = m_installRoot;
306     timestampFile.append(".timestamp");
307     std::ofstream f(timestampFile.c_str(), std::ios::out | std::ios::trunc);
308     f << m_retrievedTime << std::endl;
309 }
310
311 unsigned int Catalog::ageInSeconds() const
312 {
313     time_t now;
314     time(&now);
315     int diff = ::difftime(now, m_retrievedTime);
316     return (diff < 0) ? 0 : diff;
317 }
318
319 bool Catalog::needsRefresh() const
320 {
321     unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
322     return (ageInSeconds() > maxAge);
323 }
324     
325 std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
326 {
327     if (aRoot->hasChild(m_root->getLocale())) {
328         const SGPropertyNode* localeRoot = aRoot->getChild(m_root->getLocale().c_str());
329         if (localeRoot->hasChild(aName)) {
330             return localeRoot->getStringValue(aName);
331         }
332     }
333     
334     return aRoot->getStringValue(aName);
335 }
336
337 void Catalog::refreshComplete(Delegate::FailureCode aReason)
338 {
339     m_root->catalogRefreshComplete(this, aReason);
340 }
341
342
343 } // of namespace pkg
344
345 } // of namespace simgear