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