Fix a dircache bug (opening a directory opens the first file of that directory)....
[kugel-rb.git] / firmware / common / dircache.c
blob7ff497790efc7a61131057b8054c81e2edd51d33
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* TODO:
23 - Allow cache live updating while transparent rebuild is running.
26 #include "config.h"
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include "debug.h"
34 #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"
41 #include "buffer.h"
42 #include "dir.h"
43 #if CONFIG_RTC
44 #include "time.h"
45 #include "timefuncs.h"
46 #endif
48 /* Queue commands. */
49 #define DIRCACHE_BUILD 1
50 #define DIRCACHE_STOP 2
52 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
53 #define MAX_OPEN_DIRS 12
54 #else
55 #define MAX_OPEN_DIRS 8
56 #endif
57 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
59 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
60 static struct dircache_entry *dircache_root;
61 #ifdef HAVE_MULTIVOLUME
62 static struct dircache_entry *append_position;
63 #endif
65 static bool dircache_initialized = false;
66 static bool dircache_initializing = false;
67 static bool thread_enabled = false;
68 static unsigned long allocated_size = DIRCACHE_LIMIT;
69 static unsigned long dircache_size = 0;
70 static unsigned long entry_count = 0;
71 static unsigned long reserve_used = 0;
72 static unsigned int cache_build_ticks = 0;
73 static unsigned long appflags = 0;
74 static char dircache_cur_path[MAX_PATH*2];
76 static struct event_queue dircache_queue;
77 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
78 static const char dircache_thread_name[] = "dircache";
80 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
81 static int fdbind_idx = 0;
83 /* --- Internal cache structure control functions --- */
85 /**
86 * Internal function to allocate a new dircache_entry from memory.
88 static struct dircache_entry* allocate_entry(void)
90 struct dircache_entry *next_entry;
92 if (dircache_size > allocated_size - MAX_PATH*2)
94 logf("size limit reached");
95 return NULL;
98 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
99 #ifdef ROCKBOX_STRICT_ALIGN
100 /* Make sure the entry is long aligned. */
101 if ((long)next_entry & 0x03)
103 dircache_size += 4 - ((long)next_entry & 0x03);
104 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
106 #endif
107 next_entry->name_len = 0;
108 next_entry->d_name = NULL;
109 next_entry->up = NULL;
110 next_entry->down = NULL;
111 next_entry->next = NULL;
113 dircache_size += sizeof(struct dircache_entry);
115 return next_entry;
119 * Internal function to allocate a dircache_entry and set
120 * ->next entry pointers.
122 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
124 struct dircache_entry *next_entry;
126 if ( (next_entry = allocate_entry()) == NULL)
127 return NULL;
128 next_entry->up = ce->up;
129 ce->next = next_entry;
131 return next_entry;
135 * Internal function to allocate a dircache_entry and set
136 * ->down entry pointers.
138 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
140 struct dircache_entry *next_entry;
142 if ( (next_entry = allocate_entry()) == NULL)
143 return NULL;
144 next_entry->up = ce;
145 ce->down = next_entry;
147 return next_entry;
150 /* This will eat ~30 KiB of memory!
151 * We should probably use that as additional reserve buffer in future. */
152 #define MAX_SCAN_DEPTH 16
153 static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
156 * Returns true if there is an event waiting in the queue
157 * that requires the current operation to be aborted.
159 static bool check_event_queue(void)
161 struct queue_event ev;
163 queue_wait_w_tmo(&dircache_queue, &ev, 0);
164 switch (ev.id)
166 case DIRCACHE_STOP:
167 case SYS_USB_CONNECTED:
168 #ifdef HAVE_HOTSWAP
169 case SYS_FS_CHANGED:
170 #endif
171 /* Put the event back into the queue. */
172 queue_post(&dircache_queue, ev.id, ev.data);
173 return true;
176 return false;
180 * Internal function to iterate a path.
182 static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
184 #ifdef SIMULATOR
185 #ifdef HAVE_MULTIVOLUME
186 (void)volume;
187 #endif
188 while ( ( td->entry = readdir_uncached(td->dir) ) )
189 #else
190 while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
191 #endif
193 #ifdef SIMULATOR
194 if (!strcmp(".", td->entry->d_name) ||
195 !strcmp("..", td->entry->d_name))
197 continue;
200 td->ce->attribute = td->entry->attribute;
201 td->ce->name_len = strlen(td->entry->d_name) + 1;
202 td->ce->d_name = ((char *)dircache_root+dircache_size);
203 td->ce->size = td->entry->size;
204 td->ce->wrtdate = td->entry->wrtdate;
205 td->ce->wrttime = td->entry->wrttime;
206 memcpy(td->ce->d_name, td->entry->d_name, td->ce->name_len);
207 #else
208 if (!strcmp(".", td->entry.name) ||
209 !strcmp("..", td->entry.name))
211 continue;
214 td->ce->attribute = td->entry.attr;
215 td->ce->name_len = strlen(td->entry.name) + 1;
216 td->ce->d_name = ((char *)dircache_root+dircache_size);
217 td->ce->startcluster = td->entry.firstcluster;
218 td->ce->size = td->entry.filesize;
219 td->ce->wrtdate = td->entry.wrtdate;
220 td->ce->wrttime = td->entry.wrttime;
221 memcpy(td->ce->d_name, td->entry.name, td->ce->name_len);
222 #endif
223 dircache_size += td->ce->name_len;
224 entry_count++;
226 #ifdef SIMULATOR
227 if (td->entry->attribute & ATTR_DIRECTORY)
228 #else
229 if (td->entry.attr & FAT_ATTR_DIRECTORY)
230 #endif
233 td->down_entry = dircache_gen_down(td->ce);
234 if (td->down_entry == NULL)
235 return -2;
237 td->pathpos = strlen(dircache_cur_path);
238 strlcpy(&dircache_cur_path[td->pathpos], "/",
239 sizeof(dircache_cur_path) - td->pathpos);
240 #ifdef SIMULATOR
241 strlcpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
242 sizeof(dircache_cur_path) - td->pathpos - 1);
244 td->newdir = opendir_uncached(dircache_cur_path);
245 if (td->newdir == NULL)
247 logf("Failed to opendir_uncached(): %s", dircache_cur_path);
248 return -3;
250 #else
251 strlcpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
252 sizeof(dircache_cur_path) - td->pathpos - 1);
254 td->newdir = *td->dir;
255 if (fat_opendir(IF_MV2(volume,) &td->newdir,
256 td->entry.firstcluster, td->dir) < 0 )
258 return -3;
260 #endif
262 td->ce = dircache_gen_next(td->ce);
263 if (td->ce == NULL)
264 return -4;
266 return 1;
269 td->ce->down = NULL;
270 td->ce = dircache_gen_next(td->ce);
271 if (td->ce == NULL)
272 return -5;
274 /* When simulator is used, it's only safe to yield here. */
275 if (thread_enabled)
277 /* Stop if we got an external signal. */
278 if (check_event_queue())
279 return -6;
280 yield();
285 return 0;
288 /**
289 * Recursively scan the hard disk and build the cache.
291 #ifdef SIMULATOR
292 static int dircache_travel(IF_MV2(int volume,) DIR_UNCACHED *dir, struct dircache_entry *ce)
293 #else
294 static int dircache_travel(IF_MV2(int volume,) struct fat_dir *dir, struct dircache_entry *ce)
295 #endif
297 int depth = 0;
298 int result;
300 memset(ce, 0, sizeof(struct dircache_entry));
302 #if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR)
303 if (volume > 0)
305 ce->d_name = ((char *)dircache_root+dircache_size);
306 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
307 ce->name_len = VOL_ENUM_POS + 3;
308 dircache_size += ce->name_len;
309 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
310 ce->size = 0;
311 append_position = dircache_gen_next(ce);
312 ce = dircache_gen_down(ce);
314 #endif
316 dir_recursion[0].dir = dir;
317 dir_recursion[0].ce = ce;
318 dir_recursion[0].first = ce;
320 do {
321 //logf("=> %s", dircache_cur_path);
322 result = dircache_scan(IF_MV2(volume,) &dir_recursion[depth]);
323 switch (result) {
324 case 0: /* Leaving the current directory. */
325 /* Add the standard . and .. entries. */
326 ce = dir_recursion[depth].ce;
327 ce->d_name = ".";
328 ce->name_len = 2;
329 #ifdef SIMULATOR
330 closedir_uncached(dir_recursion[depth].dir);
331 ce->attribute = ATTR_DIRECTORY;
332 #else
333 ce->attribute = FAT_ATTR_DIRECTORY;
334 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
335 #endif
336 ce->size = 0;
337 ce->down = dir_recursion[depth].first;
339 depth--;
340 if (depth < 0)
341 break ;
343 dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
345 ce = dircache_gen_next(ce);
346 if (ce == NULL)
348 logf("memory allocation error");
349 return -3;
351 #ifdef SIMULATOR
352 ce->attribute = ATTR_DIRECTORY;
353 #else
354 ce->attribute = FAT_ATTR_DIRECTORY;
355 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
356 #endif
357 ce->d_name = "..";
358 ce->name_len = 3;
359 ce->size = 0;
360 ce->down = dir_recursion[depth].first;
362 break ;
364 case 1: /* Going down in the directory tree. */
365 depth++;
366 if (depth >= MAX_SCAN_DEPTH)
368 logf("Too deep directory structure");
369 return -2;
372 #ifdef SIMULATOR
373 dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
374 #else
375 dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
376 #endif
377 dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
378 dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
379 break ;
381 default:
382 logf("Scan failed");
383 logf("-> %s", dircache_cur_path);
384 return -1;
386 } while (depth >= 0) ;
388 return 0;
392 * Internal function to get a pointer to dircache_entry for a given filename.
393 * path: Absolute path to a file or directory (see comment)
394 * go_down: Returns the first entry of the directory given by the path (see comment)
396 * As a a special case to handle buggy code, accept path="" as an alias for "/"
398 * * If get_down=true:
399 * If path="/", the returned entry is the first of root directory (ie dircache_root)
400 * Otherwise, if 'entry' is the returned value when get_down=false,
401 * the functions returns entry->down (which can be NULL)
403 * * If get_down=false:
404 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
405 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
406 * Which means that
407 * dircache_get_entry(path)->d_name == chunk_n
409 * If path="/", the returned entry is NULL.
410 * If the entry doesn't exist, return NULL
412 * NOTE: this functions silently handles double '/'
414 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
416 char namecopy[MAX_PATH];
417 char* part;
418 char* end;
420 bool at_root = true;
421 struct dircache_entry *cache_entry = dircache_root;
423 /* check that the path is absolute (accept empty path also) */
424 if(path[0] != '/' && path[0] != 0)
425 return NULL;
427 /* make a copy of the path because strok_r modifies the path */
428 /* also handle the weird "" alias for "/" */
429 if(path[0] != 0)
430 strlcpy(namecopy, path, sizeof(namecopy));
431 else
432 strlcpy(namecopy, "/", sizeof(namecopy));
434 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
436 /* If request another chunk, the current entry has to be directory
437 * and so cache_entry->down has to be non-NULL/
438 * Special case of root because it's already the first entry of the root directory
440 * NOTE: this is safe even if cache_entry->down is NULL */
441 if(!at_root)
442 cache_entry = cache_entry->down;
443 else
444 at_root = false;
446 /* scan dir for name */
447 while(cache_entry != NULL)
449 /* skip unused entries */
450 if(cache_entry->name_len == 0)
452 cache_entry = cache_entry->next;
453 continue;
455 /* compare names */
456 if(!strcasecmp(part, cache_entry->d_name))
457 break;
458 /* go to next entry */
459 cache_entry = cache_entry->next;
462 /* handle not found case */
463 if(cache_entry == NULL)
464 return NULL;
467 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
468 if(go_down)
469 return at_root ? cache_entry : cache_entry->down;
470 else
471 return at_root ? NULL : cache_entry;
474 #ifdef HAVE_EEPROM_SETTINGS
476 * Function to load the internal cache structure from disk to initialize
477 * the dircache really fast and little disk access.
479 int dircache_load(void)
481 struct dircache_maindata maindata;
482 int bytes_read;
483 int fd;
485 if (dircache_initialized)
486 return -1;
488 logf("Loading directory cache");
489 dircache_size = 0;
491 fd = open(DIRCACHE_FILE, O_RDONLY);
492 if (fd < 0)
493 return -2;
495 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
496 if (bytes_read != sizeof(struct dircache_maindata)
497 || maindata.size <= 0)
499 logf("Dircache file header error");
500 close(fd);
501 remove(DIRCACHE_FILE);
502 return -3;
505 dircache_root = buffer_alloc(0);
506 if ((long)maindata.root_entry != (long)dircache_root)
508 logf("Position missmatch");
509 close(fd);
510 remove(DIRCACHE_FILE);
511 return -4;
514 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
515 entry_count = maindata.entry_count;
516 appflags = maindata.appflags;
517 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
518 close(fd);
519 remove(DIRCACHE_FILE);
521 if (bytes_read != maindata.size)
523 logf("Dircache read failed");
524 return -6;
527 /* Cache successfully loaded. */
528 dircache_size = maindata.size;
529 allocated_size = dircache_size + DIRCACHE_RESERVE;
530 reserve_used = 0;
531 logf("Done, %ld KiB used", dircache_size / 1024);
532 dircache_initialized = true;
533 memset(fd_bindings, 0, sizeof(fd_bindings));
535 return 0;
539 * Function to save the internal cache stucture to disk for fast loading
540 * on boot.
542 int dircache_save(void)
544 struct dircache_maindata maindata;
545 int fd;
546 unsigned long bytes_written;
548 remove(DIRCACHE_FILE);
550 if (!dircache_initialized)
551 return -1;
553 logf("Saving directory cache");
554 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
556 maindata.magic = DIRCACHE_MAGIC;
557 maindata.size = dircache_size;
558 maindata.root_entry = dircache_root;
559 maindata.entry_count = entry_count;
560 maindata.appflags = appflags;
562 /* Save the info structure */
563 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
564 if (bytes_written != sizeof(struct dircache_maindata))
566 close(fd);
567 logf("dircache: write failed #1");
568 return -2;
571 /* Dump whole directory cache to disk */
572 bytes_written = write(fd, dircache_root, dircache_size);
573 close(fd);
574 if (bytes_written != dircache_size)
576 logf("dircache: write failed #2");
577 return -3;
580 return 0;
582 #endif /* #if 0 */
585 * Internal function which scans the disk and creates the dircache structure.
587 static int dircache_do_rebuild(void)
589 #ifdef SIMULATOR
590 DIR_UNCACHED *pdir;
591 #else
592 struct fat_dir dir, *pdir;
593 #endif
594 unsigned int start_tick;
595 int i;
597 /* Measure how long it takes build the cache. */
598 start_tick = current_tick;
599 dircache_initializing = true;
600 appflags = 0;
601 entry_count = 0;
603 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
604 dircache_size = sizeof(struct dircache_entry);
606 #ifdef HAVE_MULTIVOLUME
607 append_position = dircache_root;
609 for (i = NUM_VOLUMES; i >= 0; i--)
611 if (fat_ismounted(i))
613 #endif
614 #ifdef SIMULATOR
615 pdir = opendir_uncached("/");
616 if (pdir == NULL)
618 logf("Failed to open rootdir");
619 dircache_initializing = false;
620 return -3;
622 #else
623 #ifdef HAVE_MULTIVOLUME
624 if ( fat_opendir(IF_MV2(i,) &dir, 0, NULL) < 0 ) {
625 #else
626 if ( fat_opendir(IF_MV2(0,) &dir, 0, NULL) < 0 ) {
627 #endif /* HAVE_MULTIVOLUME */
628 logf("Failed opening root dir");
629 dircache_initializing = false;
630 return -3;
632 pdir = &dir;
633 #endif
634 cpu_boost(true);
635 #ifdef HAVE_MULTIVOLUME
636 if (dircache_travel(IF_MV2(i,) pdir, append_position) < 0)
637 #else
638 if (dircache_travel(IF_MV2(0,) pdir, dircache_root) < 0)
639 #endif /* HAVE_MULTIVOLUME */
641 logf("dircache_travel failed");
642 cpu_boost(false);
643 dircache_size = 0;
644 dircache_initializing = false;
645 return -2;
647 cpu_boost(false);
648 #ifdef HAVE_MULTIVOLUME
651 #endif
653 logf("Done, %ld KiB used", dircache_size / 1024);
655 dircache_initialized = true;
656 dircache_initializing = false;
657 cache_build_ticks = current_tick - start_tick;
659 /* Initialized fd bindings. */
660 memset(fd_bindings, 0, sizeof(fd_bindings));
661 for (i = 0; i < fdbind_idx; i++)
662 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
663 fdbind_idx = 0;
665 if (thread_enabled)
667 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
668 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
670 else
672 /* We have to long align the audiobuf to keep the buffer access fast. */
673 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
674 audiobuf += DIRCACHE_RESERVE;
675 allocated_size = dircache_size + DIRCACHE_RESERVE;
676 reserve_used = 0;
679 return 1;
683 * Internal thread that controls transparent cache building.
685 static void dircache_thread(void)
687 struct queue_event ev;
689 while (1)
691 queue_wait(&dircache_queue, &ev);
693 switch (ev.id)
695 #ifdef HAVE_HOTSWAP
696 case SYS_FS_CHANGED:
697 if (!dircache_initialized)
698 break;
699 dircache_initialized = false;
700 #endif
701 case DIRCACHE_BUILD:
702 thread_enabled = true;
703 dircache_do_rebuild();
704 thread_enabled = false;
705 break ;
707 case DIRCACHE_STOP:
708 logf("Stopped the rebuilding.");
709 dircache_initialized = false;
710 break ;
712 #ifndef SIMULATOR
713 case SYS_USB_CONNECTED:
714 usb_acknowledge(SYS_USB_CONNECTED_ACK);
715 usb_wait_for_disconnect(&dircache_queue);
716 break ;
717 #endif
723 * Start scanning the disk to build the dircache.
724 * Either transparent or non-transparent build method is used.
726 int dircache_build(int last_size)
728 if (dircache_initialized || thread_enabled)
729 return -3;
731 logf("Building directory cache");
732 remove(DIRCACHE_FILE);
734 /* Background build, dircache has been previously allocated */
735 if (dircache_size > 0)
737 thread_enabled = true;
738 dircache_initializing = true;
739 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
740 return 2;
743 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
745 allocated_size = last_size + DIRCACHE_RESERVE;
746 dircache_root = buffer_alloc(allocated_size);
747 thread_enabled = true;
749 /* Start a transparent rebuild. */
750 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
751 return 3;
754 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
756 /* Start a non-transparent rebuild. */
757 return dircache_do_rebuild();
761 * Steal the allocated dircache buffer and disable dircache.
763 void* dircache_steal_buffer(long *size)
765 dircache_disable();
766 if (dircache_size == 0)
768 *size = 0;
769 return NULL;
772 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
774 return dircache_root;
778 * Main initialization function that must be called before any other
779 * operations within the dircache.
781 void dircache_init(void)
783 int i;
785 dircache_initialized = false;
786 dircache_initializing = false;
788 memset(opendirs, 0, sizeof(opendirs));
789 for (i = 0; i < MAX_OPEN_DIRS; i++)
791 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
794 queue_init(&dircache_queue, true);
795 create_thread(dircache_thread, dircache_stack,
796 sizeof(dircache_stack), 0, dircache_thread_name
797 IF_PRIO(, PRIORITY_BACKGROUND)
798 IF_COP(, CPU));
802 * Returns true if dircache has been initialized and is ready to be used.
804 bool dircache_is_enabled(void)
806 return dircache_initialized;
810 * Returns true if dircache is being initialized.
812 bool dircache_is_initializing(void)
814 return dircache_initializing || thread_enabled;
818 * Set application flags used to determine if dircache is still intact.
820 void dircache_set_appflag(long mask)
822 appflags |= mask;
826 * Get application flags used to determine if dircache is still intact.
828 bool dircache_get_appflag(long mask)
830 return dircache_is_enabled() && (appflags & mask);
834 * Returns the current number of entries (directories and files) in the cache.
836 int dircache_get_entry_count(void)
838 return entry_count;
842 * Returns the allocated space for dircache (without reserve space).
844 int dircache_get_cache_size(void)
846 return dircache_is_enabled() ? dircache_size : 0;
850 * Returns how many bytes of the reserve allocation for live cache
851 * updates have been used.
853 int dircache_get_reserve_used(void)
855 return dircache_is_enabled() ? reserve_used : 0;
859 * Returns the time in kernel ticks that took to build the cache.
861 int dircache_get_build_ticks(void)
863 return dircache_is_enabled() ? cache_build_ticks : 0;
867 * Disables the dircache. Usually called on shutdown or when
868 * accepting a usb connection.
870 void dircache_disable(void)
872 int i;
873 bool cache_in_use;
875 if (thread_enabled)
876 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
878 while (thread_enabled)
879 sleep(1);
880 dircache_initialized = false;
882 logf("Waiting for cached dirs to release");
883 do {
884 cache_in_use = false;
885 for (i = 0; i < MAX_OPEN_DIRS; i++) {
886 if (!opendirs[i].regulardir && opendirs[i].busy)
888 cache_in_use = true;
889 sleep(1);
890 break ;
893 } while (cache_in_use) ;
895 logf("Cache released");
896 entry_count = 0;
900 * Usermode function to return dircache_entry pointer to the given path.
902 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
904 if (!dircache_initialized || filename == NULL)
905 return NULL;
907 return dircache_get_entry(filename, false);
911 * Function to copy the full absolute path from dircache to the given buffer
912 * using the given dircache_entry pointer.
914 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
916 const struct dircache_entry *down[MAX_SCAN_DEPTH];
917 int depth = 0;
919 if (size <= 0)
920 return ;
922 buf[0] = '\0';
924 if (entry == NULL)
925 return ;
927 do {
928 down[depth] = entry;
929 entry = entry->up;
930 depth++;
931 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
933 while (--depth >= 0)
935 snprintf(buf, size, "/%s", down[depth]->d_name);
936 buf += down[depth]->name_len; /* '/' + d_name */
937 size -= down[depth]->name_len;
938 if (size <= 0)
939 break ;
943 /* --- Directory cache live updating functions --- */
944 static int block_until_ready(void)
946 /* Block until dircache has been built. */
947 while (!dircache_initialized && dircache_is_initializing())
948 sleep(1);
950 if (!dircache_initialized)
951 return -1;
953 return 0;
956 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
958 struct dircache_entry *entry;
959 char basedir[MAX_PATH*2];
960 char *new;
961 long last_cache_size = dircache_size;
963 strlcpy(basedir, path, sizeof(basedir));
964 new = strrchr(basedir, '/');
965 if (new == NULL)
967 logf("error occurred");
968 dircache_initialized = false;
969 return NULL;
972 *new = '\0';
973 new++;
975 entry = dircache_get_entry(basedir, true);
976 if (entry == NULL)
978 logf("basedir not found!");
979 logf("%s", basedir);
980 dircache_initialized = false;
981 return NULL;
984 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
985 >= DIRCACHE_RESERVE)
987 logf("not enough space");
988 dircache_initialized = false;
989 return NULL;
992 while (entry->next != NULL)
993 entry = entry->next;
995 if (entry->name_len > 0)
996 entry = dircache_gen_next(entry);
998 if (entry == NULL)
1000 dircache_initialized = false;
1001 return NULL;
1004 entry->attribute = attribute;
1005 entry->name_len = MIN(254, strlen(new)) + 1;
1006 entry->d_name = ((char *)dircache_root+dircache_size);
1007 entry->startcluster = 0;
1008 entry->wrtdate = 0;
1009 entry->wrttime = 0;
1010 entry->size = 0;
1011 memcpy(entry->d_name, new, entry->name_len);
1012 dircache_size += entry->name_len;
1014 if (attribute & ATTR_DIRECTORY)
1016 logf("gen_down");
1017 dircache_gen_down(entry);
1020 reserve_used += dircache_size - last_cache_size;
1022 return entry;
1025 void dircache_bind(int fd, const char *path)
1027 struct dircache_entry *entry;
1029 /* Queue requests until dircache has been built. */
1030 if (!dircache_initialized && dircache_is_initializing())
1032 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1033 return ;
1034 strlcpy(fdbind_cache[fdbind_idx].path, path,
1035 sizeof(fdbind_cache[fdbind_idx].path));
1036 fdbind_cache[fdbind_idx].fd = fd;
1037 fdbind_idx++;
1038 return ;
1041 if (!dircache_initialized)
1042 return ;
1044 logf("bind: %d/%s", fd, path);
1045 entry = dircache_get_entry(path, false);
1046 if (entry == NULL)
1048 logf("not found!");
1049 dircache_initialized = false;
1050 return ;
1053 fd_bindings[fd] = entry;
1056 void dircache_update_filesize(int fd, long newsize, long startcluster)
1058 if (!dircache_initialized || fd < 0)
1059 return ;
1061 if (fd_bindings[fd] == NULL)
1063 logf("dircache fd access error");
1064 dircache_initialized = false;
1065 return ;
1068 fd_bindings[fd]->size = newsize;
1069 fd_bindings[fd]->startcluster = startcluster;
1071 void dircache_update_filetime(int fd)
1073 #if CONFIG_RTC == 0
1074 (void)fd;
1075 #else
1076 short year;
1077 struct tm *now = get_time();
1078 if (!dircache_initialized || fd < 0)
1079 return ;
1081 if (fd_bindings[fd] == NULL)
1083 logf("dircache fd access error");
1084 dircache_initialized = false;
1085 return ;
1087 year = now->tm_year+1900-1980;
1088 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1089 (((now->tm_mon+1)&0xf)<<5) |
1090 (((now->tm_mday)&0x1f));
1091 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1092 (((now->tm_min)&0x3f)<<5) |
1093 (((now->tm_sec/2)&0x1f));
1094 #endif
1097 void dircache_mkdir(const char *path)
1098 { /* Test ok. */
1099 if (block_until_ready())
1100 return ;
1102 logf("mkdir: %s", path);
1103 dircache_new_entry(path, ATTR_DIRECTORY);
1106 void dircache_rmdir(const char *path)
1107 { /* Test ok. */
1108 struct dircache_entry *entry;
1110 if (block_until_ready())
1111 return ;
1113 logf("rmdir: %s", path);
1114 entry = dircache_get_entry(path, false);
1115 if (entry == NULL || entry->down == NULL)
1117 logf("not found or not a directory!");
1118 dircache_initialized = false;
1119 return ;
1122 entry->down = NULL;
1123 entry->name_len = 0;
1126 /* Remove a file from cache */
1127 void dircache_remove(const char *name)
1128 { /* Test ok. */
1129 struct dircache_entry *entry;
1131 if (block_until_ready())
1132 return ;
1134 logf("remove: %s", name);
1136 entry = dircache_get_entry(name, false);
1138 if (entry == NULL)
1140 logf("not found!");
1141 dircache_initialized = false;
1142 return ;
1145 entry->name_len = 0;
1148 void dircache_rename(const char *oldpath, const char *newpath)
1149 { /* Test ok. */
1150 struct dircache_entry *entry, *newentry;
1151 struct dircache_entry oldentry;
1152 char absolute_path[MAX_PATH*2];
1153 char *p;
1155 if (block_until_ready())
1156 return ;
1158 logf("rename: %s->%s", oldpath, newpath);
1160 entry = dircache_get_entry(oldpath, false);
1161 if (entry == NULL)
1163 logf("not found!");
1164 dircache_initialized = false;
1165 return ;
1168 /* Delete the old entry. */
1169 entry->name_len = 0;
1171 /** If we rename the same filename twice in a row, we need to
1172 * save the data, because the entry will be re-used. */
1173 oldentry = *entry;
1175 /* Generate the absolute path for destination if necessary. */
1176 if (newpath[0] != '/')
1178 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1179 p = strrchr(absolute_path, '/');
1180 if (!p)
1182 logf("Invalid path");
1183 dircache_initialized = false;
1184 return ;
1187 *p = '\0';
1188 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1189 newpath = absolute_path;
1192 newentry = dircache_new_entry(newpath, entry->attribute);
1193 if (newentry == NULL)
1195 dircache_initialized = false;
1196 return ;
1199 newentry->down = oldentry.down;
1200 newentry->size = oldentry.size;
1201 newentry->startcluster = oldentry.startcluster;
1202 newentry->wrttime = oldentry.wrttime;
1203 newentry->wrtdate = oldentry.wrtdate;
1206 void dircache_add_file(const char *path, long startcluster)
1208 struct dircache_entry *entry;
1210 if (block_until_ready())
1211 return ;
1213 logf("add file: %s", path);
1214 entry = dircache_new_entry(path, 0);
1215 if (entry == NULL)
1216 return ;
1218 entry->startcluster = startcluster;
1221 DIR_CACHED* opendir_cached(const char* name)
1223 struct dircache_entry *cache_entry;
1224 int dd;
1225 DIR_CACHED* pdir = opendirs;
1227 if ( name[0] != '/' )
1229 DEBUGF("Only absolute paths supported right now\n");
1230 return NULL;
1233 /* find a free dir descriptor */
1234 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1235 if ( !pdir->busy )
1236 break;
1238 if ( dd == MAX_OPEN_DIRS )
1240 DEBUGF("Too many dirs open\n");
1241 errno = EMFILE;
1242 return NULL;
1245 if (!dircache_initialized)
1247 pdir->regulardir = opendir_uncached(name);
1248 if (!pdir->regulardir)
1249 return NULL;
1251 pdir->busy = true;
1252 return pdir;
1255 pdir->busy = true;
1256 pdir->regulardir = NULL;
1257 cache_entry = dircache_get_entry(name, true);
1258 pdir->entry = cache_entry;
1260 if (cache_entry == NULL)
1262 pdir->busy = false;
1263 return NULL;
1266 return pdir;
1269 struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1271 struct dirent_uncached *regentry;
1272 struct dircache_entry *ce;
1274 if (!dir->busy)
1275 return NULL;
1277 if (dir->regulardir != NULL)
1279 regentry = readdir_uncached(dir->regulardir);
1280 if (regentry == NULL)
1281 return NULL;
1283 strlcpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH);
1284 dir->secondary_entry.size = regentry->size;
1285 dir->secondary_entry.startcluster = regentry->startcluster;
1286 dir->secondary_entry.attribute = regentry->attribute;
1287 dir->secondary_entry.wrttime = regentry->wrttime;
1288 dir->secondary_entry.wrtdate = regentry->wrtdate;
1289 dir->secondary_entry.next = NULL;
1291 return &dir->secondary_entry;
1294 do {
1295 if (dir->entry == NULL)
1296 return NULL;
1298 ce = dir->entry;
1299 if (ce->name_len == 0)
1300 dir->entry = ce->next;
1301 } while (ce->name_len == 0) ;
1303 dir->entry = ce->next;
1305 strlcpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH);
1306 /* Can't do `dir->secondary_entry = *ce`
1307 because that modifies the d_name pointer. */
1308 dir->secondary_entry.size = ce->size;
1309 dir->secondary_entry.startcluster = ce->startcluster;
1310 dir->secondary_entry.attribute = ce->attribute;
1311 dir->secondary_entry.wrttime = ce->wrttime;
1312 dir->secondary_entry.wrtdate = ce->wrtdate;
1313 dir->secondary_entry.next = NULL;
1314 dir->internal_entry = ce;
1316 //logf("-> %s", ce->name);
1317 return &dir->secondary_entry;
1320 int closedir_cached(DIR_CACHED* dir)
1322 if (!dir->busy)
1323 return -1;
1325 dir->busy=false;
1326 if (dir->regulardir != NULL)
1327 return closedir_uncached(dir->regulardir);
1329 return 0;
1332 int mkdir_cached(const char *name)
1334 int rc=mkdir_uncached(name);
1335 if (rc >= 0)
1336 dircache_mkdir(name);
1337 return(rc);
1340 int rmdir_cached(const char* name)
1342 int rc=rmdir_uncached(name);
1343 if(rc>=0)
1344 dircache_rmdir(name);
1345 return(rc);