| 770 |
user890104 |
1 |
//
|
|
|
2 |
//
|
|
|
3 |
// Copyright 2011 user890104
|
|
|
4 |
//
|
|
|
5 |
//
|
|
|
6 |
// This file is part of emCORE.
|
|
|
7 |
//
|
|
|
8 |
// emCORE is free software: you can redistribute it and/or
|
|
|
9 |
// modify it under the terms of the GNU General Public License as
|
|
|
10 |
// published by the Free Software Foundation, either version 2 of the
|
|
|
11 |
// License, or (at your option) any later version.
|
|
|
12 |
//
|
|
|
13 |
// emCORE is distributed in the hope that it will be useful,
|
|
|
14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
16 |
// See the GNU General Public License for more details.
|
|
|
17 |
//
|
|
|
18 |
// You should have received a copy of the GNU General Public License along
|
|
|
19 |
// with emCORE. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
//
|
|
|
21 |
//
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
#include "global.h"
|
|
|
25 |
|
|
|
26 |
#include "cache.h"
|
|
|
27 |
#include "emcore.h"
|
|
|
28 |
#include "util.h"
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
struct emcore_dir_entry* emcore_dir_entry_cache = NULL;
|
|
|
32 |
size_t emcore_dir_cache_length = 0;
|
|
|
33 |
|
|
|
34 |
void cache_init(void)
|
|
|
35 |
{
|
|
|
36 |
int32_t datetime = unix_ts_to_fat_time(time(NULL));
|
|
|
37 |
#ifdef DEBUG
|
|
|
38 |
fprintf(stderr, "Init cache...\n");
|
|
|
39 |
#endif
|
|
|
40 |
emcore_dir_entry_cache = calloc(sizeof(*emcore_dir_entry_cache), 1);
|
|
|
41 |
|
|
|
42 |
emcore_dir_entry_cache->name = strdup("/");
|
|
|
43 |
emcore_dir_entry_cache->attributes = 0x10;
|
|
|
44 |
emcore_dir_entry_cache->size = 0x1000;
|
|
|
45 |
emcore_dir_entry_cache->wrtdate = datetime >> 0x10;
|
|
|
46 |
emcore_dir_entry_cache->wrttime = datetime & 0xffff;
|
|
|
47 |
|
|
|
48 |
emcore_dir_cache_length = 1;
|
|
|
49 |
#ifdef DEBUG
|
|
|
50 |
fprintf(stderr, "Cache init done!\n");
|
|
|
51 |
#endif
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
struct emcore_dir_entry* cache_get(const char* name)
|
|
|
55 |
{
|
|
|
56 |
size_t i;
|
|
|
57 |
|
|
|
58 |
if (!emcore_dir_cache_length)
|
|
|
59 |
{
|
|
|
60 |
return NULL;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
for (i = 0; i < emcore_dir_cache_length; ++i)
|
|
|
64 |
{
|
|
|
65 |
#ifdef DEBUG2
|
| 782 |
user890104 |
66 |
fprintf(stderr, "cache_get: strcmp([%s], [%s]) == %d\n", name, emcore_dir_entry_cache[i].name, strcmp(name, emcore_dir_entry_cache[i].name));
|
| 770 |
user890104 |
67 |
#endif
|
|
|
68 |
if (0 == strcmp(name, emcore_dir_entry_cache[i].name))
|
|
|
69 |
{
|
|
|
70 |
return &emcore_dir_entry_cache[i];
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
return NULL;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
void cache_insert(const char* dir_name, const struct emcore_dir_entry* entry)
|
|
|
78 |
{
|
|
|
79 |
#ifdef DEBUG2
|
|
|
80 |
fprintf(stderr, "CACHE INSERT: dir=[%s], entry=[%s]\n", dir_name, entry->name);
|
|
|
81 |
#endif
|
|
|
82 |
void* new_ptr;
|
|
|
83 |
struct emcore_dir_entry* cache_entry;
|
|
|
84 |
char* new_name;
|
|
|
85 |
size_t new_name_len = 1;
|
|
|
86 |
|
|
|
87 |
new_name_len += strlen(dir_name) + strlen(entry->name);
|
|
|
88 |
|
|
|
89 |
if (strcmp(dir_name, "/") != 0)
|
|
|
90 |
{
|
|
|
91 |
++new_name_len;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
new_name = calloc(sizeof(char), new_name_len);
|
|
|
95 |
strcat(new_name, dir_name);
|
|
|
96 |
|
|
|
97 |
if (strcmp(dir_name, "/") != 0)
|
|
|
98 |
{
|
|
|
99 |
strcat(new_name, "/");
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
strcat(new_name, entry->name);
|
|
|
103 |
|
|
|
104 |
if (cache_get(new_name))
|
|
|
105 |
{
|
|
|
106 |
free(new_name);
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
new_ptr = realloc(emcore_dir_entry_cache,
|
|
|
111 |
sizeof(*emcore_dir_entry_cache) * (emcore_dir_cache_length + 1));
|
|
|
112 |
|
|
|
113 |
if (!new_ptr)
|
|
|
114 |
{
|
|
|
115 |
free(new_name);
|
|
|
116 |
return;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
emcore_dir_entry_cache = new_ptr;
|
|
|
120 |
|
|
|
121 |
cache_entry = malloc(sizeof(*cache_entry));
|
|
|
122 |
|
|
|
123 |
if (!cache_entry)
|
|
|
124 |
{
|
|
|
125 |
free(new_name);
|
|
|
126 |
return;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
memcpy(cache_entry, entry, sizeof(*entry));
|
|
|
130 |
|
|
|
131 |
cache_entry->name = new_name;
|
|
|
132 |
|
|
|
133 |
memcpy(emcore_dir_entry_cache + emcore_dir_cache_length, cache_entry, sizeof(*cache_entry));
|
|
|
134 |
|
|
|
135 |
#ifdef DEBUG
|
|
|
136 |
fprintf(stderr, "Inserting [%s] to cache\n", emcore_dir_entry_cache[emcore_dir_cache_length].name);
|
|
|
137 |
#endif
|
|
|
138 |
++emcore_dir_cache_length;
|
|
|
139 |
}
|
|
|
140 |
|
| 782 |
user890104 |
141 |
void cache_remove(const char* name)
|
|
|
142 |
{
|
|
|
143 |
size_t i;
|
|
|
144 |
void* new_ptr;
|
|
|
145 |
|
|
|
146 |
for (i = 0; i < emcore_dir_cache_length; ++i)
|
|
|
147 |
{
|
|
|
148 |
#ifdef DEBUG2
|
|
|
149 |
fprintf(stderr, "cache_remove: strcmp([%s], [%s]) == %d\n", name, emcore_dir_entry_cache[i].name, strcmp(name, emcore_dir_entry_cache[i].name));
|
|
|
150 |
#endif
|
|
|
151 |
if (0 == strcmp(name, emcore_dir_entry_cache[i].name))
|
|
|
152 |
{
|
|
|
153 |
#ifdef DEBUG2
|
|
|
154 |
fprintf(stderr, "CACHE REMOVE: [%s]\n", name);
|
|
|
155 |
#endif
|
|
|
156 |
free(emcore_dir_entry_cache[i].name);
|
|
|
157 |
|
|
|
158 |
if (emcore_dir_cache_length > i + 1) {
|
|
|
159 |
memcpy(emcore_dir_entry_cache + i, emcore_dir_entry_cache + i + 1, (emcore_dir_cache_length - i - 1) * sizeof(*emcore_dir_entry_cache));
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
--emcore_dir_cache_length;
|
|
|
163 |
|
|
|
164 |
new_ptr = realloc(emcore_dir_entry_cache,
|
|
|
165 |
sizeof(*emcore_dir_entry_cache) * (emcore_dir_cache_length));
|
|
|
166 |
|
|
|
167 |
if (!new_ptr)
|
|
|
168 |
{
|
|
|
169 |
return;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
emcore_dir_entry_cache = new_ptr;
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
}
|
|
|
177 |
|
| 770 |
user890104 |
178 |
void cache_destroy(void)
|
|
|
179 |
{
|
|
|
180 |
#ifdef DEBUG
|
|
|
181 |
fprintf(stderr, "Destroying cache...\n");
|
|
|
182 |
#endif
|
|
|
183 |
while (emcore_dir_cache_length)
|
|
|
184 |
{
|
|
|
185 |
free(emcore_dir_entry_cache[--emcore_dir_cache_length].name);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
free(emcore_dir_entry_cache);
|
|
|
189 |
#ifdef DEBUG
|
|
|
190 |
fprintf(stderr, "Cache destroyed!\n");
|
|
|
191 |
#endif
|
|
|
192 |
}
|
| 782 |
user890104 |
193 |
|
|
|
194 |
#ifdef DEBUG2
|
|
|
195 |
void cache_dump(void)
|
|
|
196 |
{
|
|
|
197 |
size_t i;
|
|
|
198 |
|
|
|
199 |
if (!emcore_dir_cache_length)
|
|
|
200 |
{
|
|
|
201 |
return;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
for (i = 0; i < emcore_dir_cache_length; ++i)
|
|
|
205 |
{
|
|
|
206 |
fprintf(stderr, "cache_dump: [%s] 0x%08x %d %d %lu\n", emcore_dir_entry_cache[i].name, emcore_dir_entry_cache[i].attributes, emcore_dir_entry_cache[i].size, emcore_dir_entry_cache[i].startcluster, fat_time_to_unix_ts(emcore_dir_entry_cache[i].wrtdate, emcore_dir_entry_cache[i].wrttime));
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
#endif
|