From 29fa47d43d0a9a0fc98fddfa315145ecc7727044 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 10 Mar 2021 19:07:06 -0500 Subject: Open Plugin cache plugin entry when possible leave plugin entries in ram and try not to save them unless necessary doesn't use more space just a bit of careful ordering with the buffer Change-Id: I1973e9ad4655c2544f596b37cee35601a0cffa94 --- apps/misc.c | 2 + apps/open_plugin.c | 145 +++++++++++++++++++++++++++++++++++------------------ apps/open_plugin.h | 1 + apps/root_menu.c | 3 ++ 4 files changed, 103 insertions(+), 48 deletions(-) diff --git a/apps/misc.c b/apps/misc.c index 3a8798fec0..96ad534c68 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -65,6 +65,7 @@ #include "viewport.h" #include "list.h" #include "fixedpoint.h" +#include "open_plugin.h" #include "debug.h" @@ -279,6 +280,7 @@ static void system_flush(void) { playlist_shutdown(); tree_flush(); + open_plugin_cache_flush(); call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */ } diff --git a/apps/open_plugin.c b/apps/open_plugin.c index 7448018ed9..2e9975adea 100644 --- a/apps/open_plugin.c +++ b/apps/open_plugin.c @@ -32,25 +32,82 @@ #define OP_EXT "opx" #define OP_LEN 4 -struct open_plugin_entry_t open_plugin_entry; +struct open_plugin_entry_t open_plugin_entry = {0}; static const int op_entry_sz = sizeof(struct open_plugin_entry_t); +static int open_plugin_hash_get_entry(uint32_t hash, + struct open_plugin_entry_t *entry, + const char* dat_file); + +static inline void op_clear_entry(struct open_plugin_entry_t *entry) +{ + if (entry) + { + memset(entry, 0, op_entry_sz); + entry->lang_id = -1; + } +} + +static int op_update_dat(struct open_plugin_entry_t *entry) +{ + int fd, fd1; + uint32_t hash; + + if (!entry || entry->hash == 0) + return -1; + + hash = entry->hash; + + fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (!fd) + return -1; + write(fd, entry, op_entry_sz); + + fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY); + if (fd1) + { + while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz) + { + if (open_plugin_entry.hash != hash) + write(fd, &open_plugin_entry, op_entry_sz); + } + close(fd1); + remove(OPEN_PLUGIN_DAT); + } + close(fd); + + rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); + + op_clear_entry(&open_plugin_entry); + return 0; +} + uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter) { int len; bool is_valid = false; uint32_t hash; - char *pos; - int fd = 0; - int fd1 = 0; + int32_t lang_id; + char *pos = "\0"; + + if(!key) + { + op_clear_entry(&open_plugin_entry); + return 0; + } - /*strlcpy(plug_entry.key, key, sizeof(plug_entry.key));*/ - open_plugin_entry.lang_id = P2ID((unsigned char*)key); + lang_id = P2ID((unsigned char*)key); key = P2STR((unsigned char *)key); open_plugin_get_hash(key, &hash); - open_plugin_entry.hash = hash; + + + if(open_plugin_entry.hash != hash) + { + /* the entry in ram needs saved */ + op_update_dat(&open_plugin_entry); + } if (plugin) { @@ -74,49 +131,22 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0) { is_valid = true; - /* path */ - strlcpy(open_plugin_entry.path, - VIEWERS_DATA_DIR "/open_plugins." ROCK_EXT, OPEN_PLUGIN_BUFSZ); - /* parameter */ - strlcpy(open_plugin_entry.param, plugin, OPEN_PLUGIN_BUFSZ); - - write(fd, &open_plugin_entry, op_entry_sz); - } - - if (is_valid) - { - fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (!fd) - return 0; - write(fd, &open_plugin_entry, op_entry_sz); - } - else - { - if (open_plugin_entry.lang_id != LANG_SHORTCUTS) - splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos); - return 0; + open_plugin_hash_get_entry(0, &open_plugin_entry, plugin); } } - fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY); - if (fd1) + if (!is_valid) { - while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz) - { - if (open_plugin_entry.hash != hash) - write(fd, &open_plugin_entry, op_entry_sz); - } - close(fd1); + if (open_plugin_entry.lang_id != LANG_SHORTCUTS) + splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos); + op_clear_entry(&open_plugin_entry); + hash = 0; } - close(fd); - - if(fd1) + else { - remove(OPEN_PLUGIN_DAT); - rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); + open_plugin_entry.hash = hash; + open_plugin_entry.lang_id = lang_id; } - else - hash = 0; return hash; } @@ -140,20 +170,31 @@ void open_plugin_browse(const char *key) open_plugin_add_path(key, tmp_buf, NULL); } -static int open_plugin_hash_get_entry(uint32_t hash, struct open_plugin_entry_t *entry) +static int open_plugin_hash_get_entry(uint32_t hash, + struct open_plugin_entry_t *entry, + const char* dat_file) { int ret = -1, record = -1; if (entry) { - int fd = open(OPEN_PLUGIN_DAT, O_RDONLY); + + if (hash != 0) + { + if(entry->hash == hash) /* hasn't been flushed yet? */ + return 0; + else + op_update_dat(&open_plugin_entry); + } + + int fd = open(dat_file, O_RDONLY); if (fd) { while (read(fd, entry, op_entry_sz) == op_entry_sz) { record++; - if (entry->hash == hash) + if (hash == 0 || entry->hash == hash) { ret = record; break; @@ -176,8 +217,8 @@ int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry) uint32_t hash; key = P2STR((unsigned char *)key); - open_plugin_get_hash(key, &hash); - return open_plugin_hash_get_entry(hash, entry); + open_plugin_get_hash(key, &hash); /* in open_plugin.h */ + return open_plugin_hash_get_entry(hash, entry, OPEN_PLUGIN_DAT); } int open_plugin_run(const char *key) @@ -196,7 +237,15 @@ int open_plugin_run(const char *key) if (path) ret = plugin_load(path, param); + if (ret != GO_TO_PLUGIN) + op_clear_entry(&open_plugin_entry); + return ret; } +void open_plugin_cache_flush(void) +{ + op_update_dat(&open_plugin_entry); +} + #endif /* ndef __PCTOOL__ */ diff --git a/apps/open_plugin.h b/apps/open_plugin.h index 9f20d7ffda..8c09c4ac58 100644 --- a/apps/open_plugin.h +++ b/apps/open_plugin.h @@ -59,6 +59,7 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry); void open_plugin_browse(const char *key); int open_plugin_run(const char *key); +void open_plugin_cache_flush(void); /* flush to disk */ #endif #endif /*ndef __PCTOOL__ */ diff --git a/apps/root_menu.c b/apps/root_menu.c index 8df9f87d44..f4f984b823 100644 --- a/apps/root_menu.c +++ b/apps/root_menu.c @@ -856,6 +856,9 @@ void root_menu(void) next_screen = load_plugin_screen(path, param); + if (next_screen != GO_TO_PLUGIN) + open_plugin_add_path(NULL, NULL, NULL); + /* shortcuts may take several trips through the GO_TO_PLUGIN case make sure we preserve and restore the origin */ if (next_screen == GO_TO_PREVIOUS && shortcut_origin != GO_TO_ROOT) -- cgit v1.2.3