Factor out opening and removing DIRCACHE_FILE into separate functions.
[kugel-rb.git] / firmware / common / dircache.c
blob906527f8f26be8f6ced729ed692d8db57d653568
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* TODO:
23 - Allow cache live updating while transparent rebuild is running.
26 #include "config.h"
28 #include <stdio.h>
29 #include <errno.h>
30 #include "string-extra.h"
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include "debug.h"
34 #include "system.h"
35 #include "logf.h"
36 #include "dircache.h"
37 #include "thread.h"
38 #include "kernel.h"
39 #include "usb.h"
40 #include "file.h"
41 #include "buffer.h"
42 #include "dir.h"
43 #include "storage.h"
44 #if CONFIG_RTC
45 #include "time.h"
46 #include "timefuncs.h"
47 #endif
49 /* Queue commands. */
50 #define DIRCACHE_BUILD 1
51 #define DIRCACHE_STOP 2
53 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
54 #define MAX_OPEN_DIRS 12
55 #else
56 #define MAX_OPEN_DIRS 8
57 #endif
58 static DIR_CACHED opendirs[MAX_OPEN_DIRS];
60 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
61 static struct dircache_entry *dircache_root;
62 #ifdef HAVE_MULTIVOLUME
63 static struct dircache_entry *append_position;
64 #endif
66 static bool dircache_initialized = false;
67 static bool dircache_initializing = false;
68 static bool thread_enabled = false;
69 static unsigned long allocated_size = DIRCACHE_LIMIT;
70 static unsigned long dircache_size = 0;
71 static unsigned long entry_count = 0;
72 static unsigned long reserve_used = 0;
73 static unsigned int cache_build_ticks = 0;
74 static unsigned long appflags = 0;
76 static struct event_queue dircache_queue;
77 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x400)/sizeof(long)];
78 static const char dircache_thread_name[] = "dircache";
80 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
81 static int fdbind_idx = 0;
83 /* --- Internal cache structure control functions --- */
85 #ifdef HAVE_EEPROM_SETTINGS
86 /**
87 * Open the dircache file to save a snapshot on disk
89 static int open_dircache_file(unsigned flags, int permissions)
91 if (permissions != 0)
92 return open(DIRCACHE_FILE, flags, permissions);
94 return open(DIRCACHE_FILE, flags);
97 /**
98 * Remove the snapshot file
100 static int remove_dircache_file(void)
102 return remove(DIRCACHE_FILE);
104 #endif
105 /**
106 * Internal function to allocate a new dircache_entry from memory.
108 static struct dircache_entry* allocate_entry(void)
110 struct dircache_entry *next_entry;
112 if (dircache_size > allocated_size - MAX_PATH*2)
114 logf("size limit reached");
115 return NULL;
118 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
119 #ifdef ROCKBOX_STRICT_ALIGN
120 /* Make sure the entry is long aligned. */
121 if ((long)next_entry & 0x03)
123 dircache_size += 4 - ((long)next_entry & 0x03);
124 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
126 #endif
127 next_entry->name_len = 0;
128 next_entry->d_name = NULL;
129 next_entry->up = NULL;
130 next_entry->down = NULL;
131 next_entry->next = NULL;
133 dircache_size += sizeof(struct dircache_entry);
135 return next_entry;
139 * Internal function to allocate a dircache_entry and set
140 * ->next entry pointers.
142 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
144 struct dircache_entry *next_entry;
146 if ( (next_entry = allocate_entry()) == NULL)
147 return NULL;
148 next_entry->up = ce->up;
149 ce->next = next_entry;
151 return next_entry;
155 * Internal function to allocate a dircache_entry and set
156 * ->down entry pointers.
158 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
160 struct dircache_entry *next_entry;
162 if ( (next_entry = allocate_entry()) == NULL)
163 return NULL;
164 next_entry->up = ce;
165 ce->down = next_entry;
167 return next_entry;
171 * Returns true if there is an event waiting in the queue
172 * that requires the current operation to be aborted.
174 static bool check_event_queue(void)
176 struct queue_event ev;
178 if(!queue_peek(&dircache_queue, &ev))
179 return false;
181 switch (ev.id)
183 case DIRCACHE_STOP:
184 case SYS_USB_CONNECTED:
185 #ifdef HAVE_HOTSWAP
186 case SYS_FS_CHANGED:
187 #endif
188 return true;
191 return false;
194 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
195 /* scan and build static data (avoid redundancy on stack) */
196 static struct
198 #ifdef HAVE_MULTIVOLUME
199 int volume;
200 #endif
201 struct fat_dir *dir;
202 struct fat_direntry *direntry;
203 }sab;
205 static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
207 /* normally, opendir expects a full fat_dir as parent but in our case,
208 * it's completely useless because we don't modify anything
209 * WARNING: this heavily relies on current FAT implementation ! */
211 /* those field are necessary to update the FAT entry in case of modification
212 here we don't touch anything so we put dummy values */
213 sab.dir->entry = 0;
214 sab.dir->entrycount = 0;
215 sab.dir->file.firstcluster = 0;
216 /* open directory */
217 int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
218 if(rc < 0)
220 logf("fat_opendir failed: %d", rc);
221 return rc;
224 /* first pass : read dir */
225 struct dircache_entry *first_ce = ce;
227 /* read through directory */
228 while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
230 if(!strcmp(".", sab.direntry->name) ||
231 !strcmp("..", sab.direntry->name))
232 continue;
234 ce->attribute = sab.direntry->attr;
235 ce->name_len = strlen(sab.direntry->name) + 1;
236 ce->d_name = ((char *)dircache_root + dircache_size);
237 ce->startcluster = sab.direntry->firstcluster;
238 ce->size = sab.direntry->filesize;
239 ce->wrtdate = sab.direntry->wrtdate;
240 ce->wrttime = sab.direntry->wrttime;
241 memcpy(ce->d_name, sab.direntry->name, ce->name_len);
243 dircache_size += ce->name_len;
244 entry_count++;
246 if(ce->attribute & FAT_ATTR_DIRECTORY)
247 dircache_gen_down(ce);
249 ce = dircache_gen_next(ce);
250 if(ce == NULL)
251 return -5;
253 /* When simulator is used, it's only safe to yield here. */
254 if(thread_enabled)
256 /* Stop if we got an external signal. */
257 if(check_event_queue())
258 return -6;
259 yield();
263 /* add "." and ".." */
264 ce->d_name = ".";
265 ce->name_len = 2;
266 ce->attribute = FAT_ATTR_DIRECTORY;
267 ce->startcluster = startcluster;
268 ce->size = 0;
269 ce->down = first_ce;
271 ce = dircache_gen_next(ce);
273 ce->d_name = "..";
274 ce->name_len = 3;
275 ce->attribute = FAT_ATTR_DIRECTORY;
276 ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
277 ce->size = 0;
278 ce->down = first_ce->up;
280 /* second pass: recurse ! */
281 ce = first_ce;
283 while(rc >= 0 && ce)
285 if(ce->name_len != 0 && ce->down != NULL && strcmp(ce->d_name, ".") && strcmp(ce->d_name, ".."))
286 rc = sab_process_dir(ce->startcluster, ce->down);
288 ce = ce->next;
291 return rc;
294 /* used during the generation */
295 static struct fat_dir sab_fat_dir;
297 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
299 memset(ce, 0, sizeof(struct dircache_entry));
301 #ifdef HAVE_MULTIVOLUME
302 if (volume > 0)
304 ce->d_name = ((char *)dircache_root+dircache_size);
305 snprintf(ce->d_name, VOL_ENUM_POS + 3, VOL_NAMES, volume);
306 ce->name_len = VOL_ENUM_POS + 3;
307 dircache_size += ce->name_len;
308 ce->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
309 ce->size = 0;
310 append_position = dircache_gen_next(ce);
311 ce = dircache_gen_down(ce);
313 #endif
315 struct fat_direntry direntry; /* ditto */
316 #ifdef HAVE_MULTIVOLUME
317 sab.volume = volume;
318 #endif
319 sab.dir = &sab_fat_dir;
320 sab.direntry = &direntry;
322 return sab_process_dir(0, ce);
324 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
325 static char sab_path[MAX_PATH];
327 static int sab_process_dir(struct dircache_entry *ce)
329 struct dirent_uncached *entry;
330 struct dircache_entry *first_ce = ce;
331 DIR_UNCACHED *dir = opendir_uncached(sab_path);
332 if(dir == NULL)
334 logf("Failed to opendir_uncached(%s)", sab_path);
335 return -1;
338 while((entry = readdir_uncached(dir)))
340 if(!strcmp(".", entry->d_name) ||
341 !strcmp("..", entry->d_name))
342 continue;
344 ce->attribute = entry->attribute;
345 ce->name_len = strlen(entry->d_name) + 1;
346 ce->d_name = ((char *)dircache_root + dircache_size);
347 ce->size = entry->size;
348 ce->wrtdate = entry->wrtdate;
349 ce->wrttime = entry->wrttime;
350 memcpy(ce->d_name, entry->d_name, ce->name_len);
352 dircache_size += ce->name_len;
353 entry_count++;
355 if(entry->attribute & ATTR_DIRECTORY)
357 dircache_gen_down(ce);
358 if(ce->down == NULL)
360 closedir_uncached(dir);
361 return -1;
363 /* save current paths size */
364 int pathpos = strlen(sab_path);
365 /* append entry */
366 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
367 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
369 int rc = sab_process_dir(ce->down);
370 /* restore path */
371 sab_path[pathpos] = '\0';
373 if(rc < 0)
375 closedir_uncached(dir);
376 return rc;
380 ce = dircache_gen_next(ce);
381 if(ce == NULL)
382 return -5;
384 /* When simulator is used, it's only safe to yield here. */
385 if(thread_enabled)
387 /* Stop if we got an external signal. */
388 if(check_event_queue())
389 return -1;
390 yield();
394 /* add "." and ".." */
395 ce->d_name = ".";
396 ce->name_len = 2;
397 ce->attribute = ATTR_DIRECTORY;
398 ce->size = 0;
399 ce->down = first_ce;
401 ce = dircache_gen_next(ce);
403 ce->d_name = "..";
404 ce->name_len = 3;
405 ce->attribute = ATTR_DIRECTORY;
406 ce->size = 0;
407 ce->down = first_ce->up;
409 closedir_uncached(dir);
410 return 0;
413 static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
415 #ifdef HAVE_MULTIVOLUME
416 (void) volume;
417 #endif
418 memset(ce, 0, sizeof(struct dircache_entry));
420 strlcpy(sab_path, "/", sizeof sab_path);
421 return sab_process_dir(ce);
423 #endif /* PLATFORM_NATIVE */
426 * Internal function to get a pointer to dircache_entry for a given filename.
427 * path: Absolute path to a file or directory (see comment)
428 * go_down: Returns the first entry of the directory given by the path (see comment)
430 * As a a special case, accept path="" as an alias for "/".
431 * Also if the path omits the first '/', it will be accepted.
433 * * If get_down=true:
434 * If path="/", the returned entry is the first of root directory (ie dircache_root)
435 * Otherwise, if 'entry' is the returned value when get_down=false,
436 * the functions returns entry->down (which can be NULL)
438 * * If get_down=false:
439 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
440 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
441 * Which means that
442 * dircache_get_entry(path)->d_name == chunk_n
444 * If path="/", the returned entry is NULL.
445 * If the entry doesn't exist, return NULL
447 * NOTE: this functions silently handles double '/'
449 static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
451 char namecopy[MAX_PATH];
452 char* part;
453 char* end;
455 bool at_root = true;
456 struct dircache_entry *cache_entry = dircache_root;
458 strlcpy(namecopy, path, sizeof(namecopy));
460 for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
462 /* If request another chunk, the current entry has to be directory
463 * and so cache_entry->down has to be non-NULL/
464 * Special case of root because it's already the first entry of the root directory
466 * NOTE: this is safe even if cache_entry->down is NULL */
467 if(!at_root)
468 cache_entry = cache_entry->down;
469 else
470 at_root = false;
472 /* scan dir for name */
473 while(cache_entry != NULL)
475 /* skip unused entries */
476 if(cache_entry->name_len == 0)
478 cache_entry = cache_entry->next;
479 continue;
481 /* compare names */
482 if(!strcasecmp(part, cache_entry->d_name))
483 break;
484 /* go to next entry */
485 cache_entry = cache_entry->next;
488 /* handle not found case */
489 if(cache_entry == NULL)
490 return NULL;
493 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
494 if(go_down)
495 return at_root ? cache_entry : cache_entry->down;
496 else
497 return at_root ? NULL : cache_entry;
500 #ifdef HAVE_EEPROM_SETTINGS
502 * Function to load the internal cache structure from disk to initialize
503 * the dircache really fast and little disk access.
505 int dircache_load(void)
507 struct dircache_maindata maindata;
508 int bytes_read;
509 int fd;
511 if (dircache_initialized)
512 return -1;
514 logf("Loading directory cache");
515 dircache_size = 0;
517 fd = open_dircache_file(O_RDONLY, 0);
518 if (fd < 0)
519 return -2;
521 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
522 if (bytes_read != sizeof(struct dircache_maindata)
523 || maindata.size <= 0)
525 logf("Dircache file header error");
526 close(fd);
527 remove_dircache_file();
528 return -3;
531 dircache_root = buffer_alloc(0);
532 if ((long)maindata.root_entry != (long)dircache_root)
534 logf("Position missmatch");
535 close(fd);
536 remove_dircache_file();
537 return -4;
540 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
541 entry_count = maindata.entry_count;
542 appflags = maindata.appflags;
543 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
544 close(fd);
545 remove_dircache_file();
547 if (bytes_read != maindata.size)
549 logf("Dircache read failed");
550 return -6;
553 /* Cache successfully loaded. */
554 dircache_size = maindata.size;
555 allocated_size = dircache_size + DIRCACHE_RESERVE;
556 reserve_used = 0;
557 logf("Done, %ld KiB used", dircache_size / 1024);
558 dircache_initialized = true;
559 memset(fd_bindings, 0, sizeof(fd_bindings));
561 return 0;
565 * Function to save the internal cache stucture to disk for fast loading
566 * on boot.
568 int dircache_save(void)
570 struct dircache_maindata maindata;
571 int fd;
572 unsigned long bytes_written;
574 remove_dircache_file();
576 if (!dircache_initialized)
577 return -1;
579 logf("Saving directory cache");
580 fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
582 maindata.magic = DIRCACHE_MAGIC;
583 maindata.size = dircache_size;
584 maindata.root_entry = dircache_root;
585 maindata.entry_count = entry_count;
586 maindata.appflags = appflags;
588 /* Save the info structure */
589 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
590 if (bytes_written != sizeof(struct dircache_maindata))
592 close(fd);
593 logf("dircache: write failed #1");
594 return -2;
597 /* Dump whole directory cache to disk */
598 bytes_written = write(fd, dircache_root, dircache_size);
599 close(fd);
600 if (bytes_written != dircache_size)
602 logf("dircache: write failed #2");
603 return -3;
606 return 0;
608 #endif /* HAVE_EEPROM_SETTINGS */
611 * Internal function which scans the disk and creates the dircache structure.
613 static int dircache_do_rebuild(void)
615 unsigned int start_tick;
616 int i;
618 /* Measure how long it takes build the cache. */
619 start_tick = current_tick;
620 dircache_initializing = true;
621 appflags = 0;
622 entry_count = 0;
624 dircache_size = sizeof(struct dircache_entry);
626 #ifdef HAVE_MULTIVOLUME
627 append_position = dircache_root;
629 for (i = NUM_VOLUMES; i >= 0; i--)
631 if (fat_ismounted(i))
633 #endif
634 cpu_boost(true);
635 #ifdef HAVE_MULTIVOLUME
636 if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
637 #else
638 if (dircache_scan_and_build(IF_MV2(0,) dircache_root) < 0)
639 #endif /* HAVE_MULTIVOLUME */
641 logf("dircache_scan_and_build failed");
642 cpu_boost(false);
643 dircache_size = 0;
644 dircache_initializing = false;
645 return -2;
647 cpu_boost(false);
648 #ifdef HAVE_MULTIVOLUME
651 #endif
653 logf("Done, %ld KiB used", dircache_size / 1024);
655 dircache_initialized = true;
656 dircache_initializing = false;
657 cache_build_ticks = current_tick - start_tick;
659 /* Initialized fd bindings. */
660 memset(fd_bindings, 0, sizeof(fd_bindings));
661 for (i = 0; i < fdbind_idx; i++)
662 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
663 fdbind_idx = 0;
665 if (thread_enabled)
667 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
668 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
670 else
672 /* We have to long align the audiobuf to keep the buffer access fast. */
673 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
674 audiobuf += DIRCACHE_RESERVE;
675 allocated_size = dircache_size + DIRCACHE_RESERVE;
676 reserve_used = 0;
679 return 1;
683 * Internal thread that controls transparent cache building.
685 static void dircache_thread(void)
687 struct queue_event ev;
689 while (1)
691 queue_wait(&dircache_queue, &ev);
693 switch (ev.id)
695 #ifdef HAVE_HOTSWAP
696 case SYS_FS_CHANGED:
697 if (!dircache_initialized)
698 break;
699 dircache_initialized = false;
700 #endif
701 case DIRCACHE_BUILD:
702 thread_enabled = true;
703 dircache_do_rebuild();
704 thread_enabled = false;
705 break ;
707 case DIRCACHE_STOP:
708 logf("Stopped the rebuilding.");
709 dircache_initialized = false;
710 break ;
712 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
713 case SYS_USB_CONNECTED:
714 usb_acknowledge(SYS_USB_CONNECTED_ACK);
715 usb_wait_for_disconnect(&dircache_queue);
716 break ;
717 #endif
723 * Start scanning the disk to build the dircache.
724 * Either transparent or non-transparent build method is used.
726 int dircache_build(int last_size)
728 if (dircache_initialized || thread_enabled)
729 return -3;
731 logf("Building directory cache");
732 #ifdef HAVE_EEPROM_SETTINGS
733 remove_dircache_file();
734 #endif
736 /* Background build, dircache has been previously allocated */
737 if (dircache_size > 0)
739 thread_enabled = true;
740 dircache_initializing = true;
741 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
742 return 2;
745 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
747 allocated_size = last_size + DIRCACHE_RESERVE;
748 dircache_root = buffer_alloc(allocated_size);
749 thread_enabled = true;
751 /* Start a transparent rebuild. */
752 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
753 return 3;
756 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
758 /* Start a non-transparent rebuild. */
759 return dircache_do_rebuild();
763 * Steal the allocated dircache buffer and disable dircache.
765 void* dircache_steal_buffer(long *size)
767 dircache_disable();
768 if (dircache_size == 0)
770 *size = 0;
771 return NULL;
774 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
776 return dircache_root;
780 * Main initialization function that must be called before any other
781 * operations within the dircache.
783 void dircache_init(void)
785 int i;
786 int thread_id;
788 dircache_initialized = false;
789 dircache_initializing = false;
791 memset(opendirs, 0, sizeof(opendirs));
792 for (i = 0; i < MAX_OPEN_DIRS; i++)
794 opendirs[i].theent.d_name = buffer_alloc(MAX_PATH);
797 queue_init(&dircache_queue, true);
798 thread_id = create_thread(dircache_thread, dircache_stack,
799 sizeof(dircache_stack), 0, dircache_thread_name
800 IF_PRIO(, PRIORITY_BACKGROUND)
801 IF_COP(, CPU));
802 #ifdef HAVE_IO_PRIORITY
803 thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
804 #endif
809 * Returns true if dircache has been initialized and is ready to be used.
811 bool dircache_is_enabled(void)
813 return dircache_initialized;
817 * Returns true if dircache is being initialized.
819 bool dircache_is_initializing(void)
821 return dircache_initializing || thread_enabled;
825 * Set application flags used to determine if dircache is still intact.
827 void dircache_set_appflag(long mask)
829 appflags |= mask;
833 * Get application flags used to determine if dircache is still intact.
835 bool dircache_get_appflag(long mask)
837 return dircache_is_enabled() && (appflags & mask);
841 * Returns the current number of entries (directories and files) in the cache.
843 int dircache_get_entry_count(void)
845 return entry_count;
849 * Returns the allocated space for dircache (without reserve space).
851 int dircache_get_cache_size(void)
853 return dircache_is_enabled() ? dircache_size : 0;
857 * Returns how many bytes of the reserve allocation for live cache
858 * updates have been used.
860 int dircache_get_reserve_used(void)
862 return dircache_is_enabled() ? reserve_used : 0;
866 * Returns the time in kernel ticks that took to build the cache.
868 int dircache_get_build_ticks(void)
870 return dircache_is_enabled() ? cache_build_ticks : 0;
874 * Disables the dircache. Usually called on shutdown or when
875 * accepting a usb connection.
877 void dircache_disable(void)
879 int i;
880 bool cache_in_use;
882 if (thread_enabled)
883 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
885 while (thread_enabled)
886 sleep(1);
887 dircache_initialized = false;
889 logf("Waiting for cached dirs to release");
890 do {
891 cache_in_use = false;
892 for (i = 0; i < MAX_OPEN_DIRS; i++) {
893 if (!opendirs[i].regulardir && opendirs[i].busy)
895 cache_in_use = true;
896 sleep(1);
897 break ;
900 } while (cache_in_use) ;
902 logf("Cache released");
903 entry_count = 0;
907 * Usermode function to return dircache_entry pointer to the given path.
909 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
911 if (!dircache_initialized || filename == NULL)
912 return NULL;
914 return dircache_get_entry(filename, false);
918 * Function to copy the full absolute path from dircache to the given buffer
919 * using the given dircache_entry pointer.
921 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
923 int path_size = 0;
924 int idx;
925 const struct dircache_entry *temp = entry;
927 if (size <= 0)
928 return ;
930 /* first compute the necessary size */
931 while(temp != NULL)
933 path_size += temp->name_len; /* '/' + d_name */
934 temp = temp->up;
937 /* now copy the path */
938 /* doesn't matter with trailing 0 because it's added later */
939 idx = path_size;
940 while(entry != NULL)
942 idx -= entry->name_len;
943 /* available size */
944 int rem = size - idx;
946 if(rem >= 1)
948 buf[idx] = '/';
949 memcpy(buf + idx + 1, entry->d_name, MIN((size_t)(rem - 1), (size_t)(entry->name_len - 1)));
951 entry = entry->up;
954 /* add trailing 0 */
955 buf[MIN(path_size, size-1)] = 0;
958 /* --- Directory cache live updating functions --- */
959 static int block_until_ready(void)
961 /* Block until dircache has been built. */
962 while (!dircache_initialized && dircache_is_initializing())
963 sleep(1);
965 if (!dircache_initialized)
966 return -1;
968 return 0;
971 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
973 struct dircache_entry *entry;
974 char basedir[MAX_PATH*2];
975 char *new;
976 long last_cache_size = dircache_size;
978 strlcpy(basedir, path, sizeof(basedir));
979 new = strrchr(basedir, '/');
980 if (new == NULL)
982 logf("error occurred");
983 dircache_initialized = false;
984 return NULL;
987 *new = '\0';
988 new++;
990 entry = dircache_get_entry(basedir, true);
991 if (entry == NULL)
993 logf("basedir not found!");
994 logf("%s", basedir);
995 dircache_initialized = false;
996 return NULL;
999 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
1000 >= DIRCACHE_RESERVE)
1002 logf("not enough space");
1003 dircache_initialized = false;
1004 return NULL;
1007 while (entry->next != NULL)
1008 entry = entry->next;
1010 if (entry->name_len > 0)
1011 entry = dircache_gen_next(entry);
1013 if (entry == NULL)
1015 dircache_initialized = false;
1016 return NULL;
1019 entry->attribute = attribute;
1020 entry->name_len = MIN(254, strlen(new)) + 1;
1021 entry->d_name = ((char *)dircache_root+dircache_size);
1022 entry->startcluster = 0;
1023 entry->wrtdate = 0;
1024 entry->wrttime = 0;
1025 entry->size = 0;
1026 memcpy(entry->d_name, new, entry->name_len);
1027 dircache_size += entry->name_len;
1029 if (attribute & ATTR_DIRECTORY)
1031 logf("gen_down");
1032 dircache_gen_down(entry);
1035 reserve_used += dircache_size - last_cache_size;
1037 return entry;
1040 void dircache_bind(int fd, const char *path)
1042 struct dircache_entry *entry;
1044 /* Queue requests until dircache has been built. */
1045 if (!dircache_initialized && dircache_is_initializing())
1047 if (fdbind_idx >= MAX_PENDING_BINDINGS)
1048 return ;
1049 strlcpy(fdbind_cache[fdbind_idx].path, path,
1050 sizeof(fdbind_cache[fdbind_idx].path));
1051 fdbind_cache[fdbind_idx].fd = fd;
1052 fdbind_idx++;
1053 return ;
1056 if (!dircache_initialized)
1057 return ;
1059 logf("bind: %d/%s", fd, path);
1060 entry = dircache_get_entry(path, false);
1061 if (entry == NULL)
1063 logf("not found!");
1064 dircache_initialized = false;
1065 return ;
1068 fd_bindings[fd] = entry;
1071 void dircache_update_filesize(int fd, long newsize, long startcluster)
1073 if (!dircache_initialized || fd < 0)
1074 return ;
1076 if (fd_bindings[fd] == NULL)
1078 logf("dircache fd(%d) access error", fd);
1079 dircache_initialized = false;
1080 return ;
1083 fd_bindings[fd]->size = newsize;
1084 fd_bindings[fd]->startcluster = startcluster;
1086 void dircache_update_filetime(int fd)
1088 #if CONFIG_RTC == 0
1089 (void)fd;
1090 #else
1091 short year;
1092 struct tm *now = get_time();
1093 if (!dircache_initialized || fd < 0)
1094 return ;
1096 if (fd_bindings[fd] == NULL)
1098 logf("dircache fd access error");
1099 dircache_initialized = false;
1100 return ;
1102 year = now->tm_year+1900-1980;
1103 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1104 (((now->tm_mon+1)&0xf)<<5) |
1105 (((now->tm_mday)&0x1f));
1106 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1107 (((now->tm_min)&0x3f)<<5) |
1108 (((now->tm_sec/2)&0x1f));
1109 #endif
1112 void dircache_mkdir(const char *path)
1113 { /* Test ok. */
1114 if (block_until_ready())
1115 return ;
1118 logf("mkdir: %s", path);
1119 dircache_new_entry(path, ATTR_DIRECTORY);
1122 void dircache_rmdir(const char *path)
1123 { /* Test ok. */
1124 struct dircache_entry *entry;
1126 if (block_until_ready())
1127 return ;
1129 logf("rmdir: %s", path);
1130 entry = dircache_get_entry(path, false);
1131 if (entry == NULL || entry->down == NULL)
1133 logf("not found or not a directory!");
1134 dircache_initialized = false;
1135 return ;
1138 entry->down = NULL;
1139 entry->name_len = 0;
1142 /* Remove a file from cache */
1143 void dircache_remove(const char *name)
1144 { /* Test ok. */
1145 struct dircache_entry *entry;
1147 if (block_until_ready())
1148 return ;
1150 logf("remove: %s", name);
1152 entry = dircache_get_entry(name, false);
1154 if (entry == NULL)
1156 logf("not found!");
1157 dircache_initialized = false;
1158 return ;
1161 entry->name_len = 0;
1164 void dircache_rename(const char *oldpath, const char *newpath)
1165 { /* Test ok. */
1166 struct dircache_entry *entry, *newentry;
1167 struct dircache_entry oldentry;
1168 char absolute_path[MAX_PATH*2];
1169 char *p;
1171 if (block_until_ready())
1172 return ;
1174 logf("rename: %s->%s", oldpath, newpath);
1176 entry = dircache_get_entry(oldpath, false);
1177 if (entry == NULL)
1179 logf("not found!");
1180 dircache_initialized = false;
1181 return ;
1184 /* Delete the old entry. */
1185 entry->name_len = 0;
1187 /** If we rename the same filename twice in a row, we need to
1188 * save the data, because the entry will be re-used. */
1189 oldentry = *entry;
1191 /* Generate the absolute path for destination if necessary. */
1192 if (newpath[0] != '/')
1194 strlcpy(absolute_path, oldpath, sizeof(absolute_path));
1195 p = strrchr(absolute_path, '/');
1196 if (!p)
1198 logf("Invalid path");
1199 dircache_initialized = false;
1200 return ;
1203 *p = '\0';
1204 strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
1205 newpath = absolute_path;
1208 newentry = dircache_new_entry(newpath, entry->attribute);
1209 if (newentry == NULL)
1211 dircache_initialized = false;
1212 return ;
1215 newentry->down = oldentry.down;
1216 newentry->size = oldentry.size;
1217 newentry->startcluster = oldentry.startcluster;
1218 newentry->wrttime = oldentry.wrttime;
1219 newentry->wrtdate = oldentry.wrtdate;
1222 void dircache_add_file(const char *path, long startcluster)
1224 struct dircache_entry *entry;
1226 if (block_until_ready())
1227 return ;
1229 logf("add file: %s", path);
1230 entry = dircache_new_entry(path, 0);
1231 if (entry == NULL)
1232 return ;
1234 entry->startcluster = startcluster;
1237 static bool is_disable_msg_pending(void)
1239 return check_event_queue();
1242 DIR_CACHED* opendir_cached(const char* name)
1244 int dd;
1245 DIR_CACHED* pdir = opendirs;
1247 if ( name[0] != '/' )
1249 DEBUGF("Only absolute paths supported right now\n");
1250 return NULL;
1253 /* find a free dir descriptor */
1254 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1255 if ( !pdir->busy )
1256 break;
1258 if ( dd == MAX_OPEN_DIRS )
1260 DEBUGF("Too many dirs open\n");
1261 errno = EMFILE;
1262 return NULL;
1265 pdir->busy = true;
1267 if (!dircache_initialized || is_disable_msg_pending())
1269 pdir->internal_entry = NULL;
1270 pdir->regulardir = opendir_uncached(name);
1272 else
1274 pdir->regulardir = NULL;
1275 pdir->internal_entry = dircache_get_entry(name, true);
1276 pdir->theent.attribute = -1; /* used to make readdir_cached aware of the first call */
1279 if (pdir->internal_entry == NULL && pdir->regulardir == NULL)
1281 pdir->busy = false;
1282 return NULL;
1285 return pdir;
1288 struct dirent_cached* readdir_cached(DIR_CACHED* dir)
1290 struct dircache_entry *ce = dir->internal_entry;
1291 struct dirent_uncached *regentry;
1293 if (!dir->busy)
1294 return NULL;
1296 if (dir->regulardir != NULL)
1298 regentry = readdir_uncached(dir->regulardir);
1299 if (regentry == NULL)
1300 return NULL;
1302 strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
1303 dir->theent.size = regentry->size;
1304 dir->theent.startcluster = regentry->startcluster;
1305 dir->theent.attribute = regentry->attribute;
1306 dir->theent.wrttime = regentry->wrttime;
1307 dir->theent.wrtdate = regentry->wrtdate;
1309 return &dir->theent;
1312 /* if theent.attribute=-1 then this is the first call */
1313 /* otherwise, this is is not so we first take the entry's ->next */
1314 /* NOTE: normal file can't have attribute=-1 */
1315 if(dir->theent.attribute != -1)
1316 ce = ce->next;
1317 /* skip unused entries */
1318 while(ce != NULL && ce->name_len == 0)
1319 ce = ce->next;
1321 if (ce == NULL)
1322 return NULL;
1324 strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
1325 /* Can't do `dir->theent = *ce`
1326 because that modifies the d_name pointer. */
1327 dir->theent.size = ce->size;
1328 dir->theent.startcluster = ce->startcluster;
1329 dir->theent.attribute = ce->attribute;
1330 dir->theent.wrttime = ce->wrttime;
1331 dir->theent.wrtdate = ce->wrtdate;
1332 dir->internal_entry = ce;
1334 //logf("-> %s", ce->name);
1335 return &dir->theent;
1338 int closedir_cached(DIR_CACHED* dir)
1340 if (!dir->busy)
1341 return -1;
1343 dir->busy=false;
1344 if (dir->regulardir != NULL)
1345 return closedir_uncached(dir->regulardir);
1347 return 0;
1350 int mkdir_cached(const char *name)
1352 int rc=mkdir_uncached(name);
1353 if (rc >= 0)
1354 dircache_mkdir(name);
1355 return(rc);
1358 int rmdir_cached(const char* name)
1360 int rc=rmdir_uncached(name);
1361 if(rc >= 0)
1362 dircache_rmdir(name);
1363 return(rc);