OpenCPN Partial API docs
All Classes Namespaces Functions Variables Pages
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 "base_platform.h"
32#include "ocpn_utils.h"
33#include "plugin_cache.h"
34
35#ifdef __ANDROID__
36#include "androidUTIL.h"
37#endif
38
39extern BasePlatform* g_BasePlatform;
40
41static std::string cache_path() {
42 wxFileName path;
43 path.AssignDir(g_BasePlatform->GetPrivateDataDir());
44 path.AppendDir("plugins");
45 path.AppendDir("cache");
46 return path.GetFullPath().ToStdString();
47}
48
49static std::string tarball_path(const char* basename, bool create = false) {
50 wxFileName dirs(cache_path());
51 dirs.AppendDir("tarballs");
52 if (create) {
53 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
54 }
55 wxFileName path(dirs.GetFullPath(), wxString(basename));
56 return path.GetFullPath().ToStdString();
57}
58
59static bool copy_file(const char* src_path, const char* dest_path) {
60#ifdef __ANDROID__
61 return AndroidSecureCopyFile(src_path, dest_path);
62#else
63 return wxCopyFile(src_path, dest_path);
64#endif
65}
66
67namespace ocpn {
68
69std::string get_basename(const char* path) {
70 wxString sep(wxFileName::GetPathSeparator());
71 auto parts = ocpn::split(path, sep.ToStdString());
72 return parts[parts.size() - 1];
73}
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
Standard, mostly strings utilities.
Definition: ocpn_utils.cpp:41
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.