Rewrite dircache generation to take advantage for the FAT code. Reduce RAM usage...
[kugel-rb.git] / firmware / common / dircache.c
blobe6107801c3ab693dd61979efe5c5fcca1f14d9b7
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.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 #if CONFIG_RTC
44 #include "time.h"
45 #include "timefuncs.h"
46 #endif
48 /* Queue commands. */
49 #define DIRCACHE_BUILD 1
50 #define DIRCACHE_STOP 2
52 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
53 #define MAX_OPEN_DIRS 12
54 #else
55 #define MAX_OPEN_DIRS 8
56 #endif
57 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
59 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
60 static struct dircache_entry *dircache_root;
61 #ifdef HAVE_MULTIVOLUME
62 static struct dircache_entry *append_position;
63 #endif
65 static bool dircache_initialized = false;
66 static bool dircache_initializing = false;
67 static bool thread_enabled = false;
68 static unsigned long allocated_size = DIRCACHE_LIMIT;
69 static unsigned long dircache_size = 0;
70 static unsigned long entry_count = 0;
71 static unsigned long reserve_used = 0;
72 static unsigned int cache_build_ticks = 0;
73 static unsigned long appflags = 0;
75 static struct event_queue dircache_queue;
76 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
77 static const char dircache_thread_name[] = "dircache";
79 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
80 static int fdbind_idx = 0;
82 /* --- Internal cache structure control functions --- */
84 /**
85 * Internal function to allocate a new dircache_entry from memory.
87 static struct dircache_entry* allocate_entry(void)
89 struct dircache_entry *next_entry;
91 if (dircache_size > allocated_size - MAX_PATH*2)
93 logf("size limit reached");
94 return NULL;
97 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
98 #ifdef ROCKBOX_STRICT_ALIGN
99 /* Make sure the entry is long aligned. */
100 if ((long)next_entry & 0x03)
102 dircache_size += 4 - ((long)next_entry & 0x03);
103 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
105 #endif
106 next_entry->name_len = 0;
107 next_entry->d_name = NULL;
108 next_entry->up = NULL;
109 next_entry->down = NULL;
110 next_entry->next = NULL;
112 dircache_size += sizeof(struct dircache_entry);
114 return next_entry;
118 * Internal function to allocate a dircache_entry and set
119 * ->next entry pointers.
121 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
123 struct dircache_entry *next_entry;
125 if ( (next_entry = allocate_entry()) == NULL)
126 return NULL;
127 next_entry->up = ce->up;
128 ce->next = next_entry;
130 return next_entry;
134 * Internal function to allocate a dircache_entry and set
135 * ->down entry pointers.
137 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
139 struct dircache_entry *next_entry;
141 if ( (next_entry = allocate_entry()) == NULL)
142 return NULL;
143 next_entry->up = ce;
144 ce->down = next_entry;
146 return next_entry;
150 * Returns true if there is an event waiting in the queue
151 * that requires the current operation to be aborted.
153 static bool check_event_queue(void)
155 struct queue_event ev;
157 queue_wait_w_tmo(&dircache_queue, &ev, 0);
158 switch (ev.id)
160 case DIRCACHE_STOP:
161 case SYS_USB_CONNECTED:
162 #ifdef HAVE_HOTSWAP
163 case SYS_FS_CHANGED:
164 #endif
165 /* Put the event back into the queue. */
166 queue_post(&dircache_queue, ev.id, ev.data);
167 return true;
170 return false;
173 #ifndef SIMULATOR
174 /* scan and build static data (avoid redundancy on stack) */
175 static struct
177 #ifdef HAVE_MULTIVOLUME
178 int volume;
179 #endif
180 struct fat_dir *dir;
181 struct fat_direntry *direntry;
182 }sab;
184 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
186 /* normally, opendir expects a full fat_dir as parent but in our case,
187 * it's completely useless because we don't modify anything
188 * WARNING: this heavily relies on current FAT implementation ! */
190 /* those field are necessary to update the FAT entry in case of modification
191 here we don't touch anything so we put dummy values */
192 sab.dir->entry = 0;
193 sab.dir->entrycount = 0;
194 sab.dir->file.firstcluster = 0;
195 /* open directory */
196 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
197 if(rc < 0)
199 logf("fat_opendir failed: %d", rc);
200 return rc;
203 /* first pass : read dir */
204 struct dircache_entry *first_ce = ce;
206 /* read through directory */
207 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
209 if(!strcmp(".", sab.direntry->name) ||
210 !strcmp("..", sab.direntry->name))
211 continue;
213 ce->attribute = sab.direntry->attr;
214 ce->name_len = strlen(sab.direntry->name) + 1;
215 ce->d_name = ((char *)dircache_root + dircache_size);
216 ce->startcluster = sab.direntry->firstcluster;
217 ce->size = sab.direntry->filesize;
218 ce->wrtdate = sab.direntry->wrtdate;
219 ce->wrttime = sab.direntry->wrttime;
220 memcpy(ce->d_name, sab.direntry->name, ce->name_len);
222 dircache_size += ce->name_len;
223 entry_count++;
225 if(ce->attribute & FAT_ATTR_DIRECTORY)
226 dircache_gen_down(ce);
228 ce = dircache_gen_next(ce);
229 if(ce == NULL)
230 return -5;
232 /* When simulator is used, it's only safe to yield here. */
233 if(thread_enabled)
235 /* Stop if we got an external signal. */
236 if(check_event_queue())
237 return -6;
238 yield();
242 /* add "." and ".." */
243 ce->d_name = ".";
244 ce->name_len = 2;
245 ce->attribute = FAT_ATTR_DIRECTORY;
246 ce->startcluster = startcluster;
247 ce->size = 0;
248 ce->down = first_ce;
250 ce = dircache_gen_next(ce);
252 ce->d_name = "..";
253 ce->name_len = 3;
254 ce->attribute = FAT_ATTR_DIRECTORY;
255 ce->startcluster = first_ce->up->startcluster;
256 ce->size = 0;
257 ce->down = first_ce->up;
259 /* second pass: recurse ! */
260 ce = first_ce;
262 while(ce)
264 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
265 sab_process_dir(ce->startcluster, ce->down);
267 ce = ce->next;
270 return rc;
273 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
275 memset(ce, 0, sizeof(struct dircache_entry));
277 #ifdef HAVE_MULTIVOLUME
278 if (volume > 0)
280 ce->d_name = ((char *)dircache_root+dircache_size);
281 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
282 ce->name_len = VOL_ENUM_POS + 3;
283 dircache_size += ce->name_len;
284 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
285 ce->size = 0;
286 append_position = dircache_gen_next(ce);
287 ce = dircache_gen_down(ce);
289 #endif
291 struct fat_dir dir; /* allocate on stack once for all scan */
292 struct fat_direntry direntry; /* ditto */
293 #ifdef HAVE_MULTIVOLUME
294 sab.volume = volume;
295 #endif
296 sab.dir = &dir;
297 sab.direntry = &direntry;
299 return sab_process_dir(0, ce);
301 #else /* !SIMULATOR */
302 static char sab_path[MAX_PATH];
304 static int sab_process_dir(struct dircache_entry *ce)
306 struct dirent_uncached *entry;
307 struct dircache_entry *first_ce = ce;
308 DIR_UNCACHED *dir = opendir_uncached(sab_path);
309 if(dir == NULL)
311 logf("Failed to opendir_uncached(%s)", sab_path);
312 return -1;
315 while((entry = readdir_uncached(dir)))
317 if(!strcmp(".", entry->d_name) ||
318 !strcmp("..", entry->d_name))
319 continue;
321 ce->attribute = entry->attribute;
322 ce->name_len = strlen(entry->d_name) + 1;
323 ce->d_name = ((char *)dircache_root + dircache_size);
324 ce->size = entry->size;
325 ce->wrtdate = entry->wrtdate;
326 ce->wrttime = entry->wrttime;
327 memcpy(ce->d_name, entry->d_name, ce->name_len);
329 dircache_size += ce->name_len;
330 entry_count++;
332 if(entry->attribute & ATTR_DIRECTORY)
334 dircache_gen_down(ce);
335 if(ce->down == NULL)
337 closedir_uncached(dir);
338 return -1;
340 /* save current paths size */
341 int pathpos = strlen(sab_path);
342 /* append entry */
343 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
344 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
346 int rc = sab_process_dir(ce->down);
347 /* restore path */
348 sab_path[pathpos] = '\0';
350 if(rc < 0)
352 closedir_uncached(dir);
353 return rc;
357 ce = dircache_gen_next(ce);
358 if(ce == NULL)
359 return -5;
361 /* When simulator is used, it's only safe to yield here. */
362 if(thread_enabled)
364 /* Stop if we got an external signal. */
365 if(check_event_queue())
366 return -1;
367 yield();
371 /* add "." and ".." */
372 ce->d_name = ".";
373 ce->name_len = 2;
374 ce->attribute = ATTR_DIRECTORY;
375 ce->size = 0;
376 ce->down = first_ce;
378 ce = dircache_gen_next(ce);
380 ce->d_name = "..";
381 ce->name_len = 3;
382 ce->attribute = ATTR_DIRECTORY;
383 ce->size = 0;
384 ce->down = first_ce->up;
386 closedir_uncached(dir);
387 return 0;
390 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
392 #ifdef HAVE_MULTIVOLUME
393 (void) volume;
394 #endif
395 memset(ce, 0, sizeof(struct dircache_entry));
397 strlcpy(sab_path, "/", sizeof sab_path);
398 return sab_process_dir(ce);
400 #endif /* SIMULATOR */
403 * Internal function to get a pointer to dircache_entry for a given filename.
404 * path: Absolute path to a file or directory (see comment)
405 * go_down: Returns the first entry of the directory given by the path (see comment)
407 * As a a special case, accept path="" as an alias for "/".
408 * Also if the path omits the first '/', it will be accepted.
410 * * If get_down=true:
411 * If path="/", the returned entry is the first of root directory (ie dircache_root)
412 * Otherwise, if 'entry' is the returned value when get_down=false,
413 * the functions returns entry->down (which can be NULL)
415 * * If get_down=false:
416 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
417 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
418 * Which means that
419 * dircache_get_entry(path)->d_name == chunk_n
421 * If path="/", the returned entry is NULL.
422 * If the entry doesn't exist, return NULL
424 * NOTE: this functions silently handles double '/'
426 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
428 char namecopy[MAX_PATH];
429 char* part;
430 char* end;
432 bool at_root = true;
433 struct dircache_entry *cache_entry = dircache_root;
435 strlcpy(namecopy, path, sizeof(namecopy));
437 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
439 /* If request another chunk, the current entry has to be directory
440 * and so cache_entry->down has to be non-NULL/
441 * Special case of root because it's already the first entry of the root directory
443 * NOTE: this is safe even if cache_entry->down is NULL */
444 if(!at_root)
445 cache_entry = cache_entry->down;
446 else
447 at_root = false;
449 /* scan dir for name */
450 while(cache_entry != NULL)
452 /* skip unused entries */
453 if(cache_entry->name_len == 0)
455 cache_entry = cache_entry->next;
456 continue;
458 /* compare names */
459 if(!strcasecmp(part, cache_entry->d_name))
460 break;
461 /* go to next entry */
462 cache_entry = cache_entry->next;
465 /* handle not found case */
466 if(cache_entry == NULL)
467 return NULL;
470 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
471 if(go_down)
472 return at_root ? cache_entry : cache_entry->down;
473 else
474 return at_root ? NULL : cache_entry;
477 #ifdef HAVE_EEPROM_SETTINGS
479 * Function to load the internal cache structure from disk to initialize
480 * the dircache really fast and little disk access.
482 int dircache_load(void)
484 struct dircache_maindata maindata;
485 int bytes_read;
486 int fd;
488 if (dircache_initialized)
489 return -1;
491 logf("Loading directory cache");
492 dircache_size = 0;
494 fd = open(DIRCACHE_FILE, O_RDONLY);
495 if (fd < 0)
496 return -2;
498 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
499 if (bytes_read != sizeof(struct dircache_maindata)
500 || maindata.size <= 0)
502 logf("Dircache file header error");
503 close(fd);
504 remove(DIRCACHE_FILE);
505 return -3;
508 dircache_root = buffer_alloc(0);
509 if ((long)maindata.root_entry != (long)dircache_root)
511 logf("Position missmatch");
512 close(fd);
513 remove(DIRCACHE_FILE);
514 return -4;
517 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
518 entry_count = maindata.entry_count;
519 appflags = maindata.appflags;
520 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
521 close(fd);
522 remove(DIRCACHE_FILE);
524 if (bytes_read != maindata.size)
526 logf("Dircache read failed");
527 return -6;
530 /* Cache successfully loaded. */
531 dircache_size = maindata.size;
532 allocated_size = dircache_size + DIRCACHE_RESERVE;
533 reserve_used = 0;
534 logf("Done, %ld KiB used", dircache_size / 1024);
535 dircache_initialized = true;
536 memset(fd_bindings, 0, sizeof(fd_bindings));
538 return 0;
542 * Function to save the internal cache stucture to disk for fast loading
543 * on boot.
545 int dircache_save(void)
547 struct dircache_maindata maindata;
548 int fd;
549 unsigned long bytes_written;
551 remove(DIRCACHE_FILE);
553 if (!dircache_initialized)
554 return -1;
556 logf("Saving directory cache");
557 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
559 maindata.magic = DIRCACHE_MAGIC;
560 maindata.size = dircache_size;
561 maindata.root_entry = dircache_root;
562 maindata.entry_count = entry_count;
563 maindata.appflags = appflags;
565 /* Save the info structure */
566 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
567 if (bytes_written != sizeof(struct dircache_maindata))
569 close(fd);
570 logf("dircache: write failed #1");
571 return -2;
574 /* Dump whole directory cache to disk */
575 bytes_written = write(fd, dircache_root, dircache_size);
576 close(fd);
577 if (bytes_written != dircache_size)
579 logf("dircache: write failed #2");
580 return -3;
583 return 0;
585 #endif /* #if 0 */
588 * Internal function which scans the disk and creates the dircache structure.
590 static int dircache_do_rebuild(void)
592 unsigned int start_tick;
593 int i;
595 /* Measure how long it takes build the cache. */
596 start_tick = current_tick;
597 dircache_initializing = true;
598 appflags = 0;
599 entry_count = 0;
601 dircache_size = sizeof(struct dircache_entry);
603 #ifdef HAVE_MULTIVOLUME
604 append_position = dircache_root;
606 for (i = NUM_VOLUMES; i >= 0; i--)
608 if (fat_ismounted(i))
610 #endif
611 cpu_boost(true);
612 #ifdef HAVE_MULTIVOLUME
613 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
614 #else
615 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
616 #endif /* HAVE_MULTIVOLUME */
618 logf("dircache_travel failed");
619 cpu_boost(false);
620 dircache_size = 0;
621 dircache_initializing = false;
622 return -2;
624 cpu_boost(false);
625 #ifdef HAVE_MULTIVOLUME
628 #endif
630 logf("Done, %ld KiB used", dircache_size / 1024);
632 dircache_initialized = true;
633 dircache_initializing = false;
634 cache_build_ticks = current_tick - start_tick;
636 /* Initialized fd bindings. */
637 memset(fd_bindings, 0, sizeof(fd_bindings));
638 for (i = 0; i < fdbind_idx; i++)
639 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
640 fdbind_idx = 0;
642 if (thread_enabled)
644 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
645 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
647 else
649 /* We have to long align the audiobuf to keep the buffer access fast. */
650 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
651 audiobuf += DIRCACHE_RESERVE;
652 allocated_size = dircache_size + DIRCACHE_RESERVE;
653 reserve_used = 0;
656 return 1;
660 * Internal thread that controls transparent cache building.
662 static void dircache_thread(void)
664 struct queue_event ev;
666 while (1)
668 queue_wait(&dircache_queue, &ev);
670 switch (ev.id)
672 #ifdef HAVE_HOTSWAP
673 case SYS_FS_CHANGED:
674 if (!dircache_initialized)
675 break;
676 dircache_initialized = false;
677 #endif
678 case DIRCACHE_BUILD:
679 thread_enabled = true;
680 dircache_do_rebuild();
681 thread_enabled = false;
682 break ;
684 case DIRCACHE_STOP:
685 logf("Stopped the rebuilding.");
686 dircache_initialized = false;
687 break ;
689 #ifndef SIMULATOR
690 case SYS_USB_CONNECTED:
691 usb_acknowledge(SYS_USB_CONNECTED_ACK);
692 usb_wait_for_disconnect(&dircache_queue);
693 break ;
694 #endif
700 * Start scanning the disk to build the dircache.
701 * Either transparent or non-transparent build method is used.
703 int dircache_build(int last_size)
705 if (dircache_initialized || thread_enabled)
706 return -3;
708 logf("Building directory cache");
709 remove(DIRCACHE_FILE);
711 /* Background build, dircache has been previously allocated */
712 if (dircache_size > 0)
714 thread_enabled = true;
715 dircache_initializing = true;
716 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
717 return 2;
720 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
722 allocated_size = last_size + DIRCACHE_RESERVE;
723 dircache_root = buffer_alloc(allocated_size);
724 thread_enabled = true;
726 /* Start a transparent rebuild. */
727 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
728 return 3;
731 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
733 /* Start a non-transparent rebuild. */
734 return dircache_do_rebuild();
738 * Steal the allocated dircache buffer and disable dircache.
740 void* dircache_steal_buffer(long *size)
742 dircache_disable();
743 if (dircache_size == 0)
745 *size = 0;
746 return NULL;
749 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
751 return dircache_root;
755 * Main initialization function that must be called before any other
756 * operations within the dircache.
758 void dircache_init(void)
760 int i;
762 dircache_initialized = false;
763 dircache_initializing = false;
765 memset(opendirs, 0, sizeof(opendirs));
766 for (i = 0; i < MAX_OPEN_DIRS; i++)
768 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
771 queue_init(&dircache_queue, true);
772 create_thread(dircache_thread, dircache_stack,
773 sizeof(dircache_stack), 0, dircache_thread_name
774 IF_PRIO(, PRIORITY_BACKGROUND)
775 IF_COP(, CPU));
779 * Returns true if dircache has been initialized and is ready to be used.
781 bool dircache_is_enabled(void)
783 return dircache_initialized;
787 * Returns true if dircache is being initialized.
789 bool dircache_is_initializing(void)
791 return dircache_initializing || thread_enabled;
795 * Set application flags used to determine if dircache is still intact.
797 void dircache_set_appflag(long mask)
799 appflags |= mask;
803 * Get application flags used to determine if dircache is still intact.
805 bool dircache_get_appflag(long mask)
807 return dircache_is_enabled() && (appflags & mask);
811 * Returns the current number of entries (directories and files) in the cache.
813 int dircache_get_entry_count(void)
815 return entry_count;
819 * Returns the allocated space for dircache (without reserve space).
821 int dircache_get_cache_size(void)
823 return dircache_is_enabled() ? dircache_size : 0;
827 * Returns how many bytes of the reserve allocation for live cache
828 * updates have been used.
830 int dircache_get_reserve_used(void)
832 return dircache_is_enabled() ? reserve_used : 0;
836 * Returns the time in kernel ticks that took to build the cache.
838 int dircache_get_build_ticks(void)
840 return dircache_is_enabled() ? cache_build_ticks : 0;
844 * Disables the dircache. Usually called on shutdown or when
845 * accepting a usb connection.
847 void dircache_disable(void)
849 int i;
850 bool cache_in_use;
852 if (thread_enabled)
853 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
855 while (thread_enabled)
856 sleep(1);
857 dircache_initialized = false;
859 logf("Waiting for cached dirs to release");
860 do {
861 cache_in_use = false;
862 for (i = 0; i < MAX_OPEN_DIRS; i++) {
863 if (!opendirs[i].regulardir && opendirs[i].busy)
865 cache_in_use = true;
866 sleep(1);
867 break ;
870 } while (cache_in_use) ;
872 logf("Cache released");
873 entry_count = 0;
877 * Usermode function to return dircache_entry pointer to the given path.
879 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
881 if (!dircache_initialized || filename == NULL)
882 return NULL;
884 return dircache_get_entry(filename, false);
888 * Function to copy the full absolute path from dircache to the given buffer
889 * using the given dircache_entry pointer.
891 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
893 int path_size = 0;
894 int idx;
895 const struct dircache_entry *temp = entry;
897 if (size <= 0)
898 return ;
900 /* first compute the necessary size */
901 while(temp != NULL)
903 path_size += temp->name_len; /* '/' + d_name */
904 temp = temp->up;
907 /* now copy the path */
908 /* doesn't matter with trailing 0 because it's added later */
909 idx = path_size;
910 while(entry != NULL)
912 idx -= entry->name_len;
913 /* available size */
914 int rem = size - idx;
916 if(rem >= 1)
918 buf[idx] = '/';
919 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
921 entry = entry->up;
924 /* add trailing 0 */
925 buf[MIN(path_size, size-1)] = 0;
928 /* --- Directory cache live updating functions --- */
929 static int block_until_ready(void)
931 /* Block until dircache has been built. */
932 while (!dircache_initialized && dircache_is_initializing())
933 sleep(1);
935 if (!dircache_initialized)
936 return -1;
938 return 0;
941 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
943 struct dircache_entry *entry;
944 char basedir[MAX_PATH*2];
945 char *new;
946 long last_cache_size = dircache_size;
948 strlcpy(basedir, path, sizeof(basedir));
949 new = strrchr(basedir, '/');
950 if (new == NULL)
952 logf("error occurred");
953 dircache_initialized = false;
954 return NULL;
957 *new = '\0';
958 new++;
960 entry = dircache_get_entry(basedir, true);
961 if (entry == NULL)
963 logf("basedir not found!");
964 logf("%s", basedir);
965 dircache_initialized = false;
966 return NULL;
969 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
970 >= DIRCACHE_RESERVE)
972 logf("not enough space");
973 dircache_initialized = false;
974 return NULL;
977 while (entry->next != NULL)
978 entry = entry->next;
980 if (entry->name_len > 0)
981 entry = dircache_gen_next(entry);
983 if (entry == NULL)
985 dircache_initialized = false;
986 return NULL;
989 entry->attribute = attribute;
990 entry->name_len = MIN(254, strlen(new)) + 1;
991 entry->d_name = ((char *)dircache_root+dircache_size);
992 entry->startcluster = 0;
993 entry->wrtdate = 0;
994 entry->wrttime = 0;
995 entry->size = 0;
996 memcpy(entry->d_name, new, entry->name_len);
997 dircache_size += entry->name_len;
999 if (attribute & ATTR_DIRECTORY)
1001 logf("gen_down");
1002 dircache_gen_down(entry);
1005 reserve_used += dircache_size - last_cache_size;
1007 return entry;
1010 void dircache_bind(int fd, const char *path)
1012 struct dircache_entry *entry;
1014 /* Queue requests until dircache has been built. */
1015 if (!dircache_initialized && dircache_is_initializing())
1017 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1018 return ;
1019 strlcpy(fdbind_cache[fdbind_idx].path, path,
1020 sizeof(fdbind_cache[fdbind_idx].path));
1021 fdbind_cache[fdbind_idx].fd = fd;
1022 fdbind_idx++;
1023 return ;
1026 if (!dircache_initialized)
1027 return ;
1029 logf("bind: %d/%s", fd, path);
1030 entry = dircache_get_entry(path, false);
1031 if (entry == NULL)
1033 logf("not found!");
1034 dircache_initialized = false;
1035 return ;
1038 fd_bindings[fd] = entry;
1041 void dircache_update_filesize(int fd, long newsize, long startcluster)
1043 if (!dircache_initialized || fd < 0)
1044 return ;
1046 if (fd_bindings[fd] == NULL)
1048 logf("dircache fd(%d) access error", fd);
1049 dircache_initialized = false;
1050 return ;
1053 fd_bindings[fd]->size = newsize;
1054 fd_bindings[fd]->startcluster = startcluster;
1056 void dircache_update_filetime(int fd)
1058 #if CONFIG_RTC == 0
1059 (void)fd;
1060 #else
1061 short year;
1062 struct tm *now = get_time();
1063 if (!dircache_initialized || fd < 0)
1064 return ;
1066 if (fd_bindings[fd] == NULL)
1068 logf("dircache fd access error");
1069 dircache_initialized = false;
1070 return ;
1072 year = now->tm_year+1900-1980;
1073 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1074 (((now->tm_mon+1)&0xf)<<5) |
1075 (((now->tm_mday)&0x1f));
1076 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1077 (((now->tm_min)&0x3f)<<5) |
1078 (((now->tm_sec/2)&0x1f));
1079 #endif
1082 void dircache_mkdir(const char *path)
1083 { /* Test ok. */
1084 if (block_until_ready())
1085 return ;
1087 logf("mkdir: %s", path);
1088 dircache_new_entry(path, ATTR_DIRECTORY);
1091 void dircache_rmdir(const char *path)
1092 { /* Test ok. */
1093 struct dircache_entry *entry;
1095 if (block_until_ready())
1096 return ;
1098 logf("rmdir: %s", path);
1099 entry = dircache_get_entry(path, false);
1100 if (entry == NULL || entry->down == NULL)
1102 logf("not found or not a directory!");
1103 dircache_initialized = false;
1104 return ;
1107 entry->down = NULL;
1108 entry->name_len = 0;
1111 /* Remove a file from cache */
1112 void dircache_remove(const char *name)
1113 { /* Test ok. */
1114 struct dircache_entry *entry;
1116 if (block_until_ready())
1117 return ;
1119 logf("remove: %s", name);
1121 entry = dircache_get_entry(name, false);
1123 if (entry == NULL)
1125 logf("not found!");
1126 dircache_initialized = false;
1127 return ;
1130 entry->name_len = 0;
1133 void dircache_rename(const char *oldpath, const char *newpath)
1134 { /* Test ok. */
1135 struct dircache_entry *entry, *newentry;
1136 struct dircache_entry oldentry;
1137 char absolute_path[MAX_PATH*2];
1138 char *p;
1140 if (block_until_ready())
1141 return ;
1143 logf("rename: %s->%s", oldpath, newpath);
1145 entry = dircache_get_entry(oldpath, false);
1146 if (entry == NULL)
1148 logf("not found!");
1149 dircache_initialized = false;
1150 return ;
1153 /* Delete the old entry. */
1154 entry->name_len = 0;
1156 /** If we rename the same filename twice in a row, we need to
1157 * save the data, because the entry will be re-used. */
1158 oldentry = *entry;
1160 /* Generate the absolute path for destination if necessary. */
1161 if (newpath[0] != '/')
1163 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1164 p = strrchr(absolute_path, '/');
1165 if (!p)
1167 logf("Invalid path");
1168 dircache_initialized = false;
1169 return ;
1172 *p = '\0';
1173 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1174 newpath = absolute_path;
1177 newentry = dircache_new_entry(newpath, entry->attribute);
1178 if (newentry == NULL)
1180 dircache_initialized = false;
1181 return ;
1184 newentry->down = oldentry.down;
1185 newentry->size = oldentry.size;
1186 newentry->startcluster = oldentry.startcluster;
1187 newentry->wrttime = oldentry.wrttime;
1188 newentry->wrtdate = oldentry.wrtdate;
1191 void dircache_add_file(const char *path, long startcluster)
1193 struct dircache_entry *entry;
1195 if (block_until_ready())
1196 return ;
1198 logf("add file: %s", path);
1199 entry = dircache_new_entry(path, 0);
1200 if (entry == NULL)
1201 return ;
1203 entry->startcluster = startcluster;
1206 DIR_CACHED* opendir_cached(const char* name)
1208 struct dircache_entry *cache_entry;
1209 int dd;
1210 DIR_CACHED* pdir = opendirs;
1212 if ( name[0] != '/' )
1214 DEBUGF("Only absolute paths supported right now\n");
1215 return NULL;
1218 /* find a free dir descriptor */
1219 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1220 if ( !pdir->busy )
1221 break;
1223 if ( dd == MAX_OPEN_DIRS )
1225 DEBUGF("Too many dirs open\n");
1226 errno = EMFILE;
1227 return NULL;
1230 if (!dircache_initialized)
1232 pdir->regulardir = opendir_uncached(name);
1233 if (!pdir->regulardir)
1234 return NULL;
1236 pdir->busy = true;
1237 return pdir;
1240 pdir->busy = true;
1241 pdir->regulardir = NULL;
1242 cache_entry = dircache_get_entry(name, true);
1243 pdir->entry = cache_entry;
1245 if (cache_entry == NULL)
1247 pdir->busy = false;
1248 return NULL;
1251 return pdir;
1254 struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1256 struct dirent_uncached *regentry;
1257 struct dircache_entry *ce;
1259 if (!dir->busy)
1260 return NULL;
1262 if (dir->regulardir != NULL)
1264 regentry = readdir_uncached(dir->regulardir);
1265 if (regentry == NULL)
1266 return NULL;
1268 strlcpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH);
1269 dir->secondary_entry.size = regentry->size;
1270 dir->secondary_entry.startcluster = regentry->startcluster;
1271 dir->secondary_entry.attribute = regentry->attribute;
1272 dir->secondary_entry.wrttime = regentry->wrttime;
1273 dir->secondary_entry.wrtdate = regentry->wrtdate;
1274 dir->secondary_entry.next = NULL;
1276 return &dir->secondary_entry;
1279 do {
1280 if (dir->entry == NULL)
1281 return NULL;
1283 ce = dir->entry;
1284 if (ce->name_len == 0)
1285 dir->entry = ce->next;
1286 } while (ce->name_len == 0) ;
1288 dir->entry = ce->next;
1290 strlcpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH);
1291 /* Can't do `dir->secondary_entry = *ce`
1292 because that modifies the d_name pointer. */
1293 dir->secondary_entry.size = ce->size;
1294 dir->secondary_entry.startcluster = ce->startcluster;
1295 dir->secondary_entry.attribute = ce->attribute;
1296 dir->secondary_entry.wrttime = ce->wrttime;
1297 dir->secondary_entry.wrtdate = ce->wrtdate;
1298 dir->secondary_entry.next = NULL;
1299 dir->internal_entry = ce;
1301 //logf("-> %s", ce->name);
1302 return &dir->secondary_entry;
1305 int closedir_cached(DIR_CACHED* dir)
1307 if (!dir->busy)
1308 return -1;
1310 dir->busy=false;
1311 if (dir->regulardir != NULL)
1312 return closedir_uncached(dir->regulardir);
1314 return 0;
1317 int mkdir_cached(const char *name)
1319 int rc=mkdir_uncached(name);
1320 if (rc >= 0)
1321 dircache_mkdir(name);
1322 return(rc);
1325 int rmdir_cached(const char* name)
1327 int rc=rmdir_uncached(name);
1328 if(rc>=0)
1329 dircache_rmdir(name);
1330 return(rc);