OpenCPN Partial API docs
Loading...
Searching...
No Matches
plugin_cache.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2019 Alec Leamas *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include <fstream>
26
27#include <wx/dir.h>
28#include <wx/filename.h>
29#include <wx/filefn.h>
30
31#include "model/base_platform.h"
32#include "model/ocpn_utils.h"
33#include "model/plugin_cache.h"
34
35#ifdef __ANDROID__
36#include "androidUTIL.h"
37#endif
38
39static std::string cache_path() {
40 wxFileName path;
41 path.AssignDir(g_BasePlatform->GetPrivateDataDir());
42 path.AppendDir("plugins");
43 path.AppendDir("cache");
44 return path.GetFullPath().ToStdString();
45}
46
47static std::string tarball_path(const char* basename, bool create = false) {
48 wxFileName dirs(cache_path());
49 dirs.AppendDir("tarballs");
50 if (create) {
51 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
52 }
53 static const auto kSeparator = wxFileName::GetPathSeparator();
54 wxFileName path(dirs.GetFullPath() + kSeparator + wxString(basename));
55 return path.GetFullPath().ToStdString();
56}
57
58static bool copy_file(const char* src_path, const char* dest_path) {
59#ifdef __ANDROID__
60 return AndroidSecureCopyFile(src_path, dest_path);
61#else
62 return wxCopyFile(src_path, dest_path);
63#endif
64}
65
66static std::string get_basename(const char* path) {
67 wxString sep(wxFileName::GetPathSeparator());
68 // To parse standard network url, use "/"
69 if (ocpn::startswith(path, "http")) sep = "/";
70 auto parts = ocpn::split(path, sep.ToStdString());
71 return parts[parts.size() - 1];
72}
73
74namespace ocpn {
75
76static std::string metadata_path(const char* basename, bool create = false) {
77 wxFileName dirs(cache_path());
78 dirs.AppendDir("metadata");
79 if (create) {
80 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
81 }
82 wxFileName path(dirs.GetFullPath(), wxString(basename));
83 return path.GetFullPath().ToStdString();
84}
85
86bool store_metadata(const char* path) {
87 auto name = get_basename(path);
88 std::string dest = metadata_path(name.c_str(), true);
89 bool ok = ::copy_file(path, dest.c_str());
90 wxLogDebug("Storing metadata %s at %s: %s", path, dest.c_str(),
91 ok ? "ok" : "fail");
92 return ok;
93}
94
95std::string lookup_metadata(const char* name) {
96 if (name == 0) {
97 name = "ocpn-plugins.xml";
98 }
99 auto path = metadata_path(name);
100 return ocpn::exists(path) ? path : "";
101}
102
103bool store_tarball(const char* path, const char* basename) {
104 std::string dest = tarball_path(basename, true);
105 bool ok = ::copy_file(path, dest.c_str());
106 wxLogDebug("Storing tarball %s at %s: %s", path, dest.c_str(),
107 ok ? "ok" : "fail");
108 return ok;
109}
110
111std::string lookup_tarball(const char* uri) {
112 std::string basename = get_basename(uri);
113 std::string path = tarball_path(basename.c_str());
114 return ocpn::exists(path) ? path : "";
115}
116
118 wxFileName dirs(cache_path());
119 dirs.AppendDir("tarballs");
120 if (!dirs.DirExists()) {
121 return 0;
122 }
123 wxDir dir(dirs.GetFullPath());
124 wxString file;
125 unsigned count = 0;
126 bool cont = dir.GetFirst(&file);
127 while (cont) {
128 count += 1;
129 cont = dir.GetNext(&file);
130 }
131 return count;
132}
133
134unsigned long cache_size() {
135 wxFileName dirs(cache_path());
136 dirs.AppendDir("tarballs");
137 if (!dirs.DirExists()) {
138 return 0;
139 }
140 wxDir dir(dirs.GetFullPath());
141 wxString file;
142 wxULongLong total = 0;
143 bool cont = dir.GetFirst(&file);
144 while (cont) {
145 dirs.SetFullName(file);
146 wxFileName fn(dirs.GetFullPath());
147 if (fn.FileExists()) { // Consider only regular files. Should be no
148 // directories here, but one never knows...
149 auto size = fn.GetSize();
150 if (size == wxInvalidSize) {
151 wxLogMessage("Cannot stat file %s",
152 dirs.GetFullPath().ToStdString().c_str());
153 continue;
154 }
155
156 total += size;
157 }
158 cont = dir.GetNext(&file);
159 }
160 total /= (1024 * 1024);
161 return total.ToULong();
162}
163
164/* mock up definitions.*/
166 wxFileName dirs(cache_path());
167 dirs.AppendDir("tarballs");
168 if (!dirs.DirExists()) {
169 return;
170 }
171 wxDir dir(dirs.GetFullPath());
172 wxString file;
173 bool cont = dir.GetFirst(&file);
174 while (cont) {
175 dirs.SetFullName(file);
176 wxRemoveFile(dirs.GetFullPath());
177 ;
178 cont = dir.GetNext(&file);
179 }
180}
181
182} // namespace ocpn
wxString & GetPrivateDataDir()
Return dir path for opencpn.log, etc., respecting -c cli option.
Standard, mostly strings utilities.
Definition datetime.cpp:31
std::string lookup_tarball(const char *uri)
Get path to tarball in cache for given filename.
std::string lookup_metadata(const char *name)
Get metadata path for a given name defaulting to ocpn-plugins.xml)
bool startswith(const std::string &str, const std::string &prefix)
Return true if s starts with given prefix.
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
void cache_clear()
Remove all files in cache:
void copy_file(const std::string &src_path, const std::string &dest_path)
Copy file contents in path src_path to dest_path.
unsigned cache_file_count()
Return number of files in cache.
bool exists(const std::string &name)
bool store_metadata(const char *path)
Store metadata in metadata cache, return success/fail.
bool store_tarball(const char *path, const char *basename)
Store a tarball in tarball cache, return success/fail.
unsigned long cache_size()
Return total size of files in cache in kbytes.
Miscellaneous utilities, many of which string related.