28#include <wx/filename.h>
31#include "model/base_platform.h"
33#include "model/plugin_cache.h"
36#include "androidUTIL.h"
39static std::string cache_path() {
42 path.AppendDir(
"plugins");
43 path.AppendDir(
"cache");
44 return path.GetFullPath().ToStdString();
47static 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();
58static 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);
66static std::string get_basename(
const char* path) {
67 wxString sep(wxFileName::GetPathSeparator());
71 return parts[parts.size() - 1];
76static std::string metadata_path(
const char* basename,
bool create =
false) {
77 wxFileName dirs(cache_path());
78 dirs.AppendDir(
"metadata");
80 dirs.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
82 wxFileName path(dirs.GetFullPath(), wxString(basename));
83 return path.GetFullPath().ToStdString();
87 auto name = get_basename(path);
88 std::string dest = metadata_path(name.c_str(),
true);
90 wxLogDebug(
"Storing metadata %s at %s: %s", path, dest.c_str(),
97 name =
"ocpn-plugins.xml";
99 auto path = metadata_path(name);
104 std::string dest = tarball_path(basename,
true);
106 wxLogDebug(
"Storing tarball %s at %s: %s", path, dest.c_str(),
112 std::string basename = get_basename(uri);
113 std::string path = tarball_path(basename.c_str());
118 wxFileName dirs(cache_path());
119 dirs.AppendDir(
"tarballs");
120 if (!dirs.DirExists()) {
123 wxDir dir(dirs.GetFullPath());
126 bool cont = dir.GetFirst(&file);
129 cont = dir.GetNext(&file);
135 wxFileName dirs(cache_path());
136 dirs.AppendDir(
"tarballs");
137 if (!dirs.DirExists()) {
140 wxDir dir(dirs.GetFullPath());
142 wxULongLong total = 0;
143 bool cont = dir.GetFirst(&file);
145 dirs.SetFullName(file);
146 wxFileName fn(dirs.GetFullPath());
147 if (fn.FileExists()) {
149 auto size = fn.GetSize();
150 if (size == wxInvalidSize) {
151 wxLogMessage(
"Cannot stat file %s",
152 dirs.GetFullPath().ToStdString().c_str());
158 cont = dir.GetNext(&file);
160 total /= (1024 * 1024);
161 return total.ToULong();
166 wxFileName dirs(cache_path());
167 dirs.AppendDir(
"tarballs");
168 if (!dirs.DirExists()) {
171 wxDir dir(dirs.GetFullPath());
173 bool cont = dir.GetFirst(&file);
175 dirs.SetFullName(file);
176 wxRemoveFile(dirs.GetFullPath());
178 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)
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.