28#include <wx/filename.h>
31#include "base_platform.h"
32#include "ocpn_utils.h"
33#include "plugin_cache.h"
36#include "androidUTIL.h"
41static std::string cache_path() {
43 path.AssignDir(g_BasePlatform->GetPrivateDataDir());
44 path.AppendDir(
"plugins");
45 path.AppendDir(
"cache");
46 return path.GetFullPath().ToStdString();
49static std::string tarball_path(
const char* basename,
bool create =
false) {
50 wxFileName dirs(cache_path());
51 dirs.AppendDir(
"tarballs");
53 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
55 wxFileName path(dirs.GetFullPath(), wxString(basename));
56 return path.GetFullPath().ToStdString();
59static bool copy_file(
const char* src_path,
const char* dest_path) {
61 return AndroidSecureCopyFile(src_path, dest_path);
63 return wxCopyFile(src_path, dest_path);
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];
75static std::string metadata_path(
const char* basename,
bool create =
false) {
76 wxFileName dirs(cache_path());
77 dirs.AppendDir(
"metadata");
79 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
81 wxFileName path(dirs.GetFullPath(), wxString(basename));
82 return path.GetFullPath().ToStdString();
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(),
96 name =
"ocpn-plugins.xml";
98 auto path = metadata_path(name);
99 return ocpn::exists(path) ? path :
"";
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(),
111 std::string basename = get_basename(uri);
112 std::string path = tarball_path(basename.c_str());
113 return ocpn::exists(path) ? path :
"";
117 wxFileName dirs(cache_path());
118 dirs.AppendDir(
"tarballs");
119 if (!dirs.DirExists()) {
122 wxDir dir(dirs.GetFullPath());
125 bool cont = dir.GetFirst(&file);
128 cont = dir.GetNext(&file);
134 wxFileName dirs(cache_path());
135 dirs.AppendDir(
"tarballs");
136 if (!dirs.DirExists()) {
139 wxDir dir(dirs.GetFullPath());
141 wxULongLong total = 0;
142 bool cont = dir.GetFirst(&file);
144 dirs.SetFullName(file);
145 wxFileName fn(dirs.GetFullPath());
146 if (fn.FileExists()) {
148 auto size = fn.GetSize();
149 if (size == wxInvalidSize) {
150 wxLogMessage(
"Cannot stat file %s",
151 dirs.GetFullPath().ToStdString().c_str());
157 cont = dir.GetNext(&file);
159 total /= (1024 * 1024);
160 return total.ToULong();
165 wxFileName dirs(cache_path());
166 dirs.AppendDir(
"tarballs");
167 if (!dirs.DirExists()) {
170 wxDir dir(dirs.GetFullPath());
172 bool cont = dir.GetFirst(&file);
174 dirs.SetFullName(file);
175 wxRemoveFile(dirs.GetFullPath());
177 cont = dir.GetNext(&file);
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.