Fix the c200 manual which was broken by yesterday's button action macros in the radio...
[Rockbox.git] / firmware / common / dircache.c
blobed392d6186f93ba8a9691c8a8286ec847fa351f5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 /* TODO:
21 - Allow cache live updating while transparent rebuild is running.
24 #include "config.h"
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include "debug.h"
31 #include "atoi.h"
32 #include "system.h"
33 #include "logf.h"
34 #include "dircache.h"
35 #include "thread.h"
36 #include "kernel.h"
37 #include "usb.h"
38 #include "file.h"
39 #include "buffer.h"
40 #if CONFIG_RTC
41 #include "time.h"
42 #include "timefuncs.h"
43 #endif
45 /* Queue commands. */
46 #define DIRCACHE_BUILD 1
47 #define DIRCACHE_STOP 2
49 #define MAX_OPEN_DIRS 8
50 DIR_CACHED opendirs[MAX_OPEN_DIRS];
52 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
53 static struct dircache_entry *dircache_root;
55 static bool dircache_initialized = false;
56 static bool dircache_initializing = false;
57 static bool thread_enabled = false;
58 static unsigned long allocated_size = DIRCACHE_LIMIT;
59 static unsigned long dircache_size = 0;
60 static unsigned long entry_count = 0;
61 static unsigned long reserve_used = 0;
62 static unsigned int cache_build_ticks = 0;
63 static unsigned long appflags = 0;
64 static char dircache_cur_path[MAX_PATH*2];
66 static struct event_queue dircache_queue;
67 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
68 static const char dircache_thread_name[] = "dircache";
70 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
71 static int fdbind_idx = 0;
73 /* --- Internal cache structure control functions --- */
75 /**
76 * Internal function to allocate a new dircache_entry from memory.
78 static struct dircache_entry* allocate_entry(void)
80 struct dircache_entry *next_entry;
82 if (dircache_size > allocated_size - MAX_PATH*2)
84 logf("size limit reached");
85 return NULL;
88 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
89 #ifdef ROCKBOX_STRICT_ALIGN
90 /* Make sure the entry is long aligned. */
91 if ((long)next_entry & 0x03)
93 dircache_size += 4 - ((long)next_entry & 0x03);
94 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
96 #endif
97 next_entry->name_len = 0;
98 next_entry->d_name = NULL;
99 next_entry->up = NULL;
100 next_entry->down = NULL;
101 next_entry->next = NULL;
103 dircache_size += sizeof(struct dircache_entry);
105 return next_entry;
109 * Internal function to allocate a dircache_entry and set
110 * ->next entry pointers.
112 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
114 struct dircache_entry *next_entry;
116 if ( (next_entry = allocate_entry()) == NULL)
117 return NULL;
118 next_entry->up = ce->up;
119 ce->next = next_entry;
121 return next_entry;
125 * Internal function to allocate a dircache_entry and set
126 * ->down entry pointers.
128 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
130 struct dircache_entry *next_entry;
132 if ( (next_entry = allocate_entry()) == NULL)
133 return NULL;
134 next_entry->up = ce;
135 ce->down = next_entry;
137 return next_entry;
140 /* This will eat ~30 KiB of memory!
141 * We should probably use that as additional reserve buffer in future. */
142 #define MAX_SCAN_DEPTH 16
143 static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
146 * Returns true if there is an event waiting in the queue
147 * that requires the current operation to be aborted.
149 static bool check_event_queue(void)
151 struct queue_event ev;
153 queue_wait_w_tmo(&dircache_queue, &ev, 0);
154 switch (ev.id)
156 case DIRCACHE_STOP:
157 case SYS_USB_CONNECTED:
158 /* Put the event back into the queue. */
159 queue_post(&dircache_queue, ev.id, ev.data);
160 return true;
163 return false;
167 * Internal function to iterate a path.
169 static int dircache_scan(struct travel_data *td)
171 #ifdef SIMULATOR
172 while ( ( td->entry = readdir_uncached(td->dir) ) )
173 #else
174 while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
175 #endif
177 #ifdef SIMULATOR
178 if (!strcmp(".", td->entry->d_name) ||
179 !strcmp("..", td->entry->d_name))
181 continue;
184 td->ce->attribute = td->entry->attribute;
185 td->ce->name_len = strlen(td->entry->d_name) + 1;
186 td->ce->d_name = ((char *)dircache_root+dircache_size);
187 td->ce->size = td->entry->size;
188 td->ce->wrtdate = td->entry->wrtdate;
189 td->ce->wrttime = td->entry->wrttime;
190 memcpy(td->ce->d_name, td->entry->d_name, td->ce->name_len);
191 #else
192 if (!strcmp(".", td->entry.name) ||
193 !strcmp("..", td->entry.name))
195 continue;
198 td->ce->attribute = td->entry.attr;
199 td->ce->name_len = strlen(td->entry.name) + 1;
200 td->ce->d_name = ((char *)dircache_root+dircache_size);
201 td->ce->startcluster = td->entry.firstcluster;
202 td->ce->size = td->entry.filesize;
203 td->ce->wrtdate = td->entry.wrtdate;
204 td->ce->wrttime = td->entry.wrttime;
205 memcpy(td->ce->d_name, td->entry.name, td->ce->name_len);
206 #endif
207 dircache_size += td->ce->name_len;
208 entry_count++;
210 #ifdef SIMULATOR
211 if (td->entry->attribute & ATTR_DIRECTORY)
212 #else
213 if (td->entry.attr & FAT_ATTR_DIRECTORY)
214 #endif
217 td->down_entry = dircache_gen_down(td->ce);
218 if (td->down_entry == NULL)
219 return -2;
221 td->pathpos = strlen(dircache_cur_path);
222 strncpy(&dircache_cur_path[td->pathpos], "/",
223 sizeof(dircache_cur_path) - td->pathpos - 1);
224 #ifdef SIMULATOR
225 strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
226 sizeof(dircache_cur_path) - td->pathpos - 2);
228 td->newdir = opendir_uncached(dircache_cur_path);
229 if (td->newdir == NULL)
231 logf("Failed to opendir_uncached(): %s", dircache_cur_path);
232 return -3;
234 #else
235 strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
236 sizeof(dircache_cur_path) - td->pathpos - 2);
238 td->newdir = *td->dir;
239 if (fat_opendir(IF_MV2(volume,) &td->newdir,
240 td->entry.firstcluster, td->dir) < 0 )
242 return -3;
244 #endif
246 td->ce = dircache_gen_next(td->ce);
247 if (td->ce == NULL)
248 return -4;
250 return 1;
253 td->ce->down = NULL;
254 td->ce = dircache_gen_next(td->ce);
255 if (td->ce == NULL)
256 return -5;
258 /* When simulator is used, it's only safe to yield here. */
259 if (thread_enabled)
261 /* Stop if we got an external signal. */
262 if (check_event_queue())
263 return -6;
264 yield();
269 return 0;
272 /**
273 * Recursively scan the hard disk and build the cache.
275 #ifdef SIMULATOR
276 static int dircache_travel(DIR_UNCACHED *dir, struct dircache_entry *ce)
277 #else
278 static int dircache_travel(struct fat_dir *dir, struct dircache_entry *ce)
279 #endif
281 int depth = 0;
282 int result;
284 memset(ce, 0, sizeof(struct dircache_entry));
285 dir_recursion[0].dir = dir;
286 dir_recursion[0].ce = ce;
287 dir_recursion[0].first = ce;
289 do {
290 //logf("=> %s", dircache_cur_path);
291 result = dircache_scan(&dir_recursion[depth]);
292 switch (result) {
293 case 0: /* Leaving the current directory. */
294 /* Add the standard . and .. entries. */
295 ce = dir_recursion[depth].ce;
296 ce->d_name = ".";
297 ce->name_len = 2;
298 #ifdef SIMULATOR
299 closedir_uncached(dir_recursion[depth].dir);
300 ce->attribute = ATTR_DIRECTORY;
301 #else
302 ce->attribute = FAT_ATTR_DIRECTORY;
303 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
304 #endif
305 ce->size = 0;
306 ce->down = dir_recursion[depth].first;
308 depth--;
309 if (depth < 0)
310 break ;
312 dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
314 ce = dircache_gen_next(ce);
315 if (ce == NULL)
317 logf("memory allocation error");
318 return -3;
320 #ifdef SIMULATOR
321 ce->attribute = ATTR_DIRECTORY;
322 #else
323 ce->attribute = FAT_ATTR_DIRECTORY;
324 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
325 #endif
326 ce->d_name = "..";
327 ce->name_len = 3;
328 ce->size = 0;
329 ce->down = dir_recursion[depth].first;
331 break ;
333 case 1: /* Going down in the directory tree. */
334 depth++;
335 if (depth >= MAX_SCAN_DEPTH)
337 logf("Too deep directory structure");
338 return -2;
341 #ifdef SIMULATOR
342 dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
343 #else
344 dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
345 #endif
346 dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
347 dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
348 break ;
350 default:
351 logf("Scan failed");
352 logf("-> %s", dircache_cur_path);
353 return -1;
355 } while (depth >= 0) ;
357 return 0;
361 * Internal function to get a pointer to dircache_entry for a given filename.
362 * path: Absolute path to a file or directory.
363 * get_before: Returns the cache pointer before the last valid entry found.
364 * only_directories: Match only filenames which are a directory type.
366 static struct dircache_entry* dircache_get_entry(const char *path,
367 bool get_before, bool only_directories)
369 struct dircache_entry *cache_entry, *before;
370 char namecopy[MAX_PATH*2];
371 char* part;
372 char* end;
374 strncpy(namecopy, path, sizeof(namecopy) - 1);
375 cache_entry = dircache_root;
376 before = NULL;
378 for ( part = strtok_r(namecopy, "/", &end); part;
379 part = strtok_r(NULL, "/", &end)) {
381 /* scan dir for name */
382 while (1)
384 if (cache_entry == NULL)
386 return NULL;
388 else if (cache_entry->name_len == 0)
390 cache_entry = cache_entry->next;
391 continue ;
394 if (!strcasecmp(part, cache_entry->d_name))
396 before = cache_entry;
397 if (cache_entry->down || only_directories)
398 cache_entry = cache_entry->down;
399 break ;
402 cache_entry = cache_entry->next;
406 if (get_before)
407 cache_entry = before;
409 return cache_entry;
412 #ifdef HAVE_EEPROM_SETTINGS
414 * Function to load the internal cache structure from disk to initialize
415 * the dircache really fast and little disk access.
417 int dircache_load(void)
419 struct dircache_maindata maindata;
420 int bytes_read;
421 int fd;
423 if (dircache_initialized)
424 return -1;
426 logf("Loading directory cache");
427 dircache_size = 0;
429 fd = open(DIRCACHE_FILE, O_RDONLY);
430 if (fd < 0)
431 return -2;
433 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
434 if (bytes_read != sizeof(struct dircache_maindata)
435 || maindata.size <= 0)
437 logf("Dircache file header error");
438 close(fd);
439 remove(DIRCACHE_FILE);
440 return -3;
443 dircache_root = buffer_alloc(0);
444 if ((long)maindata.root_entry != (long)dircache_root)
446 logf("Position missmatch");
447 close(fd);
448 remove(DIRCACHE_FILE);
449 return -4;
452 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
453 entry_count = maindata.entry_count;
454 appflags = maindata.appflags;
455 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
456 close(fd);
457 remove(DIRCACHE_FILE);
459 if (bytes_read != maindata.size)
461 logf("Dircache read failed");
462 return -6;
465 /* Cache successfully loaded. */
466 dircache_size = maindata.size;
467 allocated_size = dircache_size + DIRCACHE_RESERVE;
468 reserve_used = 0;
469 logf("Done, %ld KiB used", dircache_size / 1024);
470 dircache_initialized = true;
471 memset(fd_bindings, 0, sizeof(fd_bindings));
473 return 0;
477 * Function to save the internal cache stucture to disk for fast loading
478 * on boot.
480 int dircache_save(void)
482 struct dircache_maindata maindata;
483 int fd;
484 unsigned long bytes_written;
486 remove(DIRCACHE_FILE);
488 if (!dircache_initialized)
489 return -1;
491 logf("Saving directory cache");
492 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
494 maindata.magic = DIRCACHE_MAGIC;
495 maindata.size = dircache_size;
496 maindata.root_entry = dircache_root;
497 maindata.entry_count = entry_count;
498 maindata.appflags = appflags;
500 /* Save the info structure */
501 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
502 if (bytes_written != sizeof(struct dircache_maindata))
504 close(fd);
505 logf("dircache: write failed #1");
506 return -2;
509 /* Dump whole directory cache to disk */
510 bytes_written = write(fd, dircache_root, dircache_size);
511 close(fd);
512 if (bytes_written != dircache_size)
514 logf("dircache: write failed #2");
515 return -3;
518 return 0;
520 #endif /* #if 0 */
523 * Internal function which scans the disk and creates the dircache structure.
525 static int dircache_do_rebuild(void)
527 #ifdef SIMULATOR
528 DIR_UNCACHED *pdir;
529 #else
530 struct fat_dir dir, *pdir;
531 #endif
532 unsigned int start_tick;
533 int i;
535 /* Measure how long it takes build the cache. */
536 start_tick = current_tick;
537 dircache_initializing = true;
538 appflags = 0;
540 #ifdef SIMULATOR
541 pdir = opendir_uncached("/");
542 if (pdir == NULL)
544 logf("Failed to open rootdir");
545 dircache_initializing = false;
546 return -3;
548 #else
549 if ( fat_opendir(IF_MV2(volume,) &dir, 0, NULL) < 0 ) {
550 logf("Failed opening root dir");
551 dircache_initializing = false;
552 return -3;
554 pdir = &dir;
555 #endif
557 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
558 dircache_size = sizeof(struct dircache_entry);
560 cpu_boost(true);
561 if (dircache_travel(pdir, dircache_root) < 0)
563 logf("dircache_travel failed");
564 cpu_boost(false);
565 dircache_size = 0;
566 dircache_initializing = false;
567 return -2;
569 cpu_boost(false);
571 logf("Done, %ld KiB used", dircache_size / 1024);
573 dircache_initialized = true;
574 dircache_initializing = false;
575 cache_build_ticks = current_tick - start_tick;
577 /* Initialized fd bindings. */
578 memset(fd_bindings, 0, sizeof(fd_bindings));
579 for (i = 0; i < fdbind_idx; i++)
580 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
581 fdbind_idx = 0;
583 if (thread_enabled)
585 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
586 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
588 else
590 /* We have to long align the audiobuf to keep the buffer access fast. */
591 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
592 audiobuf += DIRCACHE_RESERVE;
593 allocated_size = dircache_size + DIRCACHE_RESERVE;
594 reserve_used = 0;
597 return 1;
601 * Internal thread that controls transparent cache building.
603 static void dircache_thread(void)
605 struct queue_event ev;
607 while (1)
609 queue_wait(&dircache_queue, &ev);
611 switch (ev.id)
613 case DIRCACHE_BUILD:
614 thread_enabled = true;
615 dircache_do_rebuild();
616 thread_enabled = false;
617 break ;
619 case DIRCACHE_STOP:
620 logf("Stopped the rebuilding.");
621 dircache_initialized = false;
622 break ;
624 #ifndef SIMULATOR
625 case SYS_USB_CONNECTED:
626 usb_acknowledge(SYS_USB_CONNECTED_ACK);
627 usb_wait_for_disconnect(&dircache_queue);
628 break ;
629 #endif
635 * Start scanning the disk to build the dircache.
636 * Either transparent or non-transparent build method is used.
638 int dircache_build(int last_size)
640 if (dircache_initialized || thread_enabled)
641 return -3;
643 logf("Building directory cache");
644 remove(DIRCACHE_FILE);
646 /* Background build, dircache has been previously allocated */
647 if (dircache_size > 0)
649 thread_enabled = true;
650 dircache_initializing = true;
651 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
652 return 2;
655 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
657 allocated_size = last_size + DIRCACHE_RESERVE;
658 dircache_root = buffer_alloc(allocated_size);
659 thread_enabled = true;
661 /* Start a transparent rebuild. */
662 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
663 return 3;
666 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
668 /* Start a non-transparent rebuild. */
669 return dircache_do_rebuild();
673 * Steal the allocated dircache buffer and disable dircache.
675 void* dircache_steal_buffer(long *size)
677 dircache_disable();
678 if (dircache_size == 0)
680 *size = 0;
681 return NULL;
684 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
686 return dircache_root;
690 * Main initialization function that must be called before any other
691 * operations within the dircache.
693 void dircache_init(void)
695 int i;
697 dircache_initialized = false;
698 dircache_initializing = false;
700 memset(opendirs, 0, sizeof(opendirs));
701 for (i = 0; i < MAX_OPEN_DIRS; i++)
703 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
706 queue_init(&dircache_queue, true);
707 create_thread(dircache_thread, dircache_stack,
708 sizeof(dircache_stack), 0, dircache_thread_name
709 IF_PRIO(, PRIORITY_BACKGROUND)
710 IF_COP(, CPU));
714 * Returns true if dircache has been initialized and is ready to be used.
716 bool dircache_is_enabled(void)
718 return dircache_initialized;
722 * Returns true if dircache is being initialized.
724 bool dircache_is_initializing(void)
726 return dircache_initializing || thread_enabled;
730 * Set application flags used to determine if dircache is still intact.
732 void dircache_set_appflag(long mask)
734 appflags |= mask;
738 * Get application flags used to determine if dircache is still intact.
740 bool dircache_get_appflag(long mask)
742 return dircache_is_enabled() && (appflags & mask);
746 * Returns the current number of entries (directories and files) in the cache.
748 int dircache_get_entry_count(void)
750 return entry_count;
754 * Returns the allocated space for dircache (without reserve space).
756 int dircache_get_cache_size(void)
758 return dircache_is_enabled() ? dircache_size : 0;
762 * Returns how many bytes of the reserve allocation for live cache
763 * updates have been used.
765 int dircache_get_reserve_used(void)
767 return dircache_is_enabled() ? reserve_used : 0;
771 * Returns the time in kernel ticks that took to build the cache.
773 int dircache_get_build_ticks(void)
775 return dircache_is_enabled() ? cache_build_ticks : 0;
779 * Disables the dircache. Usually called on shutdown or when
780 * accepting a usb connection.
782 void dircache_disable(void)
784 int i;
785 bool cache_in_use;
787 if (thread_enabled)
788 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
790 while (thread_enabled)
791 sleep(1);
792 dircache_initialized = false;
794 logf("Waiting for cached dirs to release");
795 do {
796 cache_in_use = false;
797 for (i = 0; i < MAX_OPEN_DIRS; i++) {
798 if (!opendirs[i].regulardir && opendirs[i].busy)
800 cache_in_use = true;
801 sleep(1);
802 break ;
805 } while (cache_in_use) ;
807 logf("Cache released");
808 entry_count = 0;
812 * Usermode function to return dircache_entry pointer to the given path.
814 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
816 if (!dircache_initialized || filename == NULL)
817 return NULL;
819 return dircache_get_entry(filename, false, false);
823 * Function to copy the full absolute path from dircache to the given buffer
824 * using the given dircache_entry pointer.
826 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
828 const struct dircache_entry *down[MAX_SCAN_DEPTH];
829 int depth = 0;
831 if (size <= 0)
832 return ;
834 buf[0] = '\0';
836 if (entry == NULL)
837 return ;
839 do {
840 down[depth] = entry;
841 entry = entry->up;
842 depth++;
843 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
845 while (--depth >= 0)
847 snprintf(buf, size, "/%s", down[depth]->d_name);
848 buf += down[depth]->name_len; /* '/' + d_name */
849 size -= down[depth]->name_len;
850 if (size <= 0)
851 break ;
855 /* --- Directory cache live updating functions --- */
856 static int block_until_ready(void)
858 /* Block until dircache has been built. */
859 while (!dircache_initialized && dircache_is_initializing())
860 sleep(1);
862 if (!dircache_initialized)
863 return -1;
865 return 0;
868 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
870 struct dircache_entry *entry;
871 char basedir[MAX_PATH*2];
872 char *new;
873 long last_cache_size = dircache_size;
875 strncpy(basedir, path, sizeof(basedir)-1);
876 new = strrchr(basedir, '/');
877 if (new == NULL)
879 logf("error occurred");
880 dircache_initialized = false;
881 return NULL;
884 *new = '\0';
885 new++;
887 entry = dircache_get_entry(basedir, false, true);
888 if (entry == NULL)
890 logf("basedir not found!");
891 logf("%s", basedir);
892 dircache_initialized = false;
893 return NULL;
896 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
897 >= DIRCACHE_RESERVE)
899 logf("not enough space");
900 dircache_initialized = false;
901 return NULL;
904 while (entry->next != NULL)
905 entry = entry->next;
907 if (entry->name_len > 0)
908 entry = dircache_gen_next(entry);
910 if (entry == NULL)
912 dircache_initialized = false;
913 return NULL;
916 entry->attribute = attribute;
917 entry->name_len = MIN(254, strlen(new)) + 1;
918 entry->d_name = ((char *)dircache_root+dircache_size);
919 entry->startcluster = 0;
920 entry->wrtdate = 0;
921 entry->wrttime = 0;
922 entry->size = 0;
923 memcpy(entry->d_name, new, entry->name_len);
924 dircache_size += entry->name_len;
926 if (attribute & ATTR_DIRECTORY)
928 logf("gen_down");
929 dircache_gen_down(entry);
932 reserve_used += dircache_size - last_cache_size;
934 return entry;
937 void dircache_bind(int fd, const char *path)
939 struct dircache_entry *entry;
941 /* Queue requests until dircache has been built. */
942 if (!dircache_initialized && dircache_is_initializing())
944 if (fdbind_idx >= MAX_PENDING_BINDINGS)
945 return ;
946 strncpy(fdbind_cache[fdbind_idx].path, path,
947 sizeof(fdbind_cache[fdbind_idx].path)-1);
948 fdbind_cache[fdbind_idx].fd = fd;
949 fdbind_idx++;
950 return ;
953 if (!dircache_initialized)
954 return ;
956 logf("bind: %d/%s", fd, path);
957 entry = dircache_get_entry(path, false, false);
958 if (entry == NULL)
960 logf("not found!");
961 dircache_initialized = false;
962 return ;
965 fd_bindings[fd] = entry;
968 void dircache_update_filesize(int fd, long newsize, long startcluster)
970 if (!dircache_initialized || fd < 0)
971 return ;
973 if (fd_bindings[fd] == NULL)
975 logf("dircache fd access error");
976 dircache_initialized = false;
977 return ;
980 fd_bindings[fd]->size = newsize;
981 fd_bindings[fd]->startcluster = startcluster;
983 void dircache_update_filetime(int fd)
985 #if CONFIG_RTC == 0
986 (void)fd;
987 #else
988 short year;
989 struct tm *now = get_time();
990 if (!dircache_initialized || fd < 0)
991 return ;
993 if (fd_bindings[fd] == NULL)
995 logf("dircache fd access error");
996 dircache_initialized = false;
997 return ;
999 year = now->tm_year+1900-1980;
1000 fd_bindings[fd]->wrtdate = (((year)&0x7f)<<9) |
1001 (((now->tm_mon+1)&0xf)<<5) |
1002 (((now->tm_mday)&0x1f));
1003 fd_bindings[fd]->wrttime = (((now->tm_hour)&0x1f)<<11) |
1004 (((now->tm_min)&0x3f)<<5) |
1005 (((now->tm_sec/2)&0x1f));
1006 #endif
1009 void dircache_mkdir(const char *path)
1010 { /* Test ok. */
1011 if (block_until_ready())
1012 return ;
1014 logf("mkdir: %s", path);
1015 dircache_new_entry(path, ATTR_DIRECTORY);
1018 void dircache_rmdir(const char *path)
1019 { /* Test ok. */
1020 struct dircache_entry *entry;
1022 if (block_until_ready())
1023 return ;
1025 logf("rmdir: %s", path);
1026 entry = dircache_get_entry(path, true, true);
1027 if (entry == NULL)
1029 logf("not found!");
1030 dircache_initialized = false;
1031 return ;
1034 entry->down = NULL;
1035 entry->name_len = 0;
1038 /* Remove a file from cache */
1039 void dircache_remove(const char *name)
1040 { /* Test ok. */
1041 struct dircache_entry *entry;
1043 if (block_until_ready())
1044 return ;
1046 logf("remove: %s", name);
1048 entry = dircache_get_entry(name, false, false);
1050 if (entry == NULL)
1052 logf("not found!");
1053 dircache_initialized = false;
1054 return ;
1057 entry->name_len = 0;
1060 void dircache_rename(const char *oldpath, const char *newpath)
1061 { /* Test ok. */
1062 struct dircache_entry *entry, *newentry;
1063 struct dircache_entry oldentry;
1064 char absolute_path[MAX_PATH*2];
1065 char *p;
1067 if (block_until_ready())
1068 return ;
1070 logf("rename: %s->%s", oldpath, newpath);
1072 entry = dircache_get_entry(oldpath, true, false);
1073 if (entry == NULL)
1075 logf("not found!");
1076 dircache_initialized = false;
1077 return ;
1080 /* Delete the old entry. */
1081 entry->name_len = 0;
1083 /** If we rename the same filename twice in a row, we need to
1084 * save the data, because the entry will be re-used. */
1085 oldentry = *entry;
1087 /* Generate the absolute path for destination if necessary. */
1088 if (newpath[0] != '/')
1090 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
1091 p = strrchr(absolute_path, '/');
1092 if (!p)
1094 logf("Invalid path");
1095 dircache_initialized = false;
1096 return ;
1099 *p = '\0';
1100 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
1101 newpath = absolute_path;
1104 newentry = dircache_new_entry(newpath, entry->attribute);
1105 if (newentry == NULL)
1107 dircache_initialized = false;
1108 return ;
1111 newentry->down = oldentry.down;
1112 newentry->size = oldentry.size;
1113 newentry->startcluster = oldentry.startcluster;
1114 newentry->wrttime = oldentry.wrttime;
1115 newentry->wrtdate = oldentry.wrtdate;
1118 void dircache_add_file(const char *path, long startcluster)
1120 struct dircache_entry *entry;
1122 if (block_until_ready())
1123 return ;
1125 logf("add file: %s", path);
1126 entry = dircache_new_entry(path, 0);
1127 if (entry == NULL)
1128 return ;
1130 entry->startcluster = startcluster;
1133 DIR_CACHED* opendir_cached(const char* name)
1135 struct dircache_entry *cache_entry;
1136 int dd;
1137 DIR_CACHED* pdir = opendirs;
1139 if ( name[0] != '/' )
1141 DEBUGF("Only absolute paths supported right now\n");
1142 return NULL;
1145 /* find a free dir descriptor */
1146 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1147 if ( !pdir->busy )
1148 break;
1150 if ( dd == MAX_OPEN_DIRS )
1152 DEBUGF("Too many dirs open\n");
1153 errno = EMFILE;
1154 return NULL;
1157 if (!dircache_initialized)
1159 pdir->regulardir = opendir_uncached(name);
1160 if (!pdir->regulardir)
1161 return NULL;
1163 pdir->busy = true;
1164 return pdir;
1167 pdir->busy = true;
1168 pdir->regulardir = NULL;
1169 cache_entry = dircache_get_entry(name, false, true);
1170 pdir->entry = cache_entry;
1172 if (cache_entry == NULL)
1174 pdir->busy = false;
1175 return NULL;
1178 return pdir;
1181 struct dircache_entry* readdir_cached(DIR_CACHED* dir)
1183 struct dirent_uncached *regentry;
1184 struct dircache_entry *ce;
1186 if (!dir->busy)
1187 return NULL;
1189 if (dir->regulardir != NULL)
1191 regentry = readdir_uncached(dir->regulardir);
1192 if (regentry == NULL)
1193 return NULL;
1195 strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
1196 dir->secondary_entry.size = regentry->size;
1197 dir->secondary_entry.startcluster = regentry->startcluster;
1198 dir->secondary_entry.attribute = regentry->attribute;
1199 dir->secondary_entry.wrttime = regentry->wrttime;
1200 dir->secondary_entry.wrtdate = regentry->wrtdate;
1201 dir->secondary_entry.next = NULL;
1203 return &dir->secondary_entry;
1206 do {
1207 if (dir->entry == NULL)
1208 return NULL;
1210 ce = dir->entry;
1211 if (ce->name_len == 0)
1212 dir->entry = ce->next;
1213 } while (ce->name_len == 0) ;
1215 dir->entry = ce->next;
1217 strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
1218 /* Can't do `dir->secondary_entry = *ce`
1219 because that modifies the d_name pointer. */
1220 dir->secondary_entry.size = ce->size;
1221 dir->secondary_entry.startcluster = ce->startcluster;
1222 dir->secondary_entry.attribute = ce->attribute;
1223 dir->secondary_entry.wrttime = ce->wrttime;
1224 dir->secondary_entry.wrtdate = ce->wrtdate;
1225 dir->secondary_entry.next = NULL;
1226 dir->internal_entry = ce;
1228 //logf("-> %s", ce->name);
1229 return &dir->secondary_entry;
1232 int closedir_cached(DIR_CACHED* dir)
1234 if (!dir->busy)
1235 return -1;
1237 dir->busy=false;
1238 if (dir->regulardir != NULL)
1239 return closedir_uncached(dir->regulardir);
1241 return 0;
1244 int mkdir_cached(const char *name)
1246 int rc=mkdir_uncached(name);
1247 if (rc >= 0)
1248 dircache_mkdir(name);
1249 return(rc);
1252 int rmdir_cached(const char* name)
1254 int rc=rmdir_uncached(name);
1255 if(rc>=0)
1256 dircache_rmdir(name);
1257 return(rc);