blob: 313102b9a1c6492485c047c0c87454c74f8fefe6 [file] [log] [blame]
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Miika Pekkarinen
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Miika Pekkarinenab78b042005-10-07 17:38:05 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/* TODO:
23 - Allow cache live updating while transparent rebuild is running.
Miika Pekkarinenab78b042005-10-07 17:38:05 +000024*/
25
26#include "config.h"
27
Miika Pekkarinenab78b042005-10-07 17:38:05 +000028#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <stdbool.h>
Bertrik Sikken28434692008-04-28 16:18:04 +000032#include <stdlib.h>
Miika Pekkarinenab78b042005-10-07 17:38:05 +000033#include "debug.h"
Miika Pekkarinenab78b042005-10-07 17:38:05 +000034#include "system.h"
35#include "logf.h"
36#include "dircache.h"
37#include "thread.h"
38#include "kernel.h"
39#include "usb.h"
40#include "file.h"
Miika Pekkarinen5127cfa2006-05-30 18:13:18 +000041#include "buffer.h"
Miika Pekkarinen52d827a2008-03-11 19:39:26 +000042#include "dir.h"
Jonathan Gordone73f2872007-09-02 13:24:51 +000043#if CONFIG_RTC
44#include "time.h"
45#include "timefuncs.h"
46#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +000047
48/* Queue commands. */
49#define DIRCACHE_BUILD 1
Miika Pekkarinen4b2aa982005-10-20 08:33:59 +000050#define DIRCACHE_STOP 2
Miika Pekkarinenab78b042005-10-07 17:38:05 +000051
Miika Pekkarinenab78b042005-10-07 17:38:05 +000052#define MAX_OPEN_DIRS 8
Bertrik Sikkendb2d61f2008-04-20 10:24:15 +000053static DIR_CACHED opendirs[MAX_OPEN_DIRS];
Miika Pekkarinenab78b042005-10-07 17:38:05 +000054
55static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
56static struct dircache_entry *dircache_root;
Miika Pekkarinen52d827a2008-03-11 19:39:26 +000057#ifdef HAVE_MULTIVOLUME
58static struct dircache_entry *append_position;
59#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +000060
61static bool dircache_initialized = false;
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +000062static bool dircache_initializing = false;
Miika Pekkarinenab78b042005-10-07 17:38:05 +000063static bool thread_enabled = false;
64static unsigned long allocated_size = DIRCACHE_LIMIT;
65static unsigned long dircache_size = 0;
Miika Pekkarinende281db2006-01-31 10:08:53 +000066static unsigned long entry_count = 0;
Miika Pekkarinenab78b042005-10-07 17:38:05 +000067static unsigned long reserve_used = 0;
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +000068static unsigned int cache_build_ticks = 0;
Miika Pekkarinen39c597b2008-01-13 19:13:37 +000069static unsigned long appflags = 0;
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +000070static char dircache_cur_path[MAX_PATH*2];
Miika Pekkarinenab78b042005-10-07 17:38:05 +000071
Michael Sevakis84f5c5c2007-10-16 22:00:51 +000072static struct event_queue dircache_queue;
Jonathan Gordonb0d1bb82007-01-17 10:25:11 +000073static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
Miika Pekkarinenab78b042005-10-07 17:38:05 +000074static const char dircache_thread_name[] = "dircache";
75
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +000076static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
77static int fdbind_idx = 0;
78
Miika Pekkarinenab78b042005-10-07 17:38:05 +000079/* --- Internal cache structure control functions --- */
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +000080
81/**
82 * Internal function to allocate a new dircache_entry from memory.
83 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +000084static struct dircache_entry* allocate_entry(void)
85{
86 struct dircache_entry *next_entry;
87
88 if (dircache_size > allocated_size - MAX_PATH*2)
89 {
90 logf("size limit reached");
91 return NULL;
92 }
93
94 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
Miika Pekkarinende281db2006-01-31 10:08:53 +000095#ifdef ROCKBOX_STRICT_ALIGN
96 /* Make sure the entry is long aligned. */
97 if ((long)next_entry & 0x03)
98 {
Miika Pekkarinende281db2006-01-31 10:08:53 +000099 dircache_size += 4 - ((long)next_entry & 0x03);
Miika Pekkarinen7c4e0c82006-03-26 11:33:42 +0000100 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
Miika Pekkarinende281db2006-01-31 10:08:53 +0000101 }
102#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000103 next_entry->name_len = 0;
104 next_entry->d_name = NULL;
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000105 next_entry->up = NULL;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000106 next_entry->down = NULL;
107 next_entry->next = NULL;
Miika Pekkarinende281db2006-01-31 10:08:53 +0000108
109 dircache_size += sizeof(struct dircache_entry);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000110
111 return next_entry;
112}
113
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000114/**
115 * Internal function to allocate a dircache_entry and set
116 * ->next entry pointers.
117 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000118static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
119{
120 struct dircache_entry *next_entry;
121
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000122 if ( (next_entry = allocate_entry()) == NULL)
123 return NULL;
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000124 next_entry->up = ce->up;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000125 ce->next = next_entry;
126
127 return next_entry;
128}
129
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000130/*
131 * Internal function to allocate a dircache_entry and set
132 * ->down entry pointers.
133 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000134static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
135{
136 struct dircache_entry *next_entry;
137
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000138 if ( (next_entry = allocate_entry()) == NULL)
139 return NULL;
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000140 next_entry->up = ce;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000141 ce->down = next_entry;
142
143 return next_entry;
144}
145
146/* This will eat ~30 KiB of memory!
147 * We should probably use that as additional reserve buffer in future. */
148#define MAX_SCAN_DEPTH 16
149static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
150
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000151/**
Miika Pekkarinene4c0ef32006-03-22 17:09:13 +0000152 * Returns true if there is an event waiting in the queue
153 * that requires the current operation to be aborted.
154 */
155static bool check_event_queue(void)
156{
Michael Sevakisa9b2fb52007-10-16 01:25:17 +0000157 struct queue_event ev;
Miika Pekkarinene4c0ef32006-03-22 17:09:13 +0000158
159 queue_wait_w_tmo(&dircache_queue, &ev, 0);
160 switch (ev.id)
161 {
162 case DIRCACHE_STOP:
163 case SYS_USB_CONNECTED:
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000164#ifdef HAVE_HOTSWAP
165 case SYS_FS_CHANGED:
166#endif
Miika Pekkarinene4c0ef32006-03-22 17:09:13 +0000167 /* Put the event back into the queue. */
168 queue_post(&dircache_queue, ev.id, ev.data);
169 return true;
170 }
171
172 return false;
173}
174
175/**
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000176 * Internal function to iterate a path.
177 */
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000178static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000179{
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000180#ifdef SIMULATOR
Kevin Ferrare011a3252007-07-20 17:06:55 +0000181 while ( ( td->entry = readdir_uncached(td->dir) ) )
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000182#else
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000183 while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000184#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000185 {
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000186#ifdef SIMULATOR
187 if (!strcmp(".", td->entry->d_name) ||
188 !strcmp("..", td->entry->d_name))
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000189 {
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000190 continue;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000191 }
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000192
193 td->ce->attribute = td->entry->attribute;
Steve Bavin74db69e2007-02-01 17:15:50 +0000194 td->ce->name_len = strlen(td->entry->d_name) + 1;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000195 td->ce->d_name = ((char *)dircache_root+dircache_size);
196 td->ce->size = td->entry->size;
197 td->ce->wrtdate = td->entry->wrtdate;
198 td->ce->wrttime = td->entry->wrttime;
199 memcpy(td->ce->d_name, td->entry->d_name, td->ce->name_len);
200#else
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000201 if (!strcmp(".", td->entry.name) ||
202 !strcmp("..", td->entry.name))
203 {
204 continue;
205 }
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000206
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000207 td->ce->attribute = td->entry.attr;
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000208 td->ce->name_len = strlen(td->entry.name) + 1;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000209 td->ce->d_name = ((char *)dircache_root+dircache_size);
210 td->ce->startcluster = td->entry.firstcluster;
211 td->ce->size = td->entry.filesize;
212 td->ce->wrtdate = td->entry.wrtdate;
213 td->ce->wrttime = td->entry.wrttime;
214 memcpy(td->ce->d_name, td->entry.name, td->ce->name_len);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000215#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000216 dircache_size += td->ce->name_len;
Miika Pekkarinen7c4e0c82006-03-26 11:33:42 +0000217 entry_count++;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000218
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000219#ifdef SIMULATOR
220 if (td->entry->attribute & ATTR_DIRECTORY)
221#else
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000222 if (td->entry.attr & FAT_ATTR_DIRECTORY)
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000223#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000224 {
225
226 td->down_entry = dircache_gen_down(td->ce);
227 if (td->down_entry == NULL)
228 return -2;
229
230 td->pathpos = strlen(dircache_cur_path);
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000231 strncpy(&dircache_cur_path[td->pathpos], "/",
232 sizeof(dircache_cur_path) - td->pathpos - 1);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000233#ifdef SIMULATOR
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000234 strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
235 sizeof(dircache_cur_path) - td->pathpos - 2);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000236
Kevin Ferrare011a3252007-07-20 17:06:55 +0000237 td->newdir = opendir_uncached(dircache_cur_path);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000238 if (td->newdir == NULL)
239 {
Kevin Ferrare011a3252007-07-20 17:06:55 +0000240 logf("Failed to opendir_uncached(): %s", dircache_cur_path);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000241 return -3;
242 }
243#else
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000244 strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
245 sizeof(dircache_cur_path) - td->pathpos - 2);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000246
247 td->newdir = *td->dir;
248 if (fat_opendir(IF_MV2(volume,) &td->newdir,
249 td->entry.firstcluster, td->dir) < 0 )
250 {
251 return -3;
252 }
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000253#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000254
255 td->ce = dircache_gen_next(td->ce);
256 if (td->ce == NULL)
257 return -4;
258
259 return 1;
260 }
261
262 td->ce->down = NULL;
263 td->ce = dircache_gen_next(td->ce);
264 if (td->ce == NULL)
265 return -5;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000266
267 /* When simulator is used, it's only safe to yield here. */
268 if (thread_enabled)
269 {
270 /* Stop if we got an external signal. */
271 if (check_event_queue())
272 return -6;
273 yield();
274 }
275
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000276 }
277
278 return 0;
279}
280
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000281/**
282 * Recursively scan the hard disk and build the cache.
283 */
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000284#ifdef SIMULATOR
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000285static int dircache_travel(IF_MV2(int volume,) DIR_UNCACHED *dir, struct dircache_entry *ce)
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000286#else
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000287static int dircache_travel(IF_MV2(int volume,) struct fat_dir *dir, struct dircache_entry *ce)
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000288#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000289{
290 int depth = 0;
291 int result;
292
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000293 memset(ce, 0, sizeof(struct dircache_entry));
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000294
295#if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR)
296 if (volume > 0)
297 {
298 ce->d_name = ((char *)dircache_root+dircache_size);
299 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
300 ce->name_len = VOL_ENUM_POS + 3;
301 dircache_size += ce->name_len;
302 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
303 ce->size = 0;
304 append_position = dircache_gen_next(ce);
305 ce = dircache_gen_down(ce);
306 }
307#endif
308
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000309 dir_recursion[0].dir = dir;
310 dir_recursion[0].ce = ce;
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000311 dir_recursion[0].first = ce;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000312
313 do {
314 //logf("=> %s", dircache_cur_path);
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000315 result = dircache_scan(IF_MV2(volume,) &dir_recursion[depth]);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000316 switch (result) {
317 case 0: /* Leaving the current directory. */
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000318 /* Add the standard . and .. entries. */
319 ce = dir_recursion[depth].ce;
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000320 ce->d_name = ".";
321 ce->name_len = 2;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000322#ifdef SIMULATOR
Kevin Ferrare011a3252007-07-20 17:06:55 +0000323 closedir_uncached(dir_recursion[depth].dir);
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000324 ce->attribute = ATTR_DIRECTORY;
325#else
326 ce->attribute = FAT_ATTR_DIRECTORY;
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000327 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000328#endif
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000329 ce->size = 0;
330 ce->down = dir_recursion[depth].first;
331
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000332 depth--;
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000333 if (depth < 0)
334 break ;
335
336 dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
337
338 ce = dircache_gen_next(ce);
339 if (ce == NULL)
340 {
341 logf("memory allocation error");
342 return -3;
343 }
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000344#ifdef SIMULATOR
345 ce->attribute = ATTR_DIRECTORY;
346#else
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000347 ce->attribute = FAT_ATTR_DIRECTORY;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000348 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
349#endif
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000350 ce->d_name = "..";
351 ce->name_len = 3;
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000352 ce->size = 0;
353 ce->down = dir_recursion[depth].first;
354
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000355 break ;
356
357 case 1: /* Going down in the directory tree. */
358 depth++;
359 if (depth >= MAX_SCAN_DEPTH)
360 {
361 logf("Too deep directory structure");
362 return -2;
363 }
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000364
365#ifdef SIMULATOR
366 dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
367#else
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000368 dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000369#endif
Miika Pekkarinen871575f2006-02-28 11:41:35 +0000370 dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000371 dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
372 break ;
373
374 default:
375 logf("Scan failed");
376 logf("-> %s", dircache_cur_path);
377 return -1;
378 }
379 } while (depth >= 0) ;
380
381 return 0;
382}
383
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000384/**
385 * Internal function to get a pointer to dircache_entry for a given filename.
386 * path: Absolute path to a file or directory.
387 * get_before: Returns the cache pointer before the last valid entry found.
388 * only_directories: Match only filenames which are a directory type.
389 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000390static struct dircache_entry* dircache_get_entry(const char *path,
391 bool get_before, bool only_directories)
392{
393 struct dircache_entry *cache_entry, *before;
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000394 char namecopy[MAX_PATH*2];
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000395 char* part;
396 char* end;
397
398 strncpy(namecopy, path, sizeof(namecopy) - 1);
399 cache_entry = dircache_root;
400 before = NULL;
401
402 for ( part = strtok_r(namecopy, "/", &end); part;
403 part = strtok_r(NULL, "/", &end)) {
404
405 /* scan dir for name */
406 while (1)
407 {
408 if (cache_entry == NULL)
409 {
410 return NULL;
411 }
412 else if (cache_entry->name_len == 0)
413 {
414 cache_entry = cache_entry->next;
415 continue ;
416 }
417
418 if (!strcasecmp(part, cache_entry->d_name))
419 {
420 before = cache_entry;
421 if (cache_entry->down || only_directories)
422 cache_entry = cache_entry->down;
423 break ;
424 }
425
426 cache_entry = cache_entry->next;
427 }
428 }
429
430 if (get_before)
431 cache_entry = before;
432
433 return cache_entry;
434}
435
Miika Pekkarinen7b1e8272007-04-07 17:48:51 +0000436#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000437/**
438 * Function to load the internal cache structure from disk to initialize
439 * the dircache really fast and little disk access.
440 */
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000441int dircache_load(void)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000442{
443 struct dircache_maindata maindata;
444 int bytes_read;
445 int fd;
446
447 if (dircache_initialized)
448 return -1;
449
450 logf("Loading directory cache");
451 dircache_size = 0;
452
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000453 fd = open(DIRCACHE_FILE, O_RDONLY);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000454 if (fd < 0)
455 return -2;
456
457 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
458 if (bytes_read != sizeof(struct dircache_maindata)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000459 || maindata.size <= 0)
460 {
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000461 logf("Dircache file header error");
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000462 close(fd);
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000463 remove(DIRCACHE_FILE);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000464 return -3;
465 }
466
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000467 dircache_root = buffer_alloc(0);
468 if ((long)maindata.root_entry != (long)dircache_root)
469 {
470 logf("Position missmatch");
471 close(fd);
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000472 remove(DIRCACHE_FILE);
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000473 return -4;
474 }
475
476 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
Miika Pekkarinende281db2006-01-31 10:08:53 +0000477 entry_count = maindata.entry_count;
Miika Pekkarinen39c597b2008-01-13 19:13:37 +0000478 appflags = maindata.appflags;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000479 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
480 close(fd);
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000481 remove(DIRCACHE_FILE);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000482
483 if (bytes_read != maindata.size)
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000484 {
485 logf("Dircache read failed");
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000486 return -6;
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000487 }
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000488
489 /* Cache successfully loaded. */
490 dircache_size = maindata.size;
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000491 allocated_size = dircache_size + DIRCACHE_RESERVE;
492 reserve_used = 0;
Magnus Holmgren01a010f2007-03-18 09:50:53 +0000493 logf("Done, %ld KiB used", dircache_size / 1024);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000494 dircache_initialized = true;
495 memset(fd_bindings, 0, sizeof(fd_bindings));
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000496
497 return 0;
498}
499
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000500/**
501 * Function to save the internal cache stucture to disk for fast loading
502 * on boot.
503 */
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000504int dircache_save(void)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000505{
506 struct dircache_maindata maindata;
507 int fd;
508 unsigned long bytes_written;
509
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000510 remove(DIRCACHE_FILE);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000511
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000512 if (!dircache_initialized)
513 return -1;
514
515 logf("Saving directory cache");
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000516 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000517
518 maindata.magic = DIRCACHE_MAGIC;
519 maindata.size = dircache_size;
520 maindata.root_entry = dircache_root;
Miika Pekkarinende281db2006-01-31 10:08:53 +0000521 maindata.entry_count = entry_count;
Miika Pekkarinen39c597b2008-01-13 19:13:37 +0000522 maindata.appflags = appflags;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000523
524 /* Save the info structure */
525 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
526 if (bytes_written != sizeof(struct dircache_maindata))
527 {
528 close(fd);
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000529 logf("dircache: write failed #1");
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000530 return -2;
531 }
532
533 /* Dump whole directory cache to disk */
534 bytes_written = write(fd, dircache_root, dircache_size);
535 close(fd);
536 if (bytes_written != dircache_size)
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000537 {
538 logf("dircache: write failed #2");
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000539 return -3;
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000540 }
541
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000542 return 0;
543}
544#endif /* #if 0 */
545
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000546/**
547 * Internal function which scans the disk and creates the dircache structure.
548 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000549static int dircache_do_rebuild(void)
550{
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000551#ifdef SIMULATOR
Kevin Ferrare011a3252007-07-20 17:06:55 +0000552 DIR_UNCACHED *pdir;
Miika Pekkarinen3881ce92006-03-30 18:53:44 +0000553#else
554 struct fat_dir dir, *pdir;
555#endif
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000556 unsigned int start_tick;
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000557 int i;
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000558
559 /* Measure how long it takes build the cache. */
560 start_tick = current_tick;
Steve Bavin12ef3102007-01-12 11:10:04 +0000561 dircache_initializing = true;
Miika Pekkarinen39c597b2008-01-13 19:13:37 +0000562 appflags = 0;
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000563 entry_count = 0;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000564
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000565 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000566 dircache_size = sizeof(struct dircache_entry);
567
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000568#ifdef HAVE_MULTIVOLUME
569 append_position = dircache_root;
570
571 for (i = NUM_VOLUMES; i >= 0; i--)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000572 {
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000573 if (fat_ismounted(i))
574 {
575#endif
576#ifdef SIMULATOR
577 pdir = opendir_uncached("/");
578 if (pdir == NULL)
579 {
580 logf("Failed to open rootdir");
581 dircache_initializing = false;
582 return -3;
583 }
584#else
585#ifdef HAVE_MULTIVOLUME
586 if ( fat_opendir(IF_MV2(i,) &dir, 0, NULL) < 0 ) {
587#else
588 if ( fat_opendir(IF_MV2(0,) &dir, 0, NULL) < 0 ) {
589#endif /* HAVE_MULTIVOLUME */
590 logf("Failed opening root dir");
591 dircache_initializing = false;
592 return -3;
593 }
594 pdir = &dir;
595#endif
596 cpu_boost(true);
597#ifdef HAVE_MULTIVOLUME
598 if (dircache_travel(IF_MV2(i,) pdir, append_position) < 0)
599#else
600 if (dircache_travel(IF_MV2(0,) pdir, dircache_root) < 0)
601#endif /* HAVE_MULTIVOLUME */
602 {
603 logf("dircache_travel failed");
604 cpu_boost(false);
605 dircache_size = 0;
606 dircache_initializing = false;
607 return -2;
608 }
609 cpu_boost(false);
610#ifdef HAVE_MULTIVOLUME
611 }
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000612 }
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000613#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000614
Magnus Holmgren01a010f2007-03-18 09:50:53 +0000615 logf("Done, %ld KiB used", dircache_size / 1024);
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000616
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000617 dircache_initialized = true;
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000618 dircache_initializing = false;
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000619 cache_build_ticks = current_tick - start_tick;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000620
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000621 /* Initialized fd bindings. */
622 memset(fd_bindings, 0, sizeof(fd_bindings));
623 for (i = 0; i < fdbind_idx; i++)
624 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
Miika Pekkarinen6523ba42006-08-26 09:24:20 +0000625 fdbind_idx = 0;
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000626
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000627 if (thread_enabled)
628 {
629 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
630 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
631 }
632 else
633 {
634 /* We have to long align the audiobuf to keep the buffer access fast. */
635 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
636 audiobuf += DIRCACHE_RESERVE;
637 allocated_size = dircache_size + DIRCACHE_RESERVE;
Miika Pekkarinen3b313462006-04-16 17:32:54 +0000638 reserve_used = 0;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000639 }
640
641 return 1;
642}
643
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000644/**
645 * Internal thread that controls transparent cache building.
646 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000647static void dircache_thread(void)
648{
Michael Sevakisa9b2fb52007-10-16 01:25:17 +0000649 struct queue_event ev;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000650
651 while (1)
652 {
653 queue_wait(&dircache_queue, &ev);
654
655 switch (ev.id)
656 {
Miika Pekkarinen52d827a2008-03-11 19:39:26 +0000657#ifdef HAVE_HOTSWAP
658 case SYS_FS_CHANGED:
659 if (!dircache_initialized)
660 break;
661 dircache_initialized = false;
662#endif
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000663 case DIRCACHE_BUILD:
664 thread_enabled = true;
665 dircache_do_rebuild();
666 thread_enabled = false;
667 break ;
668
Miika Pekkarinen4b2aa982005-10-20 08:33:59 +0000669 case DIRCACHE_STOP:
670 logf("Stopped the rebuilding.");
671 dircache_initialized = false;
672 break ;
673
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000674#ifndef SIMULATOR
675 case SYS_USB_CONNECTED:
676 usb_acknowledge(SYS_USB_CONNECTED_ACK);
677 usb_wait_for_disconnect(&dircache_queue);
678 break ;
679#endif
680 }
681 }
682}
683
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000684/**
685 * Start scanning the disk to build the dircache.
686 * Either transparent or non-transparent build method is used.
687 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000688int dircache_build(int last_size)
689{
Miika Pekkarinen3b313462006-04-16 17:32:54 +0000690 if (dircache_initialized || thread_enabled)
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000691 return -3;
692
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000693 logf("Building directory cache");
Miika Pekkarinenc8a9ca72006-12-22 09:11:09 +0000694 remove(DIRCACHE_FILE);
695
Miika Pekkarinen954b7322006-08-05 20:19:10 +0000696 /* Background build, dircache has been previously allocated */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000697 if (dircache_size > 0)
698 {
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000699 thread_enabled = true;
Miika Pekkarinen5019b322006-08-14 19:03:37 +0000700 dircache_initializing = true;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000701 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
702 return 2;
703 }
Miika Pekkarinen3b313462006-04-16 17:32:54 +0000704
Miika Pekkarinen3b313462006-04-16 17:32:54 +0000705 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000706 {
707 allocated_size = last_size + DIRCACHE_RESERVE;
Miika Pekkarinen6523ba42006-08-26 09:24:20 +0000708 dircache_root = buffer_alloc(allocated_size);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000709 thread_enabled = true;
710
711 /* Start a transparent rebuild. */
712 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
713 return 3;
714 }
715
Miika Pekkarinend8ac6072006-08-02 17:39:34 +0000716 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
717
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000718 /* Start a non-transparent rebuild. */
719 return dircache_do_rebuild();
720}
721
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000722/**
Miika Pekkarinen3b313462006-04-16 17:32:54 +0000723 * Steal the allocated dircache buffer and disable dircache.
724 */
725void* dircache_steal_buffer(long *size)
726{
727 dircache_disable();
728 if (dircache_size == 0)
729 {
730 *size = 0;
731 return NULL;
732 }
733
734 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
735
736 return dircache_root;
737}
738
739/**
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000740 * Main initialization function that must be called before any other
741 * operations within the dircache.
742 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000743void dircache_init(void)
744{
745 int i;
746
Miika Pekkarinen5127cfa2006-05-30 18:13:18 +0000747 dircache_initialized = false;
Miika Pekkarinen5019b322006-08-14 19:03:37 +0000748 dircache_initializing = false;
Miika Pekkarinen5127cfa2006-05-30 18:13:18 +0000749
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000750 memset(opendirs, 0, sizeof(opendirs));
751 for (i = 0; i < MAX_OPEN_DIRS; i++)
752 {
Miika Pekkarinend8ac6072006-08-02 17:39:34 +0000753 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000754 }
755
Miika Pekkarinena85044b2006-09-16 16:18:11 +0000756 queue_init(&dircache_queue, true);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000757 create_thread(dircache_thread, dircache_stack,
Michael Sevakisa9b2fb52007-10-16 01:25:17 +0000758 sizeof(dircache_stack), 0, dircache_thread_name
759 IF_PRIO(, PRIORITY_BACKGROUND)
760 IF_COP(, CPU));
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000761}
762
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000763/**
764 * Returns true if dircache has been initialized and is ready to be used.
765 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000766bool dircache_is_enabled(void)
767{
768 return dircache_initialized;
769}
770
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000771/**
Miika Pekkarinen9cd5c3e2006-07-10 16:22:03 +0000772 * Returns true if dircache is being initialized.
773 */
774bool dircache_is_initializing(void)
775{
Miika Pekkarinen7b1e8272007-04-07 17:48:51 +0000776 return dircache_initializing || thread_enabled;
Miika Pekkarinen9cd5c3e2006-07-10 16:22:03 +0000777}
778
779/**
Miika Pekkarinen39c597b2008-01-13 19:13:37 +0000780 * Set application flags used to determine if dircache is still intact.
781 */
782void dircache_set_appflag(long mask)
783{
784 appflags |= mask;
785}
786
787/**
788 * Get application flags used to determine if dircache is still intact.
789 */
790bool dircache_get_appflag(long mask)
791{
Miika Pekkarinen7aedb022008-01-13 19:20:04 +0000792 return dircache_is_enabled() && (appflags & mask);
Miika Pekkarinen39c597b2008-01-13 19:13:37 +0000793}
794
795/**
Miika Pekkarinende281db2006-01-31 10:08:53 +0000796 * Returns the current number of entries (directories and files) in the cache.
797 */
798int dircache_get_entry_count(void)
799{
800 return entry_count;
801}
802
803/**
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000804 * Returns the allocated space for dircache (without reserve space).
805 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000806int dircache_get_cache_size(void)
807{
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000808 return dircache_is_enabled() ? dircache_size : 0;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000809}
810
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000811/**
812 * Returns how many bytes of the reserve allocation for live cache
813 * updates have been used.
814 */
Miika Pekkarinen8f280a32005-11-26 20:22:19 +0000815int dircache_get_reserve_used(void)
816{
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000817 return dircache_is_enabled() ? reserve_used : 0;
Miika Pekkarinen8f280a32005-11-26 20:22:19 +0000818}
819
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000820/**
821 * Returns the time in kernel ticks that took to build the cache.
822 */
823int dircache_get_build_ticks(void)
824{
825 return dircache_is_enabled() ? cache_build_ticks : 0;
826}
827
828/**
829 * Disables the dircache. Usually called on shutdown or when
830 * accepting a usb connection.
831 */
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000832void dircache_disable(void)
833{
834 int i;
Miika Pekkarinen4b2aa982005-10-20 08:33:59 +0000835 bool cache_in_use;
836
837 if (thread_enabled)
838 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000839
840 while (thread_enabled)
841 sleep(1);
842 dircache_initialized = false;
843
Miika Pekkarinen4b2aa982005-10-20 08:33:59 +0000844 logf("Waiting for cached dirs to release");
845 do {
846 cache_in_use = false;
847 for (i = 0; i < MAX_OPEN_DIRS; i++) {
848 if (!opendirs[i].regulardir && opendirs[i].busy)
849 {
850 cache_in_use = true;
851 sleep(1);
852 break ;
853 }
854 }
855 } while (cache_in_use) ;
Miika Pekkarinen5019b322006-08-14 19:03:37 +0000856
Miika Pekkarinen4b2aa982005-10-20 08:33:59 +0000857 logf("Cache released");
Miika Pekkarinen5019b322006-08-14 19:03:37 +0000858 entry_count = 0;
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000859}
860
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000861/**
862 * Usermode function to return dircache_entry pointer to the given path.
863 */
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000864const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
865{
866 if (!dircache_initialized || filename == NULL)
867 return NULL;
868
869 return dircache_get_entry(filename, false, false);
870}
871
Miika Pekkarinen29e04ef2005-11-26 20:47:10 +0000872/**
873 * Function to copy the full absolute path from dircache to the given buffer
874 * using the given dircache_entry pointer.
875 */
Miika Pekkarinen735f4532005-11-17 19:31:29 +0000876void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
877{
878 const struct dircache_entry *down[MAX_SCAN_DEPTH];
879 int depth = 0;
880
881 if (size <= 0)
882 return ;
883
884 buf[0] = '\0';
885
886 if (entry == NULL)
887 return ;
888
889 do {
890 down[depth] = entry;
891 entry = entry->up;
892 depth++;
893 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
894
895 while (--depth >= 0)
896 {
897 snprintf(buf, size, "/%s", down[depth]->d_name);
898 buf += down[depth]->name_len; /* '/' + d_name */
899 size -= down[depth]->name_len;
900 if (size <= 0)
901 break ;
902 }
903}
904
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000905/* --- Directory cache live updating functions --- */
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000906static int block_until_ready(void)
907{
908 /* Block until dircache has been built. */
Miika Pekkarinen7b1e8272007-04-07 17:48:51 +0000909 while (!dircache_initialized && dircache_is_initializing())
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000910 sleep(1);
911
912 if (!dircache_initialized)
913 return -1;
914
915 return 0;
916}
917
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000918static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
919{
920 struct dircache_entry *entry;
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000921 char basedir[MAX_PATH*2];
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000922 char *new;
923 long last_cache_size = dircache_size;
924
925 strncpy(basedir, path, sizeof(basedir)-1);
926 new = strrchr(basedir, '/');
927 if (new == NULL)
928 {
929 logf("error occurred");
930 dircache_initialized = false;
931 return NULL;
932 }
933
934 *new = '\0';
935 new++;
936
937 entry = dircache_get_entry(basedir, false, true);
938 if (entry == NULL)
939 {
940 logf("basedir not found!");
Nicolas Pennequinfa1e7312007-10-02 15:35:12 +0000941 logf("%s", basedir);
Miika Pekkarinenab78b042005-10-07 17:38:05 +0000942 dircache_initialized = false;
943 return NULL;
944 }
945
946 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
947 >= DIRCACHE_RESERVE)
948 {
949 logf("not enough space");
950 dircache_initialized = false;
951 return NULL;
952 }
953
954 while (entry->next != NULL)
955 entry = entry->next;
956
957 if (entry->name_len > 0)
958 entry = dircache_gen_next(entry);
959
960 if (entry == NULL)
961 {
962 dircache_initialized = false;
963 return NULL;
964 }
965
966 entry->attribute = attribute;
967 entry->name_len = MIN(254, strlen(new)) + 1;
968 entry->d_name = ((char *)dircache_root+dircache_size);
969 entry->startcluster = 0;
970 entry->wrtdate = 0;
971 entry->wrttime = 0;
972 entry->size = 0;
973 memcpy(entry->d_name, new, entry->name_len);
974 dircache_size += entry->name_len;
975
976 if (attribute & ATTR_DIRECTORY)
977 {
978 logf("gen_down");
979 dircache_gen_down(entry);
980 }
981
982 reserve_used += dircache_size - last_cache_size;
983
984 return entry;
985}
986
987void dircache_bind(int fd, const char *path)
988{
989 struct dircache_entry *entry;
990
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000991 /* Queue requests until dircache has been built. */
Miika Pekkarinen7b1e8272007-04-07 17:48:51 +0000992 if (!dircache_initialized && dircache_is_initializing())
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +0000993 {
994 if (fdbind_idx >= MAX_PENDING_BINDINGS)
995 return ;
996 strncpy(fdbind_cache[fdbind_idx].path, path,
997 sizeof(fdbind_cache[fdbind_idx].path)-1);
998 fdbind_cache[fdbind_idx].fd = fd;
999 fdbind_idx++;
1000 return ;
1001 }
1002
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001003 if (!dircache_initialized)
1004 return ;
1005
1006 logf("bind: %d/%s", fd, path);
1007 entry = dircache_get_entry(path, false, false);
1008 if (entry == NULL)
1009 {
1010 logf("not found!");
1011 dircache_initialized = false;
1012 return ;
1013 }
1014
1015 fd_bindings[fd] = entry;
1016}
1017
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001018void dircache_update_filesize(int fd, long newsize, long startcluster)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001019{
1020 if (!dircache_initialized || fd < 0)
1021 return ;
1022
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001023 if (fd_bindings[fd] == NULL)
1024 {
1025 logf("dircache fd access error");
1026 dircache_initialized = false;
1027 return ;
1028 }
1029
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001030 fd_bindings[fd]->size = newsize;
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001031 fd_bindings[fd]->startcluster = startcluster;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001032}
Jonathan Gordone73f2872007-09-02 13:24:51 +00001033void dircache_update_filetime(int fd)
1034{
1035#if CONFIG_RTC == 0
1036 (void)fd;
1037#else
1038 short year;
1039 struct tm *now = get_time();
1040 if (!dircache_initialized || fd < 0)
1041 return ;
1042
1043 if (fd_bindings[fd] == NULL)
1044 {
1045 logf("dircache fd access error");
1046 dircache_initialized = false;
1047 return ;
1048 }
1049 year = now->tm_year+1900-1980;
1050 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1051 (((now->tm_mon+1)&0xf)<<5) |
1052 (((now->tm_mday)&0x1f));
1053 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1054 (((now->tm_min)&0x3f)<<5) |
1055 (((now->tm_sec/2)&0x1f));
1056#endif
1057}
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001058
1059void dircache_mkdir(const char *path)
1060{ /* Test ok. */
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001061 if (block_until_ready())
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001062 return ;
1063
1064 logf("mkdir: %s", path);
1065 dircache_new_entry(path, ATTR_DIRECTORY);
1066}
1067
1068void dircache_rmdir(const char *path)
1069{ /* Test ok. */
1070 struct dircache_entry *entry;
1071
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001072 if (block_until_ready())
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001073 return ;
1074
1075 logf("rmdir: %s", path);
1076 entry = dircache_get_entry(path, true, true);
1077 if (entry == NULL)
1078 {
1079 logf("not found!");
1080 dircache_initialized = false;
1081 return ;
1082 }
1083
1084 entry->down = NULL;
1085 entry->name_len = 0;
1086}
1087
1088/* Remove a file from cache */
1089void dircache_remove(const char *name)
1090{ /* Test ok. */
1091 struct dircache_entry *entry;
1092
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001093 if (block_until_ready())
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001094 return ;
1095
1096 logf("remove: %s", name);
1097
1098 entry = dircache_get_entry(name, false, false);
1099
1100 if (entry == NULL)
1101 {
1102 logf("not found!");
1103 dircache_initialized = false;
1104 return ;
1105 }
1106
1107 entry->name_len = 0;
1108}
1109
1110void dircache_rename(const char *oldpath, const char *newpath)
1111{ /* Test ok. */
1112 struct dircache_entry *entry, *newentry;
Miika Pekkarinen10f2f892005-11-18 19:28:22 +00001113 struct dircache_entry oldentry;
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +00001114 char absolute_path[MAX_PATH*2];
Miika Pekkarinen871575f2006-02-28 11:41:35 +00001115 char *p;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001116
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001117 if (block_until_ready())
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001118 return ;
1119
1120 logf("rename: %s->%s", oldpath, newpath);
Miika Pekkarinen871575f2006-02-28 11:41:35 +00001121
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001122 entry = dircache_get_entry(oldpath, true, false);
1123 if (entry == NULL)
1124 {
1125 logf("not found!");
1126 dircache_initialized = false;
1127 return ;
1128 }
1129
1130 /* Delete the old entry. */
1131 entry->name_len = 0;
1132
Miika Pekkarinen10f2f892005-11-18 19:28:22 +00001133 /** If we rename the same filename twice in a row, we need to
1134 * save the data, because the entry will be re-used. */
1135 oldentry = *entry;
1136
Miika Pekkarinen871575f2006-02-28 11:41:35 +00001137 /* Generate the absolute path for destination if necessary. */
1138 if (newpath[0] != '/')
1139 {
1140 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
1141 p = strrchr(absolute_path, '/');
1142 if (!p)
1143 {
1144 logf("Invalid path");
1145 dircache_initialized = false;
1146 return ;
1147 }
1148
1149 *p = '\0';
1150 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
1151 newpath = absolute_path;
1152 }
1153
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001154 newentry = dircache_new_entry(newpath, entry->attribute);
1155 if (newentry == NULL)
1156 {
1157 dircache_initialized = false;
1158 return ;
1159 }
1160
Miika Pekkarinen10f2f892005-11-18 19:28:22 +00001161 newentry->down = oldentry.down;
Miika Pekkarinen10f2f892005-11-18 19:28:22 +00001162 newentry->size = oldentry.size;
1163 newentry->startcluster = oldentry.startcluster;
1164 newentry->wrttime = oldentry.wrttime;
1165 newentry->wrtdate = oldentry.wrtdate;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001166}
1167
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001168void dircache_add_file(const char *path, long startcluster)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001169{
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001170 struct dircache_entry *entry;
1171
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001172 if (block_until_ready())
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001173 return ;
Miika Pekkarinen6ce466e2006-03-29 09:38:45 +00001174
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001175 logf("add file: %s", path);
Miika Pekkarinen2d934952006-03-28 11:51:12 +00001176 entry = dircache_new_entry(path, 0);
1177 if (entry == NULL)
1178 return ;
1179
1180 entry->startcluster = startcluster;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001181}
1182
Kevin Ferrare011a3252007-07-20 17:06:55 +00001183DIR_CACHED* opendir_cached(const char* name)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001184{
1185 struct dircache_entry *cache_entry;
1186 int dd;
Kevin Ferrare011a3252007-07-20 17:06:55 +00001187 DIR_CACHED* pdir = opendirs;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001188
1189 if ( name[0] != '/' )
1190 {
1191 DEBUGF("Only absolute paths supported right now\n");
1192 return NULL;
1193 }
1194
1195 /* find a free dir descriptor */
1196 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1197 if ( !pdir->busy )
1198 break;
1199
1200 if ( dd == MAX_OPEN_DIRS )
1201 {
1202 DEBUGF("Too many dirs open\n");
1203 errno = EMFILE;
1204 return NULL;
1205 }
1206
1207 if (!dircache_initialized)
1208 {
Kevin Ferrare011a3252007-07-20 17:06:55 +00001209 pdir->regulardir = opendir_uncached(name);
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001210 if (!pdir->regulardir)
1211 return NULL;
1212
1213 pdir->busy = true;
1214 return pdir;
1215 }
1216
1217 pdir->busy = true;
1218 pdir->regulardir = NULL;
1219 cache_entry = dircache_get_entry(name, false, true);
1220 pdir->entry = cache_entry;
1221
1222 if (cache_entry == NULL)
1223 {
1224 pdir->busy = false;
1225 return NULL;
1226 }
1227
1228 return pdir;
1229}
1230
Kevin Ferrare011a3252007-07-20 17:06:55 +00001231struct dircache_entry* readdir_cached(DIR_CACHED* dir)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001232{
Kevin Ferrare011a3252007-07-20 17:06:55 +00001233 struct dirent_uncached *regentry;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001234 struct dircache_entry *ce;
1235
1236 if (!dir->busy)
1237 return NULL;
1238
Miika Pekkarinen20bc58d2005-10-09 07:32:23 +00001239 if (dir->regulardir != NULL)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001240 {
Kevin Ferrare011a3252007-07-20 17:06:55 +00001241 regentry = readdir_uncached(dir->regulardir);
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001242 if (regentry == NULL)
1243 return NULL;
Miika Pekkarinende281db2006-01-31 10:08:53 +00001244
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001245 strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
1246 dir->secondary_entry.size = regentry->size;
1247 dir->secondary_entry.startcluster = regentry->startcluster;
1248 dir->secondary_entry.attribute = regentry->attribute;
1249 dir->secondary_entry.wrttime = regentry->wrttime;
1250 dir->secondary_entry.wrtdate = regentry->wrtdate;
1251 dir->secondary_entry.next = NULL;
1252
1253 return &dir->secondary_entry;
1254 }
1255
1256 do {
1257 if (dir->entry == NULL)
1258 return NULL;
1259
1260 ce = dir->entry;
1261 if (ce->name_len == 0)
1262 dir->entry = ce->next;
1263 } while (ce->name_len == 0) ;
1264
1265 dir->entry = ce->next;
1266
Miika Pekkarinenb76e8c02005-10-08 18:41:11 +00001267 strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
1268 /* Can't do `dir->secondary_entry = *ce`
1269 because that modifies the d_name pointer. */
1270 dir->secondary_entry.size = ce->size;
1271 dir->secondary_entry.startcluster = ce->startcluster;
1272 dir->secondary_entry.attribute = ce->attribute;
1273 dir->secondary_entry.wrttime = ce->wrttime;
1274 dir->secondary_entry.wrtdate = ce->wrtdate;
1275 dir->secondary_entry.next = NULL;
Miika Pekkarinende281db2006-01-31 10:08:53 +00001276 dir->internal_entry = ce;
Miika Pekkarinenb76e8c02005-10-08 18:41:11 +00001277
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001278 //logf("-> %s", ce->name);
Miika Pekkarinenb76e8c02005-10-08 18:41:11 +00001279 return &dir->secondary_entry;
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001280}
1281
Kevin Ferrare011a3252007-07-20 17:06:55 +00001282int closedir_cached(DIR_CACHED* dir)
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001283{
Miika Pekkarinen20bc58d2005-10-09 07:32:23 +00001284 if (!dir->busy)
1285 return -1;
1286
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001287 dir->busy=false;
Miika Pekkarinen20bc58d2005-10-09 07:32:23 +00001288 if (dir->regulardir != NULL)
Kevin Ferrare011a3252007-07-20 17:06:55 +00001289 return closedir_uncached(dir->regulardir);
Miika Pekkarinenab78b042005-10-07 17:38:05 +00001290
1291 return 0;
1292}
1293
Kevin Ferrare011a3252007-07-20 17:06:55 +00001294int mkdir_cached(const char *name)
1295{
1296 int rc=mkdir_uncached(name);
1297 if (rc >= 0)
1298 dircache_mkdir(name);
1299 return(rc);
1300}
1301
1302int rmdir_cached(const char* name)
1303{
1304 int rc=rmdir_uncached(name);
1305 if(rc>=0)
1306 dircache_rmdir(name);
1307 return(rc);
1308}