Fix several printf-style warnings in logf builds.
[Rockbox.git] / firmware / common / dircache.c
blob023f00f8485c491fc0f475bcc5f563bed78e6cbb
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 "dir.h"
31 #include "debug.h"
32 #include "atoi.h"
33 #include "system.h"
34 #include "logf.h"
35 #include "dircache.h"
36 #include "thread.h"
37 #include "kernel.h"
38 #include "usb.h"
39 #include "file.h"
40 #include "buffer.h"
42 /* Queue commands. */
43 #define DIRCACHE_BUILD 1
44 #define DIRCACHE_STOP 2
46 #define MAX_OPEN_DIRS 8
47 DIRCACHED opendirs[MAX_OPEN_DIRS];
49 static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
50 static struct dircache_entry *dircache_root;
52 static bool dircache_initialized = false;
53 static bool dircache_initializing = false;
54 static bool thread_enabled = false;
55 static unsigned long allocated_size = DIRCACHE_LIMIT;
56 static unsigned long dircache_size = 0;
57 static unsigned long entry_count = 0;
58 static unsigned long reserve_used = 0;
59 static unsigned int cache_build_ticks = 0;
60 static char dircache_cur_path[MAX_PATH*2];
62 static struct event_queue dircache_queue;
63 static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
64 static const char dircache_thread_name[] = "dircache";
66 static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
67 static int fdbind_idx = 0;
69 /* --- Internal cache structure control functions --- */
71 /**
72 * Internal function to allocate a new dircache_entry from memory.
74 static struct dircache_entry* allocate_entry(void)
76 struct dircache_entry *next_entry;
78 if (dircache_size > allocated_size - MAX_PATH*2)
80 logf("size limit reached");
81 return NULL;
84 next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
85 #ifdef ROCKBOX_STRICT_ALIGN
86 /* Make sure the entry is long aligned. */
87 if ((long)next_entry & 0x03)
89 dircache_size += 4 - ((long)next_entry & 0x03);
90 next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
92 #endif
93 next_entry->name_len = 0;
94 next_entry->d_name = NULL;
95 next_entry->up = NULL;
96 next_entry->down = NULL;
97 next_entry->next = NULL;
99 dircache_size += sizeof(struct dircache_entry);
101 return next_entry;
105 * Internal function to allocate a dircache_entry and set
106 * ->next entry pointers.
108 static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
110 struct dircache_entry *next_entry;
112 if ( (next_entry = allocate_entry()) == NULL)
113 return NULL;
114 next_entry->up = ce->up;
115 ce->next = next_entry;
117 return next_entry;
121 * Internal function to allocate a dircache_entry and set
122 * ->down entry pointers.
124 static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
126 struct dircache_entry *next_entry;
128 if ( (next_entry = allocate_entry()) == NULL)
129 return NULL;
130 next_entry->up = ce;
131 ce->down = next_entry;
133 return next_entry;
136 /* This will eat ~30 KiB of memory!
137 * We should probably use that as additional reserve buffer in future. */
138 #define MAX_SCAN_DEPTH 16
139 static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
142 * Returns true if there is an event waiting in the queue
143 * that requires the current operation to be aborted.
145 static bool check_event_queue(void)
147 struct event ev;
149 queue_wait_w_tmo(&dircache_queue, &ev, 0);
150 switch (ev.id)
152 case DIRCACHE_STOP:
153 case SYS_USB_CONNECTED:
154 /* Put the event back into the queue. */
155 queue_post(&dircache_queue, ev.id, ev.data);
156 return true;
159 return false;
163 * Internal function to iterate a path.
165 static int dircache_scan(struct travel_data *td)
167 #ifdef SIMULATOR
168 while ( ( td->entry = readdir(td->dir) ) )
169 #else
170 while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
171 #endif
173 #ifdef SIMULATOR
174 if (!strcmp(".", td->entry->d_name) ||
175 !strcmp("..", td->entry->d_name))
177 continue;
180 td->ce->attribute = td->entry->attribute;
181 td->ce->name_len = strlen(td->entry->d_name) + 1;
182 td->ce->d_name = ((char *)dircache_root+dircache_size);
183 td->ce->size = td->entry->size;
184 td->ce->wrtdate = td->entry->wrtdate;
185 td->ce->wrttime = td->entry->wrttime;
186 memcpy(td->ce->d_name, td->entry->d_name, td->ce->name_len);
187 #else
188 if (!strcmp(".", td->entry.name) ||
189 !strcmp("..", td->entry.name))
191 continue;
194 td->ce->attribute = td->entry.attr;
195 td->ce->name_len = strlen(td->entry.name) + 1;
196 td->ce->d_name = ((char *)dircache_root+dircache_size);
197 td->ce->startcluster = td->entry.firstcluster;
198 td->ce->size = td->entry.filesize;
199 td->ce->wrtdate = td->entry.wrtdate;
200 td->ce->wrttime = td->entry.wrttime;
201 memcpy(td->ce->d_name, td->entry.name, td->ce->name_len);
202 #endif
203 dircache_size += td->ce->name_len;
204 entry_count++;
206 #ifdef SIMULATOR
207 if (td->entry->attribute & ATTR_DIRECTORY)
208 #else
209 if (td->entry.attr & FAT_ATTR_DIRECTORY)
210 #endif
213 td->down_entry = dircache_gen_down(td->ce);
214 if (td->down_entry == NULL)
215 return -2;
217 td->pathpos = strlen(dircache_cur_path);
218 strncpy(&dircache_cur_path[td->pathpos], "/",
219 sizeof(dircache_cur_path) - td->pathpos - 1);
220 #ifdef SIMULATOR
221 strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
222 sizeof(dircache_cur_path) - td->pathpos - 2);
224 td->newdir = opendir(dircache_cur_path);
225 if (td->newdir == NULL)
227 logf("Failed to opendir(): %s", dircache_cur_path);
228 return -3;
230 #else
231 strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
232 sizeof(dircache_cur_path) - td->pathpos - 2);
234 td->newdir = *td->dir;
235 if (fat_opendir(IF_MV2(volume,) &td->newdir,
236 td->entry.firstcluster, td->dir) < 0 )
238 return -3;
240 #endif
242 td->ce = dircache_gen_next(td->ce);
243 if (td->ce == NULL)
244 return -4;
246 return 1;
249 td->ce->down = NULL;
250 td->ce = dircache_gen_next(td->ce);
251 if (td->ce == NULL)
252 return -5;
254 /* When simulator is used, it's only safe to yield here. */
255 if (thread_enabled)
257 /* Stop if we got an external signal. */
258 if (check_event_queue())
259 return -6;
260 yield();
265 return 0;
268 /**
269 * Recursively scan the hard disk and build the cache.
271 #ifdef SIMULATOR
272 static int dircache_travel(DIR *dir, struct dircache_entry *ce)
273 #else
274 static int dircache_travel(struct fat_dir *dir, struct dircache_entry *ce)
275 #endif
277 int depth = 0;
278 int result;
280 memset(ce, 0, sizeof(struct dircache_entry));
281 dir_recursion[0].dir = dir;
282 dir_recursion[0].ce = ce;
283 dir_recursion[0].first = ce;
285 do {
286 //logf("=> %s", dircache_cur_path);
287 result = dircache_scan(&dir_recursion[depth]);
288 switch (result) {
289 case 0: /* Leaving the current directory. */
290 /* Add the standard . and .. entries. */
291 ce = dir_recursion[depth].ce;
292 ce->d_name = ".";
293 ce->name_len = 2;
294 #ifdef SIMULATOR
295 closedir(dir_recursion[depth].dir);
296 ce->attribute = ATTR_DIRECTORY;
297 #else
298 ce->attribute = FAT_ATTR_DIRECTORY;
299 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
300 #endif
301 ce->size = 0;
302 ce->down = dir_recursion[depth].first;
304 depth--;
305 if (depth < 0)
306 break ;
308 dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
310 ce = dircache_gen_next(ce);
311 if (ce == NULL)
313 logf("memory allocation error");
314 return -3;
316 #ifdef SIMULATOR
317 ce->attribute = ATTR_DIRECTORY;
318 #else
319 ce->attribute = FAT_ATTR_DIRECTORY;
320 ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
321 #endif
322 ce->d_name = "..";
323 ce->name_len = 3;
324 ce->size = 0;
325 ce->down = dir_recursion[depth].first;
327 break ;
329 case 1: /* Going down in the directory tree. */
330 depth++;
331 if (depth >= MAX_SCAN_DEPTH)
333 logf("Too deep directory structure");
334 return -2;
337 #ifdef SIMULATOR
338 dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
339 #else
340 dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
341 #endif
342 dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
343 dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
344 break ;
346 default:
347 logf("Scan failed");
348 logf("-> %s", dircache_cur_path);
349 return -1;
351 } while (depth >= 0) ;
353 return 0;
357 * Internal function to get a pointer to dircache_entry for a given filename.
358 * path: Absolute path to a file or directory.
359 * get_before: Returns the cache pointer before the last valid entry found.
360 * only_directories: Match only filenames which are a directory type.
362 static struct dircache_entry* dircache_get_entry(const char *path,
363 bool get_before, bool only_directories)
365 struct dircache_entry *cache_entry, *before;
366 char namecopy[MAX_PATH*2];
367 char* part;
368 char* end;
370 strncpy(namecopy, path, sizeof(namecopy) - 1);
371 cache_entry = dircache_root;
372 before = NULL;
374 for ( part = strtok_r(namecopy, "/", &end); part;
375 part = strtok_r(NULL, "/", &end)) {
377 /* scan dir for name */
378 while (1)
380 if (cache_entry == NULL)
382 return NULL;
384 else if (cache_entry->name_len == 0)
386 cache_entry = cache_entry->next;
387 continue ;
390 if (!strcasecmp(part, cache_entry->d_name))
392 before = cache_entry;
393 if (cache_entry->down || only_directories)
394 cache_entry = cache_entry->down;
395 break ;
398 cache_entry = cache_entry->next;
402 if (get_before)
403 cache_entry = before;
405 return cache_entry;
408 #if 1
410 * Function to load the internal cache structure from disk to initialize
411 * the dircache really fast and little disk access.
413 int dircache_load(void)
415 struct dircache_maindata maindata;
416 int bytes_read;
417 int fd;
419 if (dircache_initialized)
420 return -1;
422 logf("Loading directory cache");
423 dircache_size = 0;
425 fd = open(DIRCACHE_FILE, O_RDONLY);
426 if (fd < 0)
427 return -2;
429 bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
430 if (bytes_read != sizeof(struct dircache_maindata)
431 || maindata.size <= 0)
433 logf("Dircache file header error");
434 close(fd);
435 remove(DIRCACHE_FILE);
436 return -3;
439 dircache_root = buffer_alloc(0);
440 if ((long)maindata.root_entry != (long)dircache_root)
442 logf("Position missmatch");
443 close(fd);
444 remove(DIRCACHE_FILE);
445 return -4;
448 dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
449 entry_count = maindata.entry_count;
450 bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
451 close(fd);
452 remove(DIRCACHE_FILE);
454 if (bytes_read != maindata.size)
456 logf("Dircache read failed");
457 return -6;
460 /* Cache successfully loaded. */
461 dircache_size = maindata.size;
462 allocated_size = dircache_size + DIRCACHE_RESERVE;
463 reserve_used = 0;
464 logf("Done, %ld KiB used", dircache_size / 1024);
465 dircache_initialized = true;
466 memset(fd_bindings, 0, sizeof(fd_bindings));
468 return 0;
472 * Function to save the internal cache stucture to disk for fast loading
473 * on boot.
475 int dircache_save(void)
477 struct dircache_maindata maindata;
478 int fd;
479 unsigned long bytes_written;
481 remove(DIRCACHE_FILE);
483 while (thread_enabled)
484 sleep(1);
486 if (!dircache_initialized)
487 return -1;
489 logf("Saving directory cache");
490 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
492 maindata.magic = DIRCACHE_MAGIC;
493 maindata.size = dircache_size;
494 maindata.root_entry = dircache_root;
495 maindata.entry_count = entry_count;
497 /* Save the info structure */
498 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
499 if (bytes_written != sizeof(struct dircache_maindata))
501 close(fd);
502 logf("dircache: write failed #1");
503 return -2;
506 /* Dump whole directory cache to disk */
507 bytes_written = write(fd, dircache_root, dircache_size);
508 close(fd);
509 if (bytes_written != dircache_size)
511 logf("dircache: write failed #2");
512 return -3;
515 return 0;
517 #endif /* #if 0 */
520 * Internal function which scans the disk and creates the dircache structure.
522 static int dircache_do_rebuild(void)
524 #ifdef SIMULATOR
525 DIR *pdir;
526 #else
527 struct fat_dir dir, *pdir;
528 #endif
529 unsigned int start_tick;
530 int i;
532 /* Measure how long it takes build the cache. */
533 start_tick = current_tick;
534 remove(DIRCACHE_FILE);
535 dircache_initializing = true;
537 #ifdef SIMULATOR
538 pdir = opendir("/");
539 if (pdir == NULL)
541 logf("Failed to open rootdir");
542 return -3;
544 #else
545 if ( fat_opendir(IF_MV2(volume,) &dir, 0, NULL) < 0 ) {
546 logf("Failed opening root dir");
547 dircache_initializing = false;
548 return -3;
550 pdir = &dir;
551 #endif
553 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
554 dircache_size = sizeof(struct dircache_entry);
556 cpu_boost(true);
557 if (dircache_travel(pdir, dircache_root) < 0)
559 logf("dircache_travel failed");
560 cpu_boost(false);
561 dircache_size = 0;
562 dircache_initializing = false;
563 return -2;
565 cpu_boost(false);
567 logf("Done, %ld KiB used", dircache_size / 1024);
569 dircache_initialized = true;
570 dircache_initializing = false;
571 cache_build_ticks = current_tick - start_tick;
573 /* Initialized fd bindings. */
574 memset(fd_bindings, 0, sizeof(fd_bindings));
575 for (i = 0; i < fdbind_idx; i++)
576 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
577 fdbind_idx = 0;
579 if (thread_enabled)
581 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
582 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
584 else
586 /* We have to long align the audiobuf to keep the buffer access fast. */
587 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
588 audiobuf += DIRCACHE_RESERVE;
589 allocated_size = dircache_size + DIRCACHE_RESERVE;
590 reserve_used = 0;
593 return 1;
597 * Internal thread that controls transparent cache building.
599 static void dircache_thread(void)
601 struct event ev;
603 while (1)
605 queue_wait(&dircache_queue, &ev);
607 switch (ev.id)
609 case DIRCACHE_BUILD:
610 thread_enabled = true;
611 dircache_do_rebuild();
612 thread_enabled = false;
613 break ;
615 case DIRCACHE_STOP:
616 logf("Stopped the rebuilding.");
617 dircache_initialized = false;
618 break ;
620 #ifndef SIMULATOR
621 case SYS_USB_CONNECTED:
622 usb_acknowledge(SYS_USB_CONNECTED_ACK);
623 usb_wait_for_disconnect(&dircache_queue);
624 break ;
625 #endif
631 * Start scanning the disk to build the dircache.
632 * Either transparent or non-transparent build method is used.
634 int dircache_build(int last_size)
636 if (dircache_initialized || thread_enabled)
637 return -3;
639 logf("Building directory cache");
640 remove(DIRCACHE_FILE);
642 /* Background build, dircache has been previously allocated */
643 if (dircache_size > 0)
645 thread_enabled = true;
646 dircache_initializing = true;
647 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
648 return 2;
651 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
653 allocated_size = last_size + DIRCACHE_RESERVE;
654 dircache_root = buffer_alloc(allocated_size);
655 thread_enabled = true;
657 /* Start a transparent rebuild. */
658 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
659 return 3;
662 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
664 /* Start a non-transparent rebuild. */
665 return dircache_do_rebuild();
669 * Steal the allocated dircache buffer and disable dircache.
671 void* dircache_steal_buffer(long *size)
673 dircache_disable();
674 if (dircache_size == 0)
676 *size = 0;
677 return NULL;
680 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
682 return dircache_root;
686 * Main initialization function that must be called before any other
687 * operations within the dircache.
689 void dircache_init(void)
691 int i;
693 dircache_initialized = false;
694 dircache_initializing = false;
696 memset(opendirs, 0, sizeof(opendirs));
697 for (i = 0; i < MAX_OPEN_DIRS; i++)
699 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
702 queue_init(&dircache_queue, true);
703 create_thread(dircache_thread, dircache_stack,
704 sizeof(dircache_stack), dircache_thread_name IF_PRIO(, PRIORITY_BACKGROUND)
705 IF_COP(, CPU, false));
709 * Returns true if dircache has been initialized and is ready to be used.
711 bool dircache_is_enabled(void)
713 return dircache_initialized;
717 * Returns true if dircache is being initialized.
719 bool dircache_is_initializing(void)
721 return dircache_initializing;
725 * Returns the current number of entries (directories and files) in the cache.
727 int dircache_get_entry_count(void)
729 return entry_count;
733 * Returns the allocated space for dircache (without reserve space).
735 int dircache_get_cache_size(void)
737 return dircache_is_enabled() ? dircache_size : 0;
741 * Returns how many bytes of the reserve allocation for live cache
742 * updates have been used.
744 int dircache_get_reserve_used(void)
746 return dircache_is_enabled() ? reserve_used : 0;
750 * Returns the time in kernel ticks that took to build the cache.
752 int dircache_get_build_ticks(void)
754 return dircache_is_enabled() ? cache_build_ticks : 0;
758 * Disables the dircache. Usually called on shutdown or when
759 * accepting a usb connection.
761 void dircache_disable(void)
763 int i;
764 bool cache_in_use;
766 if (thread_enabled)
767 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
769 while (thread_enabled)
770 sleep(1);
771 dircache_initialized = false;
773 logf("Waiting for cached dirs to release");
774 do {
775 cache_in_use = false;
776 for (i = 0; i < MAX_OPEN_DIRS; i++) {
777 if (!opendirs[i].regulardir && opendirs[i].busy)
779 cache_in_use = true;
780 sleep(1);
781 break ;
784 } while (cache_in_use) ;
786 logf("Cache released");
787 entry_count = 0;
791 * Usermode function to return dircache_entry pointer to the given path.
793 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
795 if (!dircache_initialized || filename == NULL)
796 return NULL;
798 return dircache_get_entry(filename, false, false);
802 * Function to copy the full absolute path from dircache to the given buffer
803 * using the given dircache_entry pointer.
805 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
807 const struct dircache_entry *down[MAX_SCAN_DEPTH];
808 int depth = 0;
810 if (size <= 0)
811 return ;
813 buf[0] = '\0';
815 if (entry == NULL)
816 return ;
818 do {
819 down[depth] = entry;
820 entry = entry->up;
821 depth++;
822 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
824 while (--depth >= 0)
826 snprintf(buf, size, "/%s", down[depth]->d_name);
827 buf += down[depth]->name_len; /* '/' + d_name */
828 size -= down[depth]->name_len;
829 if (size <= 0)
830 break ;
834 /* --- Directory cache live updating functions --- */
835 static int block_until_ready(void)
837 /* Block until dircache has been built. */
838 while (!dircache_initialized && dircache_initializing)
839 sleep(1);
841 if (!dircache_initialized)
842 return -1;
844 return 0;
847 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
849 struct dircache_entry *entry;
850 char basedir[MAX_PATH*2];
851 char *new;
852 long last_cache_size = dircache_size;
854 strncpy(basedir, path, sizeof(basedir)-1);
855 new = strrchr(basedir, '/');
856 if (new == NULL)
858 logf("error occurred");
859 dircache_initialized = false;
860 return NULL;
863 *new = '\0';
864 new++;
866 entry = dircache_get_entry(basedir, false, true);
867 if (entry == NULL)
869 logf("basedir not found!");
870 logf(basedir);
871 dircache_initialized = false;
872 return NULL;
875 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
876 >= DIRCACHE_RESERVE)
878 logf("not enough space");
879 dircache_initialized = false;
880 return NULL;
883 while (entry->next != NULL)
884 entry = entry->next;
886 if (entry->name_len > 0)
887 entry = dircache_gen_next(entry);
889 if (entry == NULL)
891 dircache_initialized = false;
892 return NULL;
895 entry->attribute = attribute;
896 entry->name_len = MIN(254, strlen(new)) + 1;
897 entry->d_name = ((char *)dircache_root+dircache_size);
898 entry->startcluster = 0;
899 entry->wrtdate = 0;
900 entry->wrttime = 0;
901 entry->size = 0;
902 memcpy(entry->d_name, new, entry->name_len);
903 dircache_size += entry->name_len;
905 if (attribute & ATTR_DIRECTORY)
907 logf("gen_down");
908 dircache_gen_down(entry);
911 reserve_used += dircache_size - last_cache_size;
913 return entry;
916 void dircache_bind(int fd, const char *path)
918 struct dircache_entry *entry;
920 /* Queue requests until dircache has been built. */
921 if (!dircache_initialized && dircache_initializing)
923 if (fdbind_idx >= MAX_PENDING_BINDINGS)
924 return ;
925 strncpy(fdbind_cache[fdbind_idx].path, path,
926 sizeof(fdbind_cache[fdbind_idx].path)-1);
927 fdbind_cache[fdbind_idx].fd = fd;
928 fdbind_idx++;
929 return ;
932 if (!dircache_initialized)
933 return ;
935 logf("bind: %d/%s", fd, path);
936 entry = dircache_get_entry(path, false, false);
937 if (entry == NULL)
939 logf("not found!");
940 dircache_initialized = false;
941 return ;
944 fd_bindings[fd] = entry;
947 void dircache_update_filesize(int fd, long newsize, long startcluster)
949 if (!dircache_initialized || fd < 0)
950 return ;
952 if (fd_bindings[fd] == NULL)
954 logf("dircache fd access error");
955 dircache_initialized = false;
956 return ;
959 fd_bindings[fd]->size = newsize;
960 fd_bindings[fd]->startcluster = startcluster;
963 void dircache_mkdir(const char *path)
964 { /* Test ok. */
965 if (block_until_ready())
966 return ;
968 logf("mkdir: %s", path);
969 dircache_new_entry(path, ATTR_DIRECTORY);
972 void dircache_rmdir(const char *path)
973 { /* Test ok. */
974 struct dircache_entry *entry;
976 if (block_until_ready())
977 return ;
979 logf("rmdir: %s", path);
980 entry = dircache_get_entry(path, true, true);
981 if (entry == NULL)
983 logf("not found!");
984 dircache_initialized = false;
985 return ;
988 entry->down = NULL;
989 entry->name_len = 0;
992 /* Remove a file from cache */
993 void dircache_remove(const char *name)
994 { /* Test ok. */
995 struct dircache_entry *entry;
997 if (block_until_ready())
998 return ;
1000 logf("remove: %s", name);
1002 entry = dircache_get_entry(name, false, false);
1004 if (entry == NULL)
1006 logf("not found!");
1007 dircache_initialized = false;
1008 return ;
1011 entry->name_len = 0;
1014 void dircache_rename(const char *oldpath, const char *newpath)
1015 { /* Test ok. */
1016 struct dircache_entry *entry, *newentry;
1017 struct dircache_entry oldentry;
1018 char absolute_path[MAX_PATH*2];
1019 char *p;
1021 if (block_until_ready())
1022 return ;
1024 logf("rename: %s->%s", oldpath, newpath);
1026 entry = dircache_get_entry(oldpath, true, false);
1027 if (entry == NULL)
1029 logf("not found!");
1030 dircache_initialized = false;
1031 return ;
1034 /* Delete the old entry. */
1035 entry->name_len = 0;
1037 /** If we rename the same filename twice in a row, we need to
1038 * save the data, because the entry will be re-used. */
1039 oldentry = *entry;
1041 /* Generate the absolute path for destination if necessary. */
1042 if (newpath[0] != '/')
1044 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
1045 p = strrchr(absolute_path, '/');
1046 if (!p)
1048 logf("Invalid path");
1049 dircache_initialized = false;
1050 return ;
1053 *p = '\0';
1054 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
1055 newpath = absolute_path;
1058 newentry = dircache_new_entry(newpath, entry->attribute);
1059 if (newentry == NULL)
1061 dircache_initialized = false;
1062 return ;
1065 newentry->down = oldentry.down;
1066 newentry->size = oldentry.size;
1067 newentry->startcluster = oldentry.startcluster;
1068 newentry->wrttime = oldentry.wrttime;
1069 newentry->wrtdate = oldentry.wrtdate;
1072 void dircache_add_file(const char *path, long startcluster)
1074 struct dircache_entry *entry;
1076 if (block_until_ready())
1077 return ;
1079 logf("add file: %s", path);
1080 entry = dircache_new_entry(path, 0);
1081 if (entry == NULL)
1082 return ;
1084 entry->startcluster = startcluster;
1087 DIRCACHED* opendir_cached(const char* name)
1089 struct dircache_entry *cache_entry;
1090 int dd;
1091 DIRCACHED* pdir = opendirs;
1093 if ( name[0] != '/' )
1095 DEBUGF("Only absolute paths supported right now\n");
1096 return NULL;
1099 /* find a free dir descriptor */
1100 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1101 if ( !pdir->busy )
1102 break;
1104 if ( dd == MAX_OPEN_DIRS )
1106 DEBUGF("Too many dirs open\n");
1107 errno = EMFILE;
1108 return NULL;
1111 if (!dircache_initialized)
1113 pdir->regulardir = opendir(name);
1114 if (!pdir->regulardir)
1115 return NULL;
1117 pdir->busy = true;
1118 return pdir;
1121 pdir->busy = true;
1122 pdir->regulardir = NULL;
1123 cache_entry = dircache_get_entry(name, false, true);
1124 pdir->entry = cache_entry;
1126 if (cache_entry == NULL)
1128 pdir->busy = false;
1129 return NULL;
1132 return pdir;
1135 struct dircache_entry* readdir_cached(DIRCACHED* dir)
1137 struct dirent *regentry;
1138 struct dircache_entry *ce;
1140 if (!dir->busy)
1141 return NULL;
1143 if (dir->regulardir != NULL)
1145 regentry = readdir(dir->regulardir);
1146 if (regentry == NULL)
1147 return NULL;
1149 strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
1150 dir->secondary_entry.size = regentry->size;
1151 dir->secondary_entry.startcluster = regentry->startcluster;
1152 dir->secondary_entry.attribute = regentry->attribute;
1153 dir->secondary_entry.wrttime = regentry->wrttime;
1154 dir->secondary_entry.wrtdate = regentry->wrtdate;
1155 dir->secondary_entry.next = NULL;
1157 return &dir->secondary_entry;
1160 do {
1161 if (dir->entry == NULL)
1162 return NULL;
1164 ce = dir->entry;
1165 if (ce->name_len == 0)
1166 dir->entry = ce->next;
1167 } while (ce->name_len == 0) ;
1169 dir->entry = ce->next;
1171 strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
1172 /* Can't do `dir->secondary_entry = *ce`
1173 because that modifies the d_name pointer. */
1174 dir->secondary_entry.size = ce->size;
1175 dir->secondary_entry.startcluster = ce->startcluster;
1176 dir->secondary_entry.attribute = ce->attribute;
1177 dir->secondary_entry.wrttime = ce->wrttime;
1178 dir->secondary_entry.wrtdate = ce->wrtdate;
1179 dir->secondary_entry.next = NULL;
1180 dir->internal_entry = ce;
1182 //logf("-> %s", ce->name);
1183 return &dir->secondary_entry;
1186 int closedir_cached(DIRCACHED* dir)
1188 if (!dir->busy)
1189 return -1;
1191 dir->busy=false;
1192 if (dir->regulardir != NULL)
1193 return closedir(dir->regulardir);
1195 return 0;