Prepare new, unofficial pandora build
[maemo-rb.git] / firmware / common / dircache.c
blob375ffa63ecbf6993a563bc07141a59b964e4dee5
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"
51 /* Queue commands. */
52 #define DIRCACHE_BUILD 1
53 #define DIRCACHE_STOP 2
55 #if (MEMORYSIZE > 8)
56 #define MAX_OPEN_DIRS 12
57 #else
58 #define MAX_OPEN_DIRS 8
59 #endif
61 #define MAX_PENDING_BINDINGS 2
62 struct fdbind_queue {
63 char path[MAX_PATH];
64 int fd;
67 /* Exported structures. */
68 struct dircache_entry {
69 struct dirinfo info;
70 struct dircache_entry *next;
71 struct dircache_entry *up;
72 struct dircache_entry *down;
73 long startcluster;
74 char *d_name;
77 /* Cache Layout:
79 * x - array of struct dircache_entry
80 * r - reserved buffer
81 * d - name buffer for the d_name entry of the struct dircache_entry
82 * |xxxxxx|rrrrrrrrr|dddddd|
84 * subsequent x are allocated from the front, d are allocated from the back,
85 * using the reserve buffer for entries added after initial scan
87 * after a while the cache may look like:
88 * |xxxxxxxx|rrrrr|dddddddd|
90 * after a reboot, the reserve buffer is restored in it's size, so that the
91 * total allocation size grows
92 * |xxxxxxxx|rrrrrrrrr|dddddddd|
94 /* this points to the beginnging of the buffer and the first entry */
95 static struct dircache_entry *dircache_root;
96 /* these point to the start and end of the name buffer (d above) */
97 static char *d_names_start, *d_names_end;
98 /* put "." and ".." into the d_names buffer to enable easy pointer logic */
99 static char *dot, *dotdot;
100 #ifdef HAVE_MULTIVOLUME
101 static struct dircache_entry *append_position;
102 #endif
104 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
105 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
107 static bool dircache_initialized = false;
108 static bool dircache_initializing = false;
109 static bool thread_enabled = false;
110 static unsigned long allocated_size = DIRCACHE_LIMIT;
111 static unsigned long dircache_size = 0;
112 static unsigned long entry_count = 0;
113 static unsigned long reserve_used = 0;
114 static unsigned int cache_build_ticks = 0;
115 static unsigned long appflags = 0;
117 static struct event_queue dircache_queue SHAREDBSS_ATTR;
118 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x400)/sizeof(long)];
119 static const char dircache_thread_name[] = "dircache";
121 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
122 static int fdbind_idx = 0;
124 /* --- Internal cache structure control functions --- */
126 static inline struct dircache_entry* get_entry(int id)
128 return &dircache_root[id];
131 #ifdef HAVE_EEPROM_SETTINGS
133 * Open the dircache file to save a snapshot on disk
135 static int open_dircache_file(unsigned flags, int permissions)
137 if (permissions != 0)
138 return open(DIRCACHE_FILE, flags, permissions);
140 return open(DIRCACHE_FILE, flags);
144 * Remove the snapshot file
146 static int remove_dircache_file(void)
148 return remove(DIRCACHE_FILE);
150 #endif
151 /**
152 * Internal function to allocate a new dircache_entry from memory.
154 static struct dircache_entry* allocate_entry(void)
156 struct dircache_entry *next_entry;
158 if (dircache_size > allocated_size - MAX_PATH*2)
160 logf("size limit reached");
161 return NULL;
164 next_entry = &dircache_root[entry_count++];
165 next_entry->d_name = NULL;
166 next_entry->up = NULL;
167 next_entry->down = NULL;
168 next_entry->next = NULL;
170 dircache_size += sizeof(struct dircache_entry);
172 return next_entry;
176 * Internal function to allocate a dircache_entry and set
177 * ->next entry pointers.
179 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
181 struct dircache_entry *next_entry;
183 if ( (next_entry = allocate_entry()) == NULL)
184 return NULL;
185 next_entry->up = ce->up;
186 ce->next = next_entry;
188 return next_entry;
192 * Internal function to allocate a dircache_entry and set
193 * ->down entry pointers.
195 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
197 struct dircache_entry *next_entry;
199 if ( (next_entry = allocate_entry()) == NULL)
200 return NULL;
201 next_entry->up = ce;
202 ce->down = next_entry;
204 return next_entry;
208 * Returns true if there is an event waiting in the queue
209 * that requires the current operation to be aborted.
211 static bool check_event_queue(void)
213 struct queue_event ev;
215 if(!queue_peek(&dircache_queue, &ev))
216 return false;
218 switch (ev.id)
220 case DIRCACHE_STOP:
221 case SYS_USB_CONNECTED:
222 #ifdef HAVE_HOTSWAP
223 case SYS_FS_CHANGED:
224 #endif
225 return true;
228 return false;
231 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
232 /* scan and build static data (avoid redundancy on stack) */
233 static struct
235 #ifdef HAVE_MULTIVOLUME
236 int volume;
237 #endif
238 struct fat_dir *dir;
239 struct fat_direntry *direntry;
240 }sab;
242 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
244 /* normally, opendir expects a full fat_dir as parent but in our case,
245 * it's completely useless because we don't modify anything
246 * WARNING: this heavily relies on current FAT implementation ! */
248 /* those field are necessary to update the FAT entry in case of modification
249 here we don't touch anything so we put dummy values */
250 sab.dir->entry = 0;
251 sab.dir->entrycount = 0;
252 sab.dir->file.firstcluster = 0;
253 /* open directory */
254 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
255 if(rc < 0)
257 logf("fat_opendir failed: %d", rc);
258 return rc;
261 /* first pass : read dir */
262 struct dircache_entry *first_ce = ce;
264 /* read through directory */
265 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
267 if(!strcmp(".", sab.direntry->name) ||
268 !strcmp("..", sab.direntry->name))
269 continue;
271 size_t size = strlen(sab.direntry->name) + 1;
272 ce->d_name = (d_names_start -= size);
273 ce->startcluster = sab.direntry->firstcluster;
274 ce->info.size = sab.direntry->filesize;
275 ce->info.attribute = sab.direntry->attr;
276 ce->info.wrtdate = sab.direntry->wrtdate;
277 ce->info.wrttime = sab.direntry->wrttime;
279 strcpy(ce->d_name, sab.direntry->name);
280 dircache_size += size;
282 if(ce->info.attribute & FAT_ATTR_DIRECTORY)
283 dircache_gen_down(ce);
285 ce = dircache_gen_next(ce);
286 if(ce == NULL)
287 return -5;
289 /* When simulator is used, it's only safe to yield here. */
290 if(thread_enabled)
292 /* Stop if we got an external signal. */
293 if(check_event_queue())
294 return -6;
295 yield();
299 /* add "." and ".." */
300 ce->d_name = dot;
301 ce->info.attribute = FAT_ATTR_DIRECTORY;
302 ce->startcluster = startcluster;
303 ce->info.size = 0;
304 ce->down = first_ce;
306 ce = dircache_gen_next(ce);
308 ce->d_name = dotdot;
309 ce->info.attribute = FAT_ATTR_DIRECTORY;
310 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
311 ce->info.size = 0;
312 ce->down = first_ce->up;
314 /* second pass: recurse ! */
315 ce = first_ce;
317 while(rc >= 0 && ce)
319 if(ce->d_name != NULL && ce->down != NULL && strcmp(ce->d_name, ".")
320 && strcmp(ce->d_name, ".."))
321 rc = sab_process_dir(ce->startcluster, ce->down);
323 ce = ce->next;
326 return rc;
329 /* used during the generation */
330 static struct fat_dir sab_fat_dir;
332 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
334 memset(ce, 0, sizeof(struct dircache_entry));
336 #ifdef HAVE_MULTIVOLUME
337 if (volume > 0)
339 ce->d_name = (d_names_start -= sizeof(VOL_NAMES));
340 size_t len = snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume)+1;
341 dircache_size += len;
342 ce->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
343 ce->info.size = 0;
344 append_position = dircache_gen_next(ce);
345 ce = dircache_gen_down(ce);
347 #endif
349 struct fat_direntry direntry; /* ditto */
350 #ifdef HAVE_MULTIVOLUME
351 sab.volume = volume;
352 #endif
353 sab.dir = &sab_fat_dir;
354 sab.direntry = &direntry;
356 return sab_process_dir(0, ce);
358 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
359 static char sab_path[MAX_PATH];
361 static int sab_process_dir(struct dircache_entry *ce)
363 struct dirent_uncached *entry;
364 struct dircache_entry *first_ce = ce;
365 DIR_UNCACHED *dir = opendir_uncached(sab_path);
366 if(dir == NULL)
368 logf("Failed to opendir_uncached(%s)", sab_path);
369 return -1;
372 while((entry = readdir_uncached(dir)))
374 if(!strcmp(".", entry->d_name) ||
375 !strcmp("..", entry->d_name))
376 continue;
378 size_t size = strlen(entry->d_name) + 1;
379 ce->d_name = (d_names_start -= size);
380 ce->info = entry->info;
382 strcpy(ce->d_name, entry->d_name);
383 dircache_size += size;
385 if(entry->info.attribute & ATTR_DIRECTORY)
387 dircache_gen_down(ce);
388 if(ce->down == NULL)
390 closedir_uncached(dir);
391 return -1;
393 /* save current paths size */
394 int pathpos = strlen(sab_path);
395 /* append entry */
396 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
397 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
399 int rc = sab_process_dir(ce->down);
400 /* restore path */
401 sab_path[pathpos] = '\0';
403 if(rc < 0)
405 closedir_uncached(dir);
406 return rc;
410 ce = dircache_gen_next(ce);
411 if(ce == NULL)
412 return -5;
414 /* When simulator is used, it's only safe to yield here. */
415 if(thread_enabled)
417 /* Stop if we got an external signal. */
418 if(check_event_queue())
419 return -1;
420 yield();
424 /* add "." and ".." */
425 ce->d_name = dot;
426 ce->info.attribute = ATTR_DIRECTORY;
427 ce->info.size = 0;
428 ce->down = first_ce;
430 ce = dircache_gen_next(ce);
432 ce->d_name = dotdot;
433 ce->info.attribute = ATTR_DIRECTORY;
434 ce->info.size = 0;
435 ce->down = first_ce->up;
437 closedir_uncached(dir);
438 return 0;
441 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
443 #ifdef HAVE_MULTIVOLUME
444 (void) volume;
445 #endif
446 memset(ce, 0, sizeof(struct dircache_entry));
448 strlcpy(sab_path, "/", sizeof sab_path);
449 return sab_process_dir(ce);
451 #endif /* PLATFORM_NATIVE */
454 * Internal function to get a pointer to dircache_entry for a given filename.
455 * path: Absolute path to a file or directory (see comment)
456 * go_down: Returns the first entry of the directory given by the path (see comment)
458 * As a a special case, accept path="" as an alias for "/".
459 * Also if the path omits the first '/', it will be accepted.
461 * * If get_down=true:
462 * If path="/", the returned entry is the first of root directory (ie dircache_root)
463 * Otherwise, if 'entry' is the returned value when get_down=false,
464 * the functions returns entry->down (which can be NULL)
466 * * If get_down=false:
467 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
468 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
469 * Which means that
470 * dircache_get_entry(path)->d_name == chunk_n
472 * If path="/", the returned entry is NULL.
473 * If the entry doesn't exist, return NULL
475 * NOTE: this functions silently handles double '/'
477 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
479 char namecopy[MAX_PATH];
480 char* part;
481 char* end;
483 bool at_root = true;
484 struct dircache_entry *cache_entry = dircache_root;
486 strlcpy(namecopy, path, sizeof(namecopy));
488 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
490 /* If request another chunk, the current entry has to be directory
491 * and so cache_entry->down has to be non-NULL/
492 * Special case of root because it's already the first entry of the root directory
494 * NOTE: this is safe even if cache_entry->down is NULL */
495 if(!at_root)
496 cache_entry = cache_entry->down;
497 else
498 at_root = false;
500 /* scan dir for name */
501 while(cache_entry != NULL)
503 /* skip unused entries */
504 if(cache_entry->d_name == NULL)
506 cache_entry = cache_entry->next;
507 continue;
509 /* compare names */
510 if(!strcasecmp(part, cache_entry->d_name))
511 break;
512 /* go to next entry */
513 cache_entry = cache_entry->next;
516 /* handle not found case */
517 if(cache_entry == NULL)
518 return NULL;
521 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
522 if(go_down)
523 return at_root ? cache_entry : cache_entry->down;
524 else
525 return at_root ? NULL : cache_entry;
528 #ifdef HAVE_EEPROM_SETTINGS
530 #define DIRCACHE_MAGIC 0x00d0c0a1
531 struct dircache_maindata {
532 long magic;
533 long size;
534 long entry_count;
535 long appflags;
536 struct dircache_entry *root_entry;
537 char *d_names_start;
541 * Function to load the internal cache structure from disk to initialize
542 * the dircache really fast and little disk access.
544 int dircache_load(void)
546 struct dircache_maindata maindata;
547 ssize_t bytes_read;
548 int fd;
550 if (dircache_initialized)
551 return -1;
553 logf("Loading directory cache");
554 dircache_size = 0;
556 fd = open_dircache_file(O_RDONLY, 0);
557 if (fd < 0)
558 return -2;
560 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
561 if (bytes_read != sizeof(struct dircache_maindata)
562 || maindata.magic != DIRCACHE_MAGIC || maindata.size <= 0)
564 logf("Dircache file header error");
565 close(fd);
566 remove_dircache_file();
567 return -3;
570 allocated_size = maindata.size + DIRCACHE_RESERVE;
571 dircache_root = buffer_alloc(allocated_size);
572 /* needs to be struct-size aligned so that the pointer arithmetic below works */
573 ALIGN_BUFFER(dircache_root, allocated_size, sizeof(struct dircache_entry));
574 entry_count = maindata.entry_count;
575 appflags = maindata.appflags;
577 /* read the dircache file into memory,
578 * start with the struct dircache_entries */
579 ssize_t bytes_to_read = entry_count*sizeof(struct dircache_entry);
580 bytes_read = read(fd, dircache_root, bytes_to_read);
582 if (bytes_read != bytes_to_read)
584 logf("Dircache read failed #1");
585 return -6;
588 /* continue with the d_names. Fix up pointers to them if needed */
589 bytes_to_read = maindata.size - bytes_to_read;
590 d_names_start = (char*)dircache_root + allocated_size - bytes_to_read;
591 bytes_read = read(fd, d_names_start, bytes_to_read);
592 close(fd);
593 remove_dircache_file();
594 if (bytes_read != bytes_to_read)
596 logf("Dircache read failed #2");
597 return -7;
600 d_names_end = d_names_start + bytes_read;
601 dot = d_names_end - sizeof(".");
602 dotdot = dot - sizeof("..");
604 /* d_names are in reverse order, so the last entry points to the first string */
605 ptrdiff_t offset_d_names = maindata.d_names_start - d_names_start,
606 offset_entries = maindata.root_entry - dircache_root;
608 /* offset_entries is less likely to differ, so check if it's 0 in the loop
609 * offset_d_names however is almost always non-zero, since dircache_save()
610 * creates a file which causes the reserve buffer to be used. since
611 * we allocate a new, empty DIRCACHE_RESERVE here, the strings are
612 * farther behind */
613 if (offset_entries != 0 || offset_d_names != 0)
615 for(unsigned i = 0; i < entry_count; i++)
617 if (dircache_root[i].d_name)
618 dircache_root[i].d_name -= offset_d_names;
620 if (offset_entries == 0)
621 continue;
622 if (dircache_root[i].next)
623 dircache_root[i].next -= offset_entries;
624 if (dircache_root[i].up)
625 dircache_root[i].up -= offset_entries;
626 if (dircache_root[i].down)
627 dircache_root[i].down -= offset_entries;
631 /* Cache successfully loaded. */
632 dircache_size = maindata.size;
633 reserve_used = 0;
634 logf("Done, %ld KiB used", dircache_size / 1024);
635 dircache_initialized = true;
636 memset(fd_bindings, 0, sizeof(fd_bindings));
638 return 0;
642 * Function to save the internal cache stucture to disk for fast loading
643 * on boot.
645 int dircache_save(void)
647 struct dircache_maindata maindata;
648 int fd;
649 unsigned long bytes_written;
651 remove_dircache_file();
653 if (!dircache_initialized)
654 return -1;
656 logf("Saving directory cache");
657 fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
659 maindata.magic = DIRCACHE_MAGIC;
660 maindata.size = dircache_size;
661 maindata.root_entry = dircache_root;
662 maindata.d_names_start = d_names_start;
663 maindata.entry_count = entry_count;
664 maindata.appflags = appflags;
666 /* Save the info structure */
667 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
668 if (bytes_written != sizeof(struct dircache_maindata))
670 close(fd);
671 logf("dircache: write failed #1");
672 return -2;
675 /* Dump whole directory cache to disk
676 * start by writing the dircache_entries */
677 size_t bytes_to_write = entry_count*sizeof(struct dircache_entry);
678 bytes_written = write(fd, dircache_root, bytes_to_write);
679 if (bytes_written != bytes_to_write)
681 logf("dircache: write failed #2");
682 return -3;
685 /* continue with the d_names */
686 bytes_to_write = d_names_end - d_names_start;
687 bytes_written = write(fd, d_names_start, bytes_to_write);
688 close(fd);
689 if (bytes_written != bytes_to_write)
691 logf("dircache: write failed #3");
692 return -4;
696 return 0;
698 #endif /* HAVE_EEPROM_SETTINGS */
701 * Internal function which scans the disk and creates the dircache structure.
703 static int dircache_do_rebuild(void)
705 struct dircache_entry* root_entry;
706 unsigned int start_tick;
707 int i;
709 /* Measure how long it takes build the cache. */
710 start_tick = current_tick;
711 dircache_initializing = true;
712 appflags = 0;
714 /* reset dircache and alloc root entry */
715 entry_count = 0;
716 root_entry = allocate_entry();
718 #ifdef HAVE_MULTIVOLUME
719 append_position = root_entry;
721 for (i = NUM_VOLUMES; i >= 0; i--)
723 if (fat_ismounted(i))
725 #endif
726 cpu_boost(true);
727 #ifdef HAVE_MULTIVOLUME
728 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
729 #else
730 if (dircache_scan_and_build(IF_MV2(0,) root_entry) < 0)
731 #endif /* HAVE_MULTIVOLUME */
733 logf("dircache_scan_and_build failed");
734 cpu_boost(false);
735 dircache_size = 0;
736 dircache_initializing = false;
737 return -2;
739 cpu_boost(false);
740 #ifdef HAVE_MULTIVOLUME
743 #endif
745 logf("Done, %ld KiB used", dircache_size / 1024);
747 dircache_initialized = true;
748 dircache_initializing = false;
749 cache_build_ticks = current_tick - start_tick;
751 /* Initialized fd bindings. */
752 memset(fd_bindings, 0, sizeof(fd_bindings));
753 for (i = 0; i < fdbind_idx; i++)
754 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
755 fdbind_idx = 0;
757 if (thread_enabled)
759 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
760 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
763 return 1;
767 * Internal thread that controls transparent cache building.
769 static void dircache_thread(void)
771 struct queue_event ev;
773 while (1)
775 queue_wait(&dircache_queue, &ev);
777 switch (ev.id)
779 #ifdef HAVE_HOTSWAP
780 case SYS_FS_CHANGED:
781 if (!dircache_initialized)
782 break;
783 dircache_initialized = false;
784 #endif
785 case DIRCACHE_BUILD:
786 thread_enabled = true;
787 dircache_do_rebuild();
788 thread_enabled = false;
789 break ;
791 case DIRCACHE_STOP:
792 logf("Stopped the rebuilding.");
793 dircache_initialized = false;
794 break ;
796 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
797 case SYS_USB_CONNECTED:
798 usb_acknowledge(SYS_USB_CONNECTED_ACK);
799 usb_wait_for_disconnect(&dircache_queue);
800 break ;
801 #endif
806 static void generate_dot_d_names(void)
808 dot = (d_names_start -= sizeof("."));
809 dotdot = (d_names_start -= sizeof(".."));
810 dircache_size += sizeof(".") + sizeof("..");
811 strcpy(dot, ".");
812 strcpy(dotdot, "..");
815 * Start scanning the disk to build the dircache.
816 * Either transparent or non-transparent build method is used.
818 int dircache_build(int last_size)
820 if (dircache_initialized || thread_enabled)
821 return -3;
823 logf("Building directory cache");
824 #ifdef HAVE_EEPROM_SETTINGS
825 remove_dircache_file();
826 #endif
828 /* Background build, dircache has been previously allocated */
829 if (dircache_size > 0)
831 thread_enabled = true;
832 dircache_initializing = true;
833 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
834 return 2;
836 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
838 allocated_size = last_size + DIRCACHE_RESERVE;
839 dircache_root = buffer_alloc(allocated_size);
840 ALIGN_BUFFER(dircache_root, allocated_size, sizeof(struct dircache_entry));
841 d_names_start =
842 d_names_end = ((char*)dircache_root)+allocated_size-1;
843 dircache_size = 0;
844 thread_enabled = true;
845 generate_dot_d_names();
847 /* Start a transparent rebuild. */
848 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
849 return 3;
852 /* We'll use the entire audiobuf to allocate the dircache
853 * struct dircache_entrys are allocated from the beginning
854 * and their corresponding d_name from the end
855 * after generation the buffer will be compacted with DIRCACHE_RESERVE
856 * free bytes inbetween */
857 audiobuf = ALIGN_UP(audiobuf, sizeof(struct dircache_entry));
858 dircache_root = (struct dircache_entry*)audiobuf;
859 d_names_start = d_names_end = audiobufend - 1;
860 dircache_size = 0;
861 generate_dot_d_names();
863 /* Start a non-transparent rebuild. */
864 int res = dircache_do_rebuild();
865 if (res < 0)
866 return res;
868 /* now compact the dircache buffer */
869 char* dst = ((char*)&dircache_root[entry_count] + DIRCACHE_RESERVE);
870 ptrdiff_t offset = d_names_start - dst, size_to_move;
871 if (offset <= 0) /* something went wrong */
872 return -1;
874 /* memmove d_names down, there's a possibility of overlap */
875 size_to_move = dircache_size - entry_count*sizeof(struct dircache_entry);
876 memmove(dst, d_names_start, size_to_move);
878 /* fix up pointers to the d_names */
879 for(unsigned i = 0; i < entry_count; i++)
880 dircache_root[i].d_name -= offset;
882 d_names_end -= offset;
883 /* equivalent to dircache_size + DIRCACHE_RESERVE */
884 allocated_size = (d_names_end - (char*)dircache_root);
885 reserve_used = 0;
886 audiobuf += allocated_size;
888 return res;
892 * Steal the allocated dircache buffer and disable dircache.
894 void* dircache_steal_buffer(long *size)
896 dircache_disable();
897 if (dircache_size == 0)
899 *size = 0;
900 return NULL;
903 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
905 return dircache_root;
909 * Main initialization function that must be called before any other
910 * operations within the dircache.
912 void dircache_init(void)
914 int i;
915 int thread_id __attribute__((unused));
917 dircache_initialized = false;
918 dircache_initializing = false;
920 memset(opendirs, 0, sizeof(opendirs));
921 for (i = 0; i < MAX_OPEN_DIRS; i++)
923 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
926 queue_init(&dircache_queue, true);
927 thread_id = create_thread(dircache_thread, dircache_stack,
928 sizeof(dircache_stack), 0, dircache_thread_name
929 IF_PRIO(, PRIORITY_BACKGROUND)
930 IF_COP(, CPU));
931 #ifdef HAVE_IO_PRIORITY
932 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
933 #endif
938 * Returns true if dircache has been initialized and is ready to be used.
940 bool dircache_is_enabled(void)
942 return dircache_initialized;
946 * Returns true if dircache is being initialized.
948 bool dircache_is_initializing(void)
950 return dircache_initializing || thread_enabled;
954 * Set application flags used to determine if dircache is still intact.
956 void dircache_set_appflag(long mask)
958 appflags |= mask;
962 * Get application flags used to determine if dircache is still intact.
964 bool dircache_get_appflag(long mask)
966 return dircache_is_enabled() && (appflags & mask);
970 * Returns the current number of entries (directories and files) in the cache.
972 int dircache_get_entry_count(void)
974 return entry_count;
978 * Returns the allocated space for dircache (without reserve space).
980 int dircache_get_cache_size(void)
982 return dircache_is_enabled() ? dircache_size : 0;
986 * Returns how many bytes of the reserve allocation for live cache
987 * updates have been used.
989 int dircache_get_reserve_used(void)
991 return dircache_is_enabled() ? reserve_used : 0;
995 * Returns the time in kernel ticks that took to build the cache.
997 int dircache_get_build_ticks(void)
999 return dircache_is_enabled() ? cache_build_ticks : 0;
1003 * Disables the dircache. Usually called on shutdown or when
1004 * accepting a usb connection.
1006 void dircache_disable(void)
1008 int i;
1009 bool cache_in_use;
1011 if (thread_enabled)
1012 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
1014 while (thread_enabled)
1015 sleep(1);
1016 dircache_initialized = false;
1018 logf("Waiting for cached dirs to release");
1019 do {
1020 cache_in_use = false;
1021 for (i = 0; i < MAX_OPEN_DIRS; i++) {
1022 if (!opendirs[i].regulardir && opendirs[i].busy)
1024 cache_in_use = true;
1025 sleep(1);
1026 break ;
1029 } while (cache_in_use) ;
1031 logf("Cache released");
1032 entry_count = 0;
1036 * Usermode function to return dircache_entry index to the given path.
1038 static int dircache_get_entry_id_ex(const char *filename, bool go_down)
1040 if (!dircache_initialized || filename == NULL)
1041 return -1;
1043 struct dircache_entry* res = dircache_get_entry(filename, go_down);
1044 return res ? res - dircache_root : -1;
1047 int dircache_get_entry_id(const char* filename)
1049 return dircache_get_entry_id_ex(filename, false);
1053 * Internal: Get the startcluster for the index
1055 long _dircache_get_entry_startcluster(int id)
1057 return get_entry(id)->startcluster;
1061 * Internal: Get the struct dirinfo for the index
1063 struct dirinfo* _dircache_get_entry_dirinfo(int id)
1065 return &get_entry(id)->info;
1069 * build a path from an entry upto the root using recursion
1071 * it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
1072 * and it will leave a trailing '/'
1074 * returns the position of that trailing '/' so it can be deleted afterwards
1075 * (or, in case of truncation, the position of the nul byte */
1076 static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size)
1078 int offset = 1;
1079 /* has parent? */
1080 if (entry->up)
1081 offset += copy_path_helper(entry->up, buf, size);
1083 size_t len = strlcpy(buf+offset, entry->d_name, size - offset) + offset;
1084 if (len < size)
1086 buf[len++] = '/';
1087 buf[len] = '\0';
1089 return len-1;
1092 * Function to copy the full absolute path from dircache to the given buffer
1093 * using the given dircache_entry pointer.
1095 * Returns the size of the resulting string, or 0 if an error occured
1097 size_t dircache_copy_path(int index, char *buf, size_t size)
1099 if (!size || !buf || index < 0)
1100 return 0;
1102 buf[0] = '/';
1103 size_t res = copy_path_helper(&dircache_root[index], buf, size - 1);
1104 /* fixup trailing '/' */
1105 buf[res] = '\0';
1106 return res;
1109 /* --- Directory cache live updating functions --- */
1110 static int block_until_ready(void)
1112 /* Block until dircache has been built. */
1113 while (!dircache_initialized && dircache_is_initializing())
1114 sleep(1);
1116 if (!dircache_initialized)
1117 return -1;
1119 return 0;
1122 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
1124 struct dircache_entry *entry;
1125 char basedir[MAX_PATH*2];
1126 char *new;
1127 long last_cache_size = dircache_size;
1129 strlcpy(basedir, path, sizeof(basedir));
1130 new = strrchr(basedir, '/');
1131 if (new == NULL)
1133 logf("error occurred");
1134 dircache_initialized = false;
1135 return NULL;
1138 *new = '\0';
1139 new++;
1141 entry = dircache_get_entry(basedir, true);
1142 if (entry == NULL)
1144 logf("basedir not found!");
1145 logf("%s", basedir);
1146 dircache_initialized = false;
1147 return NULL;
1150 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
1151 >= DIRCACHE_RESERVE)
1153 logf("not enough space");
1154 dircache_initialized = false;
1155 return NULL;
1158 while (entry->next != NULL)
1159 entry = entry->next;
1161 if (entry->d_name != NULL)
1163 entry = dircache_gen_next(entry);
1164 if (entry == NULL)
1166 dircache_initialized = false;
1167 return NULL;
1171 size_t size = strlen(new) + 1;
1172 entry->d_name = (d_names_start -= size);
1173 entry->startcluster = 0;
1174 memset(&entry->info, 0, sizeof(entry->info));
1175 entry->info.attribute = attribute;
1177 strcpy(entry->d_name, new);
1178 dircache_size += size;
1180 if (attribute & ATTR_DIRECTORY)
1182 logf("gen_down");
1183 dircache_gen_down(entry);
1186 reserve_used += dircache_size - last_cache_size;
1188 return entry;
1191 void dircache_bind(int fd, const char *path)
1193 struct dircache_entry *entry;
1195 /* Queue requests until dircache has been built. */
1196 if (!dircache_initialized && dircache_is_initializing())
1198 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1199 return ;
1200 strlcpy(fdbind_cache[fdbind_idx].path, path,
1201 sizeof(fdbind_cache[fdbind_idx].path));
1202 fdbind_cache[fdbind_idx].fd = fd;
1203 fdbind_idx++;
1204 return ;
1207 if (!dircache_initialized)
1208 return ;
1210 logf("bind: %d/%s", fd, path);
1211 entry = dircache_get_entry(path, false);
1212 if (entry == NULL)
1214 logf("not found!");
1215 dircache_initialized = false;
1216 return ;
1219 fd_bindings[fd] = entry;
1222 void dircache_update_filesize(int fd, long newsize, long startcluster)
1224 if (!dircache_initialized || fd < 0)
1225 return ;
1227 if (fd_bindings[fd] == NULL)
1229 logf("dircache fd(%d) access error", fd);
1230 dircache_initialized = false;
1231 return ;
1234 fd_bindings[fd]->info.size = newsize;
1235 fd_bindings[fd]->startcluster = startcluster;
1237 void dircache_update_filetime(int fd)
1239 #if CONFIG_RTC == 0
1240 (void)fd;
1241 #else
1242 short year;
1243 struct tm *now = get_time();
1244 if (!dircache_initialized || fd < 0)
1245 return ;
1247 if (fd_bindings[fd] == NULL)
1249 logf("dircache fd access error");
1250 dircache_initialized = false;
1251 return ;
1253 year = now->tm_year+1900-1980;
1254 fd_bindings[fd]->info.wrtdate = (((year)&0x7f)<<9) |
1255 (((now->tm_mon+1)&0xf)<<5) |
1256 (((now->tm_mday)&0x1f));
1257 fd_bindings[fd]->info.wrttime = (((now->tm_hour)&0x1f)<<11) |
1258 (((now->tm_min)&0x3f)<<5) |
1259 (((now->tm_sec/2)&0x1f));
1260 #endif
1263 void dircache_mkdir(const char *path)
1264 { /* Test ok. */
1265 if (block_until_ready())
1266 return ;
1269 logf("mkdir: %s", path);
1270 dircache_new_entry(path, ATTR_DIRECTORY);
1273 void dircache_rmdir(const char *path)
1274 { /* Test ok. */
1275 struct dircache_entry *entry;
1277 if (block_until_ready())
1278 return ;
1280 logf("rmdir: %s", path);
1281 entry = dircache_get_entry(path, false);
1282 if (entry == NULL || entry->down == NULL)
1284 logf("not found or not a directory!");
1285 dircache_initialized = false;
1286 return ;
1289 entry->down = NULL;
1290 entry->d_name = NULL;
1293 /* Remove a file from cache */
1294 void dircache_remove(const char *name)
1295 { /* Test ok. */
1296 struct dircache_entry *entry;
1298 if (block_until_ready())
1299 return ;
1301 logf("remove: %s", name);
1303 entry = dircache_get_entry(name, false);
1305 if (entry == NULL)
1307 logf("not found!");
1308 dircache_initialized = false;
1309 return ;
1312 entry->d_name = NULL;
1315 void dircache_rename(const char *oldpath, const char *newpath)
1316 { /* Test ok. */
1317 struct dircache_entry *entry, *newentry;
1318 struct dircache_entry oldentry;
1319 char absolute_path[MAX_PATH*2];
1320 char *p;
1322 if (block_until_ready())
1323 return ;
1325 logf("rename: %s->%s", oldpath, newpath);
1327 entry = dircache_get_entry(oldpath, false);
1328 if (entry == NULL)
1330 logf("not found!");
1331 dircache_initialized = false;
1332 return ;
1335 /* Delete the old entry. */
1336 entry->d_name = NULL;
1338 /** If we rename the same filename twice in a row, we need to
1339 * save the data, because the entry will be re-used. */
1340 oldentry = *entry;
1342 /* Generate the absolute path for destination if necessary. */
1343 if (newpath[0] != '/')
1345 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1346 p = strrchr(absolute_path, '/');
1347 if (!p)
1349 logf("Invalid path");
1350 dircache_initialized = false;
1351 return ;
1354 *p = '\0';
1355 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1356 newpath = absolute_path;
1359 newentry = dircache_new_entry(newpath, entry->info.attribute);
1360 if (newentry == NULL)
1362 dircache_initialized = false;
1363 return ;
1366 newentry->down = oldentry.down;
1367 newentry->startcluster = oldentry.startcluster;
1368 newentry->info.size = oldentry.info.size;
1369 newentry->info.wrtdate = oldentry.info.wrtdate;
1370 newentry->info.wrttime = oldentry.info.wrttime;
1373 void dircache_add_file(const char *path, long startcluster)
1375 struct dircache_entry *entry;
1377 if (block_until_ready())
1378 return ;
1380 logf("add file: %s", path);
1381 entry = dircache_new_entry(path, 0);
1382 if (entry == NULL)
1383 return ;
1385 entry->startcluster = startcluster;
1388 static bool is_disable_msg_pending(void)
1390 return check_event_queue();
1393 DIR_CACHED* opendir_cached(const char* name)
1395 int dd;
1396 DIR_CACHED* pdir = opendirs;
1398 if ( name[0] != '/' )
1400 DEBUGF("Only absolute paths supported right now\n");
1401 return NULL;
1404 /* find a free dir descriptor */
1405 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1406 if ( !pdir->busy )
1407 break;
1409 if ( dd == MAX_OPEN_DIRS )
1411 DEBUGF("Too many dirs open\n");
1412 errno = EMFILE;
1413 return NULL;
1416 pdir->busy = true;
1418 if (!dircache_initialized || is_disable_msg_pending())
1420 pdir->internal_entry = -1;
1421 pdir->regulardir = opendir_uncached(name);
1423 else
1425 pdir->regulardir = NULL;
1426 pdir->internal_entry = dircache_get_entry_id_ex(name, true);
1427 pdir->theent.info.attribute = -1; /* used to make readdir_cached aware of the first call */
1430 if (pdir->internal_entry == -1 && pdir->regulardir == NULL)
1432 pdir->busy = false;
1433 return NULL;
1436 return pdir;
1439 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1441 struct dircache_entry *ce = get_entry(dir->internal_entry);
1442 struct dirent_uncached *regentry;
1444 if (!dir->busy)
1445 return NULL;
1447 if (dir->regulardir != NULL)
1449 regentry = readdir_uncached(dir->regulardir);
1450 if (regentry == NULL)
1451 return NULL;
1453 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1454 dir->theent.startcluster = regentry->startcluster;
1455 dir->theent.info = regentry->info;
1457 return &dir->theent;
1460 /* if theent.attribute=-1 then this is the first call */
1461 /* otherwise, this is is not so we first take the entry's ->next */
1462 /* NOTE: normal file can't have attribute=-1 */
1463 if(dir->theent.info.attribute != -1)
1464 ce = ce->next;
1465 /* skip unused entries */
1466 while(ce != NULL && ce->d_name == NULL)
1467 ce = ce->next;
1469 if (ce == NULL)
1470 return NULL;
1472 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1473 /* Can't do `dir->theent = *ce`
1474 because that modifies the d_name pointer. */
1475 dir->theent.startcluster = ce->startcluster;
1476 dir->theent.info = ce->info;
1477 dir->internal_entry = ce - dircache_root;
1479 //logf("-> %s", ce->d_name);
1480 return &dir->theent;
1483 int closedir_cached(DIR_CACHED* dir)
1485 if (!dir->busy)
1486 return -1;
1488 dir->busy=false;
1489 if (dir->regulardir != NULL)
1490 return closedir_uncached(dir->regulardir);
1492 return 0;
1495 int mkdir_cached(const char *name)
1497 int rc=mkdir_uncached(name);
1498 if (rc >= 0)
1499 dircache_mkdir(name);
1500 return(rc);
1503 int rmdir_cached(const char* name)
1505 int rc=rmdir_uncached(name);
1506 if(rc >= 0)
1507 dircache_rmdir(name);
1508 return(rc);