Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / firmware / common / dircache.c
blob313102b9a1c6492485c047c0c87454c74f8fefe6
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 #define MAX_OPEN_DIRS 8
53 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
55 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
56 static struct dircache_entry *dircache_root;
57 #ifdef HAVE_MULTIVOLUME
58 static struct dircache_entry *append_position;
59 #endif
61 static bool dircache_initialized = false;
62 static bool dircache_initializing = false;
63 static bool thread_enabled = false;
64 static unsigned long allocated_size = DIRCACHE_LIMIT;
65 static unsigned long dircache_size = 0;
66 static unsigned long entry_count = 0;
67 static unsigned long reserve_used = 0;
68 static unsigned int cache_build_ticks = 0;
69 static unsigned long appflags = 0;
70 static char dircache_cur_path[MAX_PATH*2];
72 static struct event_queue dircache_queue;
73 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
74 static const char dircache_thread_name[] = "dircache";
76 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
77 static int fdbind_idx = 0;
79 /* --- Internal cache structure control functions --- */
81 /**
82 * Internal function to allocate a new dircache_entry from memory.
84 static struct dircache_entry* allocate_entry(void)
86 struct dircache_entry *next_entry;
88 if (dircache_size > allocated_size - MAX_PATH*2)
90 logf("size limit reached");
91 return NULL;
94 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
95 #ifdef ROCKBOX_STRICT_ALIGN
96 /* Make sure the entry is long aligned. */
97 if ((long)next_entry & 0x03)
99 dircache_size += 4 - ((long)next_entry & 0x03);
100 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
102 #endif
103 next_entry->name_len = 0;
104 next_entry->d_name = NULL;
105 next_entry->up = NULL;
106 next_entry->down = NULL;
107 next_entry->next = NULL;
109 dircache_size += sizeof(struct dircache_entry);
111 return next_entry;
115 * Internal function to allocate a dircache_entry and set
116 * ->next entry pointers.
118 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
120 struct dircache_entry *next_entry;
122 if ( (next_entry = allocate_entry()) == NULL)
123 return NULL;
124 next_entry->up = ce->up;
125 ce->next = next_entry;
127 return next_entry;
131 * Internal function to allocate a dircache_entry and set
132 * ->down entry pointers.
134 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
136 struct dircache_entry *next_entry;
138 if ( (next_entry = allocate_entry()) == NULL)
139 return NULL;
140 next_entry->up = ce;
141 ce->down = next_entry;
143 return next_entry;
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
149 static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
152 * Returns true if there is an event waiting in the queue
153 * that requires the current operation to be aborted.
155 static bool check_event_queue(void)
157 struct queue_event ev;
159 queue_wait_w_tmo(&dircache_queue, &ev, 0);
160 switch (ev.id)
162 case DIRCACHE_STOP:
163 case SYS_USB_CONNECTED:
164 #ifdef HAVE_HOTSWAP
165 case SYS_FS_CHANGED:
166 #endif
167 /* Put the event back into the queue. */
168 queue_post(&dircache_queue, ev.id, ev.data);
169 return true;
172 return false;
176 * Internal function to iterate a path.
178 static int dircache_scan(IF_MV2(int volume,) struct travel_data *td)
180 #ifdef SIMULATOR
181 while ( ( td->entry = readdir_uncached(td->dir) ) )
182 #else
183 while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
184 #endif
186 #ifdef SIMULATOR
187 if (!strcmp(".", td->entry->d_name) ||
188 !strcmp("..", td->entry->d_name))
190 continue;
193 td->ce->attribute = td->entry->attribute;
194 td->ce->name_len = strlen(td->entry->d_name) + 1;
195 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
201 if (!strcmp(".", td->entry.name) ||
202 !strcmp("..", td->entry.name))
204 continue;
207 td->ce->attribute = td->entry.attr;
208 td->ce->name_len = strlen(td->entry.name) + 1;
209 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);
215 #endif
216 dircache_size += td->ce->name_len;
217 entry_count++;
219 #ifdef SIMULATOR
220 if (td->entry->attribute & ATTR_DIRECTORY)
221 #else
222 if (td->entry.attr & FAT_ATTR_DIRECTORY)
223 #endif
226 td->down_entry = dircache_gen_down(td->ce);
227 if (td->down_entry == NULL)
228 return -2;
230 td->pathpos = strlen(dircache_cur_path);
231 strncpy(&dircache_cur_path[td->pathpos], "/",
232 sizeof(dircache_cur_path) - td->pathpos - 1);
233 #ifdef SIMULATOR
234 strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
235 sizeof(dircache_cur_path) - td->pathpos - 2);
237 td->newdir = opendir_uncached(dircache_cur_path);
238 if (td->newdir == NULL)
240 logf("Failed to opendir_uncached(): %s", dircache_cur_path);
241 return -3;
243 #else
244 strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
245 sizeof(dircache_cur_path) - td->pathpos - 2);
247 td->newdir = *td->dir;
248 if (fat_opendir(IF_MV2(volume,) &td->newdir,
249 td->entry.firstcluster, td->dir) < 0 )
251 return -3;
253 #endif
255 td->ce = dircache_gen_next(td->ce);
256 if (td->ce == NULL)
257 return -4;
259 return 1;
262 td->ce->down = NULL;
263 td->ce = dircache_gen_next(td->ce);
264 if (td->ce == NULL)
265 return -5;
267 /* When simulator is used, it's only safe to yield here. */
268 if (thread_enabled)
270 /* Stop if we got an external signal. */
271 if (check_event_queue())
272 return -6;
273 yield();
278 return 0;
281 /**
282 * Recursively scan the hard disk and build the cache.
284 #ifdef SIMULATOR
285 static int dircache_travel(IF_MV2(int volume,) DIR_UNCACHED *dir, struct dircache_entry *ce)
286 #else
287 static int dircache_travel(IF_MV2(int volume,) struct fat_dir *dir, struct dircache_entry *ce)
288 #endif
290 int depth = 0;
291 int result;
293 memset(ce, 0, sizeof(struct dircache_entry));
295 #if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR)
296 if (volume > 0)
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);
307 #endif
309 dir_recursion[0].dir = dir;
310 dir_recursion[0].ce = ce;
311 dir_recursion[0].first = ce;
313 do {
314 //logf("=> %s", dircache_cur_path);
315 result = dircache_scan(IF_MV2(volume,) &dir_recursion[depth]);
316 switch (result) {
317 case 0: /* Leaving the current directory. */
318 /* Add the standard . and .. entries. */
319 ce = dir_recursion[depth].ce;
320 ce->d_name = ".";
321 ce->name_len = 2;
322 #ifdef SIMULATOR
323 closedir_uncached(dir_recursion[depth].dir);
324 ce->attribute = ATTR_DIRECTORY;
325 #else
326 ce->attribute = FAT_ATTR_DIRECTORY;
327 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
328 #endif
329 ce->size = 0;
330 ce->down = dir_recursion[depth].first;
332 depth--;
333 if (depth < 0)
334 break ;
336 dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
338 ce = dircache_gen_next(ce);
339 if (ce == NULL)
341 logf("memory allocation error");
342 return -3;
344 #ifdef SIMULATOR
345 ce->attribute = ATTR_DIRECTORY;
346 #else
347 ce->attribute = FAT_ATTR_DIRECTORY;
348 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
349 #endif
350 ce->d_name = "..";
351 ce->name_len = 3;
352 ce->size = 0;
353 ce->down = dir_recursion[depth].first;
355 break ;
357 case 1: /* Going down in the directory tree. */
358 depth++;
359 if (depth >= MAX_SCAN_DEPTH)
361 logf("Too deep directory structure");
362 return -2;
365 #ifdef SIMULATOR
366 dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
367 #else
368 dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
369 #endif
370 dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
371 dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
372 break ;
374 default:
375 logf("Scan failed");
376 logf("-> %s", dircache_cur_path);
377 return -1;
379 } while (depth >= 0) ;
381 return 0;
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.
390 static struct dircache_entry* dircache_get_entry(const char *path,
391 bool get_before, bool only_directories)
393 struct dircache_entry *cache_entry, *before;
394 char namecopy[MAX_PATH*2];
395 char* part;
396 char* end;
398 strncpy(namecopy, path, sizeof(namecopy) - 1);
399 cache_entry = dircache_root;
400 before = NULL;
402 for ( part = strtok_r(namecopy, "/", &end); part;
403 part = strtok_r(NULL, "/", &end)) {
405 /* scan dir for name */
406 while (1)
408 if (cache_entry == NULL)
410 return NULL;
412 else if (cache_entry->name_len == 0)
414 cache_entry = cache_entry->next;
415 continue ;
418 if (!strcasecmp(part, cache_entry->d_name))
420 before = cache_entry;
421 if (cache_entry->down || only_directories)
422 cache_entry = cache_entry->down;
423 break ;
426 cache_entry = cache_entry->next;
430 if (get_before)
431 cache_entry = before;
433 return cache_entry;
436 #ifdef HAVE_EEPROM_SETTINGS
438 * Function to load the internal cache structure from disk to initialize
439 * the dircache really fast and little disk access.
441 int dircache_load(void)
443 struct dircache_maindata maindata;
444 int bytes_read;
445 int fd;
447 if (dircache_initialized)
448 return -1;
450 logf("Loading directory cache");
451 dircache_size = 0;
453 fd = open(DIRCACHE_FILE, O_RDONLY);
454 if (fd < 0)
455 return -2;
457 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
458 if (bytes_read != sizeof(struct dircache_maindata)
459 || maindata.size <= 0)
461 logf("Dircache file header error");
462 close(fd);
463 remove(DIRCACHE_FILE);
464 return -3;
467 dircache_root = buffer_alloc(0);
468 if ((long)maindata.root_entry != (long)dircache_root)
470 logf("Position missmatch");
471 close(fd);
472 remove(DIRCACHE_FILE);
473 return -4;
476 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
477 entry_count = maindata.entry_count;
478 appflags = maindata.appflags;
479 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
480 close(fd);
481 remove(DIRCACHE_FILE);
483 if (bytes_read != maindata.size)
485 logf("Dircache read failed");
486 return -6;
489 /* Cache successfully loaded. */
490 dircache_size = maindata.size;
491 allocated_size = dircache_size + DIRCACHE_RESERVE;
492 reserve_used = 0;
493 logf("Done, %ld KiB used", dircache_size / 1024);
494 dircache_initialized = true;
495 memset(fd_bindings, 0, sizeof(fd_bindings));
497 return 0;
501 * Function to save the internal cache stucture to disk for fast loading
502 * on boot.
504 int dircache_save(void)
506 struct dircache_maindata maindata;
507 int fd;
508 unsigned long bytes_written;
510 remove(DIRCACHE_FILE);
512 if (!dircache_initialized)
513 return -1;
515 logf("Saving directory cache");
516 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
518 maindata.magic = DIRCACHE_MAGIC;
519 maindata.size = dircache_size;
520 maindata.root_entry = dircache_root;
521 maindata.entry_count = entry_count;
522 maindata.appflags = appflags;
524 /* Save the info structure */
525 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
526 if (bytes_written != sizeof(struct dircache_maindata))
528 close(fd);
529 logf("dircache: write failed #1");
530 return -2;
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)
538 logf("dircache: write failed #2");
539 return -3;
542 return 0;
544 #endif /* #if 0 */
547 * Internal function which scans the disk and creates the dircache structure.
549 static int dircache_do_rebuild(void)
551 #ifdef SIMULATOR
552 DIR_UNCACHED *pdir;
553 #else
554 struct fat_dir dir, *pdir;
555 #endif
556 unsigned int start_tick;
557 int i;
559 /* Measure how long it takes build the cache. */
560 start_tick = current_tick;
561 dircache_initializing = true;
562 appflags = 0;
563 entry_count = 0;
565 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
566 dircache_size = sizeof(struct dircache_entry);
568 #ifdef HAVE_MULTIVOLUME
569 append_position = dircache_root;
571 for (i = NUM_VOLUMES; i >= 0; i--)
573 if (fat_ismounted(i))
575 #endif
576 #ifdef SIMULATOR
577 pdir = opendir_uncached("/");
578 if (pdir == NULL)
580 logf("Failed to open rootdir");
581 dircache_initializing = false;
582 return -3;
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;
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 */
603 logf("dircache_travel failed");
604 cpu_boost(false);
605 dircache_size = 0;
606 dircache_initializing = false;
607 return -2;
609 cpu_boost(false);
610 #ifdef HAVE_MULTIVOLUME
613 #endif
615 logf("Done, %ld KiB used", dircache_size / 1024);
617 dircache_initialized = true;
618 dircache_initializing = false;
619 cache_build_ticks = current_tick - start_tick;
621 /* 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);
625 fdbind_idx = 0;
627 if (thread_enabled)
629 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
630 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
632 else
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;
638 reserve_used = 0;
641 return 1;
645 * Internal thread that controls transparent cache building.
647 static void dircache_thread(void)
649 struct queue_event ev;
651 while (1)
653 queue_wait(&dircache_queue, &ev);
655 switch (ev.id)
657 #ifdef HAVE_HOTSWAP
658 case SYS_FS_CHANGED:
659 if (!dircache_initialized)
660 break;
661 dircache_initialized = false;
662 #endif
663 case DIRCACHE_BUILD:
664 thread_enabled = true;
665 dircache_do_rebuild();
666 thread_enabled = false;
667 break ;
669 case DIRCACHE_STOP:
670 logf("Stopped the rebuilding.");
671 dircache_initialized = false;
672 break ;
674 #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
685 * Start scanning the disk to build the dircache.
686 * Either transparent or non-transparent build method is used.
688 int dircache_build(int last_size)
690 if (dircache_initialized || thread_enabled)
691 return -3;
693 logf("Building directory cache");
694 remove(DIRCACHE_FILE);
696 /* Background build, dircache has been previously allocated */
697 if (dircache_size > 0)
699 thread_enabled = true;
700 dircache_initializing = true;
701 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
702 return 2;
705 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
707 allocated_size = last_size + DIRCACHE_RESERVE;
708 dircache_root = buffer_alloc(allocated_size);
709 thread_enabled = true;
711 /* Start a transparent rebuild. */
712 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
713 return 3;
716 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
718 /* Start a non-transparent rebuild. */
719 return dircache_do_rebuild();
723 * Steal the allocated dircache buffer and disable dircache.
725 void* dircache_steal_buffer(long *size)
727 dircache_disable();
728 if (dircache_size == 0)
730 *size = 0;
731 return NULL;
734 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
736 return dircache_root;
740 * Main initialization function that must be called before any other
741 * operations within the dircache.
743 void dircache_init(void)
745 int i;
747 dircache_initialized = false;
748 dircache_initializing = false;
750 memset(opendirs, 0, sizeof(opendirs));
751 for (i = 0; i < MAX_OPEN_DIRS; i++)
753 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
756 queue_init(&dircache_queue, true);
757 create_thread(dircache_thread, dircache_stack,
758 sizeof(dircache_stack), 0, dircache_thread_name
759 IF_PRIO(, PRIORITY_BACKGROUND)
760 IF_COP(, CPU));
764 * Returns true if dircache has been initialized and is ready to be used.
766 bool dircache_is_enabled(void)
768 return dircache_initialized;
772 * Returns true if dircache is being initialized.
774 bool dircache_is_initializing(void)
776 return dircache_initializing || thread_enabled;
780 * Set application flags used to determine if dircache is still intact.
782 void dircache_set_appflag(long mask)
784 appflags |= mask;
788 * Get application flags used to determine if dircache is still intact.
790 bool dircache_get_appflag(long mask)
792 return dircache_is_enabled() && (appflags & mask);
796 * Returns the current number of entries (directories and files) in the cache.
798 int dircache_get_entry_count(void)
800 return entry_count;
804 * Returns the allocated space for dircache (without reserve space).
806 int dircache_get_cache_size(void)
808 return dircache_is_enabled() ? dircache_size : 0;
812 * Returns how many bytes of the reserve allocation for live cache
813 * updates have been used.
815 int dircache_get_reserve_used(void)
817 return dircache_is_enabled() ? reserve_used : 0;
821 * Returns the time in kernel ticks that took to build the cache.
823 int dircache_get_build_ticks(void)
825 return dircache_is_enabled() ? cache_build_ticks : 0;
829 * Disables the dircache. Usually called on shutdown or when
830 * accepting a usb connection.
832 void dircache_disable(void)
834 int i;
835 bool cache_in_use;
837 if (thread_enabled)
838 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
840 while (thread_enabled)
841 sleep(1);
842 dircache_initialized = false;
844 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)
850 cache_in_use = true;
851 sleep(1);
852 break ;
855 } while (cache_in_use) ;
857 logf("Cache released");
858 entry_count = 0;
862 * Usermode function to return dircache_entry pointer to the given path.
864 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
866 if (!dircache_initialized || filename == NULL)
867 return NULL;
869 return dircache_get_entry(filename, false, false);
873 * Function to copy the full absolute path from dircache to the given buffer
874 * using the given dircache_entry pointer.
876 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
878 const struct dircache_entry *down[MAX_SCAN_DEPTH];
879 int depth = 0;
881 if (size <= 0)
882 return ;
884 buf[0] = '\0';
886 if (entry == NULL)
887 return ;
889 do {
890 down[depth] = entry;
891 entry = entry->up;
892 depth++;
893 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
895 while (--depth >= 0)
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 ;
905 /* --- Directory cache live updating functions --- */
906 static int block_until_ready(void)
908 /* Block until dircache has been built. */
909 while (!dircache_initialized && dircache_is_initializing())
910 sleep(1);
912 if (!dircache_initialized)
913 return -1;
915 return 0;
918 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
920 struct dircache_entry *entry;
921 char basedir[MAX_PATH*2];
922 char *new;
923 long last_cache_size = dircache_size;
925 strncpy(basedir, path, sizeof(basedir)-1);
926 new = strrchr(basedir, '/');
927 if (new == NULL)
929 logf("error occurred");
930 dircache_initialized = false;
931 return NULL;
934 *new = '\0';
935 new++;
937 entry = dircache_get_entry(basedir, false, true);
938 if (entry == NULL)
940 logf("basedir not found!");
941 logf("%s", basedir);
942 dircache_initialized = false;
943 return NULL;
946 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
947 >= DIRCACHE_RESERVE)
949 logf("not enough space");
950 dircache_initialized = false;
951 return NULL;
954 while (entry->next != NULL)
955 entry = entry->next;
957 if (entry->name_len > 0)
958 entry = dircache_gen_next(entry);
960 if (entry == NULL)
962 dircache_initialized = false;
963 return NULL;
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;
976 if (attribute & ATTR_DIRECTORY)
978 logf("gen_down");
979 dircache_gen_down(entry);
982 reserve_used += dircache_size - last_cache_size;
984 return entry;
987 void dircache_bind(int fd, const char *path)
989 struct dircache_entry *entry;
991 /* Queue requests until dircache has been built. */
992 if (!dircache_initialized && dircache_is_initializing())
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 ;
1003 if (!dircache_initialized)
1004 return ;
1006 logf("bind: %d/%s", fd, path);
1007 entry = dircache_get_entry(path, false, false);
1008 if (entry == NULL)
1010 logf("not found!");
1011 dircache_initialized = false;
1012 return ;
1015 fd_bindings[fd] = entry;
1018 void dircache_update_filesize(int fd, long newsize, long startcluster)
1020 if (!dircache_initialized || fd < 0)
1021 return ;
1023 if (fd_bindings[fd] == NULL)
1025 logf("dircache fd access error");
1026 dircache_initialized = false;
1027 return ;
1030 fd_bindings[fd]->size = newsize;
1031 fd_bindings[fd]->startcluster = startcluster;
1033 void dircache_update_filetime(int fd)
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 ;
1043 if (fd_bindings[fd] == NULL)
1045 logf("dircache fd access error");
1046 dircache_initialized = false;
1047 return ;
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
1059 void dircache_mkdir(const char *path)
1060 { /* Test ok. */
1061 if (block_until_ready())
1062 return ;
1064 logf("mkdir: %s", path);
1065 dircache_new_entry(path, ATTR_DIRECTORY);
1068 void dircache_rmdir(const char *path)
1069 { /* Test ok. */
1070 struct dircache_entry *entry;
1072 if (block_until_ready())
1073 return ;
1075 logf("rmdir: %s", path);
1076 entry = dircache_get_entry(path, true, true);
1077 if (entry == NULL)
1079 logf("not found!");
1080 dircache_initialized = false;
1081 return ;
1084 entry->down = NULL;
1085 entry->name_len = 0;
1088 /* Remove a file from cache */
1089 void dircache_remove(const char *name)
1090 { /* Test ok. */
1091 struct dircache_entry *entry;
1093 if (block_until_ready())
1094 return ;
1096 logf("remove: %s", name);
1098 entry = dircache_get_entry(name, false, false);
1100 if (entry == NULL)
1102 logf("not found!");
1103 dircache_initialized = false;
1104 return ;
1107 entry->name_len = 0;
1110 void dircache_rename(const char *oldpath, const char *newpath)
1111 { /* Test ok. */
1112 struct dircache_entry *entry, *newentry;
1113 struct dircache_entry oldentry;
1114 char absolute_path[MAX_PATH*2];
1115 char *p;
1117 if (block_until_ready())
1118 return ;
1120 logf("rename: %s->%s", oldpath, newpath);
1122 entry = dircache_get_entry(oldpath, true, false);
1123 if (entry == NULL)
1125 logf("not found!");
1126 dircache_initialized = false;
1127 return ;
1130 /* Delete the old entry. */
1131 entry->name_len = 0;
1133 /** 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;
1137 /* Generate the absolute path for destination if necessary. */
1138 if (newpath[0] != '/')
1140 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
1141 p = strrchr(absolute_path, '/');
1142 if (!p)
1144 logf("Invalid path");
1145 dircache_initialized = false;
1146 return ;
1149 *p = '\0';
1150 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
1151 newpath = absolute_path;
1154 newentry = dircache_new_entry(newpath, entry->attribute);
1155 if (newentry == NULL)
1157 dircache_initialized = false;
1158 return ;
1161 newentry->down = oldentry.down;
1162 newentry->size = oldentry.size;
1163 newentry->startcluster = oldentry.startcluster;
1164 newentry->wrttime = oldentry.wrttime;
1165 newentry->wrtdate = oldentry.wrtdate;
1168 void dircache_add_file(const char *path, long startcluster)
1170 struct dircache_entry *entry;
1172 if (block_until_ready())
1173 return ;
1175 logf("add file: %s", path);
1176 entry = dircache_new_entry(path, 0);
1177 if (entry == NULL)
1178 return ;
1180 entry->startcluster = startcluster;
1183 DIR_CACHED* opendir_cached(const char* name)
1185 struct dircache_entry *cache_entry;
1186 int dd;
1187 DIR_CACHED* pdir = opendirs;
1189 if ( name[0] != '/' )
1191 DEBUGF("Only absolute paths supported right now\n");
1192 return NULL;
1195 /* find a free dir descriptor */
1196 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1197 if ( !pdir->busy )
1198 break;
1200 if ( dd == MAX_OPEN_DIRS )
1202 DEBUGF("Too many dirs open\n");
1203 errno = EMFILE;
1204 return NULL;
1207 if (!dircache_initialized)
1209 pdir->regulardir = opendir_uncached(name);
1210 if (!pdir->regulardir)
1211 return NULL;
1213 pdir->busy = true;
1214 return pdir;
1217 pdir->busy = true;
1218 pdir->regulardir = NULL;
1219 cache_entry = dircache_get_entry(name, false, true);
1220 pdir->entry = cache_entry;
1222 if (cache_entry == NULL)
1224 pdir->busy = false;
1225 return NULL;
1228 return pdir;
1231 struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1233 struct dirent_uncached *regentry;
1234 struct dircache_entry *ce;
1236 if (!dir->busy)
1237 return NULL;
1239 if (dir->regulardir != NULL)
1241 regentry = readdir_uncached(dir->regulardir);
1242 if (regentry == NULL)
1243 return NULL;
1245 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;
1253 return &dir->secondary_entry;
1256 do {
1257 if (dir->entry == NULL)
1258 return NULL;
1260 ce = dir->entry;
1261 if (ce->name_len == 0)
1262 dir->entry = ce->next;
1263 } while (ce->name_len == 0) ;
1265 dir->entry = ce->next;
1267 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;
1276 dir->internal_entry = ce;
1278 //logf("-> %s", ce->name);
1279 return &dir->secondary_entry;
1282 int closedir_cached(DIR_CACHED* dir)
1284 if (!dir->busy)
1285 return -1;
1287 dir->busy=false;
1288 if (dir->regulardir != NULL)
1289 return closedir_uncached(dir->regulardir);
1291 return 0;
1294 int mkdir_cached(const char *name)
1296 int rc=mkdir_uncached(name);
1297 if (rc >= 0)
1298 dircache_mkdir(name);
1299 return(rc);
1302 int rmdir_cached(const char* name)
1304 int rc=rmdir_uncached(name);
1305 if(rc>=0)
1306 dircache_rmdir(name);
1307 return(rc);