Add note to Bookmarking sections that bookmarking only works from the file browser...
[kugel-rb.git] / firmware / common / dircache.c
blobf844e548f65ed30609cb3542082dd5630a0ddd36
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.
394 * get_before: Returns the cache pointer before the last valid entry found.
395 * only_directories: Match only filenames which are a directory type.
397 static struct dircache_entry* dircache_get_entry(const char *path,
398 bool get_before, bool only_directories)
400 struct dircache_entry *cache_entry, *before;
401 char namecopy[MAX_PATH*2];
402 char* part;
403 char* end;
405 strlcpy(namecopy, path, sizeof(namecopy));
406 cache_entry = dircache_root;
407 before = NULL;
409 for ( part = strtok_r(namecopy, "/", &end); part;
410 part = strtok_r(NULL, "/", &end)) {
412 /* scan dir for name */
413 while (1)
415 if (cache_entry == NULL)
417 return NULL;
419 else if (cache_entry->name_len == 0)
421 cache_entry = cache_entry->next;
422 continue ;
425 if (!strcasecmp(part, cache_entry->d_name))
427 before = cache_entry;
428 if (cache_entry->down || only_directories)
429 cache_entry = cache_entry->down;
430 break ;
433 cache_entry = cache_entry->next;
437 if (get_before)
438 cache_entry = before;
440 return cache_entry;
443 #ifdef HAVE_EEPROM_SETTINGS
445 * Function to load the internal cache structure from disk to initialize
446 * the dircache really fast and little disk access.
448 int dircache_load(void)
450 struct dircache_maindata maindata;
451 int bytes_read;
452 int fd;
454 if (dircache_initialized)
455 return -1;
457 logf("Loading directory cache");
458 dircache_size = 0;
460 fd = open(DIRCACHE_FILE, O_RDONLY);
461 if (fd < 0)
462 return -2;
464 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
465 if (bytes_read != sizeof(struct dircache_maindata)
466 || maindata.size <= 0)
468 logf("Dircache file header error");
469 close(fd);
470 remove(DIRCACHE_FILE);
471 return -3;
474 dircache_root = buffer_alloc(0);
475 if ((long)maindata.root_entry != (long)dircache_root)
477 logf("Position missmatch");
478 close(fd);
479 remove(DIRCACHE_FILE);
480 return -4;
483 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
484 entry_count = maindata.entry_count;
485 appflags = maindata.appflags;
486 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
487 close(fd);
488 remove(DIRCACHE_FILE);
490 if (bytes_read != maindata.size)
492 logf("Dircache read failed");
493 return -6;
496 /* Cache successfully loaded. */
497 dircache_size = maindata.size;
498 allocated_size = dircache_size + DIRCACHE_RESERVE;
499 reserve_used = 0;
500 logf("Done, %ld KiB used", dircache_size / 1024);
501 dircache_initialized = true;
502 memset(fd_bindings, 0, sizeof(fd_bindings));
504 return 0;
508 * Function to save the internal cache stucture to disk for fast loading
509 * on boot.
511 int dircache_save(void)
513 struct dircache_maindata maindata;
514 int fd;
515 unsigned long bytes_written;
517 remove(DIRCACHE_FILE);
519 if (!dircache_initialized)
520 return -1;
522 logf("Saving directory cache");
523 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
525 maindata.magic = DIRCACHE_MAGIC;
526 maindata.size = dircache_size;
527 maindata.root_entry = dircache_root;
528 maindata.entry_count = entry_count;
529 maindata.appflags = appflags;
531 /* Save the info structure */
532 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
533 if (bytes_written != sizeof(struct dircache_maindata))
535 close(fd);
536 logf("dircache: write failed #1");
537 return -2;
540 /* Dump whole directory cache to disk */
541 bytes_written = write(fd, dircache_root, dircache_size);
542 close(fd);
543 if (bytes_written != dircache_size)
545 logf("dircache: write failed #2");
546 return -3;
549 return 0;
551 #endif /* #if 0 */
554 * Internal function which scans the disk and creates the dircache structure.
556 static int dircache_do_rebuild(void)
558 #ifdef SIMULATOR
559 DIR_UNCACHED *pdir;
560 #else
561 struct fat_dir dir, *pdir;
562 #endif
563 unsigned int start_tick;
564 int i;
566 /* Measure how long it takes build the cache. */
567 start_tick = current_tick;
568 dircache_initializing = true;
569 appflags = 0;
570 entry_count = 0;
572 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
573 dircache_size = sizeof(struct dircache_entry);
575 #ifdef HAVE_MULTIVOLUME
576 append_position = dircache_root;
578 for (i = NUM_VOLUMES; i >= 0; i--)
580 if (fat_ismounted(i))
582 #endif
583 #ifdef SIMULATOR
584 pdir = opendir_uncached("/");
585 if (pdir == NULL)
587 logf("Failed to open rootdir");
588 dircache_initializing = false;
589 return -3;
591 #else
592 #ifdef HAVE_MULTIVOLUME
593 if ( fat_opendir(IF_MV2(i,) &dir, 0, NULL) < 0 ) {
594 #else
595 if ( fat_opendir(IF_MV2(0,) &dir, 0, NULL) < 0 ) {
596 #endif /* HAVE_MULTIVOLUME */
597 logf("Failed opening root dir");
598 dircache_initializing = false;
599 return -3;
601 pdir = &dir;
602 #endif
603 cpu_boost(true);
604 #ifdef HAVE_MULTIVOLUME
605 if (dircache_travel(IF_MV2(i,) pdir, append_position) < 0)
606 #else
607 if (dircache_travel(IF_MV2(0,) pdir, dircache_root) < 0)
608 #endif /* HAVE_MULTIVOLUME */
610 logf("dircache_travel failed");
611 cpu_boost(false);
612 dircache_size = 0;
613 dircache_initializing = false;
614 return -2;
616 cpu_boost(false);
617 #ifdef HAVE_MULTIVOLUME
620 #endif
622 logf("Done, %ld KiB used", dircache_size / 1024);
624 dircache_initialized = true;
625 dircache_initializing = false;
626 cache_build_ticks = current_tick - start_tick;
628 /* Initialized fd bindings. */
629 memset(fd_bindings, 0, sizeof(fd_bindings));
630 for (i = 0; i < fdbind_idx; i++)
631 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
632 fdbind_idx = 0;
634 if (thread_enabled)
636 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
637 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
639 else
641 /* We have to long align the audiobuf to keep the buffer access fast. */
642 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
643 audiobuf += DIRCACHE_RESERVE;
644 allocated_size = dircache_size + DIRCACHE_RESERVE;
645 reserve_used = 0;
648 return 1;
652 * Internal thread that controls transparent cache building.
654 static void dircache_thread(void)
656 struct queue_event ev;
658 while (1)
660 queue_wait(&dircache_queue, &ev);
662 switch (ev.id)
664 #ifdef HAVE_HOTSWAP
665 case SYS_FS_CHANGED:
666 if (!dircache_initialized)
667 break;
668 dircache_initialized = false;
669 #endif
670 case DIRCACHE_BUILD:
671 thread_enabled = true;
672 dircache_do_rebuild();
673 thread_enabled = false;
674 break ;
676 case DIRCACHE_STOP:
677 logf("Stopped the rebuilding.");
678 dircache_initialized = false;
679 break ;
681 #ifndef SIMULATOR
682 case SYS_USB_CONNECTED:
683 usb_acknowledge(SYS_USB_CONNECTED_ACK);
684 usb_wait_for_disconnect(&dircache_queue);
685 break ;
686 #endif
692 * Start scanning the disk to build the dircache.
693 * Either transparent or non-transparent build method is used.
695 int dircache_build(int last_size)
697 if (dircache_initialized || thread_enabled)
698 return -3;
700 logf("Building directory cache");
701 remove(DIRCACHE_FILE);
703 /* Background build, dircache has been previously allocated */
704 if (dircache_size > 0)
706 thread_enabled = true;
707 dircache_initializing = true;
708 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
709 return 2;
712 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
714 allocated_size = last_size + DIRCACHE_RESERVE;
715 dircache_root = buffer_alloc(allocated_size);
716 thread_enabled = true;
718 /* Start a transparent rebuild. */
719 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
720 return 3;
723 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
725 /* Start a non-transparent rebuild. */
726 return dircache_do_rebuild();
730 * Steal the allocated dircache buffer and disable dircache.
732 void* dircache_steal_buffer(long *size)
734 dircache_disable();
735 if (dircache_size == 0)
737 *size = 0;
738 return NULL;
741 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
743 return dircache_root;
747 * Main initialization function that must be called before any other
748 * operations within the dircache.
750 void dircache_init(void)
752 int i;
754 dircache_initialized = false;
755 dircache_initializing = false;
757 memset(opendirs, 0, sizeof(opendirs));
758 for (i = 0; i < MAX_OPEN_DIRS; i++)
760 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
763 queue_init(&dircache_queue, true);
764 create_thread(dircache_thread, dircache_stack,
765 sizeof(dircache_stack), 0, dircache_thread_name
766 IF_PRIO(, PRIORITY_BACKGROUND)
767 IF_COP(, CPU));
771 * Returns true if dircache has been initialized and is ready to be used.
773 bool dircache_is_enabled(void)
775 return dircache_initialized;
779 * Returns true if dircache is being initialized.
781 bool dircache_is_initializing(void)
783 return dircache_initializing || thread_enabled;
787 * Set application flags used to determine if dircache is still intact.
789 void dircache_set_appflag(long mask)
791 appflags |= mask;
795 * Get application flags used to determine if dircache is still intact.
797 bool dircache_get_appflag(long mask)
799 return dircache_is_enabled() && (appflags & mask);
803 * Returns the current number of entries (directories and files) in the cache.
805 int dircache_get_entry_count(void)
807 return entry_count;
811 * Returns the allocated space for dircache (without reserve space).
813 int dircache_get_cache_size(void)
815 return dircache_is_enabled() ? dircache_size : 0;
819 * Returns how many bytes of the reserve allocation for live cache
820 * updates have been used.
822 int dircache_get_reserve_used(void)
824 return dircache_is_enabled() ? reserve_used : 0;
828 * Returns the time in kernel ticks that took to build the cache.
830 int dircache_get_build_ticks(void)
832 return dircache_is_enabled() ? cache_build_ticks : 0;
836 * Disables the dircache. Usually called on shutdown or when
837 * accepting a usb connection.
839 void dircache_disable(void)
841 int i;
842 bool cache_in_use;
844 if (thread_enabled)
845 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
847 while (thread_enabled)
848 sleep(1);
849 dircache_initialized = false;
851 logf("Waiting for cached dirs to release");
852 do {
853 cache_in_use = false;
854 for (i = 0; i < MAX_OPEN_DIRS; i++) {
855 if (!opendirs[i].regulardir && opendirs[i].busy)
857 cache_in_use = true;
858 sleep(1);
859 break ;
862 } while (cache_in_use) ;
864 logf("Cache released");
865 entry_count = 0;
869 * Usermode function to return dircache_entry pointer to the given path.
871 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
873 if (!dircache_initialized || filename == NULL)
874 return NULL;
876 return dircache_get_entry(filename, false, false);
880 * Function to copy the full absolute path from dircache to the given buffer
881 * using the given dircache_entry pointer.
883 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
885 const struct dircache_entry *down[MAX_SCAN_DEPTH];
886 int depth = 0;
888 if (size <= 0)
889 return ;
891 buf[0] = '\0';
893 if (entry == NULL)
894 return ;
896 do {
897 down[depth] = entry;
898 entry = entry->up;
899 depth++;
900 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
902 while (--depth >= 0)
904 snprintf(buf, size, "/%s", down[depth]->d_name);
905 buf += down[depth]->name_len; /* '/' + d_name */
906 size -= down[depth]->name_len;
907 if (size <= 0)
908 break ;
912 /* --- Directory cache live updating functions --- */
913 static int block_until_ready(void)
915 /* Block until dircache has been built. */
916 while (!dircache_initialized && dircache_is_initializing())
917 sleep(1);
919 if (!dircache_initialized)
920 return -1;
922 return 0;
925 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
927 struct dircache_entry *entry;
928 char basedir[MAX_PATH*2];
929 char *new;
930 long last_cache_size = dircache_size;
932 strlcpy(basedir, path, sizeof(basedir));
933 new = strrchr(basedir, '/');
934 if (new == NULL)
936 logf("error occurred");
937 dircache_initialized = false;
938 return NULL;
941 *new = '\0';
942 new++;
944 entry = dircache_get_entry(basedir, false, true);
945 if (entry == NULL)
947 logf("basedir not found!");
948 logf("%s", basedir);
949 dircache_initialized = false;
950 return NULL;
953 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
954 >= DIRCACHE_RESERVE)
956 logf("not enough space");
957 dircache_initialized = false;
958 return NULL;
961 while (entry->next != NULL)
962 entry = entry->next;
964 if (entry->name_len > 0)
965 entry = dircache_gen_next(entry);
967 if (entry == NULL)
969 dircache_initialized = false;
970 return NULL;
973 entry->attribute = attribute;
974 entry->name_len = MIN(254, strlen(new)) + 1;
975 entry->d_name = ((char *)dircache_root+dircache_size);
976 entry->startcluster = 0;
977 entry->wrtdate = 0;
978 entry->wrttime = 0;
979 entry->size = 0;
980 memcpy(entry->d_name, new, entry->name_len);
981 dircache_size += entry->name_len;
983 if (attribute & ATTR_DIRECTORY)
985 logf("gen_down");
986 dircache_gen_down(entry);
989 reserve_used += dircache_size - last_cache_size;
991 return entry;
994 void dircache_bind(int fd, const char *path)
996 struct dircache_entry *entry;
998 /* Queue requests until dircache has been built. */
999 if (!dircache_initialized && dircache_is_initializing())
1001 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1002 return ;
1003 strlcpy(fdbind_cache[fdbind_idx].path, path,
1004 sizeof(fdbind_cache[fdbind_idx].path));
1005 fdbind_cache[fdbind_idx].fd = fd;
1006 fdbind_idx++;
1007 return ;
1010 if (!dircache_initialized)
1011 return ;
1013 logf("bind: %d/%s", fd, path);
1014 entry = dircache_get_entry(path, false, false);
1015 if (entry == NULL)
1017 logf("not found!");
1018 dircache_initialized = false;
1019 return ;
1022 fd_bindings[fd] = entry;
1025 void dircache_update_filesize(int fd, long newsize, long startcluster)
1027 if (!dircache_initialized || fd < 0)
1028 return ;
1030 if (fd_bindings[fd] == NULL)
1032 logf("dircache fd access error");
1033 dircache_initialized = false;
1034 return ;
1037 fd_bindings[fd]->size = newsize;
1038 fd_bindings[fd]->startcluster = startcluster;
1040 void dircache_update_filetime(int fd)
1042 #if CONFIG_RTC == 0
1043 (void)fd;
1044 #else
1045 short year;
1046 struct tm *now = get_time();
1047 if (!dircache_initialized || fd < 0)
1048 return ;
1050 if (fd_bindings[fd] == NULL)
1052 logf("dircache fd access error");
1053 dircache_initialized = false;
1054 return ;
1056 year = now->tm_year+1900-1980;
1057 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1058 (((now->tm_mon+1)&0xf)<<5) |
1059 (((now->tm_mday)&0x1f));
1060 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1061 (((now->tm_min)&0x3f)<<5) |
1062 (((now->tm_sec/2)&0x1f));
1063 #endif
1066 void dircache_mkdir(const char *path)
1067 { /* Test ok. */
1068 if (block_until_ready())
1069 return ;
1071 logf("mkdir: %s", path);
1072 dircache_new_entry(path, ATTR_DIRECTORY);
1075 void dircache_rmdir(const char *path)
1076 { /* Test ok. */
1077 struct dircache_entry *entry;
1079 if (block_until_ready())
1080 return ;
1082 logf("rmdir: %s", path);
1083 entry = dircache_get_entry(path, true, true);
1084 if (entry == NULL)
1086 logf("not found!");
1087 dircache_initialized = false;
1088 return ;
1091 entry->down = NULL;
1092 entry->name_len = 0;
1095 /* Remove a file from cache */
1096 void dircache_remove(const char *name)
1097 { /* Test ok. */
1098 struct dircache_entry *entry;
1100 if (block_until_ready())
1101 return ;
1103 logf("remove: %s", name);
1105 entry = dircache_get_entry(name, false, false);
1107 if (entry == NULL)
1109 logf("not found!");
1110 dircache_initialized = false;
1111 return ;
1114 entry->name_len = 0;
1117 void dircache_rename(const char *oldpath, const char *newpath)
1118 { /* Test ok. */
1119 struct dircache_entry *entry, *newentry;
1120 struct dircache_entry oldentry;
1121 char absolute_path[MAX_PATH*2];
1122 char *p;
1124 if (block_until_ready())
1125 return ;
1127 logf("rename: %s->%s", oldpath, newpath);
1129 entry = dircache_get_entry(oldpath, true, false);
1130 if (entry == NULL)
1132 logf("not found!");
1133 dircache_initialized = false;
1134 return ;
1137 /* Delete the old entry. */
1138 entry->name_len = 0;
1140 /** If we rename the same filename twice in a row, we need to
1141 * save the data, because the entry will be re-used. */
1142 oldentry = *entry;
1144 /* Generate the absolute path for destination if necessary. */
1145 if (newpath[0] != '/')
1147 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1148 p = strrchr(absolute_path, '/');
1149 if (!p)
1151 logf("Invalid path");
1152 dircache_initialized = false;
1153 return ;
1156 *p = '\0';
1157 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1158 newpath = absolute_path;
1161 newentry = dircache_new_entry(newpath, entry->attribute);
1162 if (newentry == NULL)
1164 dircache_initialized = false;
1165 return ;
1168 newentry->down = oldentry.down;
1169 newentry->size = oldentry.size;
1170 newentry->startcluster = oldentry.startcluster;
1171 newentry->wrttime = oldentry.wrttime;
1172 newentry->wrtdate = oldentry.wrtdate;
1175 void dircache_add_file(const char *path, long startcluster)
1177 struct dircache_entry *entry;
1179 if (block_until_ready())
1180 return ;
1182 logf("add file: %s", path);
1183 entry = dircache_new_entry(path, 0);
1184 if (entry == NULL)
1185 return ;
1187 entry->startcluster = startcluster;
1190 DIR_CACHED* opendir_cached(const char* name)
1192 struct dircache_entry *cache_entry;
1193 int dd;
1194 DIR_CACHED* pdir = opendirs;
1196 if ( name[0] != '/' )
1198 DEBUGF("Only absolute paths supported right now\n");
1199 return NULL;
1202 /* find a free dir descriptor */
1203 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1204 if ( !pdir->busy )
1205 break;
1207 if ( dd == MAX_OPEN_DIRS )
1209 DEBUGF("Too many dirs open\n");
1210 errno = EMFILE;
1211 return NULL;
1214 if (!dircache_initialized)
1216 pdir->regulardir = opendir_uncached(name);
1217 if (!pdir->regulardir)
1218 return NULL;
1220 pdir->busy = true;
1221 return pdir;
1224 pdir->busy = true;
1225 pdir->regulardir = NULL;
1226 cache_entry = dircache_get_entry(name, false, true);
1227 pdir->entry = cache_entry;
1229 if (cache_entry == NULL)
1231 pdir->busy = false;
1232 return NULL;
1235 return pdir;
1238 struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1240 struct dirent_uncached *regentry;
1241 struct dircache_entry *ce;
1243 if (!dir->busy)
1244 return NULL;
1246 if (dir->regulardir != NULL)
1248 regentry = readdir_uncached(dir->regulardir);
1249 if (regentry == NULL)
1250 return NULL;
1252 strlcpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH);
1253 dir->secondary_entry.size = regentry->size;
1254 dir->secondary_entry.startcluster = regentry->startcluster;
1255 dir->secondary_entry.attribute = regentry->attribute;
1256 dir->secondary_entry.wrttime = regentry->wrttime;
1257 dir->secondary_entry.wrtdate = regentry->wrtdate;
1258 dir->secondary_entry.next = NULL;
1260 return &dir->secondary_entry;
1263 do {
1264 if (dir->entry == NULL)
1265 return NULL;
1267 ce = dir->entry;
1268 if (ce->name_len == 0)
1269 dir->entry = ce->next;
1270 } while (ce->name_len == 0) ;
1272 dir->entry = ce->next;
1274 strlcpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH);
1275 /* Can't do `dir->secondary_entry = *ce`
1276 because that modifies the d_name pointer. */
1277 dir->secondary_entry.size = ce->size;
1278 dir->secondary_entry.startcluster = ce->startcluster;
1279 dir->secondary_entry.attribute = ce->attribute;
1280 dir->secondary_entry.wrttime = ce->wrttime;
1281 dir->secondary_entry.wrtdate = ce->wrtdate;
1282 dir->secondary_entry.next = NULL;
1283 dir->internal_entry = ce;
1285 //logf("-> %s", ce->name);
1286 return &dir->secondary_entry;
1289 int closedir_cached(DIR_CACHED* dir)
1291 if (!dir->busy)
1292 return -1;
1294 dir->busy=false;
1295 if (dir->regulardir != NULL)
1296 return closedir_uncached(dir->regulardir);
1298 return 0;
1301 int mkdir_cached(const char *name)
1303 int rc=mkdir_uncached(name);
1304 if (rc >= 0)
1305 dircache_mkdir(name);
1306 return(rc);
1309 int rmdir_cached(const char* name)
1311 int rc=rmdir_uncached(name);
1312 if(rc>=0)
1313 dircache_rmdir(name);
1314 return(rc);