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
66namespace ocpn {
67
68std::string get_basename(const char* path) {
69 wxString sep(wxFileName::GetPathSeparator());
70 auto parts = ocpn::split(path, sep.ToStdString());
71 return parts[parts.size() - 1];
72}
73
74static std::string metadata_path(const char* basename, bool create = false) {
75 wxFileName dirs(cache_path());
76 dirs.AppendDir("metadata");
77 if (create) {
78 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
79 }
80 wxFileName path(dirs.GetFullPath(), wxString(basename));
81 return path.GetFullPath().ToStdString();
82}
83
84bool store_metadata(const char* path) {
85 auto name = get_basename(path);
86 std::string dest = metadata_path(name.c_str(), true);
87 bool ok = ::copy_file(path, dest.c_str());
88 wxLogDebug("Storing metadata %s at %s: %s", path, dest.c_str(),
89 ok ? "ok" : "fail");
90 return ok;
91}
92
93std::string lookup_metadata(const char* name) {
94 if (name == 0) {
95 name = "ocpn-plugins.xml";
96 }
97 auto path = metadata_path(name);
98 return ocpn::exists(path) ? path : "";
99}
100
101bool store_tarball(const char* path, const char* basename) {
102 std::string dest = tarball_path(basename, true);
103 bool ok = ::copy_file(path, dest.c_str());
104 wxLogDebug("Storing tarball %s at %s: %s", path, dest.c_str(),
105 ok ? "ok" : "fail");
106 return ok;
107}
108
109std::string lookup_tarball(const char* uri) {
110 std::string basename = get_basename(uri);
111 std::string path = tarball_path(basename.c_str());
112 return ocpn::exists(path) ? path : "";
113}
114
116 wxFileName dirs(cache_path());
117 dirs.AppendDir("tarballs");
118 if (!dirs.DirExists()) {
119 return 0;
120 }
121 wxDir dir(dirs.GetFullPath());
122 wxString file;
123 unsigned count = 0;
124 bool cont = dir.GetFirst(&file);
125 while (cont) {
126 count += 1;
127 cont = dir.GetNext(&file);
128 }
129 return count;
130}
131
132unsigned long cache_size() {
133 wxFileName dirs(cache_path());
134 dirs.AppendDir("tarballs");
135 if (!dirs.DirExists()) {
136 return 0;
137 }
138 wxDir dir(dirs.GetFullPath());
139 wxString file;
140 wxULongLong total = 0;
141 bool cont = dir.GetFirst(&file);
142 while (cont) {
143 dirs.SetFullName(file);
144 wxFileName fn(dirs.GetFullPath());
145 if (fn.FileExists()) { // Consider only regular files. Should be no
146 // directories here, but one never knows...
147 auto size = fn.GetSize();
148 if (size == wxInvalidSize) {
149 wxLogMessage("Cannot stat file %s",
150 dirs.GetFullPath().ToStdString().c_str());
151 continue;
152 }
153
154 total += size;
155 }
156 cont = dir.GetNext(&file);
157 }
158 total /= (1024 * 1024);
159 return total.ToULong();
160}
161
162/* mock up definitions.*/
164 wxFileName dirs(cache_path());
165 dirs.AppendDir("tarballs");
166 if (!dirs.DirExists()) {
167 return;
168 }
169 wxDir dir(dirs.GetFullPath());
170 wxString file;
171 bool cont = dir.GetFirst(&file);
172 while (cont) {
173 dirs.SetFullName(file);
174 wxRemoveFile(dirs.GetFullPath());
175 ;
176 cont = dir.GetNext(&file);
177 }
178}
179
180} // namespace ocpn
wxString & GetPrivateDataDir()
Return dir path for opencpn.log, etc., respecting -c cli option.
Standard, mostly strings utilities.
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)
void cache_clear()
Remove all files in cache:
unsigned cache_file_count()
Return number of files in cache.
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.