diff options
Diffstat (limited to 'apps/open_plugin.c')
-rw-r--r-- | apps/open_plugin.c | 145 |
1 files changed, 97 insertions, 48 deletions
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 @@ | |||
32 | #define OP_EXT "opx" | 32 | #define OP_EXT "opx" |
33 | #define OP_LEN 4 | 33 | #define OP_LEN 4 |
34 | 34 | ||
35 | struct open_plugin_entry_t open_plugin_entry; | 35 | struct open_plugin_entry_t open_plugin_entry = {0}; |
36 | 36 | ||
37 | static const int op_entry_sz = sizeof(struct open_plugin_entry_t); | 37 | static const int op_entry_sz = sizeof(struct open_plugin_entry_t); |
38 | 38 | ||
39 | static int open_plugin_hash_get_entry(uint32_t hash, | ||
40 | struct open_plugin_entry_t *entry, | ||
41 | const char* dat_file); | ||
42 | |||
43 | static inline void op_clear_entry(struct open_plugin_entry_t *entry) | ||
44 | { | ||
45 | if (entry) | ||
46 | { | ||
47 | memset(entry, 0, op_entry_sz); | ||
48 | entry->lang_id = -1; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | static int op_update_dat(struct open_plugin_entry_t *entry) | ||
53 | { | ||
54 | int fd, fd1; | ||
55 | uint32_t hash; | ||
56 | |||
57 | if (!entry || entry->hash == 0) | ||
58 | return -1; | ||
59 | |||
60 | hash = entry->hash; | ||
61 | |||
62 | fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); | ||
63 | if (!fd) | ||
64 | return -1; | ||
65 | write(fd, entry, op_entry_sz); | ||
66 | |||
67 | fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY); | ||
68 | if (fd1) | ||
69 | { | ||
70 | while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz) | ||
71 | { | ||
72 | if (open_plugin_entry.hash != hash) | ||
73 | write(fd, &open_plugin_entry, op_entry_sz); | ||
74 | } | ||
75 | close(fd1); | ||
76 | remove(OPEN_PLUGIN_DAT); | ||
77 | } | ||
78 | close(fd); | ||
79 | |||
80 | rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); | ||
81 | |||
82 | op_clear_entry(&open_plugin_entry); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
39 | uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter) | 86 | uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter) |
40 | { | 87 | { |
41 | int len; | 88 | int len; |
42 | bool is_valid = false; | 89 | bool is_valid = false; |
43 | uint32_t hash; | 90 | uint32_t hash; |
44 | char *pos; | 91 | int32_t lang_id; |
45 | int fd = 0; | 92 | char *pos = "\0"; |
46 | int fd1 = 0; | 93 | |
94 | if(!key) | ||
95 | { | ||
96 | op_clear_entry(&open_plugin_entry); | ||
97 | return 0; | ||
98 | } | ||
47 | 99 | ||
48 | /*strlcpy(plug_entry.key, key, sizeof(plug_entry.key));*/ | 100 | lang_id = P2ID((unsigned char*)key); |
49 | open_plugin_entry.lang_id = P2ID((unsigned char*)key); | ||
50 | key = P2STR((unsigned char *)key); | 101 | key = P2STR((unsigned char *)key); |
51 | 102 | ||
52 | open_plugin_get_hash(key, &hash); | 103 | open_plugin_get_hash(key, &hash); |
53 | open_plugin_entry.hash = hash; | 104 | |
105 | |||
106 | if(open_plugin_entry.hash != hash) | ||
107 | { | ||
108 | /* the entry in ram needs saved */ | ||
109 | op_update_dat(&open_plugin_entry); | ||
110 | } | ||
54 | 111 | ||
55 | if (plugin) | 112 | if (plugin) |
56 | { | 113 | { |
@@ -74,49 +131,22 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p | |||
74 | else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0) | 131 | else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0) |
75 | { | 132 | { |
76 | is_valid = true; | 133 | is_valid = true; |
77 | /* path */ | 134 | open_plugin_hash_get_entry(0, &open_plugin_entry, plugin); |
78 | strlcpy(open_plugin_entry.path, | ||
79 | VIEWERS_DATA_DIR "/open_plugins." ROCK_EXT, OPEN_PLUGIN_BUFSZ); | ||
80 | /* parameter */ | ||
81 | strlcpy(open_plugin_entry.param, plugin, OPEN_PLUGIN_BUFSZ); | ||
82 | |||
83 | write(fd, &open_plugin_entry, op_entry_sz); | ||
84 | } | ||
85 | |||
86 | if (is_valid) | ||
87 | { | ||
88 | fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); | ||
89 | if (!fd) | ||
90 | return 0; | ||
91 | write(fd, &open_plugin_entry, op_entry_sz); | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | if (open_plugin_entry.lang_id != LANG_SHORTCUTS) | ||
96 | splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos); | ||
97 | return 0; | ||
98 | } | 135 | } |
99 | } | 136 | } |
100 | 137 | ||
101 | fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY); | 138 | if (!is_valid) |
102 | if (fd1) | ||
103 | { | 139 | { |
104 | while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz) | 140 | if (open_plugin_entry.lang_id != LANG_SHORTCUTS) |
105 | { | 141 | splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos); |
106 | if (open_plugin_entry.hash != hash) | 142 | op_clear_entry(&open_plugin_entry); |
107 | write(fd, &open_plugin_entry, op_entry_sz); | 143 | hash = 0; |
108 | } | ||
109 | close(fd1); | ||
110 | } | 144 | } |
111 | close(fd); | 145 | else |
112 | |||
113 | if(fd1) | ||
114 | { | 146 | { |
115 | remove(OPEN_PLUGIN_DAT); | 147 | open_plugin_entry.hash = hash; |
116 | rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); | 148 | open_plugin_entry.lang_id = lang_id; |
117 | } | 149 | } |
118 | else | ||
119 | hash = 0; | ||
120 | 150 | ||
121 | return hash; | 151 | return hash; |
122 | } | 152 | } |
@@ -140,20 +170,31 @@ void open_plugin_browse(const char *key) | |||
140 | open_plugin_add_path(key, tmp_buf, NULL); | 170 | open_plugin_add_path(key, tmp_buf, NULL); |
141 | } | 171 | } |
142 | 172 | ||
143 | static int open_plugin_hash_get_entry(uint32_t hash, struct open_plugin_entry_t *entry) | 173 | static int open_plugin_hash_get_entry(uint32_t hash, |
174 | struct open_plugin_entry_t *entry, | ||
175 | const char* dat_file) | ||
144 | { | 176 | { |
145 | int ret = -1, record = -1; | 177 | int ret = -1, record = -1; |
146 | 178 | ||
147 | if (entry) | 179 | if (entry) |
148 | { | 180 | { |
149 | int fd = open(OPEN_PLUGIN_DAT, O_RDONLY); | 181 | |
182 | if (hash != 0) | ||
183 | { | ||
184 | if(entry->hash == hash) /* hasn't been flushed yet? */ | ||
185 | return 0; | ||
186 | else | ||
187 | op_update_dat(&open_plugin_entry); | ||
188 | } | ||
189 | |||
190 | int fd = open(dat_file, O_RDONLY); | ||
150 | 191 | ||
151 | if (fd) | 192 | if (fd) |
152 | { | 193 | { |
153 | while (read(fd, entry, op_entry_sz) == op_entry_sz) | 194 | while (read(fd, entry, op_entry_sz) == op_entry_sz) |
154 | { | 195 | { |
155 | record++; | 196 | record++; |
156 | if (entry->hash == hash) | 197 | if (hash == 0 || entry->hash == hash) |
157 | { | 198 | { |
158 | ret = record; | 199 | ret = record; |
159 | break; | 200 | break; |
@@ -176,8 +217,8 @@ int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry) | |||
176 | uint32_t hash; | 217 | uint32_t hash; |
177 | key = P2STR((unsigned char *)key); | 218 | key = P2STR((unsigned char *)key); |
178 | 219 | ||
179 | open_plugin_get_hash(key, &hash); | 220 | open_plugin_get_hash(key, &hash); /* in open_plugin.h */ |
180 | return open_plugin_hash_get_entry(hash, entry); | 221 | return open_plugin_hash_get_entry(hash, entry, OPEN_PLUGIN_DAT); |
181 | } | 222 | } |
182 | 223 | ||
183 | int open_plugin_run(const char *key) | 224 | int open_plugin_run(const char *key) |
@@ -196,7 +237,15 @@ int open_plugin_run(const char *key) | |||
196 | if (path) | 237 | if (path) |
197 | ret = plugin_load(path, param); | 238 | ret = plugin_load(path, param); |
198 | 239 | ||
240 | if (ret != GO_TO_PLUGIN) | ||
241 | op_clear_entry(&open_plugin_entry); | ||
242 | |||
199 | return ret; | 243 | return ret; |
200 | } | 244 | } |
201 | 245 | ||
246 | void open_plugin_cache_flush(void) | ||
247 | { | ||
248 | op_update_dat(&open_plugin_entry); | ||
249 | } | ||
250 | |||
202 | #endif /* ndef __PCTOOL__ */ | 251 | #endif /* ndef __PCTOOL__ */ |