28 #include <wx/filename.h>
29 #include <wx/filefn.h>
31 #include "model/base_platform.h"
32 #include "model/ocpn_utils.h"
33 #include "model/plugin_cache.h"
36 #include "androidUTIL.h"
39 static std::string cache_path() {
42 path.AppendDir(
"plugins");
43 path.AppendDir(
"cache");
44 return path.GetFullPath().ToStdString();
47 static std::string tarball_path(
const char* basename,
bool create =
false) {
48 wxFileName dirs(cache_path());
49 dirs.AppendDir(
"tarballs");
51 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
53 static const auto kSeparator = wxFileName::GetPathSeparator();
54 wxFileName path(dirs.GetFullPath() + kSeparator + wxString(basename));
55 return path.GetFullPath().ToStdString();
58 static bool copy_file(
const char* src_path,
const char* dest_path) {
60 return AndroidSecureCopyFile(src_path, dest_path);
62 return wxCopyFile(src_path, dest_path);
68 std::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];
74 static std::string metadata_path(
const char* basename,
bool create =
false) {
75 wxFileName dirs(cache_path());
76 dirs.AppendDir(
"metadata");
78 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
80 wxFileName path(dirs.GetFullPath(), wxString(basename));
81 return path.GetFullPath().ToStdString();
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(),
95 name =
"ocpn-plugins.xml";
97 auto path = metadata_path(name);
98 return ocpn::exists(path) ? path :
"";
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(),
110 std::string basename = get_basename(uri);
111 std::string path = tarball_path(basename.c_str());
112 return ocpn::exists(path) ? path :
"";
116 wxFileName dirs(cache_path());
117 dirs.AppendDir(
"tarballs");
118 if (!dirs.DirExists()) {
121 wxDir dir(dirs.GetFullPath());
124 bool cont = dir.GetFirst(&file);
127 cont = dir.GetNext(&file);
133 wxFileName dirs(cache_path());
134 dirs.AppendDir(
"tarballs");
135 if (!dirs.DirExists()) {
138 wxDir dir(dirs.GetFullPath());
140 wxULongLong total = 0;
141 bool cont = dir.GetFirst(&file);
143 dirs.SetFullName(file);
144 wxFileName fn(dirs.GetFullPath());
145 if (fn.FileExists()) {
147 auto size = fn.GetSize();
148 if (size == wxInvalidSize) {
149 wxLogMessage(
"Cannot stat file %s",
150 dirs.GetFullPath().ToStdString().c_str());
156 cont = dir.GetNext(&file);
158 total /= (1024 * 1024);
159 return total.ToULong();
164 wxFileName dirs(cache_path());
165 dirs.AppendDir(
"tarballs");
166 if (!dirs.DirExists()) {
169 wxDir dir(dirs.GetFullPath());
171 bool cont = dir.GetFirst(&file);
173 dirs.SetFullName(file);
174 wxRemoveFile(dirs.GetFullPath());
176 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.