All kernel objects in code shared amongs targets (core, plugins, codecs) should be...
[kugel-rb.git] / firmware / common / dircache.c
blob45726a32378c68864769f6811f553580ec8512d7
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-extra.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 #include "storage.h"
44 #if CONFIG_RTC
45 #include "time.h"
46 #include "timefuncs.h"
47 #endif
48 #include "rbpaths.h"
50 /* Queue commands. */
51 #define DIRCACHE_BUILD 1
52 #define DIRCACHE_STOP 2
54 #if (MEMORYSIZE > 8)
55 #define MAX_OPEN_DIRS 12
56 #else
57 #define MAX_OPEN_DIRS 8
58 #endif
59 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
61 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
62 static struct dircache_entry *dircache_root;
63 #ifdef HAVE_MULTIVOLUME
64 static struct dircache_entry *append_position;
65 #endif
67 static bool dircache_initialized = false;
68 static bool dircache_initializing = false;
69 static bool thread_enabled = false;
70 static unsigned long allocated_size = DIRCACHE_LIMIT;
71 static unsigned long dircache_size = 0;
72 static unsigned long entry_count = 0;
73 static unsigned long reserve_used = 0;
74 static unsigned int cache_build_ticks = 0;
75 static unsigned long appflags = 0;
77 static struct event_queue dircache_queue SHAREDBSS_ATTR;
78 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x400)/sizeof(long)];
79 static const char dircache_thread_name[] = "dircache";
81 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
82 static int fdbind_idx = 0;
84 /* --- Internal cache structure control functions --- */
86 #ifdef HAVE_EEPROM_SETTINGS
87 /**
88 * Open the dircache file to save a snapshot on disk
90 static int open_dircache_file(unsigned flags, int permissions)
92 if (permissions != 0)
93 return open(DIRCACHE_FILE, flags, permissions);
95 return open(DIRCACHE_FILE, flags);
98 /**
99 * Remove the snapshot file
101 static int remove_dircache_file(void)
103 return remove(DIRCACHE_FILE);
105 #endif
106 /**
107 * Internal function to allocate a new dircache_entry from memory.
109 static struct dircache_entry* allocate_entry(void)
111 struct dircache_entry *next_entry;
113 if (dircache_size > allocated_size - MAX_PATH*2)
115 logf("size limit reached");
116 return NULL;
119 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
120 #ifdef ROCKBOX_STRICT_ALIGN
121 /* Make sure the entry is long aligned. */
122 if ((long)next_entry & 0x03)
124 dircache_size += 4 - ((long)next_entry & 0x03);
125 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
127 #endif
128 next_entry->name_len = 0;
129 next_entry->d_name = NULL;
130 next_entry->up = NULL;
131 next_entry->down = NULL;
132 next_entry->next = NULL;
134 dircache_size += sizeof(struct dircache_entry);
136 return next_entry;
140 * Internal function to allocate a dircache_entry and set
141 * ->next entry pointers.
143 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
145 struct dircache_entry *next_entry;
147 if ( (next_entry = allocate_entry()) == NULL)
148 return NULL;
149 next_entry->up = ce->up;
150 ce->next = next_entry;
152 return next_entry;
156 * Internal function to allocate a dircache_entry and set
157 * ->down entry pointers.
159 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
161 struct dircache_entry *next_entry;
163 if ( (next_entry = allocate_entry()) == NULL)
164 return NULL;
165 next_entry->up = ce;
166 ce->down = next_entry;
168 return next_entry;
172 * Returns true if there is an event waiting in the queue
173 * that requires the current operation to be aborted.
175 static bool check_event_queue(void)
177 struct queue_event ev;
179 if(!queue_peek(&dircache_queue, &ev))
180 return false;
182 switch (ev.id)
184 case DIRCACHE_STOP:
185 case SYS_USB_CONNECTED:
186 #ifdef HAVE_HOTSWAP
187 case SYS_FS_CHANGED:
188 #endif
189 return true;
192 return false;
195 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
196 /* scan and build static data (avoid redundancy on stack) */
197 static struct
199 #ifdef HAVE_MULTIVOLUME
200 int volume;
201 #endif
202 struct fat_dir *dir;
203 struct fat_direntry *direntry;
204 }sab;
206 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
208 /* normally, opendir expects a full fat_dir as parent but in our case,
209 * it's completely useless because we don't modify anything
210 * WARNING: this heavily relies on current FAT implementation ! */
212 /* those field are necessary to update the FAT entry in case of modification
213 here we don't touch anything so we put dummy values */
214 sab.dir->entry = 0;
215 sab.dir->entrycount = 0;
216 sab.dir->file.firstcluster = 0;
217 /* open directory */
218 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
219 if(rc < 0)
221 logf("fat_opendir failed: %d", rc);
222 return rc;
225 /* first pass : read dir */
226 struct dircache_entry *first_ce = ce;
228 /* read through directory */
229 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
231 if(!strcmp(".", sab.direntry->name) ||
232 !strcmp("..", sab.direntry->name))
233 continue;
235 ce->name_len = strlen(sab.direntry->name) + 1;
236 ce->d_name = ((char *)dircache_root + dircache_size);
237 ce->startcluster = sab.direntry->firstcluster;
238 ce->info.size = sab.direntry->filesize;
239 ce->info.attribute = sab.direntry->attr;
240 ce->info.wrtdate = sab.direntry->wrtdate;
241 ce->info.wrttime = sab.direntry->wrttime;
242 memcpy(ce->d_name, sab.direntry->name, ce->name_len);
244 dircache_size += ce->name_len;
245 entry_count++;
247 if(ce->info.attribute & FAT_ATTR_DIRECTORY)
248 dircache_gen_down(ce);
250 ce = dircache_gen_next(ce);
251 if(ce == NULL)
252 return -5;
254 /* When simulator is used, it's only safe to yield here. */
255 if(thread_enabled)
257 /* Stop if we got an external signal. */
258 if(check_event_queue())
259 return -6;
260 yield();
264 /* add "." and ".." */
265 ce->d_name = ".";
266 ce->name_len = 2;
267 ce->info.attribute = FAT_ATTR_DIRECTORY;
268 ce->startcluster = startcluster;
269 ce->info.size = 0;
270 ce->down = first_ce;
272 ce = dircache_gen_next(ce);
274 ce->d_name = "..";
275 ce->name_len = 3;
276 ce->info.attribute = FAT_ATTR_DIRECTORY;
277 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
278 ce->info.size = 0;
279 ce->down = first_ce->up;
281 /* second pass: recurse ! */
282 ce = first_ce;
284 while(rc >= 0 && ce)
286 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
287 rc = sab_process_dir(ce->startcluster, ce->down);
289 ce = ce->next;
292 return rc;
295 /* used during the generation */
296 static struct fat_dir sab_fat_dir;
298 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
300 memset(ce, 0, sizeof(struct dircache_entry));
302 #ifdef HAVE_MULTIVOLUME
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->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
310 ce->info.size = 0;
311 append_position = dircache_gen_next(ce);
312 ce = dircache_gen_down(ce);
314 #endif
316 struct fat_direntry direntry; /* ditto */
317 #ifdef HAVE_MULTIVOLUME
318 sab.volume = volume;
319 #endif
320 sab.dir = &sab_fat_dir;
321 sab.direntry = &direntry;
323 return sab_process_dir(0, ce);
325 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
326 static char sab_path[MAX_PATH];
328 static int sab_process_dir(struct dircache_entry *ce)
330 struct dirent_uncached *entry;
331 struct dircache_entry *first_ce = ce;
332 DIR_UNCACHED *dir = opendir_uncached(sab_path);
333 if(dir == NULL)
335 logf("Failed to opendir_uncached(%s)", sab_path);
336 return -1;
339 while((entry = readdir_uncached(dir)))
341 if(!strcmp(".", entry->d_name) ||
342 !strcmp("..", entry->d_name))
343 continue;
345 ce->name_len = strlen(entry->d_name) + 1;
346 ce->d_name = ((char *)dircache_root + dircache_size);
347 ce->info = entry->info;
348 memcpy(ce->d_name, entry->d_name, ce->name_len);
350 dircache_size += ce->name_len;
351 entry_count++;
353 if(entry->info.attribute & ATTR_DIRECTORY)
355 dircache_gen_down(ce);
356 if(ce->down == NULL)
358 closedir_uncached(dir);
359 return -1;
361 /* save current paths size */
362 int pathpos = strlen(sab_path);
363 /* append entry */
364 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
365 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
367 int rc = sab_process_dir(ce->down);
368 /* restore path */
369 sab_path[pathpos] = '\0';
371 if(rc < 0)
373 closedir_uncached(dir);
374 return rc;
378 ce = dircache_gen_next(ce);
379 if(ce == NULL)
380 return -5;
382 /* When simulator is used, it's only safe to yield here. */
383 if(thread_enabled)
385 /* Stop if we got an external signal. */
386 if(check_event_queue())
387 return -1;
388 yield();
392 /* add "." and ".." */
393 ce->d_name = ".";
394 ce->name_len = 2;
395 ce->info.attribute = ATTR_DIRECTORY;
396 ce->info.size = 0;
397 ce->down = first_ce;
399 ce = dircache_gen_next(ce);
401 ce->d_name = "..";
402 ce->name_len = 3;
403 ce->info.attribute = ATTR_DIRECTORY;
404 ce->info.size = 0;
405 ce->down = first_ce->up;
407 closedir_uncached(dir);
408 return 0;
411 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
413 #ifdef HAVE_MULTIVOLUME
414 (void) volume;
415 #endif
416 memset(ce, 0, sizeof(struct dircache_entry));
418 strlcpy(sab_path, "/", sizeof sab_path);
419 return sab_process_dir(ce);
421 #endif /* PLATFORM_NATIVE */
424 * Internal function to get a pointer to dircache_entry for a given filename.
425 * path: Absolute path to a file or directory (see comment)
426 * go_down: Returns the first entry of the directory given by the path (see comment)
428 * As a a special case, accept path="" as an alias for "/".
429 * Also if the path omits the first '/', it will be accepted.
431 * * If get_down=true:
432 * If path="/", the returned entry is the first of root directory (ie dircache_root)
433 * Otherwise, if 'entry' is the returned value when get_down=false,
434 * the functions returns entry->down (which can be NULL)
436 * * If get_down=false:
437 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
438 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
439 * Which means that
440 * dircache_get_entry(path)->d_name == chunk_n
442 * If path="/", the returned entry is NULL.
443 * If the entry doesn't exist, return NULL
445 * NOTE: this functions silently handles double '/'
447 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
449 char namecopy[MAX_PATH];
450 char* part;
451 char* end;
453 bool at_root = true;
454 struct dircache_entry *cache_entry = dircache_root;
456 strlcpy(namecopy, path, sizeof(namecopy));
458 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
460 /* If request another chunk, the current entry has to be directory
461 * and so cache_entry->down has to be non-NULL/
462 * Special case of root because it's already the first entry of the root directory
464 * NOTE: this is safe even if cache_entry->down is NULL */
465 if(!at_root)
466 cache_entry = cache_entry->down;
467 else
468 at_root = false;
470 /* scan dir for name */
471 while(cache_entry != NULL)
473 /* skip unused entries */
474 if(cache_entry->name_len == 0)
476 cache_entry = cache_entry->next;
477 continue;
479 /* compare names */
480 if(!strcasecmp(part, cache_entry->d_name))
481 break;
482 /* go to next entry */
483 cache_entry = cache_entry->next;
486 /* handle not found case */
487 if(cache_entry == NULL)
488 return NULL;
491 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
492 if(go_down)
493 return at_root ? cache_entry : cache_entry->down;
494 else
495 return at_root ? NULL : cache_entry;
498 #ifdef HAVE_EEPROM_SETTINGS
500 * Function to load the internal cache structure from disk to initialize
501 * the dircache really fast and little disk access.
503 int dircache_load(void)
505 struct dircache_maindata maindata;
506 int bytes_read;
507 int fd;
509 if (dircache_initialized)
510 return -1;
512 logf("Loading directory cache");
513 dircache_size = 0;
515 fd = open_dircache_file(O_RDONLY, 0);
516 if (fd < 0)
517 return -2;
519 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
520 if (bytes_read != sizeof(struct dircache_maindata)
521 || maindata.size <= 0)
523 logf("Dircache file header error");
524 close(fd);
525 remove_dircache_file();
526 return -3;
529 dircache_root = buffer_alloc(0);
530 if ((long)maindata.root_entry != (long)dircache_root)
532 logf("Position missmatch");
533 close(fd);
534 remove_dircache_file();
535 return -4;
538 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
539 entry_count = maindata.entry_count;
540 appflags = maindata.appflags;
541 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
542 close(fd);
543 remove_dircache_file();
545 if (bytes_read != maindata.size)
547 logf("Dircache read failed");
548 return -6;
551 /* Cache successfully loaded. */
552 dircache_size = maindata.size;
553 allocated_size = dircache_size + DIRCACHE_RESERVE;
554 reserve_used = 0;
555 logf("Done, %ld KiB used", dircache_size / 1024);
556 dircache_initialized = true;
557 memset(fd_bindings, 0, sizeof(fd_bindings));
559 return 0;
563 * Function to save the internal cache stucture to disk for fast loading
564 * on boot.
566 int dircache_save(void)
568 struct dircache_maindata maindata;
569 int fd;
570 unsigned long bytes_written;
572 remove_dircache_file();
574 if (!dircache_initialized)
575 return -1;
577 logf("Saving directory cache");
578 fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
580 maindata.magic = DIRCACHE_MAGIC;
581 maindata.size = dircache_size;
582 maindata.root_entry = dircache_root;
583 maindata.entry_count = entry_count;
584 maindata.appflags = appflags;
586 /* Save the info structure */
587 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
588 if (bytes_written != sizeof(struct dircache_maindata))
590 close(fd);
591 logf("dircache: write failed #1");
592 return -2;
595 /* Dump whole directory cache to disk */
596 bytes_written = write(fd, dircache_root, dircache_size);
597 close(fd);
598 if (bytes_written != dircache_size)
600 logf("dircache: write failed #2");
601 return -3;
604 return 0;
606 #endif /* HAVE_EEPROM_SETTINGS */
609 * Internal function which scans the disk and creates the dircache structure.
611 static int dircache_do_rebuild(void)
613 unsigned int start_tick;
614 int i;
616 /* Measure how long it takes build the cache. */
617 start_tick = current_tick;
618 dircache_initializing = true;
619 appflags = 0;
620 entry_count = 0;
622 dircache_size = sizeof(struct dircache_entry);
624 #ifdef HAVE_MULTIVOLUME
625 append_position = dircache_root;
627 for (i = NUM_VOLUMES; i >= 0; i--)
629 if (fat_ismounted(i))
631 #endif
632 cpu_boost(true);
633 #ifdef HAVE_MULTIVOLUME
634 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
635 #else
636 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
637 #endif /* HAVE_MULTIVOLUME */
639 logf("dircache_scan_and_build failed");
640 cpu_boost(false);
641 dircache_size = 0;
642 dircache_initializing = false;
643 return -2;
645 cpu_boost(false);
646 #ifdef HAVE_MULTIVOLUME
649 #endif
651 logf("Done, %ld KiB used", dircache_size / 1024);
653 dircache_initialized = true;
654 dircache_initializing = false;
655 cache_build_ticks = current_tick - start_tick;
657 /* Initialized fd bindings. */
658 memset(fd_bindings, 0, sizeof(fd_bindings));
659 for (i = 0; i < fdbind_idx; i++)
660 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
661 fdbind_idx = 0;
663 if (thread_enabled)
665 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
666 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
668 else
670 /* We have to long align the audiobuf to keep the buffer access fast. */
671 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
672 audiobuf += DIRCACHE_RESERVE;
673 allocated_size = dircache_size + DIRCACHE_RESERVE;
674 reserve_used = 0;
677 return 1;
681 * Internal thread that controls transparent cache building.
683 static void dircache_thread(void)
685 struct queue_event ev;
687 while (1)
689 queue_wait(&dircache_queue, &ev);
691 switch (ev.id)
693 #ifdef HAVE_HOTSWAP
694 case SYS_FS_CHANGED:
695 if (!dircache_initialized)
696 break;
697 dircache_initialized = false;
698 #endif
699 case DIRCACHE_BUILD:
700 thread_enabled = true;
701 dircache_do_rebuild();
702 thread_enabled = false;
703 break ;
705 case DIRCACHE_STOP:
706 logf("Stopped the rebuilding.");
707 dircache_initialized = false;
708 break ;
710 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
711 case SYS_USB_CONNECTED:
712 usb_acknowledge(SYS_USB_CONNECTED_ACK);
713 usb_wait_for_disconnect(&dircache_queue);
714 break ;
715 #endif
721 * Start scanning the disk to build the dircache.
722 * Either transparent or non-transparent build method is used.
724 int dircache_build(int last_size)
726 if (dircache_initialized || thread_enabled)
727 return -3;
729 logf("Building directory cache");
730 #ifdef HAVE_EEPROM_SETTINGS
731 remove_dircache_file();
732 #endif
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;
784 int thread_id;
786 dircache_initialized = false;
787 dircache_initializing = false;
789 memset(opendirs, 0, sizeof(opendirs));
790 for (i = 0; i < MAX_OPEN_DIRS; i++)
792 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
795 queue_init(&dircache_queue, true);
796 thread_id = create_thread(dircache_thread, dircache_stack,
797 sizeof(dircache_stack), 0, dircache_thread_name
798 IF_PRIO(, PRIORITY_BACKGROUND)
799 IF_COP(, CPU));
800 #ifdef HAVE_IO_PRIORITY
801 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
802 #endif
807 * Returns true if dircache has been initialized and is ready to be used.
809 bool dircache_is_enabled(void)
811 return dircache_initialized;
815 * Returns true if dircache is being initialized.
817 bool dircache_is_initializing(void)
819 return dircache_initializing || thread_enabled;
823 * Set application flags used to determine if dircache is still intact.
825 void dircache_set_appflag(long mask)
827 appflags |= mask;
831 * Get application flags used to determine if dircache is still intact.
833 bool dircache_get_appflag(long mask)
835 return dircache_is_enabled() && (appflags & mask);
839 * Returns the current number of entries (directories and files) in the cache.
841 int dircache_get_entry_count(void)
843 return entry_count;
847 * Returns the allocated space for dircache (without reserve space).
849 int dircache_get_cache_size(void)
851 return dircache_is_enabled() ? dircache_size : 0;
855 * Returns how many bytes of the reserve allocation for live cache
856 * updates have been used.
858 int dircache_get_reserve_used(void)
860 return dircache_is_enabled() ? reserve_used : 0;
864 * Returns the time in kernel ticks that took to build the cache.
866 int dircache_get_build_ticks(void)
868 return dircache_is_enabled() ? cache_build_ticks : 0;
872 * Disables the dircache. Usually called on shutdown or when
873 * accepting a usb connection.
875 void dircache_disable(void)
877 int i;
878 bool cache_in_use;
880 if (thread_enabled)
881 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
883 while (thread_enabled)
884 sleep(1);
885 dircache_initialized = false;
887 logf("Waiting for cached dirs to release");
888 do {
889 cache_in_use = false;
890 for (i = 0; i < MAX_OPEN_DIRS; i++) {
891 if (!opendirs[i].regulardir && opendirs[i].busy)
893 cache_in_use = true;
894 sleep(1);
895 break ;
898 } while (cache_in_use) ;
900 logf("Cache released");
901 entry_count = 0;
905 * Usermode function to return dircache_entry pointer to the given path.
907 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
909 if (!dircache_initialized || filename == NULL)
910 return NULL;
912 return dircache_get_entry(filename, false);
916 * Function to copy the full absolute path from dircache to the given buffer
917 * using the given dircache_entry pointer.
919 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
921 int path_size = 0;
922 int idx;
923 const struct dircache_entry *temp = entry;
925 if (size <= 0)
926 return ;
928 /* first compute the necessary size */
929 while(temp != NULL)
931 path_size += temp->name_len; /* '/' + d_name */
932 temp = temp->up;
935 /* now copy the path */
936 /* doesn't matter with trailing 0 because it's added later */
937 idx = path_size;
938 while(entry != NULL)
940 idx -= entry->name_len;
941 /* available size */
942 int rem = size - idx;
944 if(rem >= 1)
946 buf[idx] = '/';
947 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
949 entry = entry->up;
952 /* add trailing 0 */
953 buf[MIN(path_size, size-1)] = 0;
956 /* --- Directory cache live updating functions --- */
957 static int block_until_ready(void)
959 /* Block until dircache has been built. */
960 while (!dircache_initialized && dircache_is_initializing())
961 sleep(1);
963 if (!dircache_initialized)
964 return -1;
966 return 0;
969 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
971 struct dircache_entry *entry;
972 char basedir[MAX_PATH*2];
973 char *new;
974 long last_cache_size = dircache_size;
976 strlcpy(basedir, path, sizeof(basedir));
977 new = strrchr(basedir, '/');
978 if (new == NULL)
980 logf("error occurred");
981 dircache_initialized = false;
982 return NULL;
985 *new = '\0';
986 new++;
988 entry = dircache_get_entry(basedir, true);
989 if (entry == NULL)
991 logf("basedir not found!");
992 logf("%s", basedir);
993 dircache_initialized = false;
994 return NULL;
997 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
998 >= DIRCACHE_RESERVE)
1000 logf("not enough space");
1001 dircache_initialized = false;
1002 return NULL;
1005 while (entry->next != NULL)
1006 entry = entry->next;
1008 if (entry->name_len > 0)
1009 entry = dircache_gen_next(entry);
1011 if (entry == NULL)
1013 dircache_initialized = false;
1014 return NULL;
1017 entry->name_len = MIN(254, strlen(new)) + 1;
1018 entry->d_name = ((char *)dircache_root+dircache_size);
1019 entry->startcluster = 0;
1020 memset(&entry->info, 0, sizeof(entry->info));
1021 entry->info.attribute = attribute;
1022 memcpy(entry->d_name, new, entry->name_len);
1023 dircache_size += entry->name_len;
1025 if (attribute & ATTR_DIRECTORY)
1027 logf("gen_down");
1028 dircache_gen_down(entry);
1031 reserve_used += dircache_size - last_cache_size;
1033 return entry;
1036 void dircache_bind(int fd, const char *path)
1038 struct dircache_entry *entry;
1040 /* Queue requests until dircache has been built. */
1041 if (!dircache_initialized && dircache_is_initializing())
1043 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1044 return ;
1045 strlcpy(fdbind_cache[fdbind_idx].path, path,
1046 sizeof(fdbind_cache[fdbind_idx].path));
1047 fdbind_cache[fdbind_idx].fd = fd;
1048 fdbind_idx++;
1049 return ;
1052 if (!dircache_initialized)
1053 return ;
1055 logf("bind: %d/%s", fd, path);
1056 entry = dircache_get_entry(path, false);
1057 if (entry == NULL)
1059 logf("not found!");
1060 dircache_initialized = false;
1061 return ;
1064 fd_bindings[fd] = entry;
1067 void dircache_update_filesize(int fd, long newsize, long startcluster)
1069 if (!dircache_initialized || fd < 0)
1070 return ;
1072 if (fd_bindings[fd] == NULL)
1074 logf("dircache fd(%d) access error", fd);
1075 dircache_initialized = false;
1076 return ;
1079 fd_bindings[fd]->info.size = newsize;
1080 fd_bindings[fd]->startcluster = startcluster;
1082 void dircache_update_filetime(int fd)
1084 #if CONFIG_RTC == 0
1085 (void)fd;
1086 #else
1087 short year;
1088 struct tm *now = get_time();
1089 if (!dircache_initialized || fd < 0)
1090 return ;
1092 if (fd_bindings[fd] == NULL)
1094 logf("dircache fd access error");
1095 dircache_initialized = false;
1096 return ;
1098 year = now->tm_year+1900-1980;
1099 fd_bindings[fd]->info.wrtdate = (((year)&0x7f)<<9) |
1100 (((now->tm_mon+1)&0xf)<<5) |
1101 (((now->tm_mday)&0x1f));
1102 fd_bindings[fd]->info.wrttime = (((now->tm_hour)&0x1f)<<11) |
1103 (((now->tm_min)&0x3f)<<5) |
1104 (((now->tm_sec/2)&0x1f));
1105 #endif
1108 void dircache_mkdir(const char *path)
1109 { /* Test ok. */
1110 if (block_until_ready())
1111 return ;
1114 logf("mkdir: %s", path);
1115 dircache_new_entry(path, ATTR_DIRECTORY);
1118 void dircache_rmdir(const char *path)
1119 { /* Test ok. */
1120 struct dircache_entry *entry;
1122 if (block_until_ready())
1123 return ;
1125 logf("rmdir: %s", path);
1126 entry = dircache_get_entry(path, false);
1127 if (entry == NULL || entry->down == NULL)
1129 logf("not found or not a directory!");
1130 dircache_initialized = false;
1131 return ;
1134 entry->down = NULL;
1135 entry->name_len = 0;
1138 /* Remove a file from cache */
1139 void dircache_remove(const char *name)
1140 { /* Test ok. */
1141 struct dircache_entry *entry;
1143 if (block_until_ready())
1144 return ;
1146 logf("remove: %s", name);
1148 entry = dircache_get_entry(name, false);
1150 if (entry == NULL)
1152 logf("not found!");
1153 dircache_initialized = false;
1154 return ;
1157 entry->name_len = 0;
1160 void dircache_rename(const char *oldpath, const char *newpath)
1161 { /* Test ok. */
1162 struct dircache_entry *entry, *newentry;
1163 struct dircache_entry oldentry;
1164 char absolute_path[MAX_PATH*2];
1165 char *p;
1167 if (block_until_ready())
1168 return ;
1170 logf("rename: %s->%s", oldpath, newpath);
1172 entry = dircache_get_entry(oldpath, false);
1173 if (entry == NULL)
1175 logf("not found!");
1176 dircache_initialized = false;
1177 return ;
1180 /* Delete the old entry. */
1181 entry->name_len = 0;
1183 /** If we rename the same filename twice in a row, we need to
1184 * save the data, because the entry will be re-used. */
1185 oldentry = *entry;
1187 /* Generate the absolute path for destination if necessary. */
1188 if (newpath[0] != '/')
1190 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1191 p = strrchr(absolute_path, '/');
1192 if (!p)
1194 logf("Invalid path");
1195 dircache_initialized = false;
1196 return ;
1199 *p = '\0';
1200 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1201 newpath = absolute_path;
1204 newentry = dircache_new_entry(newpath, entry->info.attribute);
1205 if (newentry == NULL)
1207 dircache_initialized = false;
1208 return ;
1211 newentry->down = oldentry.down;
1212 newentry->startcluster = oldentry.startcluster;
1213 newentry->info.size = oldentry.info.size;
1214 newentry->info.wrtdate = oldentry.info.wrtdate;
1215 newentry->info.wrttime = oldentry.info.wrttime;
1218 void dircache_add_file(const char *path, long startcluster)
1220 struct dircache_entry *entry;
1222 if (block_until_ready())
1223 return ;
1225 logf("add file: %s", path);
1226 entry = dircache_new_entry(path, 0);
1227 if (entry == NULL)
1228 return ;
1230 entry->startcluster = startcluster;
1233 static bool is_disable_msg_pending(void)
1235 return check_event_queue();
1238 DIR_CACHED* opendir_cached(const char* name)
1240 int dd;
1241 DIR_CACHED* pdir = opendirs;
1243 if ( name[0] != '/' )
1245 DEBUGF("Only absolute paths supported right now\n");
1246 return NULL;
1249 /* find a free dir descriptor */
1250 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1251 if ( !pdir->busy )
1252 break;
1254 if ( dd == MAX_OPEN_DIRS )
1256 DEBUGF("Too many dirs open\n");
1257 errno = EMFILE;
1258 return NULL;
1261 pdir->busy = true;
1263 if (!dircache_initialized || is_disable_msg_pending())
1265 pdir->internal_entry = NULL;
1266 pdir->regulardir = opendir_uncached(name);
1268 else
1270 pdir->regulardir = NULL;
1271 pdir->internal_entry = dircache_get_entry(name, true);
1272 pdir->theent.info.attribute = -1; /* used to make readdir_cached aware of the first call */
1275 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1277 pdir->busy = false;
1278 return NULL;
1281 return pdir;
1284 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1286 struct dircache_entry *ce = dir->internal_entry;
1287 struct dirent_uncached *regentry;
1289 if (!dir->busy)
1290 return NULL;
1292 if (dir->regulardir != NULL)
1294 regentry = readdir_uncached(dir->regulardir);
1295 if (regentry == NULL)
1296 return NULL;
1298 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1299 dir->theent.startcluster = regentry->startcluster;
1300 dir->theent.info = regentry->info;
1302 return &dir->theent;
1305 /* if theent.attribute=-1 then this is the first call */
1306 /* otherwise, this is is not so we first take the entry's ->next */
1307 /* NOTE: normal file can't have attribute=-1 */
1308 if(dir->theent.info.attribute != -1)
1309 ce = ce->next;
1310 /* skip unused entries */
1311 while(ce != NULL && ce->name_len == 0)
1312 ce = ce->next;
1314 if (ce == NULL)
1315 return NULL;
1317 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1318 /* Can't do `dir->theent = *ce`
1319 because that modifies the d_name pointer. */
1320 dir->theent.startcluster = ce->startcluster;
1321 dir->theent.info = ce->info;
1322 dir->internal_entry = ce;
1324 //logf("-> %s", ce->name);
1325 return &dir->theent;
1328 int closedir_cached(DIR_CACHED* dir)
1330 if (!dir->busy)
1331 return -1;
1333 dir->busy=false;
1334 if (dir->regulardir != NULL)
1335 return closedir_uncached(dir->regulardir);
1337 return 0;
1340 int mkdir_cached(const char *name)
1342 int rc=mkdir_uncached(name);
1343 if (rc >= 0)
1344 dircache_mkdir(name);
1345 return(rc);
1348 int rmdir_cached(const char* name)
1350 int rc=rmdir_uncached(name);
1351 if(rc >= 0)
1352 dircache_rmdir(name);
1353 return(rc);