Subversion Repositories freemyipod

Rev

Rev 784 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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