-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.c
More file actions
242 lines (199 loc) · 6.39 KB
/
cache.c
File metadata and controls
242 lines (199 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <malloc.h>
#include <pthread.h>
#include <memory.h>
#include <fcntl.h>
#include <assert.h>
#include "cache.h"
#include "loggerWorkerThread.h"
// File cache implemented as singly linked list
struct fileCacheEntry {
/**
* The path to the file.
*/
char *path;
/**
* The contents of the file.
*/
string_t file;
/**
* The size of the file.
*/
size_t size;
/**
* Pointer to next file cache entry
*/
struct fileCacheEntry *next;
};
typedef struct fileCacheEntry *fileCacheEntry_t;
struct fileCache {
/**
* Used to control access to inside of file cache.
*/
pthread_mutex_t mutex;
/**
* Maximum bytes allowed in the file cache.
*/
size_t maxBytes;
/**
* Working directory used to open files.
*/
int workingDirectory;
/**
* Current number of bytes in the file cache.
*/
size_t currentBytes;
/**
* Pointer to first cache entry.
*/
fileCacheEntry_t entryList;
/**
* The logger used
*/
channel_t logger;
};
fileCache_t createFileCache(size_t maxBytes, int workingDirectory, channel_t logger) {
fileCache_t result = malloc(sizeof(struct fileCache));
pthread_mutex_init(&result->mutex, NULL);
result->maxBytes = maxBytes;
result->workingDirectory = workingDirectory;
result->currentBytes = 0;
result->entryList = NULL;
result->logger = logger;
return result;
}
void destroyFileCache(fileCache_t self) {
fileCacheEntry_t f = self->entryList;
// Destroy the file cache chain
while (f != NULL) {
fileCacheEntry_t next = f->next;
free(f->path);
destroyString(f->file);
free(f);
f = next;
}
pthread_mutex_destroy(&self->mutex);
free(self);
}
string_t fileCacheGetFile(fileCache_t cache, const char *path) {
string_t filename = stringFromCString(path);
pthread_mutex_lock(&cache->mutex);
// First search for the file name in the cache.
// previous is the pointer to the location pointing to f
// so we can change it if we move f.
fileCacheEntry_t *prev = &cache->entryList;
fileCacheEntry_t f = cache->entryList;
for (; f != NULL; f = *(prev = &f->next)) {
if (strcmp(path, f->path) == 0) break;
}
// Found the file!
if (f != NULL) {
// Remove it from the list
*prev = f->next;
// Insert it at front
f->next = cache->entryList;
cache->entryList = f;
// Copy the contents
string_t copied = stringCopy(f->file);
pthread_mutex_unlock(&cache->mutex);
string_t toLog = stringFromCString("[cache] found cached file: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
destroyString(filename);
return copied;
}
// unlock the mutex while reading the file
pthread_mutex_unlock(&cache->mutex);
string_t toLog = stringFromCString("[cache] Failed to find cached file. Reading from file system: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
int fd = openat(cache->workingDirectory, path, O_RDONLY);
// Didn't find the file.
if (fd < 0) {
toLog = stringFromCString("[cache] File not found on file system: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
destroyString(filename);
return NULL;
}
// Read file
string_t contents = createString();
char c;
while (read(fd, &c, 1) > 0) {
append(contents, c);
}
toLog = stringFromCString("[cache] File read from file system: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
// Lock cache to put the file into the cache
pthread_mutex_lock(&cache->mutex);
// Don't cache HUUUUUUGE files
if (stringLength(contents) > cache->maxBytes) {
pthread_mutex_unlock(&cache->mutex);
toLog = stringFromCString("[cache] File too big to cache: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
destroyString(filename);
return contents;
}
//Check to see if it's been inserted while we were reading the file.
for (f = cache->entryList; f != NULL; f = f->next) {
if (strcmp(path, f->path) == 0) {
// inserted!
pthread_mutex_unlock(&cache->mutex);
toLog = stringFromCString("[cache] file was cached by another thread: ");
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
destroyString(filename);
return contents;
}
}
// Time to insert it!
size_t newBytes = cache->currentBytes + stringLength(contents);
size_t cacheEntriesRemoved = 0;
// Remove stuff from the cache if necessary
if (newBytes > cache->maxBytes) {
size_t bytesSoFar = stringLength(contents);
// first find the first entry to remove.
for (f = *(prev = &cache->entryList); f != NULL; f = *(prev = &f->next)) {
if (bytesSoFar + f->size > cache->maxBytes) {
break;
}
bytesSoFar += f->size;
}
// Remove the last few entries.
*prev = NULL;
while (f != NULL) {
fileCacheEntry_t next = f->next;
free(f->path);
destroyString(f->file);
cache->currentBytes -= f->size;
free(f);
f = next;
cacheEntriesRemoved++;
}
}
assert(cache->currentBytes + stringLength(contents) <= cache->maxBytes);
// Add the new cache entry
fileCacheEntry_t newEntry = malloc(sizeof(struct fileCacheEntry));
newEntry->size = stringLength(contents);
newEntry->path = malloc(strlen(path) + 1);
strcpy(newEntry->path, path);
newEntry->file = stringCopy(contents);
newEntry->next = cache->entryList;
cache->entryList = newEntry;
cache->currentBytes += stringLength(contents);
pthread_mutex_unlock(&cache->mutex);
// Logging >.<
// should add some util functions to make it easier to log.
toLog = stringFromCString("[cache] Removed ");
string_t i = stringFromInt((int) cacheEntriesRemoved);
plusEqual(toLog, i);
destroyString(i);
i = stringFromCString(" entries from cache. Added ");
plusEqual(toLog, i);
destroyString(i);
plusEqual(toLog, filename);
logMessage(cache->logger, toLog);
destroyString(filename);
return contents;
}