Rockbox as an application: add get_user_file_path().
[maemo-rb.git] / firmware / common / dircache.c
blob7b2cdd1d75f0b33d63a33565071ae43e39f2081d
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
49 /* Queue commands. */
50 #define DIRCACHE_BUILD 1
51 #define DIRCACHE_STOP 2
53 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
54 #define MAX_OPEN_DIRS 12
55 #else
56 #define MAX_OPEN_DIRS 8
57 #endif
58 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
60 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
61 static struct dircache_entry *dircache_root;
62 #ifdef HAVE_MULTIVOLUME
63 static struct dircache_entry *append_position;
64 #endif
66 static bool dircache_initialized = false;
67 static bool dircache_initializing = false;
68 static bool thread_enabled = false;
69 static unsigned long allocated_size = DIRCACHE_LIMIT;
70 static unsigned long dircache_size = 0;
71 static unsigned long entry_count = 0;
72 static unsigned long reserve_used = 0;
73 static unsigned int cache_build_ticks = 0;
74 static unsigned long appflags = 0;
76 static struct event_queue dircache_queue;
77 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x400)/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 #ifdef HAVE_EEPROM_SETTINGS
86 /**
87 * Open the dircache file to save a snapshot on disk
89 static int open_dircache_file(unsigned flags, int permissions)
91 char path[MAX_PATH];
92 const char *file = get_user_file_path(DIRCACHE_FILE, IS_FILE|NEED_WRITE,
93 path, sizeof(path));
94 if (permissions != 0)
95 return open(file, flags, permissions);
97 return open(file, flags);
101 * Remove the snapshot file
103 static int remove_dircache_file(void)
105 char path[MAX_PATH];
106 return remove(get_user_file_path(DIRCACHE_FILE, IS_FILE|NEED_WRITE,
107 path, sizeof(path)));
109 #endif
110 /**
111 * Internal function to allocate a new dircache_entry from memory.
113 static struct dircache_entry* allocate_entry(void)
115 struct dircache_entry *next_entry;
117 if (dircache_size > allocated_size - MAX_PATH*2)
119 logf("size limit reached");
120 return NULL;
123 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
124 #ifdef ROCKBOX_STRICT_ALIGN
125 /* Make sure the entry is long aligned. */
126 if ((long)next_entry & 0x03)
128 dircache_size += 4 - ((long)next_entry & 0x03);
129 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
131 #endif
132 next_entry->name_len = 0;
133 next_entry->d_name = NULL;
134 next_entry->up = NULL;
135 next_entry->down = NULL;
136 next_entry->next = NULL;
138 dircache_size += sizeof(struct dircache_entry);
140 return next_entry;
144 * Internal function to allocate a dircache_entry and set
145 * ->next entry pointers.
147 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
149 struct dircache_entry *next_entry;
151 if ( (next_entry = allocate_entry()) == NULL)
152 return NULL;
153 next_entry->up = ce->up;
154 ce->next = next_entry;
156 return next_entry;
160 * Internal function to allocate a dircache_entry and set
161 * ->down entry pointers.
163 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
165 struct dircache_entry *next_entry;
167 if ( (next_entry = allocate_entry()) == NULL)
168 return NULL;
169 next_entry->up = ce;
170 ce->down = next_entry;
172 return next_entry;
176 * Returns true if there is an event waiting in the queue
177 * that requires the current operation to be aborted.
179 static bool check_event_queue(void)
181 struct queue_event ev;
183 if(!queue_peek(&dircache_queue, &ev))
184 return false;
186 switch (ev.id)
188 case DIRCACHE_STOP:
189 case SYS_USB_CONNECTED:
190 #ifdef HAVE_HOTSWAP
191 case SYS_FS_CHANGED:
192 #endif
193 return true;
196 return false;
199 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
200 /* scan and build static data (avoid redundancy on stack) */
201 static struct
203 #ifdef HAVE_MULTIVOLUME
204 int volume;
205 #endif
206 struct fat_dir *dir;
207 struct fat_direntry *direntry;
208 }sab;
210 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
212 /* normally, opendir expects a full fat_dir as parent but in our case,
213 * it's completely useless because we don't modify anything
214 * WARNING: this heavily relies on current FAT implementation ! */
216 /* those field are necessary to update the FAT entry in case of modification
217 here we don't touch anything so we put dummy values */
218 sab.dir->entry = 0;
219 sab.dir->entrycount = 0;
220 sab.dir->file.firstcluster = 0;
221 /* open directory */
222 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
223 if(rc < 0)
225 logf("fat_opendir failed: %d", rc);
226 return rc;
229 /* first pass : read dir */
230 struct dircache_entry *first_ce = ce;
232 /* read through directory */
233 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
235 if(!strcmp(".", sab.direntry->name) ||
236 !strcmp("..", sab.direntry->name))
237 continue;
239 ce->attribute = sab.direntry->attr;
240 ce->name_len = strlen(sab.direntry->name) + 1;
241 ce->d_name = ((char *)dircache_root + dircache_size);
242 ce->startcluster = sab.direntry->firstcluster;
243 ce->size = sab.direntry->filesize;
244 ce->wrtdate = sab.direntry->wrtdate;
245 ce->wrttime = sab.direntry->wrttime;
246 memcpy(ce->d_name, sab.direntry->name, ce->name_len);
248 dircache_size += ce->name_len;
249 entry_count++;
251 if(ce->attribute & FAT_ATTR_DIRECTORY)
252 dircache_gen_down(ce);
254 ce = dircache_gen_next(ce);
255 if(ce == NULL)
256 return -5;
258 /* When simulator is used, it's only safe to yield here. */
259 if(thread_enabled)
261 /* Stop if we got an external signal. */
262 if(check_event_queue())
263 return -6;
264 yield();
268 /* add "." and ".." */
269 ce->d_name = ".";
270 ce->name_len = 2;
271 ce->attribute = FAT_ATTR_DIRECTORY;
272 ce->startcluster = startcluster;
273 ce->size = 0;
274 ce->down = first_ce;
276 ce = dircache_gen_next(ce);
278 ce->d_name = "..";
279 ce->name_len = 3;
280 ce->attribute = FAT_ATTR_DIRECTORY;
281 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
282 ce->size = 0;
283 ce->down = first_ce->up;
285 /* second pass: recurse ! */
286 ce = first_ce;
288 while(rc >= 0 && ce)
290 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
291 rc = sab_process_dir(ce->startcluster, ce->down);
293 ce = ce->next;
296 return rc;
299 /* used during the generation */
300 static struct fat_dir sab_fat_dir;
302 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
304 memset(ce, 0, sizeof(struct dircache_entry));
306 #ifdef HAVE_MULTIVOLUME
307 if (volume > 0)
309 ce->d_name = ((char *)dircache_root+dircache_size);
310 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
311 ce->name_len = VOL_ENUM_POS + 3;
312 dircache_size += ce->name_len;
313 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
314 ce->size = 0;
315 append_position = dircache_gen_next(ce);
316 ce = dircache_gen_down(ce);
318 #endif
320 struct fat_direntry direntry; /* ditto */
321 #ifdef HAVE_MULTIVOLUME
322 sab.volume = volume;
323 #endif
324 sab.dir = &sab_fat_dir;
325 sab.direntry = &direntry;
327 return sab_process_dir(0, ce);
329 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
330 static char sab_path[MAX_PATH];
332 static int sab_process_dir(struct dircache_entry *ce)
334 struct dirent_uncached *entry;
335 struct dircache_entry *first_ce = ce;
336 DIR_UNCACHED *dir = opendir_uncached(sab_path);
337 if(dir == NULL)
339 logf("Failed to opendir_uncached(%s)", sab_path);
340 return -1;
343 while((entry = readdir_uncached(dir)))
345 if(!strcmp(".", entry->d_name) ||
346 !strcmp("..", entry->d_name))
347 continue;
349 ce->attribute = entry->attribute;
350 ce->name_len = strlen(entry->d_name) + 1;
351 ce->d_name = ((char *)dircache_root + dircache_size);
352 ce->size = entry->size;
353 ce->wrtdate = entry->wrtdate;
354 ce->wrttime = entry->wrttime;
355 memcpy(ce->d_name, entry->d_name, ce->name_len);
357 dircache_size += ce->name_len;
358 entry_count++;
360 if(entry->attribute & ATTR_DIRECTORY)
362 dircache_gen_down(ce);
363 if(ce->down == NULL)
365 closedir_uncached(dir);
366 return -1;
368 /* save current paths size */
369 int pathpos = strlen(sab_path);
370 /* append entry */
371 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
372 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
374 int rc = sab_process_dir(ce->down);
375 /* restore path */
376 sab_path[pathpos] = '\0';
378 if(rc < 0)
380 closedir_uncached(dir);
381 return rc;
385 ce = dircache_gen_next(ce);
386 if(ce == NULL)
387 return -5;
389 /* When simulator is used, it's only safe to yield here. */
390 if(thread_enabled)
392 /* Stop if we got an external signal. */
393 if(check_event_queue())
394 return -1;
395 yield();
399 /* add "." and ".." */
400 ce->d_name = ".";
401 ce->name_len = 2;
402 ce->attribute = ATTR_DIRECTORY;
403 ce->size = 0;
404 ce->down = first_ce;
406 ce = dircache_gen_next(ce);
408 ce->d_name = "..";
409 ce->name_len = 3;
410 ce->attribute = ATTR_DIRECTORY;
411 ce->size = 0;
412 ce->down = first_ce->up;
414 closedir_uncached(dir);
415 return 0;
418 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
420 #ifdef HAVE_MULTIVOLUME
421 (void) volume;
422 #endif
423 memset(ce, 0, sizeof(struct dircache_entry));
425 strlcpy(sab_path, "/", sizeof sab_path);
426 return sab_process_dir(ce);
428 #endif /* PLATFORM_NATIVE */
431 * Internal function to get a pointer to dircache_entry for a given filename.
432 * path: Absolute path to a file or directory (see comment)
433 * go_down: Returns the first entry of the directory given by the path (see comment)
435 * As a a special case, accept path="" as an alias for "/".
436 * Also if the path omits the first '/', it will be accepted.
438 * * If get_down=true:
439 * If path="/", the returned entry is the first of root directory (ie dircache_root)
440 * Otherwise, if 'entry' is the returned value when get_down=false,
441 * the functions returns entry->down (which can be NULL)
443 * * If get_down=false:
444 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
445 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
446 * Which means that
447 * dircache_get_entry(path)->d_name == chunk_n
449 * If path="/", the returned entry is NULL.
450 * If the entry doesn't exist, return NULL
452 * NOTE: this functions silently handles double '/'
454 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
456 char namecopy[MAX_PATH];
457 char* part;
458 char* end;
460 bool at_root = true;
461 struct dircache_entry *cache_entry = dircache_root;
463 strlcpy(namecopy, path, sizeof(namecopy));
465 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
467 /* If request another chunk, the current entry has to be directory
468 * and so cache_entry->down has to be non-NULL/
469 * Special case of root because it's already the first entry of the root directory
471 * NOTE: this is safe even if cache_entry->down is NULL */
472 if(!at_root)
473 cache_entry = cache_entry->down;
474 else
475 at_root = false;
477 /* scan dir for name */
478 while(cache_entry != NULL)
480 /* skip unused entries */
481 if(cache_entry->name_len == 0)
483 cache_entry = cache_entry->next;
484 continue;
486 /* compare names */
487 if(!strcasecmp(part, cache_entry->d_name))
488 break;
489 /* go to next entry */
490 cache_entry = cache_entry->next;
493 /* handle not found case */
494 if(cache_entry == NULL)
495 return NULL;
498 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
499 if(go_down)
500 return at_root ? cache_entry : cache_entry->down;
501 else
502 return at_root ? NULL : cache_entry;
505 #ifdef HAVE_EEPROM_SETTINGS
507 * Function to load the internal cache structure from disk to initialize
508 * the dircache really fast and little disk access.
510 int dircache_load(void)
512 struct dircache_maindata maindata;
513 int bytes_read;
514 int fd;
516 if (dircache_initialized)
517 return -1;
519 logf("Loading directory cache");
520 dircache_size = 0;
522 fd = open_dircache_file(O_RDONLY, 0);
523 if (fd < 0)
524 return -2;
526 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
527 if (bytes_read != sizeof(struct dircache_maindata)
528 || maindata.size <= 0)
530 logf("Dircache file header error");
531 close(fd);
532 remove_dircache_file();
533 return -3;
536 dircache_root = buffer_alloc(0);
537 if ((long)maindata.root_entry != (long)dircache_root)
539 logf("Position missmatch");
540 close(fd);
541 remove_dircache_file();
542 return -4;
545 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
546 entry_count = maindata.entry_count;
547 appflags = maindata.appflags;
548 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
549 close(fd);
550 remove_dircache_file();
552 if (bytes_read != maindata.size)
554 logf("Dircache read failed");
555 return -6;
558 /* Cache successfully loaded. */
559 dircache_size = maindata.size;
560 allocated_size = dircache_size + DIRCACHE_RESERVE;
561 reserve_used = 0;
562 logf("Done, %ld KiB used", dircache_size / 1024);
563 dircache_initialized = true;
564 memset(fd_bindings, 0, sizeof(fd_bindings));
566 return 0;
570 * Function to save the internal cache stucture to disk for fast loading
571 * on boot.
573 int dircache_save(void)
575 struct dircache_maindata maindata;
576 int fd;
577 unsigned long bytes_written;
579 remove_dircache_file();
581 if (!dircache_initialized)
582 return -1;
584 logf("Saving directory cache");
585 fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
587 maindata.magic = DIRCACHE_MAGIC;
588 maindata.size = dircache_size;
589 maindata.root_entry = dircache_root;
590 maindata.entry_count = entry_count;
591 maindata.appflags = appflags;
593 /* Save the info structure */
594 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
595 if (bytes_written != sizeof(struct dircache_maindata))
597 close(fd);
598 logf("dircache: write failed #1");
599 return -2;
602 /* Dump whole directory cache to disk */
603 bytes_written = write(fd, dircache_root, dircache_size);
604 close(fd);
605 if (bytes_written != dircache_size)
607 logf("dircache: write failed #2");
608 return -3;
611 return 0;
613 #endif /* HAVE_EEPROM_SETTINGS */
616 * Internal function which scans the disk and creates the dircache structure.
618 static int dircache_do_rebuild(void)
620 unsigned int start_tick;
621 int i;
623 /* Measure how long it takes build the cache. */
624 start_tick = current_tick;
625 dircache_initializing = true;
626 appflags = 0;
627 entry_count = 0;
629 dircache_size = sizeof(struct dircache_entry);
631 #ifdef HAVE_MULTIVOLUME
632 append_position = dircache_root;
634 for (i = NUM_VOLUMES; i >= 0; i--)
636 if (fat_ismounted(i))
638 #endif
639 cpu_boost(true);
640 #ifdef HAVE_MULTIVOLUME
641 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
642 #else
643 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
644 #endif /* HAVE_MULTIVOLUME */
646 logf("dircache_scan_and_build failed");
647 cpu_boost(false);
648 dircache_size = 0;
649 dircache_initializing = false;
650 return -2;
652 cpu_boost(false);
653 #ifdef HAVE_MULTIVOLUME
656 #endif
658 logf("Done, %ld KiB used", dircache_size / 1024);
660 dircache_initialized = true;
661 dircache_initializing = false;
662 cache_build_ticks = current_tick - start_tick;
664 /* Initialized fd bindings. */
665 memset(fd_bindings, 0, sizeof(fd_bindings));
666 for (i = 0; i < fdbind_idx; i++)
667 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
668 fdbind_idx = 0;
670 if (thread_enabled)
672 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
673 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
675 else
677 /* We have to long align the audiobuf to keep the buffer access fast. */
678 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
679 audiobuf += DIRCACHE_RESERVE;
680 allocated_size = dircache_size + DIRCACHE_RESERVE;
681 reserve_used = 0;
684 return 1;
688 * Internal thread that controls transparent cache building.
690 static void dircache_thread(void)
692 struct queue_event ev;
694 while (1)
696 queue_wait(&dircache_queue, &ev);
698 switch (ev.id)
700 #ifdef HAVE_HOTSWAP
701 case SYS_FS_CHANGED:
702 if (!dircache_initialized)
703 break;
704 dircache_initialized = false;
705 #endif
706 case DIRCACHE_BUILD:
707 thread_enabled = true;
708 dircache_do_rebuild();
709 thread_enabled = false;
710 break ;
712 case DIRCACHE_STOP:
713 logf("Stopped the rebuilding.");
714 dircache_initialized = false;
715 break ;
717 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
718 case SYS_USB_CONNECTED:
719 usb_acknowledge(SYS_USB_CONNECTED_ACK);
720 usb_wait_for_disconnect(&dircache_queue);
721 break ;
722 #endif
728 * Start scanning the disk to build the dircache.
729 * Either transparent or non-transparent build method is used.
731 int dircache_build(int last_size)
733 if (dircache_initialized || thread_enabled)
734 return -3;
736 logf("Building directory cache");
737 #ifdef HAVE_EEPROM_SETTINGS
738 remove_dircache_file();
739 #endif
741 /* Background build, dircache has been previously allocated */
742 if (dircache_size > 0)
744 thread_enabled = true;
745 dircache_initializing = true;
746 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
747 return 2;
750 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
752 allocated_size = last_size + DIRCACHE_RESERVE;
753 dircache_root = buffer_alloc(allocated_size);
754 thread_enabled = true;
756 /* Start a transparent rebuild. */
757 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
758 return 3;
761 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
763 /* Start a non-transparent rebuild. */
764 return dircache_do_rebuild();
768 * Steal the allocated dircache buffer and disable dircache.
770 void* dircache_steal_buffer(long *size)
772 dircache_disable();
773 if (dircache_size == 0)
775 *size = 0;
776 return NULL;
779 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
781 return dircache_root;
785 * Main initialization function that must be called before any other
786 * operations within the dircache.
788 void dircache_init(void)
790 int i;
791 int thread_id;
793 dircache_initialized = false;
794 dircache_initializing = false;
796 memset(opendirs, 0, sizeof(opendirs));
797 for (i = 0; i < MAX_OPEN_DIRS; i++)
799 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
802 queue_init(&dircache_queue, true);
803 thread_id = create_thread(dircache_thread, dircache_stack,
804 sizeof(dircache_stack), 0, dircache_thread_name
805 IF_PRIO(, PRIORITY_BACKGROUND)
806 IF_COP(, CPU));
807 #ifdef HAVE_IO_PRIORITY
808 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
809 #endif
814 * Returns true if dircache has been initialized and is ready to be used.
816 bool dircache_is_enabled(void)
818 return dircache_initialized;
822 * Returns true if dircache is being initialized.
824 bool dircache_is_initializing(void)
826 return dircache_initializing || thread_enabled;
830 * Set application flags used to determine if dircache is still intact.
832 void dircache_set_appflag(long mask)
834 appflags |= mask;
838 * Get application flags used to determine if dircache is still intact.
840 bool dircache_get_appflag(long mask)
842 return dircache_is_enabled() && (appflags & mask);
846 * Returns the current number of entries (directories and files) in the cache.
848 int dircache_get_entry_count(void)
850 return entry_count;
854 * Returns the allocated space for dircache (without reserve space).
856 int dircache_get_cache_size(void)
858 return dircache_is_enabled() ? dircache_size : 0;
862 * Returns how many bytes of the reserve allocation for live cache
863 * updates have been used.
865 int dircache_get_reserve_used(void)
867 return dircache_is_enabled() ? reserve_used : 0;
871 * Returns the time in kernel ticks that took to build the cache.
873 int dircache_get_build_ticks(void)
875 return dircache_is_enabled() ? cache_build_ticks : 0;
879 * Disables the dircache. Usually called on shutdown or when
880 * accepting a usb connection.
882 void dircache_disable(void)
884 int i;
885 bool cache_in_use;
887 if (thread_enabled)
888 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
890 while (thread_enabled)
891 sleep(1);
892 dircache_initialized = false;
894 logf("Waiting for cached dirs to release");
895 do {
896 cache_in_use = false;
897 for (i = 0; i < MAX_OPEN_DIRS; i++) {
898 if (!opendirs[i].regulardir && opendirs[i].busy)
900 cache_in_use = true;
901 sleep(1);
902 break ;
905 } while (cache_in_use) ;
907 logf("Cache released");
908 entry_count = 0;
912 * Usermode function to return dircache_entry pointer to the given path.
914 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
916 if (!dircache_initialized || filename == NULL)
917 return NULL;
919 return dircache_get_entry(filename, false);
923 * Function to copy the full absolute path from dircache to the given buffer
924 * using the given dircache_entry pointer.
926 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
928 int path_size = 0;
929 int idx;
930 const struct dircache_entry *temp = entry;
932 if (size <= 0)
933 return ;
935 /* first compute the necessary size */
936 while(temp != NULL)
938 path_size += temp->name_len; /* '/' + d_name */
939 temp = temp->up;
942 /* now copy the path */
943 /* doesn't matter with trailing 0 because it's added later */
944 idx = path_size;
945 while(entry != NULL)
947 idx -= entry->name_len;
948 /* available size */
949 int rem = size - idx;
951 if(rem >= 1)
953 buf[idx] = '/';
954 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
956 entry = entry->up;
959 /* add trailing 0 */
960 buf[MIN(path_size, size-1)] = 0;
963 /* --- Directory cache live updating functions --- */
964 static int block_until_ready(void)
966 /* Block until dircache has been built. */
967 while (!dircache_initialized && dircache_is_initializing())
968 sleep(1);
970 if (!dircache_initialized)
971 return -1;
973 return 0;
976 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
978 struct dircache_entry *entry;
979 char basedir[MAX_PATH*2];
980 char *new;
981 long last_cache_size = dircache_size;
983 strlcpy(basedir, path, sizeof(basedir));
984 new = strrchr(basedir, '/');
985 if (new == NULL)
987 logf("error occurred");
988 dircache_initialized = false;
989 return NULL;
992 *new = '\0';
993 new++;
995 entry = dircache_get_entry(basedir, true);
996 if (entry == NULL)
998 logf("basedir not found!");
999 logf("%s", basedir);
1000 dircache_initialized = false;
1001 return NULL;
1004 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
1005 >= DIRCACHE_RESERVE)
1007 logf("not enough space");
1008 dircache_initialized = false;
1009 return NULL;
1012 while (entry->next != NULL)
1013 entry = entry->next;
1015 if (entry->name_len > 0)
1016 entry = dircache_gen_next(entry);
1018 if (entry == NULL)
1020 dircache_initialized = false;
1021 return NULL;
1024 entry->attribute = attribute;
1025 entry->name_len = MIN(254, strlen(new)) + 1;
1026 entry->d_name = ((char *)dircache_root+dircache_size);
1027 entry->startcluster = 0;
1028 entry->wrtdate = 0;
1029 entry->wrttime = 0;
1030 entry->size = 0;
1031 memcpy(entry->d_name, new, entry->name_len);
1032 dircache_size += entry->name_len;
1034 if (attribute & ATTR_DIRECTORY)
1036 logf("gen_down");
1037 dircache_gen_down(entry);
1040 reserve_used += dircache_size - last_cache_size;
1042 return entry;
1045 void dircache_bind(int fd, const char *path)
1047 struct dircache_entry *entry;
1049 /* Queue requests until dircache has been built. */
1050 if (!dircache_initialized && dircache_is_initializing())
1052 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1053 return ;
1054 strlcpy(fdbind_cache[fdbind_idx].path, path,
1055 sizeof(fdbind_cache[fdbind_idx].path));
1056 fdbind_cache[fdbind_idx].fd = fd;
1057 fdbind_idx++;
1058 return ;
1061 if (!dircache_initialized)
1062 return ;
1064 logf("bind: %d/%s", fd, path);
1065 entry = dircache_get_entry(path, false);
1066 if (entry == NULL)
1068 logf("not found!");
1069 dircache_initialized = false;
1070 return ;
1073 fd_bindings[fd] = entry;
1076 void dircache_update_filesize(int fd, long newsize, long startcluster)
1078 if (!dircache_initialized || fd < 0)
1079 return ;
1081 if (fd_bindings[fd] == NULL)
1083 logf("dircache fd(%d) access error", fd);
1084 dircache_initialized = false;
1085 return ;
1088 fd_bindings[fd]->size = newsize;
1089 fd_bindings[fd]->startcluster = startcluster;
1091 void dircache_update_filetime(int fd)
1093 #if CONFIG_RTC == 0
1094 (void)fd;
1095 #else
1096 short year;
1097 struct tm *now = get_time();
1098 if (!dircache_initialized || fd < 0)
1099 return ;
1101 if (fd_bindings[fd] == NULL)
1103 logf("dircache fd access error");
1104 dircache_initialized = false;
1105 return ;
1107 year = now->tm_year+1900-1980;
1108 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1109 (((now->tm_mon+1)&0xf)<<5) |
1110 (((now->tm_mday)&0x1f));
1111 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1112 (((now->tm_min)&0x3f)<<5) |
1113 (((now->tm_sec/2)&0x1f));
1114 #endif
1117 void dircache_mkdir(const char *path)
1118 { /* Test ok. */
1119 if (block_until_ready())
1120 return ;
1123 logf("mkdir: %s", path);
1124 dircache_new_entry(path, ATTR_DIRECTORY);
1127 void dircache_rmdir(const char *path)
1128 { /* Test ok. */
1129 struct dircache_entry *entry;
1131 if (block_until_ready())
1132 return ;
1134 logf("rmdir: %s", path);
1135 entry = dircache_get_entry(path, false);
1136 if (entry == NULL || entry->down == NULL)
1138 logf("not found or not a directory!");
1139 dircache_initialized = false;
1140 return ;
1143 entry->down = NULL;
1144 entry->name_len = 0;
1147 /* Remove a file from cache */
1148 void dircache_remove(const char *name)
1149 { /* Test ok. */
1150 struct dircache_entry *entry;
1152 if (block_until_ready())
1153 return ;
1155 logf("remove: %s", name);
1157 entry = dircache_get_entry(name, false);
1159 if (entry == NULL)
1161 logf("not found!");
1162 dircache_initialized = false;
1163 return ;
1166 entry->name_len = 0;
1169 void dircache_rename(const char *oldpath, const char *newpath)
1170 { /* Test ok. */
1171 struct dircache_entry *entry, *newentry;
1172 struct dircache_entry oldentry;
1173 char absolute_path[MAX_PATH*2];
1174 char *p;
1176 if (block_until_ready())
1177 return ;
1179 logf("rename: %s->%s", oldpath, newpath);
1181 entry = dircache_get_entry(oldpath, false);
1182 if (entry == NULL)
1184 logf("not found!");
1185 dircache_initialized = false;
1186 return ;
1189 /* Delete the old entry. */
1190 entry->name_len = 0;
1192 /** If we rename the same filename twice in a row, we need to
1193 * save the data, because the entry will be re-used. */
1194 oldentry = *entry;
1196 /* Generate the absolute path for destination if necessary. */
1197 if (newpath[0] != '/')
1199 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1200 p = strrchr(absolute_path, '/');
1201 if (!p)
1203 logf("Invalid path");
1204 dircache_initialized = false;
1205 return ;
1208 *p = '\0';
1209 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1210 newpath = absolute_path;
1213 newentry = dircache_new_entry(newpath, entry->attribute);
1214 if (newentry == NULL)
1216 dircache_initialized = false;
1217 return ;
1220 newentry->down = oldentry.down;
1221 newentry->size = oldentry.size;
1222 newentry->startcluster = oldentry.startcluster;
1223 newentry->wrttime = oldentry.wrttime;
1224 newentry->wrtdate = oldentry.wrtdate;
1227 void dircache_add_file(const char *path, long startcluster)
1229 struct dircache_entry *entry;
1231 if (block_until_ready())
1232 return ;
1234 logf("add file: %s", path);
1235 entry = dircache_new_entry(path, 0);
1236 if (entry == NULL)
1237 return ;
1239 entry->startcluster = startcluster;
1242 static bool is_disable_msg_pending(void)
1244 return check_event_queue();
1247 DIR_CACHED* opendir_cached(const char* name)
1249 int dd;
1250 DIR_CACHED* pdir = opendirs;
1252 if ( name[0] != '/' )
1254 DEBUGF("Only absolute paths supported right now\n");
1255 return NULL;
1258 /* find a free dir descriptor */
1259 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1260 if ( !pdir->busy )
1261 break;
1263 if ( dd == MAX_OPEN_DIRS )
1265 DEBUGF("Too many dirs open\n");
1266 errno = EMFILE;
1267 return NULL;
1270 pdir->busy = true;
1272 if (!dircache_initialized || is_disable_msg_pending())
1274 pdir->internal_entry = NULL;
1275 pdir->regulardir = opendir_uncached(name);
1277 else
1279 pdir->regulardir = NULL;
1280 pdir->internal_entry = dircache_get_entry(name, true);
1281 pdir->theent.attribute = -1; /* used to make readdir_cached aware of the first call */
1284 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1286 pdir->busy = false;
1287 return NULL;
1290 return pdir;
1293 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1295 struct dircache_entry *ce = dir->internal_entry;
1296 struct dirent_uncached *regentry;
1298 if (!dir->busy)
1299 return NULL;
1301 if (dir->regulardir != NULL)
1303 regentry = readdir_uncached(dir->regulardir);
1304 if (regentry == NULL)
1305 return NULL;
1307 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1308 dir->theent.size = regentry->size;
1309 dir->theent.startcluster = regentry->startcluster;
1310 dir->theent.attribute = regentry->attribute;
1311 dir->theent.wrttime = regentry->wrttime;
1312 dir->theent.wrtdate = regentry->wrtdate;
1314 return &dir->theent;
1317 /* if theent.attribute=-1 then this is the first call */
1318 /* otherwise, this is is not so we first take the entry's ->next */
1319 /* NOTE: normal file can't have attribute=-1 */
1320 if(dir->theent.attribute != -1)
1321 ce = ce->next;
1322 /* skip unused entries */
1323 while(ce != NULL && ce->name_len == 0)
1324 ce = ce->next;
1326 if (ce == NULL)
1327 return NULL;
1329 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1330 /* Can't do `dir->theent = *ce`
1331 because that modifies the d_name pointer. */
1332 dir->theent.size = ce->size;
1333 dir->theent.startcluster = ce->startcluster;
1334 dir->theent.attribute = ce->attribute;
1335 dir->theent.wrttime = ce->wrttime;
1336 dir->theent.wrtdate = ce->wrtdate;
1337 dir->internal_entry = ce;
1339 //logf("-> %s", ce->name);
1340 return &dir->theent;
1343 int closedir_cached(DIR_CACHED* dir)
1345 if (!dir->busy)
1346 return -1;
1348 dir->busy=false;
1349 if (dir->regulardir != NULL)
1350 return closedir_uncached(dir->regulardir);
1352 return 0;
1355 int mkdir_cached(const char *name)
1357 int rc=mkdir_uncached(name);
1358 if (rc >= 0)
1359 dircache_mkdir(name);
1360 return(rc);
1363 int rmdir_cached(const char* name)
1365 int rc=rmdir_uncached(name);
1366 if(rc >= 0)
1367 dircache_rmdir(name);
1368 return(rc);