Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
66
        fprintf(stderr, "strcmp([%s], [%s]) == %d\n", name, emcore_dir_entry_cache[i].name, strcmp(name, emcore_dir_entry_cache[i].name));
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
 
141
void cache_destroy(void)
142
{
143
#ifdef DEBUG
144
    fprintf(stderr, "Destroying cache...\n");
145
#endif
146
    while (emcore_dir_cache_length)
147
    {
148
        free(emcore_dir_entry_cache[--emcore_dir_cache_length].name);
149
    }
150
 
151
    free(emcore_dir_entry_cache);
152
#ifdef DEBUG
153
    fprintf(stderr, "Cache destroyed!\n");
154
#endif
155
}