Dircache: Rework and simplify dircache_copy_path().
[kugel-rb.git] / firmware / common / dircache.c
blobdc662bb9cd245d19f6c1631aaafc2da0ba8cf6ab
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->d_name = NULL;
129 next_entry->up = NULL;
130 next_entry->down = NULL;
131 next_entry->next = NULL;
133 dircache_size += sizeof(struct dircache_entry);
135 return next_entry;
139 * Internal function to allocate a dircache_entry and set
140 * ->next entry pointers.
142 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
144 struct dircache_entry *next_entry;
146 if ( (next_entry = allocate_entry()) == NULL)
147 return NULL;
148 next_entry->up = ce->up;
149 ce->next = next_entry;
151 return next_entry;
155 * Internal function to allocate a dircache_entry and set
156 * ->down entry pointers.
158 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
160 struct dircache_entry *next_entry;
162 if ( (next_entry = allocate_entry()) == NULL)
163 return NULL;
164 next_entry->up = ce;
165 ce->down = next_entry;
167 return next_entry;
171 * Returns true if there is an event waiting in the queue
172 * that requires the current operation to be aborted.
174 static bool check_event_queue(void)
176 struct queue_event ev;
178 if(!queue_peek(&dircache_queue, &ev))
179 return false;
181 switch (ev.id)
183 case DIRCACHE_STOP:
184 case SYS_USB_CONNECTED:
185 #ifdef HAVE_HOTSWAP
186 case SYS_FS_CHANGED:
187 #endif
188 return true;
191 return false;
194 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
195 /* scan and build static data (avoid redundancy on stack) */
196 static struct
198 #ifdef HAVE_MULTIVOLUME
199 int volume;
200 #endif
201 struct fat_dir *dir;
202 struct fat_direntry *direntry;
203 }sab;
205 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
207 /* normally, opendir expects a full fat_dir as parent but in our case,
208 * it's completely useless because we don't modify anything
209 * WARNING: this heavily relies on current FAT implementation ! */
211 /* those field are necessary to update the FAT entry in case of modification
212 here we don't touch anything so we put dummy values */
213 sab.dir->entry = 0;
214 sab.dir->entrycount = 0;
215 sab.dir->file.firstcluster = 0;
216 /* open directory */
217 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
218 if(rc < 0)
220 logf("fat_opendir failed: %d", rc);
221 return rc;
224 /* first pass : read dir */
225 struct dircache_entry *first_ce = ce;
227 /* read through directory */
228 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
230 if(!strcmp(".", sab.direntry->name) ||
231 !strcmp("..", sab.direntry->name))
232 continue;
234 ce->d_name = ((char *)dircache_root + dircache_size);
235 ce->startcluster = sab.direntry->firstcluster;
236 ce->info.size = sab.direntry->filesize;
237 ce->info.attribute = sab.direntry->attr;
238 ce->info.wrtdate = sab.direntry->wrtdate;
239 ce->info.wrttime = sab.direntry->wrttime;
241 strcpy(ce->d_name, sab.direntry->name);
242 dircache_size += strlen(ce->d_name) + 1;
244 entry_count++;
246 if(ce->info.attribute & FAT_ATTR_DIRECTORY)
247 dircache_gen_down(ce);
249 ce = dircache_gen_next(ce);
250 if(ce == NULL)
251 return -5;
253 /* When simulator is used, it's only safe to yield here. */
254 if(thread_enabled)
256 /* Stop if we got an external signal. */
257 if(check_event_queue())
258 return -6;
259 yield();
263 /* add "." and ".." */
264 ce->d_name = ".";
265 ce->info.attribute = FAT_ATTR_DIRECTORY;
266 ce->startcluster = startcluster;
267 ce->info.size = 0;
268 ce->down = first_ce;
270 ce = dircache_gen_next(ce);
272 ce->d_name = "..";
273 ce->info.attribute = FAT_ATTR_DIRECTORY;
274 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
275 ce->info.size = 0;
276 ce->down = first_ce->up;
278 /* second pass: recurse ! */
279 ce = first_ce;
281 while(rc >= 0 && ce)
283 if(ce->d_name != NULL && ce->down != NULL && strcmp(ce->d_name, ".")
284 && strcmp(ce->d_name, ".."))
285 rc = sab_process_dir(ce->startcluster, ce->down);
287 ce = ce->next;
290 return rc;
293 /* used during the generation */
294 static struct fat_dir sab_fat_dir;
296 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
298 memset(ce, 0, sizeof(struct dircache_entry));
300 #ifdef HAVE_MULTIVOLUME
301 if (volume > 0)
303 ce->d_name = ((char *)dircache_root+dircache_size);
304 size_t len = snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume)+1;
305 dircache_size += len;
306 ce->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
307 ce->info.size = 0;
308 append_position = dircache_gen_next(ce);
309 ce = dircache_gen_down(ce);
311 #endif
313 struct fat_direntry direntry; /* ditto */
314 #ifdef HAVE_MULTIVOLUME
315 sab.volume = volume;
316 #endif
317 sab.dir = &sab_fat_dir;
318 sab.direntry = &direntry;
320 return sab_process_dir(0, ce);
322 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
323 static char sab_path[MAX_PATH];
325 static int sab_process_dir(struct dircache_entry *ce)
327 struct dirent_uncached *entry;
328 struct dircache_entry *first_ce = ce;
329 DIR_UNCACHED *dir = opendir_uncached(sab_path);
330 if(dir == NULL)
332 logf("Failed to opendir_uncached(%s)", sab_path);
333 return -1;
336 while((entry = readdir_uncached(dir)))
338 if(!strcmp(".", entry->d_name) ||
339 !strcmp("..", entry->d_name))
340 continue;
342 ce->d_name = ((char *)dircache_root + dircache_size);
343 ce->info = entry->info;
345 strcpy(ce->d_name, entry->d_name);
346 dircache_size += strlen(entry->d_name) + 1;
348 entry_count++;
350 if(entry->info.attribute & ATTR_DIRECTORY)
352 dircache_gen_down(ce);
353 if(ce->down == NULL)
355 closedir_uncached(dir);
356 return -1;
358 /* save current paths size */
359 int pathpos = strlen(sab_path);
360 /* append entry */
361 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
362 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
364 int rc = sab_process_dir(ce->down);
365 /* restore path */
366 sab_path[pathpos] = '\0';
368 if(rc < 0)
370 closedir_uncached(dir);
371 return rc;
375 ce = dircache_gen_next(ce);
376 if(ce == NULL)
377 return -5;
379 /* When simulator is used, it's only safe to yield here. */
380 if(thread_enabled)
382 /* Stop if we got an external signal. */
383 if(check_event_queue())
384 return -1;
385 yield();
389 /* add "." and ".." */
390 ce->d_name = ".";
391 ce->info.attribute = ATTR_DIRECTORY;
392 ce->info.size = 0;
393 ce->down = first_ce;
395 ce = dircache_gen_next(ce);
397 ce->d_name = "..";
398 ce->info.attribute = ATTR_DIRECTORY;
399 ce->info.size = 0;
400 ce->down = first_ce->up;
402 closedir_uncached(dir);
403 return 0;
406 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
408 #ifdef HAVE_MULTIVOLUME
409 (void) volume;
410 #endif
411 memset(ce, 0, sizeof(struct dircache_entry));
413 strlcpy(sab_path, "/", sizeof sab_path);
414 return sab_process_dir(ce);
416 #endif /* PLATFORM_NATIVE */
419 * Internal function to get a pointer to dircache_entry for a given filename.
420 * path: Absolute path to a file or directory (see comment)
421 * go_down: Returns the first entry of the directory given by the path (see comment)
423 * As a a special case, accept path="" as an alias for "/".
424 * Also if the path omits the first '/', it will be accepted.
426 * * If get_down=true:
427 * If path="/", the returned entry is the first of root directory (ie dircache_root)
428 * Otherwise, if 'entry' is the returned value when get_down=false,
429 * the functions returns entry->down (which can be NULL)
431 * * If get_down=false:
432 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
433 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
434 * Which means that
435 * dircache_get_entry(path)->d_name == chunk_n
437 * If path="/", the returned entry is NULL.
438 * If the entry doesn't exist, return NULL
440 * NOTE: this functions silently handles double '/'
442 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
444 char namecopy[MAX_PATH];
445 char* part;
446 char* end;
448 bool at_root = true;
449 struct dircache_entry *cache_entry = dircache_root;
451 strlcpy(namecopy, path, sizeof(namecopy));
453 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
455 /* If request another chunk, the current entry has to be directory
456 * and so cache_entry->down has to be non-NULL/
457 * Special case of root because it's already the first entry of the root directory
459 * NOTE: this is safe even if cache_entry->down is NULL */
460 if(!at_root)
461 cache_entry = cache_entry->down;
462 else
463 at_root = false;
465 /* scan dir for name */
466 while(cache_entry != NULL)
468 /* skip unused entries */
469 if(cache_entry->d_name == NULL)
471 cache_entry = cache_entry->next;
472 continue;
474 /* compare names */
475 if(!strcasecmp(part, cache_entry->d_name))
476 break;
477 /* go to next entry */
478 cache_entry = cache_entry->next;
481 /* handle not found case */
482 if(cache_entry == NULL)
483 return NULL;
486 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
487 if(go_down)
488 return at_root ? cache_entry : cache_entry->down;
489 else
490 return at_root ? NULL : cache_entry;
493 #ifdef HAVE_EEPROM_SETTINGS
495 * Function to load the internal cache structure from disk to initialize
496 * the dircache really fast and little disk access.
498 int dircache_load(void)
500 struct dircache_maindata maindata;
501 int bytes_read;
502 int fd;
504 if (dircache_initialized)
505 return -1;
507 logf("Loading directory cache");
508 dircache_size = 0;
510 fd = open_dircache_file(O_RDONLY, 0);
511 if (fd < 0)
512 return -2;
514 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
515 if (bytes_read != sizeof(struct dircache_maindata)
516 || maindata.size <= 0)
518 logf("Dircache file header error");
519 close(fd);
520 remove_dircache_file();
521 return -3;
524 dircache_root = buffer_alloc(0);
525 if ((long)maindata.root_entry != (long)dircache_root)
527 logf("Position missmatch");
528 close(fd);
529 remove_dircache_file();
530 return -4;
533 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
534 entry_count = maindata.entry_count;
535 appflags = maindata.appflags;
536 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
537 close(fd);
538 remove_dircache_file();
540 if (bytes_read != maindata.size)
542 logf("Dircache read failed");
543 return -6;
546 /* Cache successfully loaded. */
547 dircache_size = maindata.size;
548 allocated_size = dircache_size + DIRCACHE_RESERVE;
549 reserve_used = 0;
550 logf("Done, %ld KiB used", dircache_size / 1024);
551 dircache_initialized = true;
552 memset(fd_bindings, 0, sizeof(fd_bindings));
554 return 0;
558 * Function to save the internal cache stucture to disk for fast loading
559 * on boot.
561 int dircache_save(void)
563 struct dircache_maindata maindata;
564 int fd;
565 unsigned long bytes_written;
567 remove_dircache_file();
569 if (!dircache_initialized)
570 return -1;
572 logf("Saving directory cache");
573 fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
575 maindata.magic = DIRCACHE_MAGIC;
576 maindata.size = dircache_size;
577 maindata.root_entry = dircache_root;
578 maindata.entry_count = entry_count;
579 maindata.appflags = appflags;
581 /* Save the info structure */
582 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
583 if (bytes_written != sizeof(struct dircache_maindata))
585 close(fd);
586 logf("dircache: write failed #1");
587 return -2;
590 /* Dump whole directory cache to disk */
591 bytes_written = write(fd, dircache_root, dircache_size);
592 close(fd);
593 if (bytes_written != dircache_size)
595 logf("dircache: write failed #2");
596 return -3;
599 return 0;
601 #endif /* HAVE_EEPROM_SETTINGS */
604 * Internal function which scans the disk and creates the dircache structure.
606 static int dircache_do_rebuild(void)
608 unsigned int start_tick;
609 int i;
611 /* Measure how long it takes build the cache. */
612 start_tick = current_tick;
613 dircache_initializing = true;
614 appflags = 0;
615 entry_count = 0;
617 dircache_size = sizeof(struct dircache_entry);
619 #ifdef HAVE_MULTIVOLUME
620 append_position = dircache_root;
622 for (i = NUM_VOLUMES; i >= 0; i--)
624 if (fat_ismounted(i))
626 #endif
627 cpu_boost(true);
628 #ifdef HAVE_MULTIVOLUME
629 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
630 #else
631 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
632 #endif /* HAVE_MULTIVOLUME */
634 logf("dircache_scan_and_build failed");
635 cpu_boost(false);
636 dircache_size = 0;
637 dircache_initializing = false;
638 return -2;
640 cpu_boost(false);
641 #ifdef HAVE_MULTIVOLUME
644 #endif
646 logf("Done, %ld KiB used", dircache_size / 1024);
648 dircache_initialized = true;
649 dircache_initializing = false;
650 cache_build_ticks = current_tick - start_tick;
652 /* Initialized fd bindings. */
653 memset(fd_bindings, 0, sizeof(fd_bindings));
654 for (i = 0; i < fdbind_idx; i++)
655 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
656 fdbind_idx = 0;
658 if (thread_enabled)
660 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
661 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
663 else
665 /* We have to long align the audiobuf to keep the buffer access fast. */
666 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
667 audiobuf += DIRCACHE_RESERVE;
668 allocated_size = dircache_size + DIRCACHE_RESERVE;
669 reserve_used = 0;
672 return 1;
676 * Internal thread that controls transparent cache building.
678 static void dircache_thread(void)
680 struct queue_event ev;
682 while (1)
684 queue_wait(&dircache_queue, &ev);
686 switch (ev.id)
688 #ifdef HAVE_HOTSWAP
689 case SYS_FS_CHANGED:
690 if (!dircache_initialized)
691 break;
692 dircache_initialized = false;
693 #endif
694 case DIRCACHE_BUILD:
695 thread_enabled = true;
696 dircache_do_rebuild();
697 thread_enabled = false;
698 break ;
700 case DIRCACHE_STOP:
701 logf("Stopped the rebuilding.");
702 dircache_initialized = false;
703 break ;
705 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
706 case SYS_USB_CONNECTED:
707 usb_acknowledge(SYS_USB_CONNECTED_ACK);
708 usb_wait_for_disconnect(&dircache_queue);
709 break ;
710 #endif
716 * Start scanning the disk to build the dircache.
717 * Either transparent or non-transparent build method is used.
719 int dircache_build(int last_size)
721 if (dircache_initialized || thread_enabled)
722 return -3;
724 logf("Building directory cache");
725 #ifdef HAVE_EEPROM_SETTINGS
726 remove_dircache_file();
727 #endif
729 /* Background build, dircache has been previously allocated */
730 if (dircache_size > 0)
732 thread_enabled = true;
733 dircache_initializing = true;
734 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
735 return 2;
738 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
740 allocated_size = last_size + DIRCACHE_RESERVE;
741 dircache_root = buffer_alloc(allocated_size);
742 thread_enabled = true;
744 /* Start a transparent rebuild. */
745 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
746 return 3;
749 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
751 /* Start a non-transparent rebuild. */
752 return dircache_do_rebuild();
756 * Steal the allocated dircache buffer and disable dircache.
758 void* dircache_steal_buffer(long *size)
760 dircache_disable();
761 if (dircache_size == 0)
763 *size = 0;
764 return NULL;
767 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
769 return dircache_root;
773 * Main initialization function that must be called before any other
774 * operations within the dircache.
776 void dircache_init(void)
778 int i;
779 int thread_id __attribute__((unused));
781 dircache_initialized = false;
782 dircache_initializing = false;
784 memset(opendirs, 0, sizeof(opendirs));
785 for (i = 0; i < MAX_OPEN_DIRS; i++)
787 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
790 queue_init(&dircache_queue, true);
791 thread_id = create_thread(dircache_thread, dircache_stack,
792 sizeof(dircache_stack), 0, dircache_thread_name
793 IF_PRIO(, PRIORITY_BACKGROUND)
794 IF_COP(, CPU));
795 #ifdef HAVE_IO_PRIORITY
796 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
797 #endif
802 * Returns true if dircache has been initialized and is ready to be used.
804 bool dircache_is_enabled(void)
806 return dircache_initialized;
810 * Returns true if dircache is being initialized.
812 bool dircache_is_initializing(void)
814 return dircache_initializing || thread_enabled;
818 * Set application flags used to determine if dircache is still intact.
820 void dircache_set_appflag(long mask)
822 appflags |= mask;
826 * Get application flags used to determine if dircache is still intact.
828 bool dircache_get_appflag(long mask)
830 return dircache_is_enabled() && (appflags & mask);
834 * Returns the current number of entries (directories and files) in the cache.
836 int dircache_get_entry_count(void)
838 return entry_count;
842 * Returns the allocated space for dircache (without reserve space).
844 int dircache_get_cache_size(void)
846 return dircache_is_enabled() ? dircache_size : 0;
850 * Returns how many bytes of the reserve allocation for live cache
851 * updates have been used.
853 int dircache_get_reserve_used(void)
855 return dircache_is_enabled() ? reserve_used : 0;
859 * Returns the time in kernel ticks that took to build the cache.
861 int dircache_get_build_ticks(void)
863 return dircache_is_enabled() ? cache_build_ticks : 0;
867 * Disables the dircache. Usually called on shutdown or when
868 * accepting a usb connection.
870 void dircache_disable(void)
872 int i;
873 bool cache_in_use;
875 if (thread_enabled)
876 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
878 while (thread_enabled)
879 sleep(1);
880 dircache_initialized = false;
882 logf("Waiting for cached dirs to release");
883 do {
884 cache_in_use = false;
885 for (i = 0; i < MAX_OPEN_DIRS; i++) {
886 if (!opendirs[i].regulardir && opendirs[i].busy)
888 cache_in_use = true;
889 sleep(1);
890 break ;
893 } while (cache_in_use) ;
895 logf("Cache released");
896 entry_count = 0;
900 * Usermode function to return dircache_entry pointer to the given path.
902 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
904 if (!dircache_initialized || filename == NULL)
905 return NULL;
907 return dircache_get_entry(filename, false);
911 * build a path from an entry upto the root using recursion
913 * it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
914 * and it will leave a trailing '/'
916 * returns the position of that trailing '/' so it can be deleted afterwards
917 * (or, in case of truncation, the position of the nul byte */
918 static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size)
920 /* has parent? */
921 if (entry->up)
922 copy_path_helper(entry->up, buf, size);
924 size_t len = strlcat(buf, entry->d_name, size);
925 if (len < size)
927 buf[len++] = '/';
928 buf[len] = '\0';
930 return len-1;
933 * Function to copy the full absolute path from dircache to the given buffer
934 * using the given dircache_entry pointer.
936 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
938 if (size <= 0 || !buf)
939 return ;
941 buf[0] = '/';
942 size_t res = copy_path_helper(entry, buf, size);
943 /* fixup trailing '/' */
944 buf[res] = '\0';
947 /* --- Directory cache live updating functions --- */
948 static int block_until_ready(void)
950 /* Block until dircache has been built. */
951 while (!dircache_initialized && dircache_is_initializing())
952 sleep(1);
954 if (!dircache_initialized)
955 return -1;
957 return 0;
960 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
962 struct dircache_entry *entry;
963 char basedir[MAX_PATH*2];
964 char *new;
965 long last_cache_size = dircache_size;
967 strlcpy(basedir, path, sizeof(basedir));
968 new = strrchr(basedir, '/');
969 if (new == NULL)
971 logf("error occurred");
972 dircache_initialized = false;
973 return NULL;
976 *new = '\0';
977 new++;
979 entry = dircache_get_entry(basedir, true);
980 if (entry == NULL)
982 logf("basedir not found!");
983 logf("%s", basedir);
984 dircache_initialized = false;
985 return NULL;
988 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
989 >= DIRCACHE_RESERVE)
991 logf("not enough space");
992 dircache_initialized = false;
993 return NULL;
996 while (entry->next != NULL)
997 entry = entry->next;
999 if (entry->d_name != NULL)
1001 entry = dircache_gen_next(entry);
1002 if (entry == NULL)
1004 dircache_initialized = false;
1005 return NULL;
1009 entry->d_name = ((char *)dircache_root+dircache_size);
1010 entry->startcluster = 0;
1011 memset(&entry->info, 0, sizeof(entry->info));
1012 entry->info.attribute = attribute;
1014 strcpy(entry->d_name, new);
1015 dircache_size += strlen(entry->d_name);
1017 if (attribute & ATTR_DIRECTORY)
1019 logf("gen_down");
1020 dircache_gen_down(entry);
1023 reserve_used += dircache_size - last_cache_size;
1025 return entry;
1028 void dircache_bind(int fd, const char *path)
1030 struct dircache_entry *entry;
1032 /* Queue requests until dircache has been built. */
1033 if (!dircache_initialized && dircache_is_initializing())
1035 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1036 return ;
1037 strlcpy(fdbind_cache[fdbind_idx].path, path,
1038 sizeof(fdbind_cache[fdbind_idx].path));
1039 fdbind_cache[fdbind_idx].fd = fd;
1040 fdbind_idx++;
1041 return ;
1044 if (!dircache_initialized)
1045 return ;
1047 logf("bind: %d/%s", fd, path);
1048 entry = dircache_get_entry(path, false);
1049 if (entry == NULL)
1051 logf("not found!");
1052 dircache_initialized = false;
1053 return ;
1056 fd_bindings[fd] = entry;
1059 void dircache_update_filesize(int fd, long newsize, long startcluster)
1061 if (!dircache_initialized || fd < 0)
1062 return ;
1064 if (fd_bindings[fd] == NULL)
1066 logf("dircache fd(%d) access error", fd);
1067 dircache_initialized = false;
1068 return ;
1071 fd_bindings[fd]->info.size = newsize;
1072 fd_bindings[fd]->startcluster = startcluster;
1074 void dircache_update_filetime(int fd)
1076 #if CONFIG_RTC == 0
1077 (void)fd;
1078 #else
1079 short year;
1080 struct tm *now = get_time();
1081 if (!dircache_initialized || fd < 0)
1082 return ;
1084 if (fd_bindings[fd] == NULL)
1086 logf("dircache fd access error");
1087 dircache_initialized = false;
1088 return ;
1090 year = now->tm_year+1900-1980;
1091 fd_bindings[fd]->info.wrtdate = (((year)&0x7f)<<9) |
1092 (((now->tm_mon+1)&0xf)<<5) |
1093 (((now->tm_mday)&0x1f));
1094 fd_bindings[fd]->info.wrttime = (((now->tm_hour)&0x1f)<<11) |
1095 (((now->tm_min)&0x3f)<<5) |
1096 (((now->tm_sec/2)&0x1f));
1097 #endif
1100 void dircache_mkdir(const char *path)
1101 { /* Test ok. */
1102 if (block_until_ready())
1103 return ;
1106 logf("mkdir: %s", path);
1107 dircache_new_entry(path, ATTR_DIRECTORY);
1110 void dircache_rmdir(const char *path)
1111 { /* Test ok. */
1112 struct dircache_entry *entry;
1114 if (block_until_ready())
1115 return ;
1117 logf("rmdir: %s", path);
1118 entry = dircache_get_entry(path, false);
1119 if (entry == NULL || entry->down == NULL)
1121 logf("not found or not a directory!");
1122 dircache_initialized = false;
1123 return ;
1126 entry->down = NULL;
1127 entry->d_name = NULL;
1130 /* Remove a file from cache */
1131 void dircache_remove(const char *name)
1132 { /* Test ok. */
1133 struct dircache_entry *entry;
1135 if (block_until_ready())
1136 return ;
1138 logf("remove: %s", name);
1140 entry = dircache_get_entry(name, false);
1142 if (entry == NULL)
1144 logf("not found!");
1145 dircache_initialized = false;
1146 return ;
1149 entry->d_name = NULL;
1152 void dircache_rename(const char *oldpath, const char *newpath)
1153 { /* Test ok. */
1154 struct dircache_entry *entry, *newentry;
1155 struct dircache_entry oldentry;
1156 char absolute_path[MAX_PATH*2];
1157 char *p;
1159 if (block_until_ready())
1160 return ;
1162 logf("rename: %s->%s", oldpath, newpath);
1164 entry = dircache_get_entry(oldpath, false);
1165 if (entry == NULL)
1167 logf("not found!");
1168 dircache_initialized = false;
1169 return ;
1172 /* Delete the old entry. */
1173 entry->d_name = NULL;
1175 /** If we rename the same filename twice in a row, we need to
1176 * save the data, because the entry will be re-used. */
1177 oldentry = *entry;
1179 /* Generate the absolute path for destination if necessary. */
1180 if (newpath[0] != '/')
1182 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1183 p = strrchr(absolute_path, '/');
1184 if (!p)
1186 logf("Invalid path");
1187 dircache_initialized = false;
1188 return ;
1191 *p = '\0';
1192 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1193 newpath = absolute_path;
1196 newentry = dircache_new_entry(newpath, entry->info.attribute);
1197 if (newentry == NULL)
1199 dircache_initialized = false;
1200 return ;
1203 newentry->down = oldentry.down;
1204 newentry->startcluster = oldentry.startcluster;
1205 newentry->info.size = oldentry.info.size;
1206 newentry->info.wrtdate = oldentry.info.wrtdate;
1207 newentry->info.wrttime = oldentry.info.wrttime;
1210 void dircache_add_file(const char *path, long startcluster)
1212 struct dircache_entry *entry;
1214 if (block_until_ready())
1215 return ;
1217 logf("add file: %s", path);
1218 entry = dircache_new_entry(path, 0);
1219 if (entry == NULL)
1220 return ;
1222 entry->startcluster = startcluster;
1225 static bool is_disable_msg_pending(void)
1227 return check_event_queue();
1230 DIR_CACHED* opendir_cached(const char* name)
1232 int dd;
1233 DIR_CACHED* pdir = opendirs;
1235 if ( name[0] != '/' )
1237 DEBUGF("Only absolute paths supported right now\n");
1238 return NULL;
1241 /* find a free dir descriptor */
1242 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1243 if ( !pdir->busy )
1244 break;
1246 if ( dd == MAX_OPEN_DIRS )
1248 DEBUGF("Too many dirs open\n");
1249 errno = EMFILE;
1250 return NULL;
1253 pdir->busy = true;
1255 if (!dircache_initialized || is_disable_msg_pending())
1257 pdir->internal_entry = NULL;
1258 pdir->regulardir = opendir_uncached(name);
1260 else
1262 pdir->regulardir = NULL;
1263 pdir->internal_entry = dircache_get_entry(name, true);
1264 pdir->theent.info.attribute = -1; /* used to make readdir_cached aware of the first call */
1267 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1269 pdir->busy = false;
1270 return NULL;
1273 return pdir;
1276 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1278 struct dircache_entry *ce = dir->internal_entry;
1279 struct dirent_uncached *regentry;
1281 if (!dir->busy)
1282 return NULL;
1284 if (dir->regulardir != NULL)
1286 regentry = readdir_uncached(dir->regulardir);
1287 if (regentry == NULL)
1288 return NULL;
1290 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1291 dir->theent.startcluster = regentry->startcluster;
1292 dir->theent.info = regentry->info;
1294 return &dir->theent;
1297 /* if theent.attribute=-1 then this is the first call */
1298 /* otherwise, this is is not so we first take the entry's ->next */
1299 /* NOTE: normal file can't have attribute=-1 */
1300 if(dir->theent.info.attribute != -1)
1301 ce = ce->next;
1302 /* skip unused entries */
1303 while(ce != NULL && ce->d_name == NULL)
1304 ce = ce->next;
1306 if (ce == NULL)
1307 return NULL;
1309 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1310 /* Can't do `dir->theent = *ce`
1311 because that modifies the d_name pointer. */
1312 dir->theent.startcluster = ce->startcluster;
1313 dir->theent.info = ce->info;
1314 dir->internal_entry = ce;
1316 //logf("-> %s", ce->d_name);
1317 return &dir->theent;
1320 int closedir_cached(DIR_CACHED* dir)
1322 if (!dir->busy)
1323 return -1;
1325 dir->busy=false;
1326 if (dir->regulardir != NULL)
1327 return closedir_uncached(dir->regulardir);
1329 return 0;
1332 int mkdir_cached(const char *name)
1334 int rc=mkdir_uncached(name);
1335 if (rc >= 0)
1336 dircache_mkdir(name);
1337 return(rc);
1340 int rmdir_cached(const char* name)
1342 int rc=rmdir_uncached(name);
1343 if(rc >= 0)
1344 dircache_rmdir(name);
1345 return(rc);