No need to have a variable for a thing computed by the preprocessor
[kugel-rb.git] / firmware / common / dircache.c
blobe642296a35abf97bfe25cf0af9903785ab9c258c
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 /**
86 * Internal function to allocate a new dircache_entry from memory.
88 static struct dircache_entry* allocate_entry(void)
90 struct dircache_entry *next_entry;
92 if (dircache_size > allocated_size - MAX_PATH*2)
94 logf("size limit reached");
95 return NULL;
98 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
99 #ifdef ROCKBOX_STRICT_ALIGN
100 /* Make sure the entry is long aligned. */
101 if ((long)next_entry & 0x03)
103 dircache_size += 4 - ((long)next_entry & 0x03);
104 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
106 #endif
107 next_entry->name_len = 0;
108 next_entry->d_name = NULL;
109 next_entry->up = NULL;
110 next_entry->down = NULL;
111 next_entry->next = NULL;
113 dircache_size += sizeof(struct dircache_entry);
115 return next_entry;
119 * Internal function to allocate a dircache_entry and set
120 * ->next entry pointers.
122 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
124 struct dircache_entry *next_entry;
126 if ( (next_entry = allocate_entry()) == NULL)
127 return NULL;
128 next_entry->up = ce->up;
129 ce->next = next_entry;
131 return next_entry;
135 * Internal function to allocate a dircache_entry and set
136 * ->down entry pointers.
138 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
140 struct dircache_entry *next_entry;
142 if ( (next_entry = allocate_entry()) == NULL)
143 return NULL;
144 next_entry->up = ce;
145 ce->down = next_entry;
147 return next_entry;
151 * Returns true if there is an event waiting in the queue
152 * that requires the current operation to be aborted.
154 static bool check_event_queue(void)
156 struct queue_event ev;
158 queue_wait_w_tmo(&dircache_queue, &ev, 0);
159 switch (ev.id)
161 case DIRCACHE_STOP:
162 case SYS_USB_CONNECTED:
163 #ifdef HAVE_HOTSWAP
164 case SYS_FS_CHANGED:
165 #endif
166 /* Put the event back into the queue. */
167 queue_post(&dircache_queue, ev.id, ev.data);
168 return true;
171 return false;
174 #ifndef SIMULATOR
175 /* scan and build static data (avoid redundancy on stack) */
176 static struct
178 #ifdef HAVE_MULTIVOLUME
179 int volume;
180 #endif
181 struct fat_dir *dir;
182 struct fat_direntry *direntry;
183 }sab;
185 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
187 /* normally, opendir expects a full fat_dir as parent but in our case,
188 * it's completely useless because we don't modify anything
189 * WARNING: this heavily relies on current FAT implementation ! */
191 /* those field are necessary to update the FAT entry in case of modification
192 here we don't touch anything so we put dummy values */
193 sab.dir->entry = 0;
194 sab.dir->entrycount = 0;
195 sab.dir->file.firstcluster = 0;
196 /* open directory */
197 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
198 if(rc < 0)
200 logf("fat_opendir failed: %d", rc);
201 return rc;
204 /* first pass : read dir */
205 struct dircache_entry *first_ce = ce;
207 /* read through directory */
208 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
210 if(!strcmp(".", sab.direntry->name) ||
211 !strcmp("..", sab.direntry->name))
212 continue;
214 ce->attribute = sab.direntry->attr;
215 ce->name_len = strlen(sab.direntry->name) + 1;
216 ce->d_name = ((char *)dircache_root + dircache_size);
217 ce->startcluster = sab.direntry->firstcluster;
218 ce->size = sab.direntry->filesize;
219 ce->wrtdate = sab.direntry->wrtdate;
220 ce->wrttime = sab.direntry->wrttime;
221 memcpy(ce->d_name, sab.direntry->name, ce->name_len);
223 dircache_size += ce->name_len;
224 entry_count++;
226 if(ce->attribute & FAT_ATTR_DIRECTORY)
227 dircache_gen_down(ce);
229 ce = dircache_gen_next(ce);
230 if(ce == NULL)
231 return -5;
233 /* When simulator is used, it's only safe to yield here. */
234 if(thread_enabled)
236 /* Stop if we got an external signal. */
237 if(check_event_queue())
238 return -6;
239 yield();
243 /* add "." and ".." */
244 ce->d_name = ".";
245 ce->name_len = 2;
246 ce->attribute = FAT_ATTR_DIRECTORY;
247 ce->startcluster = startcluster;
248 ce->size = 0;
249 ce->down = first_ce;
251 ce = dircache_gen_next(ce);
253 ce->d_name = "..";
254 ce->name_len = 3;
255 ce->attribute = FAT_ATTR_DIRECTORY;
256 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
257 ce->size = 0;
258 ce->down = first_ce->up;
260 /* second pass: recurse ! */
261 ce = first_ce;
263 while(rc >= 0 && ce)
265 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
266 rc = sab_process_dir(ce->startcluster, ce->down);
268 ce = ce->next;
271 return rc;
274 /* used during the generation */
275 static struct fat_dir sab_fat_dir;
277 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
279 memset(ce, 0, sizeof(struct dircache_entry));
281 #ifdef HAVE_MULTIVOLUME
282 if (volume > 0)
284 ce->d_name = ((char *)dircache_root+dircache_size);
285 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
286 ce->name_len = VOL_ENUM_POS + 3;
287 dircache_size += ce->name_len;
288 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
289 ce->size = 0;
290 append_position = dircache_gen_next(ce);
291 ce = dircache_gen_down(ce);
293 #endif
295 struct fat_direntry direntry; /* ditto */
296 #ifdef HAVE_MULTIVOLUME
297 sab.volume = volume;
298 #endif
299 sab.dir = &sab_fat_dir;
300 sab.direntry = &direntry;
302 return sab_process_dir(0, ce);
304 #else /* !SIMULATOR */
305 static char sab_path[MAX_PATH];
307 static int sab_process_dir(struct dircache_entry *ce)
309 struct dirent_uncached *entry;
310 struct dircache_entry *first_ce = ce;
311 DIR_UNCACHED *dir = opendir_uncached(sab_path);
312 if(dir == NULL)
314 logf("Failed to opendir_uncached(%s)", sab_path);
315 return -1;
318 while((entry = readdir_uncached(dir)))
320 if(!strcmp(".", entry->d_name) ||
321 !strcmp("..", entry->d_name))
322 continue;
324 ce->attribute = entry->attribute;
325 ce->name_len = strlen(entry->d_name) + 1;
326 ce->d_name = ((char *)dircache_root + dircache_size);
327 ce->size = entry->size;
328 ce->wrtdate = entry->wrtdate;
329 ce->wrttime = entry->wrttime;
330 memcpy(ce->d_name, entry->d_name, ce->name_len);
332 dircache_size += ce->name_len;
333 entry_count++;
335 if(entry->attribute & ATTR_DIRECTORY)
337 dircache_gen_down(ce);
338 if(ce->down == NULL)
340 closedir_uncached(dir);
341 return -1;
343 /* save current paths size */
344 int pathpos = strlen(sab_path);
345 /* append entry */
346 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
347 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
349 int rc = sab_process_dir(ce->down);
350 /* restore path */
351 sab_path[pathpos] = '\0';
353 if(rc < 0)
355 closedir_uncached(dir);
356 return rc;
360 ce = dircache_gen_next(ce);
361 if(ce == NULL)
362 return -5;
364 /* When simulator is used, it's only safe to yield here. */
365 if(thread_enabled)
367 /* Stop if we got an external signal. */
368 if(check_event_queue())
369 return -1;
370 yield();
374 /* add "." and ".." */
375 ce->d_name = ".";
376 ce->name_len = 2;
377 ce->attribute = ATTR_DIRECTORY;
378 ce->size = 0;
379 ce->down = first_ce;
381 ce = dircache_gen_next(ce);
383 ce->d_name = "..";
384 ce->name_len = 3;
385 ce->attribute = ATTR_DIRECTORY;
386 ce->size = 0;
387 ce->down = first_ce->up;
389 closedir_uncached(dir);
390 return 0;
393 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
395 #ifdef HAVE_MULTIVOLUME
396 (void) volume;
397 #endif
398 memset(ce, 0, sizeof(struct dircache_entry));
400 strlcpy(sab_path, "/", sizeof sab_path);
401 return sab_process_dir(ce);
403 #endif /* SIMULATOR */
406 * Internal function to get a pointer to dircache_entry for a given filename.
407 * path: Absolute path to a file or directory (see comment)
408 * go_down: Returns the first entry of the directory given by the path (see comment)
410 * As a a special case, accept path="" as an alias for "/".
411 * Also if the path omits the first '/', it will be accepted.
413 * * If get_down=true:
414 * If path="/", the returned entry is the first of root directory (ie dircache_root)
415 * Otherwise, if 'entry' is the returned value when get_down=false,
416 * the functions returns entry->down (which can be NULL)
418 * * If get_down=false:
419 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
420 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
421 * Which means that
422 * dircache_get_entry(path)->d_name == chunk_n
424 * If path="/", the returned entry is NULL.
425 * If the entry doesn't exist, return NULL
427 * NOTE: this functions silently handles double '/'
429 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
431 char namecopy[MAX_PATH];
432 char* part;
433 char* end;
435 bool at_root = true;
436 struct dircache_entry *cache_entry = dircache_root;
438 strlcpy(namecopy, path, sizeof(namecopy));
440 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
442 /* If request another chunk, the current entry has to be directory
443 * and so cache_entry->down has to be non-NULL/
444 * Special case of root because it's already the first entry of the root directory
446 * NOTE: this is safe even if cache_entry->down is NULL */
447 if(!at_root)
448 cache_entry = cache_entry->down;
449 else
450 at_root = false;
452 /* scan dir for name */
453 while(cache_entry != NULL)
455 /* skip unused entries */
456 if(cache_entry->name_len == 0)
458 cache_entry = cache_entry->next;
459 continue;
461 /* compare names */
462 if(!strcasecmp(part, cache_entry->d_name))
463 break;
464 /* go to next entry */
465 cache_entry = cache_entry->next;
468 /* handle not found case */
469 if(cache_entry == NULL)
470 return NULL;
473 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
474 if(go_down)
475 return at_root ? cache_entry : cache_entry->down;
476 else
477 return at_root ? NULL : cache_entry;
480 #ifdef HAVE_EEPROM_SETTINGS
482 * Function to load the internal cache structure from disk to initialize
483 * the dircache really fast and little disk access.
485 int dircache_load(void)
487 struct dircache_maindata maindata;
488 int bytes_read;
489 int fd;
491 if (dircache_initialized)
492 return -1;
494 logf("Loading directory cache");
495 dircache_size = 0;
497 fd = open(DIRCACHE_FILE, O_RDONLY);
498 if (fd < 0)
499 return -2;
501 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
502 if (bytes_read != sizeof(struct dircache_maindata)
503 || maindata.size <= 0)
505 logf("Dircache file header error");
506 close(fd);
507 remove(DIRCACHE_FILE);
508 return -3;
511 dircache_root = buffer_alloc(0);
512 if ((long)maindata.root_entry != (long)dircache_root)
514 logf("Position missmatch");
515 close(fd);
516 remove(DIRCACHE_FILE);
517 return -4;
520 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
521 entry_count = maindata.entry_count;
522 appflags = maindata.appflags;
523 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
524 close(fd);
525 remove(DIRCACHE_FILE);
527 if (bytes_read != maindata.size)
529 logf("Dircache read failed");
530 return -6;
533 /* Cache successfully loaded. */
534 dircache_size = maindata.size;
535 allocated_size = dircache_size + DIRCACHE_RESERVE;
536 reserve_used = 0;
537 logf("Done, %ld KiB used", dircache_size / 1024);
538 dircache_initialized = true;
539 memset(fd_bindings, 0, sizeof(fd_bindings));
541 return 0;
545 * Function to save the internal cache stucture to disk for fast loading
546 * on boot.
548 int dircache_save(void)
550 struct dircache_maindata maindata;
551 int fd;
552 unsigned long bytes_written;
554 remove(DIRCACHE_FILE);
556 if (!dircache_initialized)
557 return -1;
559 logf("Saving directory cache");
560 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666);
562 maindata.magic = DIRCACHE_MAGIC;
563 maindata.size = dircache_size;
564 maindata.root_entry = dircache_root;
565 maindata.entry_count = entry_count;
566 maindata.appflags = appflags;
568 /* Save the info structure */
569 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
570 if (bytes_written != sizeof(struct dircache_maindata))
572 close(fd);
573 logf("dircache: write failed #1");
574 return -2;
577 /* Dump whole directory cache to disk */
578 bytes_written = write(fd, dircache_root, dircache_size);
579 close(fd);
580 if (bytes_written != dircache_size)
582 logf("dircache: write failed #2");
583 return -3;
586 return 0;
588 #endif /* #if 0 */
591 * Internal function which scans the disk and creates the dircache structure.
593 static int dircache_do_rebuild(void)
595 unsigned int start_tick;
596 int i;
598 /* Measure how long it takes build the cache. */
599 start_tick = current_tick;
600 dircache_initializing = true;
601 appflags = 0;
602 entry_count = 0;
604 dircache_size = sizeof(struct dircache_entry);
606 #ifdef HAVE_MULTIVOLUME
607 append_position = dircache_root;
609 for (i = NUM_VOLUMES; i >= 0; i--)
611 if (fat_ismounted(i))
613 #endif
614 cpu_boost(true);
615 #ifdef HAVE_MULTIVOLUME
616 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
617 #else
618 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
619 #endif /* HAVE_MULTIVOLUME */
621 logf("dircache_scan_and_build failed");
622 cpu_boost(false);
623 dircache_size = 0;
624 dircache_initializing = false;
625 return -2;
627 cpu_boost(false);
628 #ifdef HAVE_MULTIVOLUME
631 #endif
633 logf("Done, %ld KiB used", dircache_size / 1024);
635 dircache_initialized = true;
636 dircache_initializing = false;
637 cache_build_ticks = current_tick - start_tick;
639 /* Initialized fd bindings. */
640 memset(fd_bindings, 0, sizeof(fd_bindings));
641 for (i = 0; i < fdbind_idx; i++)
642 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
643 fdbind_idx = 0;
645 if (thread_enabled)
647 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
648 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
650 else
652 /* We have to long align the audiobuf to keep the buffer access fast. */
653 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
654 audiobuf += DIRCACHE_RESERVE;
655 allocated_size = dircache_size + DIRCACHE_RESERVE;
656 reserve_used = 0;
659 return 1;
663 * Internal thread that controls transparent cache building.
665 static void dircache_thread(void)
667 struct queue_event ev;
669 while (1)
671 queue_wait(&dircache_queue, &ev);
673 switch (ev.id)
675 #ifdef HAVE_HOTSWAP
676 case SYS_FS_CHANGED:
677 if (!dircache_initialized)
678 break;
679 dircache_initialized = false;
680 #endif
681 case DIRCACHE_BUILD:
682 thread_enabled = true;
683 dircache_do_rebuild();
684 thread_enabled = false;
685 break ;
687 case DIRCACHE_STOP:
688 logf("Stopped the rebuilding.");
689 dircache_initialized = false;
690 break ;
692 #ifndef SIMULATOR
693 case SYS_USB_CONNECTED:
694 usb_acknowledge(SYS_USB_CONNECTED_ACK);
695 usb_wait_for_disconnect(&dircache_queue);
696 break ;
697 #endif
703 * Start scanning the disk to build the dircache.
704 * Either transparent or non-transparent build method is used.
706 int dircache_build(int last_size)
708 if (dircache_initialized || thread_enabled)
709 return -3;
711 logf("Building directory cache");
712 remove(DIRCACHE_FILE);
714 /* Background build, dircache has been previously allocated */
715 if (dircache_size > 0)
717 thread_enabled = true;
718 dircache_initializing = true;
719 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
720 return 2;
723 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
725 allocated_size = last_size + DIRCACHE_RESERVE;
726 dircache_root = buffer_alloc(allocated_size);
727 thread_enabled = true;
729 /* Start a transparent rebuild. */
730 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
731 return 3;
734 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
736 /* Start a non-transparent rebuild. */
737 return dircache_do_rebuild();
741 * Steal the allocated dircache buffer and disable dircache.
743 void* dircache_steal_buffer(long *size)
745 dircache_disable();
746 if (dircache_size == 0)
748 *size = 0;
749 return NULL;
752 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
754 return dircache_root;
758 * Main initialization function that must be called before any other
759 * operations within the dircache.
761 void dircache_init(void)
763 int i;
764 int thread_id;
766 dircache_initialized = false;
767 dircache_initializing = false;
769 memset(opendirs, 0, sizeof(opendirs));
770 for (i = 0; i < MAX_OPEN_DIRS; i++)
772 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
775 queue_init(&dircache_queue, true);
776 thread_id = create_thread(dircache_thread, dircache_stack,
777 sizeof(dircache_stack), 0, dircache_thread_name
778 IF_PRIO(, PRIORITY_BACKGROUND)
779 IF_COP(, CPU));
780 #ifdef HAVE_IO_PRIORITY
781 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
782 #endif
787 * Returns true if dircache has been initialized and is ready to be used.
789 bool dircache_is_enabled(void)
791 return dircache_initialized;
795 * Returns true if dircache is being initialized.
797 bool dircache_is_initializing(void)
799 return dircache_initializing || thread_enabled;
803 * Set application flags used to determine if dircache is still intact.
805 void dircache_set_appflag(long mask)
807 appflags |= mask;
811 * Get application flags used to determine if dircache is still intact.
813 bool dircache_get_appflag(long mask)
815 return dircache_is_enabled() && (appflags & mask);
819 * Returns the current number of entries (directories and files) in the cache.
821 int dircache_get_entry_count(void)
823 return entry_count;
827 * Returns the allocated space for dircache (without reserve space).
829 int dircache_get_cache_size(void)
831 return dircache_is_enabled() ? dircache_size : 0;
835 * Returns how many bytes of the reserve allocation for live cache
836 * updates have been used.
838 int dircache_get_reserve_used(void)
840 return dircache_is_enabled() ? reserve_used : 0;
844 * Returns the time in kernel ticks that took to build the cache.
846 int dircache_get_build_ticks(void)
848 return dircache_is_enabled() ? cache_build_ticks : 0;
852 * Disables the dircache. Usually called on shutdown or when
853 * accepting a usb connection.
855 void dircache_disable(void)
857 int i;
858 bool cache_in_use;
860 if (thread_enabled)
861 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
863 while (thread_enabled)
864 sleep(1);
865 dircache_initialized = false;
867 logf("Waiting for cached dirs to release");
868 do {
869 cache_in_use = false;
870 for (i = 0; i < MAX_OPEN_DIRS; i++) {
871 if (!opendirs[i].regulardir && opendirs[i].busy)
873 cache_in_use = true;
874 sleep(1);
875 break ;
878 } while (cache_in_use) ;
880 logf("Cache released");
881 entry_count = 0;
885 * Usermode function to return dircache_entry pointer to the given path.
887 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
889 if (!dircache_initialized || filename == NULL)
890 return NULL;
892 return dircache_get_entry(filename, false);
896 * Function to copy the full absolute path from dircache to the given buffer
897 * using the given dircache_entry pointer.
899 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
901 int path_size = 0;
902 int idx;
903 const struct dircache_entry *temp = entry;
905 if (size <= 0)
906 return ;
908 /* first compute the necessary size */
909 while(temp != NULL)
911 path_size += temp->name_len; /* '/' + d_name */
912 temp = temp->up;
915 /* now copy the path */
916 /* doesn't matter with trailing 0 because it's added later */
917 idx = path_size;
918 while(entry != NULL)
920 idx -= entry->name_len;
921 /* available size */
922 int rem = size - idx;
924 if(rem >= 1)
926 buf[idx] = '/';
927 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
929 entry = entry->up;
932 /* add trailing 0 */
933 buf[MIN(path_size, size-1)] = 0;
936 /* --- Directory cache live updating functions --- */
937 static int block_until_ready(void)
939 /* Block until dircache has been built. */
940 while (!dircache_initialized && dircache_is_initializing())
941 sleep(1);
943 if (!dircache_initialized)
944 return -1;
946 return 0;
949 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
951 struct dircache_entry *entry;
952 char basedir[MAX_PATH*2];
953 char *new;
954 long last_cache_size = dircache_size;
956 strlcpy(basedir, path, sizeof(basedir));
957 new = strrchr(basedir, '/');
958 if (new == NULL)
960 logf("error occurred");
961 dircache_initialized = false;
962 return NULL;
965 *new = '\0';
966 new++;
968 entry = dircache_get_entry(basedir, true);
969 if (entry == NULL)
971 logf("basedir not found!");
972 logf("%s", basedir);
973 dircache_initialized = false;
974 return NULL;
977 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
978 >= DIRCACHE_RESERVE)
980 logf("not enough space");
981 dircache_initialized = false;
982 return NULL;
985 while (entry->next != NULL)
986 entry = entry->next;
988 if (entry->name_len > 0)
989 entry = dircache_gen_next(entry);
991 if (entry == NULL)
993 dircache_initialized = false;
994 return NULL;
997 entry->attribute = attribute;
998 entry->name_len = MIN(254, strlen(new)) + 1;
999 entry->d_name = ((char *)dircache_root+dircache_size);
1000 entry->startcluster = 0;
1001 entry->wrtdate = 0;
1002 entry->wrttime = 0;
1003 entry->size = 0;
1004 memcpy(entry->d_name, new, entry->name_len);
1005 dircache_size += entry->name_len;
1007 if (attribute & ATTR_DIRECTORY)
1009 logf("gen_down");
1010 dircache_gen_down(entry);
1013 reserve_used += dircache_size - last_cache_size;
1015 return entry;
1018 void dircache_bind(int fd, const char *path)
1020 struct dircache_entry *entry;
1022 /* Queue requests until dircache has been built. */
1023 if (!dircache_initialized && dircache_is_initializing())
1025 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1026 return ;
1027 strlcpy(fdbind_cache[fdbind_idx].path, path,
1028 sizeof(fdbind_cache[fdbind_idx].path));
1029 fdbind_cache[fdbind_idx].fd = fd;
1030 fdbind_idx++;
1031 return ;
1034 if (!dircache_initialized)
1035 return ;
1037 logf("bind: %d/%s", fd, path);
1038 entry = dircache_get_entry(path, false);
1039 if (entry == NULL)
1041 logf("not found!");
1042 dircache_initialized = false;
1043 return ;
1046 fd_bindings[fd] = entry;
1049 void dircache_update_filesize(int fd, long newsize, long startcluster)
1051 if (!dircache_initialized || fd < 0)
1052 return ;
1054 if (fd_bindings[fd] == NULL)
1056 logf("dircache fd(%d) access error", fd);
1057 dircache_initialized = false;
1058 return ;
1061 fd_bindings[fd]->size = newsize;
1062 fd_bindings[fd]->startcluster = startcluster;
1064 void dircache_update_filetime(int fd)
1066 #if CONFIG_RTC == 0
1067 (void)fd;
1068 #else
1069 short year;
1070 struct tm *now = get_time();
1071 if (!dircache_initialized || fd < 0)
1072 return ;
1074 if (fd_bindings[fd] == NULL)
1076 logf("dircache fd access error");
1077 dircache_initialized = false;
1078 return ;
1080 year = now->tm_year+1900-1980;
1081 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1082 (((now->tm_mon+1)&0xf)<<5) |
1083 (((now->tm_mday)&0x1f));
1084 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1085 (((now->tm_min)&0x3f)<<5) |
1086 (((now->tm_sec/2)&0x1f));
1087 #endif
1090 void dircache_mkdir(const char *path)
1091 { /* Test ok. */
1092 if (block_until_ready())
1093 return ;
1095 logf("mkdir: %s", path);
1096 dircache_new_entry(path, ATTR_DIRECTORY);
1099 void dircache_rmdir(const char *path)
1100 { /* Test ok. */
1101 struct dircache_entry *entry;
1103 if (block_until_ready())
1104 return ;
1106 logf("rmdir: %s", path);
1107 entry = dircache_get_entry(path, false);
1108 if (entry == NULL || entry->down == NULL)
1110 logf("not found or not a directory!");
1111 dircache_initialized = false;
1112 return ;
1115 entry->down = NULL;
1116 entry->name_len = 0;
1119 /* Remove a file from cache */
1120 void dircache_remove(const char *name)
1121 { /* Test ok. */
1122 struct dircache_entry *entry;
1124 if (block_until_ready())
1125 return ;
1127 logf("remove: %s", name);
1129 entry = dircache_get_entry(name, false);
1131 if (entry == NULL)
1133 logf("not found!");
1134 dircache_initialized = false;
1135 return ;
1138 entry->name_len = 0;
1141 void dircache_rename(const char *oldpath, const char *newpath)
1142 { /* Test ok. */
1143 struct dircache_entry *entry, *newentry;
1144 struct dircache_entry oldentry;
1145 char absolute_path[MAX_PATH*2];
1146 char *p;
1148 if (block_until_ready())
1149 return ;
1151 logf("rename: %s->%s", oldpath, newpath);
1153 entry = dircache_get_entry(oldpath, false);
1154 if (entry == NULL)
1156 logf("not found!");
1157 dircache_initialized = false;
1158 return ;
1161 /* Delete the old entry. */
1162 entry->name_len = 0;
1164 /** If we rename the same filename twice in a row, we need to
1165 * save the data, because the entry will be re-used. */
1166 oldentry = *entry;
1168 /* Generate the absolute path for destination if necessary. */
1169 if (newpath[0] != '/')
1171 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1172 p = strrchr(absolute_path, '/');
1173 if (!p)
1175 logf("Invalid path");
1176 dircache_initialized = false;
1177 return ;
1180 *p = '\0';
1181 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1182 newpath = absolute_path;
1185 newentry = dircache_new_entry(newpath, entry->attribute);
1186 if (newentry == NULL)
1188 dircache_initialized = false;
1189 return ;
1192 newentry->down = oldentry.down;
1193 newentry->size = oldentry.size;
1194 newentry->startcluster = oldentry.startcluster;
1195 newentry->wrttime = oldentry.wrttime;
1196 newentry->wrtdate = oldentry.wrtdate;
1199 void dircache_add_file(const char *path, long startcluster)
1201 struct dircache_entry *entry;
1203 if (block_until_ready())
1204 return ;
1206 logf("add file: %s", path);
1207 entry = dircache_new_entry(path, 0);
1208 if (entry == NULL)
1209 return ;
1211 entry->startcluster = startcluster;
1214 DIR_CACHED* opendir_cached(const char* name)
1216 int dd;
1217 DIR_CACHED* pdir = opendirs;
1219 if ( name[0] != '/' )
1221 DEBUGF("Only absolute paths supported right now\n");
1222 return NULL;
1225 /* find a free dir descriptor */
1226 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1227 if ( !pdir->busy )
1228 break;
1230 if ( dd == MAX_OPEN_DIRS )
1232 DEBUGF("Too many dirs open\n");
1233 errno = EMFILE;
1234 return NULL;
1237 pdir->busy = true;
1239 if (!dircache_initialized)
1241 pdir->internal_entry = NULL;
1242 pdir->regulardir = opendir_uncached(name);
1244 else
1246 pdir->regulardir = NULL;
1247 pdir->internal_entry = dircache_get_entry(name, true);
1248 pdir->theent.attribute = -1; /* used to make readdir_cached aware of the first call */
1251 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1253 pdir->busy = false;
1254 return NULL;
1257 return pdir;
1260 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1262 struct dircache_entry *ce = dir->internal_entry;
1263 struct dirent_uncached *regentry;
1265 if (!dir->busy)
1266 return NULL;
1268 if (dir->regulardir != NULL)
1270 regentry = readdir_uncached(dir->regulardir);
1271 if (regentry == NULL)
1272 return NULL;
1274 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1275 dir->theent.size = regentry->size;
1276 dir->theent.startcluster = regentry->startcluster;
1277 dir->theent.attribute = regentry->attribute;
1278 dir->theent.wrttime = regentry->wrttime;
1279 dir->theent.wrtdate = regentry->wrtdate;
1281 return &dir->theent;
1284 /* if theent.attribute=-1 then this is the first call */
1285 /* otherwise, this is is not so we first take the entry's ->next */
1286 /* NOTE: normal file can't have attribute=-1 */
1287 if(dir->theent.attribute != -1)
1288 ce = ce->next;
1289 /* skip unused entries */
1290 while(ce != NULL && ce->name_len == 0)
1291 ce = ce->next;
1293 if (ce == NULL)
1294 return NULL;
1296 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1297 /* Can't do `dir->theent = *ce`
1298 because that modifies the d_name pointer. */
1299 dir->theent.size = ce->size;
1300 dir->theent.startcluster = ce->startcluster;
1301 dir->theent.attribute = ce->attribute;
1302 dir->theent.wrttime = ce->wrttime;
1303 dir->theent.wrtdate = ce->wrtdate;
1304 dir->internal_entry = ce;
1306 //logf("-> %s", ce->name);
1307 return &dir->theent;
1310 int closedir_cached(DIR_CACHED* dir)
1312 if (!dir->busy)
1313 return -1;
1315 dir->busy=false;
1316 if (dir->regulardir != NULL)
1317 return closedir_uncached(dir->regulardir);
1319 return 0;
1322 int mkdir_cached(const char *name)
1324 int rc=mkdir_uncached(name);
1325 if (rc >= 0)
1326 dircache_mkdir(name);
1327 return(rc);
1330 int rmdir_cached(const char* name)
1332 int rc=rmdir_uncached(name);
1333 if(rc >= 0)
1334 dircache_rmdir(name);
1335 return(rc);