Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / firmware / common / dircache.c
blob9f19ac3d24f51d2b68f7c0b5bbb77a858f500904
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 + 0x200)/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 ? first_ce->up->startcluster : 0);
256 ce->size = 0;
257 ce->down = first_ce->up;
259 /* second pass: recurse ! */
260 ce = first_ce;
262 while(rc >= 0 && ce)
264 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
265 rc = sab_process_dir(ce->startcluster, ce->down);
267 ce = ce->next;
270 return rc;
273 /* used during the generation */
274 static struct fat_dir sab_fat_dir;
276 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
278 memset(ce, 0, sizeof(struct dircache_entry));
280 #ifdef HAVE_MULTIVOLUME
281 if (volume > 0)
283 ce->d_name = ((char *)dircache_root+dircache_size);
284 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
285 ce->name_len = VOL_ENUM_POS + 3;
286 dircache_size += ce->name_len;
287 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
288 ce->size = 0;
289 append_position = dircache_gen_next(ce);
290 ce = dircache_gen_down(ce);
292 #endif
294 struct fat_direntry direntry; /* ditto */
295 #ifdef HAVE_MULTIVOLUME
296 sab.volume = volume;
297 #endif
298 sab.dir = &sab_fat_dir;
299 sab.direntry = &direntry;
301 return sab_process_dir(0, ce);
303 #else /* !SIMULATOR */
304 static char sab_path[MAX_PATH];
306 static int sab_process_dir(struct dircache_entry *ce)
308 struct dirent_uncached *entry;
309 struct dircache_entry *first_ce = ce;
310 DIR_UNCACHED *dir = opendir_uncached(sab_path);
311 if(dir == NULL)
313 logf("Failed to opendir_uncached(%s)", sab_path);
314 return -1;
317 while((entry = readdir_uncached(dir)))
319 if(!strcmp(".", entry->d_name) ||
320 !strcmp("..", entry->d_name))
321 continue;
323 ce->attribute = entry->attribute;
324 ce->name_len = strlen(entry->d_name) + 1;
325 ce->d_name = ((char *)dircache_root + dircache_size);
326 ce->size = entry->size;
327 ce->wrtdate = entry->wrtdate;
328 ce->wrttime = entry->wrttime;
329 memcpy(ce->d_name, entry->d_name, ce->name_len);
331 dircache_size += ce->name_len;
332 entry_count++;
334 if(entry->attribute & ATTR_DIRECTORY)
336 dircache_gen_down(ce);
337 if(ce->down == NULL)
339 closedir_uncached(dir);
340 return -1;
342 /* save current paths size */
343 int pathpos = strlen(sab_path);
344 /* append entry */
345 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
346 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
348 int rc = sab_process_dir(ce->down);
349 /* restore path */
350 sab_path[pathpos] = '\0';
352 if(rc < 0)
354 closedir_uncached(dir);
355 return rc;
359 ce = dircache_gen_next(ce);
360 if(ce == NULL)
361 return -5;
363 /* When simulator is used, it's only safe to yield here. */
364 if(thread_enabled)
366 /* Stop if we got an external signal. */
367 if(check_event_queue())
368 return -1;
369 yield();
373 /* add "." and ".." */
374 ce->d_name = ".";
375 ce->name_len = 2;
376 ce->attribute = ATTR_DIRECTORY;
377 ce->size = 0;
378 ce->down = first_ce;
380 ce = dircache_gen_next(ce);
382 ce->d_name = "..";
383 ce->name_len = 3;
384 ce->attribute = ATTR_DIRECTORY;
385 ce->size = 0;
386 ce->down = first_ce->up;
388 closedir_uncached(dir);
389 return 0;
392 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
394 #ifdef HAVE_MULTIVOLUME
395 (void) volume;
396 #endif
397 memset(ce, 0, sizeof(struct dircache_entry));
399 strlcpy(sab_path, "/", sizeof sab_path);
400 return sab_process_dir(ce);
402 #endif /* SIMULATOR */
405 * Internal function to get a pointer to dircache_entry for a given filename.
406 * path: Absolute path to a file or directory (see comment)
407 * go_down: Returns the first entry of the directory given by the path (see comment)
409 * As a a special case, accept path="" as an alias for "/".
410 * Also if the path omits the first '/', it will be accepted.
412 * * If get_down=true:
413 * If path="/", the returned entry is the first of root directory (ie dircache_root)
414 * Otherwise, if 'entry' is the returned value when get_down=false,
415 * the functions returns entry->down (which can be NULL)
417 * * If get_down=false:
418 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
419 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
420 * Which means that
421 * dircache_get_entry(path)->d_name == chunk_n
423 * If path="/", the returned entry is NULL.
424 * If the entry doesn't exist, return NULL
426 * NOTE: this functions silently handles double '/'
428 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
430 char namecopy[MAX_PATH];
431 char* part;
432 char* end;
434 bool at_root = true;
435 struct dircache_entry *cache_entry = dircache_root;
437 strlcpy(namecopy, path, sizeof(namecopy));
439 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
441 /* If request another chunk, the current entry has to be directory
442 * and so cache_entry->down has to be non-NULL/
443 * Special case of root because it's already the first entry of the root directory
445 * NOTE: this is safe even if cache_entry->down is NULL */
446 if(!at_root)
447 cache_entry = cache_entry->down;
448 else
449 at_root = false;
451 /* scan dir for name */
452 while(cache_entry != NULL)
454 /* skip unused entries */
455 if(cache_entry->name_len == 0)
457 cache_entry = cache_entry->next;
458 continue;
460 /* compare names */
461 if(!strcasecmp(part, cache_entry->d_name))
462 break;
463 /* go to next entry */
464 cache_entry = cache_entry->next;
467 /* handle not found case */
468 if(cache_entry == NULL)
469 return NULL;
472 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
473 if(go_down)
474 return at_root ? cache_entry : cache_entry->down;
475 else
476 return at_root ? NULL : cache_entry;
479 #ifdef HAVE_EEPROM_SETTINGS
481 * Function to load the internal cache structure from disk to initialize
482 * the dircache really fast and little disk access.
484 int dircache_load(void)
486 struct dircache_maindata maindata;
487 int bytes_read;
488 int fd;
490 if (dircache_initialized)
491 return -1;
493 logf("Loading directory cache");
494 dircache_size = 0;
496 fd = open(DIRCACHE_FILE, O_RDONLY);
497 if (fd < 0)
498 return -2;
500 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
501 if (bytes_read != sizeof(struct dircache_maindata)
502 || maindata.size <= 0)
504 logf("Dircache file header error");
505 close(fd);
506 remove(DIRCACHE_FILE);
507 return -3;
510 dircache_root = buffer_alloc(0);
511 if ((long)maindata.root_entry != (long)dircache_root)
513 logf("Position missmatch");
514 close(fd);
515 remove(DIRCACHE_FILE);
516 return -4;
519 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
520 entry_count = maindata.entry_count;
521 appflags = maindata.appflags;
522 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
523 close(fd);
524 remove(DIRCACHE_FILE);
526 if (bytes_read != maindata.size)
528 logf("Dircache read failed");
529 return -6;
532 /* Cache successfully loaded. */
533 dircache_size = maindata.size;
534 allocated_size = dircache_size + DIRCACHE_RESERVE;
535 reserve_used = 0;
536 logf("Done, %ld KiB used", dircache_size / 1024);
537 dircache_initialized = true;
538 memset(fd_bindings, 0, sizeof(fd_bindings));
540 return 0;
544 * Function to save the internal cache stucture to disk for fast loading
545 * on boot.
547 int dircache_save(void)
549 struct dircache_maindata maindata;
550 int fd;
551 unsigned long bytes_written;
553 remove(DIRCACHE_FILE);
555 if (!dircache_initialized)
556 return -1;
558 logf("Saving directory cache");
559 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
561 maindata.magic = DIRCACHE_MAGIC;
562 maindata.size = dircache_size;
563 maindata.root_entry = dircache_root;
564 maindata.entry_count = entry_count;
565 maindata.appflags = appflags;
567 /* Save the info structure */
568 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
569 if (bytes_written != sizeof(struct dircache_maindata))
571 close(fd);
572 logf("dircache: write failed #1");
573 return -2;
576 /* Dump whole directory cache to disk */
577 bytes_written = write(fd, dircache_root, dircache_size);
578 close(fd);
579 if (bytes_written != dircache_size)
581 logf("dircache: write failed #2");
582 return -3;
585 return 0;
587 #endif /* #if 0 */
590 * Internal function which scans the disk and creates the dircache structure.
592 static int dircache_do_rebuild(void)
594 unsigned int start_tick;
595 int i;
597 /* Measure how long it takes build the cache. */
598 start_tick = current_tick;
599 dircache_initializing = true;
600 appflags = 0;
601 entry_count = 0;
603 dircache_size = sizeof(struct dircache_entry);
605 #ifdef HAVE_MULTIVOLUME
606 append_position = dircache_root;
608 for (i = NUM_VOLUMES; i >= 0; i--)
610 if (fat_ismounted(i))
612 #endif
613 cpu_boost(true);
614 #ifdef HAVE_MULTIVOLUME
615 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
616 #else
617 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
618 #endif /* HAVE_MULTIVOLUME */
620 logf("dircache_scan_and_build failed");
621 cpu_boost(false);
622 dircache_size = 0;
623 dircache_initializing = false;
624 return -2;
626 cpu_boost(false);
627 #ifdef HAVE_MULTIVOLUME
630 #endif
632 logf("Done, %ld KiB used", dircache_size / 1024);
634 dircache_initialized = true;
635 dircache_initializing = false;
636 cache_build_ticks = current_tick - start_tick;
638 /* Initialized fd bindings. */
639 memset(fd_bindings, 0, sizeof(fd_bindings));
640 for (i = 0; i < fdbind_idx; i++)
641 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
642 fdbind_idx = 0;
644 if (thread_enabled)
646 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
647 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
649 else
651 /* We have to long align the audiobuf to keep the buffer access fast. */
652 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
653 audiobuf += DIRCACHE_RESERVE;
654 allocated_size = dircache_size + DIRCACHE_RESERVE;
655 reserve_used = 0;
658 return 1;
662 * Internal thread that controls transparent cache building.
664 static void dircache_thread(void)
666 struct queue_event ev;
668 while (1)
670 queue_wait(&dircache_queue, &ev);
672 switch (ev.id)
674 #ifdef HAVE_HOTSWAP
675 case SYS_FS_CHANGED:
676 if (!dircache_initialized)
677 break;
678 dircache_initialized = false;
679 #endif
680 case DIRCACHE_BUILD:
681 thread_enabled = true;
682 dircache_do_rebuild();
683 thread_enabled = false;
684 break ;
686 case DIRCACHE_STOP:
687 logf("Stopped the rebuilding.");
688 dircache_initialized = false;
689 break ;
691 #ifndef SIMULATOR
692 case SYS_USB_CONNECTED:
693 usb_acknowledge(SYS_USB_CONNECTED_ACK);
694 usb_wait_for_disconnect(&dircache_queue);
695 break ;
696 #endif
702 * Start scanning the disk to build the dircache.
703 * Either transparent or non-transparent build method is used.
705 int dircache_build(int last_size)
707 if (dircache_initialized || thread_enabled)
708 return -3;
710 logf("Building directory cache");
711 remove(DIRCACHE_FILE);
713 /* Background build, dircache has been previously allocated */
714 if (dircache_size > 0)
716 thread_enabled = true;
717 dircache_initializing = true;
718 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
719 return 2;
722 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
724 allocated_size = last_size + DIRCACHE_RESERVE;
725 dircache_root = buffer_alloc(allocated_size);
726 thread_enabled = true;
728 /* Start a transparent rebuild. */
729 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
730 return 3;
733 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
735 /* Start a non-transparent rebuild. */
736 return dircache_do_rebuild();
740 * Steal the allocated dircache buffer and disable dircache.
742 void* dircache_steal_buffer(long *size)
744 dircache_disable();
745 if (dircache_size == 0)
747 *size = 0;
748 return NULL;
751 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
753 return dircache_root;
757 * Main initialization function that must be called before any other
758 * operations within the dircache.
760 void dircache_init(void)
762 int i;
764 dircache_initialized = false;
765 dircache_initializing = false;
767 memset(opendirs, 0, sizeof(opendirs));
768 for (i = 0; i < MAX_OPEN_DIRS; i++)
770 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
773 queue_init(&dircache_queue, true);
774 create_thread(dircache_thread, dircache_stack,
775 sizeof(dircache_stack), 0, dircache_thread_name
776 IF_PRIO(, PRIORITY_BACKGROUND)
777 IF_COP(, CPU));
781 * Returns true if dircache has been initialized and is ready to be used.
783 bool dircache_is_enabled(void)
785 return dircache_initialized;
789 * Returns true if dircache is being initialized.
791 bool dircache_is_initializing(void)
793 return dircache_initializing || thread_enabled;
797 * Set application flags used to determine if dircache is still intact.
799 void dircache_set_appflag(long mask)
801 appflags |= mask;
805 * Get application flags used to determine if dircache is still intact.
807 bool dircache_get_appflag(long mask)
809 return dircache_is_enabled() && (appflags & mask);
813 * Returns the current number of entries (directories and files) in the cache.
815 int dircache_get_entry_count(void)
817 return entry_count;
821 * Returns the allocated space for dircache (without reserve space).
823 int dircache_get_cache_size(void)
825 return dircache_is_enabled() ? dircache_size : 0;
829 * Returns how many bytes of the reserve allocation for live cache
830 * updates have been used.
832 int dircache_get_reserve_used(void)
834 return dircache_is_enabled() ? reserve_used : 0;
838 * Returns the time in kernel ticks that took to build the cache.
840 int dircache_get_build_ticks(void)
842 return dircache_is_enabled() ? cache_build_ticks : 0;
846 * Disables the dircache. Usually called on shutdown or when
847 * accepting a usb connection.
849 void dircache_disable(void)
851 int i;
852 bool cache_in_use;
854 if (thread_enabled)
855 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
857 while (thread_enabled)
858 sleep(1);
859 dircache_initialized = false;
861 logf("Waiting for cached dirs to release");
862 do {
863 cache_in_use = false;
864 for (i = 0; i < MAX_OPEN_DIRS; i++) {
865 if (!opendirs[i].regulardir && opendirs[i].busy)
867 cache_in_use = true;
868 sleep(1);
869 break ;
872 } while (cache_in_use) ;
874 logf("Cache released");
875 entry_count = 0;
879 * Usermode function to return dircache_entry pointer to the given path.
881 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
883 if (!dircache_initialized || filename == NULL)
884 return NULL;
886 return dircache_get_entry(filename, false);
890 * Function to copy the full absolute path from dircache to the given buffer
891 * using the given dircache_entry pointer.
893 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
895 int path_size = 0;
896 int idx;
897 const struct dircache_entry *temp = entry;
899 if (size <= 0)
900 return ;
902 /* first compute the necessary size */
903 while(temp != NULL)
905 path_size += temp->name_len; /* '/' + d_name */
906 temp = temp->up;
909 /* now copy the path */
910 /* doesn't matter with trailing 0 because it's added later */
911 idx = path_size;
912 while(entry != NULL)
914 idx -= entry->name_len;
915 /* available size */
916 int rem = size - idx;
918 if(rem >= 1)
920 buf[idx] = '/';
921 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
923 entry = entry->up;
926 /* add trailing 0 */
927 buf[MIN(path_size, size-1)] = 0;
930 /* --- Directory cache live updating functions --- */
931 static int block_until_ready(void)
933 /* Block until dircache has been built. */
934 while (!dircache_initialized && dircache_is_initializing())
935 sleep(1);
937 if (!dircache_initialized)
938 return -1;
940 return 0;
943 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
945 struct dircache_entry *entry;
946 char basedir[MAX_PATH*2];
947 char *new;
948 long last_cache_size = dircache_size;
950 strlcpy(basedir, path, sizeof(basedir));
951 new = strrchr(basedir, '/');
952 if (new == NULL)
954 logf("error occurred");
955 dircache_initialized = false;
956 return NULL;
959 *new = '\0';
960 new++;
962 entry = dircache_get_entry(basedir, true);
963 if (entry == NULL)
965 logf("basedir not found!");
966 logf("%s", basedir);
967 dircache_initialized = false;
968 return NULL;
971 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
972 >= DIRCACHE_RESERVE)
974 logf("not enough space");
975 dircache_initialized = false;
976 return NULL;
979 while (entry->next != NULL)
980 entry = entry->next;
982 if (entry->name_len > 0)
983 entry = dircache_gen_next(entry);
985 if (entry == NULL)
987 dircache_initialized = false;
988 return NULL;
991 entry->attribute = attribute;
992 entry->name_len = MIN(254, strlen(new)) + 1;
993 entry->d_name = ((char *)dircache_root+dircache_size);
994 entry->startcluster = 0;
995 entry->wrtdate = 0;
996 entry->wrttime = 0;
997 entry->size = 0;
998 memcpy(entry->d_name, new, entry->name_len);
999 dircache_size += entry->name_len;
1001 if (attribute & ATTR_DIRECTORY)
1003 logf("gen_down");
1004 dircache_gen_down(entry);
1007 reserve_used += dircache_size - last_cache_size;
1009 return entry;
1012 void dircache_bind(int fd, const char *path)
1014 struct dircache_entry *entry;
1016 /* Queue requests until dircache has been built. */
1017 if (!dircache_initialized && dircache_is_initializing())
1019 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1020 return ;
1021 strlcpy(fdbind_cache[fdbind_idx].path, path,
1022 sizeof(fdbind_cache[fdbind_idx].path));
1023 fdbind_cache[fdbind_idx].fd = fd;
1024 fdbind_idx++;
1025 return ;
1028 if (!dircache_initialized)
1029 return ;
1031 logf("bind: %d/%s", fd, path);
1032 entry = dircache_get_entry(path, false);
1033 if (entry == NULL)
1035 logf("not found!");
1036 dircache_initialized = false;
1037 return ;
1040 fd_bindings[fd] = entry;
1043 void dircache_update_filesize(int fd, long newsize, long startcluster)
1045 if (!dircache_initialized || fd < 0)
1046 return ;
1048 if (fd_bindings[fd] == NULL)
1050 logf("dircache fd(%d) access error", fd);
1051 dircache_initialized = false;
1052 return ;
1055 fd_bindings[fd]->size = newsize;
1056 fd_bindings[fd]->startcluster = startcluster;
1058 void dircache_update_filetime(int fd)
1060 #if CONFIG_RTC == 0
1061 (void)fd;
1062 #else
1063 short year;
1064 struct tm *now = get_time();
1065 if (!dircache_initialized || fd < 0)
1066 return ;
1068 if (fd_bindings[fd] == NULL)
1070 logf("dircache fd access error");
1071 dircache_initialized = false;
1072 return ;
1074 year = now->tm_year+1900-1980;
1075 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1076 (((now->tm_mon+1)&0xf)<<5) |
1077 (((now->tm_mday)&0x1f));
1078 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1079 (((now->tm_min)&0x3f)<<5) |
1080 (((now->tm_sec/2)&0x1f));
1081 #endif
1084 void dircache_mkdir(const char *path)
1085 { /* Test ok. */
1086 if (block_until_ready())
1087 return ;
1089 logf("mkdir: %s", path);
1090 dircache_new_entry(path, ATTR_DIRECTORY);
1093 void dircache_rmdir(const char *path)
1094 { /* Test ok. */
1095 struct dircache_entry *entry;
1097 if (block_until_ready())
1098 return ;
1100 logf("rmdir: %s", path);
1101 entry = dircache_get_entry(path, false);
1102 if (entry == NULL || entry->down == NULL)
1104 logf("not found or not a directory!");
1105 dircache_initialized = false;
1106 return ;
1109 entry->down = NULL;
1110 entry->name_len = 0;
1113 /* Remove a file from cache */
1114 void dircache_remove(const char *name)
1115 { /* Test ok. */
1116 struct dircache_entry *entry;
1118 if (block_until_ready())
1119 return ;
1121 logf("remove: %s", name);
1123 entry = dircache_get_entry(name, false);
1125 if (entry == NULL)
1127 logf("not found!");
1128 dircache_initialized = false;
1129 return ;
1132 entry->name_len = 0;
1135 void dircache_rename(const char *oldpath, const char *newpath)
1136 { /* Test ok. */
1137 struct dircache_entry *entry, *newentry;
1138 struct dircache_entry oldentry;
1139 char absolute_path[MAX_PATH*2];
1140 char *p;
1142 if (block_until_ready())
1143 return ;
1145 logf("rename: %s->%s", oldpath, newpath);
1147 entry = dircache_get_entry(oldpath, false);
1148 if (entry == NULL)
1150 logf("not found!");
1151 dircache_initialized = false;
1152 return ;
1155 /* Delete the old entry. */
1156 entry->name_len = 0;
1158 /** If we rename the same filename twice in a row, we need to
1159 * save the data, because the entry will be re-used. */
1160 oldentry = *entry;
1162 /* Generate the absolute path for destination if necessary. */
1163 if (newpath[0] != '/')
1165 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1166 p = strrchr(absolute_path, '/');
1167 if (!p)
1169 logf("Invalid path");
1170 dircache_initialized = false;
1171 return ;
1174 *p = '\0';
1175 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1176 newpath = absolute_path;
1179 newentry = dircache_new_entry(newpath, entry->attribute);
1180 if (newentry == NULL)
1182 dircache_initialized = false;
1183 return ;
1186 newentry->down = oldentry.down;
1187 newentry->size = oldentry.size;
1188 newentry->startcluster = oldentry.startcluster;
1189 newentry->wrttime = oldentry.wrttime;
1190 newentry->wrtdate = oldentry.wrtdate;
1193 void dircache_add_file(const char *path, long startcluster)
1195 struct dircache_entry *entry;
1197 if (block_until_ready())
1198 return ;
1200 logf("add file: %s", path);
1201 entry = dircache_new_entry(path, 0);
1202 if (entry == NULL)
1203 return ;
1205 entry->startcluster = startcluster;
1208 DIR_CACHED* opendir_cached(const char* name)
1210 int dd;
1211 DIR_CACHED* pdir = opendirs;
1213 if ( name[0] != '/' )
1215 DEBUGF("Only absolute paths supported right now\n");
1216 return NULL;
1219 /* find a free dir descriptor */
1220 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1221 if ( !pdir->busy )
1222 break;
1224 if ( dd == MAX_OPEN_DIRS )
1226 DEBUGF("Too many dirs open\n");
1227 errno = EMFILE;
1228 return NULL;
1231 pdir->busy = true;
1233 if (!dircache_initialized)
1235 pdir->internal_entry = NULL;
1236 pdir->regulardir = opendir_uncached(name);
1238 else
1240 pdir->regulardir = NULL;
1241 pdir->internal_entry = dircache_get_entry(name, true);
1242 pdir->theent.attribute = -1; /* used to make readdir_cached aware of the first call */
1245 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1247 pdir->busy = false;
1248 return NULL;
1251 return pdir;
1254 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1256 struct dircache_entry *ce = dir->internal_entry;
1257 struct dirent_uncached *regentry;
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->theent.d_name, regentry->d_name, MAX_PATH);
1269 dir->theent.size = regentry->size;
1270 dir->theent.startcluster = regentry->startcluster;
1271 dir->theent.attribute = regentry->attribute;
1272 dir->theent.wrttime = regentry->wrttime;
1273 dir->theent.wrtdate = regentry->wrtdate;
1275 return &dir->theent;
1278 /* if theent.attribute=-1 then this is the first call */
1279 /* otherwise, this is is not so we first take the entry's ->next */
1280 /* NOTE: normal file can't have attribute=-1 */
1281 if(dir->theent.attribute != -1)
1282 ce = ce->next;
1283 /* skip unused entries */
1284 while(ce != NULL && ce->name_len == 0)
1285 ce = ce->next;
1287 if (ce == NULL)
1288 return NULL;
1290 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1291 /* Can't do `dir->theent = *ce`
1292 because that modifies the d_name pointer. */
1293 dir->theent.size = ce->size;
1294 dir->theent.startcluster = ce->startcluster;
1295 dir->theent.attribute = ce->attribute;
1296 dir->theent.wrttime = ce->wrttime;
1297 dir->theent.wrtdate = ce->wrtdate;
1298 dir->internal_entry = ce;
1300 //logf("-> %s", ce->name);
1301 return &dir->theent;
1304 int closedir_cached(DIR_CACHED* dir)
1306 if (!dir->busy)
1307 return -1;
1309 dir->busy=false;
1310 if (dir->regulardir != NULL)
1311 return closedir_uncached(dir->regulardir);
1313 return 0;
1316 int mkdir_cached(const char *name)
1318 int rc=mkdir_uncached(name);
1319 if (rc >= 0)
1320 dircache_mkdir(name);
1321 return(rc);
1324 int rmdir_cached(const char* name)
1326 int rc=rmdir_uncached(name);
1327 if(rc >= 0)
1328 dircache_rmdir(name);
1329 return(rc);