Fix buffer overflow when adding a radio preset.
[kugel-rb.git] / firmware / common / dircache.c
blobe69ecf3d5c76b25e40fe2c9db3757f1746dddee4
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 #ifdef HAVE_EEPROM_SETTINGS
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 if (!dircache_initialized)
484 return -1;
486 logf("Saving directory cache");
487 fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
489 maindata.magic = DIRCACHE_MAGIC;
490 maindata.size = dircache_size;
491 maindata.root_entry = dircache_root;
492 maindata.entry_count = entry_count;
494 /* Save the info structure */
495 bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
496 if (bytes_written != sizeof(struct dircache_maindata))
498 close(fd);
499 logf("dircache: write failed #1");
500 return -2;
503 /* Dump whole directory cache to disk */
504 bytes_written = write(fd, dircache_root, dircache_size);
505 close(fd);
506 if (bytes_written != dircache_size)
508 logf("dircache: write failed #2");
509 return -3;
512 return 0;
514 #endif /* #if 0 */
517 * Internal function which scans the disk and creates the dircache structure.
519 static int dircache_do_rebuild(void)
521 #ifdef SIMULATOR
522 DIR *pdir;
523 #else
524 struct fat_dir dir, *pdir;
525 #endif
526 unsigned int start_tick;
527 int i;
529 /* Measure how long it takes build the cache. */
530 start_tick = current_tick;
531 remove(DIRCACHE_FILE);
532 dircache_initializing = true;
534 #ifdef SIMULATOR
535 pdir = opendir("/");
536 if (pdir == NULL)
538 logf("Failed to open rootdir");
539 return -3;
541 #else
542 if ( fat_opendir(IF_MV2(volume,) &dir, 0, NULL) < 0 ) {
543 logf("Failed opening root dir");
544 dircache_initializing = false;
545 return -3;
547 pdir = &dir;
548 #endif
550 memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
551 dircache_size = sizeof(struct dircache_entry);
553 cpu_boost(true);
554 if (dircache_travel(pdir, dircache_root) < 0)
556 logf("dircache_travel failed");
557 cpu_boost(false);
558 dircache_size = 0;
559 dircache_initializing = false;
560 return -2;
562 cpu_boost(false);
564 logf("Done, %ld KiB used", dircache_size / 1024);
566 dircache_initialized = true;
567 dircache_initializing = false;
568 cache_build_ticks = current_tick - start_tick;
570 /* Initialized fd bindings. */
571 memset(fd_bindings, 0, sizeof(fd_bindings));
572 for (i = 0; i < fdbind_idx; i++)
573 dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
574 fdbind_idx = 0;
576 if (thread_enabled)
578 if (allocated_size - dircache_size < DIRCACHE_RESERVE)
579 reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
581 else
583 /* We have to long align the audiobuf to keep the buffer access fast. */
584 audiobuf += (long)((dircache_size & ~0x03) + 0x04);
585 audiobuf += DIRCACHE_RESERVE;
586 allocated_size = dircache_size + DIRCACHE_RESERVE;
587 reserve_used = 0;
590 return 1;
594 * Internal thread that controls transparent cache building.
596 static void dircache_thread(void)
598 struct event ev;
600 while (1)
602 queue_wait(&dircache_queue, &ev);
604 switch (ev.id)
606 case DIRCACHE_BUILD:
607 thread_enabled = true;
608 dircache_do_rebuild();
609 thread_enabled = false;
610 break ;
612 case DIRCACHE_STOP:
613 logf("Stopped the rebuilding.");
614 dircache_initialized = false;
615 break ;
617 #ifndef SIMULATOR
618 case SYS_USB_CONNECTED:
619 usb_acknowledge(SYS_USB_CONNECTED_ACK);
620 usb_wait_for_disconnect(&dircache_queue);
621 break ;
622 #endif
628 * Start scanning the disk to build the dircache.
629 * Either transparent or non-transparent build method is used.
631 int dircache_build(int last_size)
633 if (dircache_initialized || thread_enabled)
634 return -3;
636 logf("Building directory cache");
637 remove(DIRCACHE_FILE);
639 /* Background build, dircache has been previously allocated */
640 if (dircache_size > 0)
642 thread_enabled = true;
643 dircache_initializing = true;
644 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
645 return 2;
648 if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
650 allocated_size = last_size + DIRCACHE_RESERVE;
651 dircache_root = buffer_alloc(allocated_size);
652 thread_enabled = true;
654 /* Start a transparent rebuild. */
655 queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
656 return 3;
659 dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
661 /* Start a non-transparent rebuild. */
662 return dircache_do_rebuild();
666 * Steal the allocated dircache buffer and disable dircache.
668 void* dircache_steal_buffer(long *size)
670 dircache_disable();
671 if (dircache_size == 0)
673 *size = 0;
674 return NULL;
677 *size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
679 return dircache_root;
683 * Main initialization function that must be called before any other
684 * operations within the dircache.
686 void dircache_init(void)
688 int i;
690 dircache_initialized = false;
691 dircache_initializing = false;
693 memset(opendirs, 0, sizeof(opendirs));
694 for (i = 0; i < MAX_OPEN_DIRS; i++)
696 opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
699 queue_init(&dircache_queue, true);
700 create_thread(dircache_thread, dircache_stack,
701 sizeof(dircache_stack), dircache_thread_name IF_PRIO(, PRIORITY_BACKGROUND)
702 IF_COP(, CPU, false));
706 * Returns true if dircache has been initialized and is ready to be used.
708 bool dircache_is_enabled(void)
710 return dircache_initialized;
714 * Returns true if dircache is being initialized.
716 bool dircache_is_initializing(void)
718 return dircache_initializing || thread_enabled;
722 * Returns the current number of entries (directories and files) in the cache.
724 int dircache_get_entry_count(void)
726 return entry_count;
730 * Returns the allocated space for dircache (without reserve space).
732 int dircache_get_cache_size(void)
734 return dircache_is_enabled() ? dircache_size : 0;
738 * Returns how many bytes of the reserve allocation for live cache
739 * updates have been used.
741 int dircache_get_reserve_used(void)
743 return dircache_is_enabled() ? reserve_used : 0;
747 * Returns the time in kernel ticks that took to build the cache.
749 int dircache_get_build_ticks(void)
751 return dircache_is_enabled() ? cache_build_ticks : 0;
755 * Disables the dircache. Usually called on shutdown or when
756 * accepting a usb connection.
758 void dircache_disable(void)
760 int i;
761 bool cache_in_use;
763 if (thread_enabled)
764 queue_post(&dircache_queue, DIRCACHE_STOP, 0);
766 while (thread_enabled)
767 sleep(1);
768 dircache_initialized = false;
770 logf("Waiting for cached dirs to release");
771 do {
772 cache_in_use = false;
773 for (i = 0; i < MAX_OPEN_DIRS; i++) {
774 if (!opendirs[i].regulardir && opendirs[i].busy)
776 cache_in_use = true;
777 sleep(1);
778 break ;
781 } while (cache_in_use) ;
783 logf("Cache released");
784 entry_count = 0;
788 * Usermode function to return dircache_entry pointer to the given path.
790 const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
792 if (!dircache_initialized || filename == NULL)
793 return NULL;
795 return dircache_get_entry(filename, false, false);
799 * Function to copy the full absolute path from dircache to the given buffer
800 * using the given dircache_entry pointer.
802 void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
804 const struct dircache_entry *down[MAX_SCAN_DEPTH];
805 int depth = 0;
807 if (size <= 0)
808 return ;
810 buf[0] = '\0';
812 if (entry == NULL)
813 return ;
815 do {
816 down[depth] = entry;
817 entry = entry->up;
818 depth++;
819 } while (entry != NULL && depth < MAX_SCAN_DEPTH);
821 while (--depth >= 0)
823 snprintf(buf, size, "/%s", down[depth]->d_name);
824 buf += down[depth]->name_len; /* '/' + d_name */
825 size -= down[depth]->name_len;
826 if (size <= 0)
827 break ;
831 /* --- Directory cache live updating functions --- */
832 static int block_until_ready(void)
834 /* Block until dircache has been built. */
835 while (!dircache_initialized && dircache_is_initializing())
836 sleep(1);
838 if (!dircache_initialized)
839 return -1;
841 return 0;
844 static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
846 struct dircache_entry *entry;
847 char basedir[MAX_PATH*2];
848 char *new;
849 long last_cache_size = dircache_size;
851 strncpy(basedir, path, sizeof(basedir)-1);
852 new = strrchr(basedir, '/');
853 if (new == NULL)
855 logf("error occurred");
856 dircache_initialized = false;
857 return NULL;
860 *new = '\0';
861 new++;
863 entry = dircache_get_entry(basedir, false, true);
864 if (entry == NULL)
866 logf("basedir not found!");
867 logf(basedir);
868 dircache_initialized = false;
869 return NULL;
872 if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
873 >= DIRCACHE_RESERVE)
875 logf("not enough space");
876 dircache_initialized = false;
877 return NULL;
880 while (entry->next != NULL)
881 entry = entry->next;
883 if (entry->name_len > 0)
884 entry = dircache_gen_next(entry);
886 if (entry == NULL)
888 dircache_initialized = false;
889 return NULL;
892 entry->attribute = attribute;
893 entry->name_len = MIN(254, strlen(new)) + 1;
894 entry->d_name = ((char *)dircache_root+dircache_size);
895 entry->startcluster = 0;
896 entry->wrtdate = 0;
897 entry->wrttime = 0;
898 entry->size = 0;
899 memcpy(entry->d_name, new, entry->name_len);
900 dircache_size += entry->name_len;
902 if (attribute & ATTR_DIRECTORY)
904 logf("gen_down");
905 dircache_gen_down(entry);
908 reserve_used += dircache_size - last_cache_size;
910 return entry;
913 void dircache_bind(int fd, const char *path)
915 struct dircache_entry *entry;
917 /* Queue requests until dircache has been built. */
918 if (!dircache_initialized && dircache_is_initializing())
920 if (fdbind_idx >= MAX_PENDING_BINDINGS)
921 return ;
922 strncpy(fdbind_cache[fdbind_idx].path, path,
923 sizeof(fdbind_cache[fdbind_idx].path)-1);
924 fdbind_cache[fdbind_idx].fd = fd;
925 fdbind_idx++;
926 return ;
929 if (!dircache_initialized)
930 return ;
932 logf("bind: %d/%s", fd, path);
933 entry = dircache_get_entry(path, false, false);
934 if (entry == NULL)
936 logf("not found!");
937 dircache_initialized = false;
938 return ;
941 fd_bindings[fd] = entry;
944 void dircache_update_filesize(int fd, long newsize, long startcluster)
946 if (!dircache_initialized || fd < 0)
947 return ;
949 if (fd_bindings[fd] == NULL)
951 logf("dircache fd access error");
952 dircache_initialized = false;
953 return ;
956 fd_bindings[fd]->size = newsize;
957 fd_bindings[fd]->startcluster = startcluster;
960 void dircache_mkdir(const char *path)
961 { /* Test ok. */
962 if (block_until_ready())
963 return ;
965 logf("mkdir: %s", path);
966 dircache_new_entry(path, ATTR_DIRECTORY);
969 void dircache_rmdir(const char *path)
970 { /* Test ok. */
971 struct dircache_entry *entry;
973 if (block_until_ready())
974 return ;
976 logf("rmdir: %s", path);
977 entry = dircache_get_entry(path, true, true);
978 if (entry == NULL)
980 logf("not found!");
981 dircache_initialized = false;
982 return ;
985 entry->down = NULL;
986 entry->name_len = 0;
989 /* Remove a file from cache */
990 void dircache_remove(const char *name)
991 { /* Test ok. */
992 struct dircache_entry *entry;
994 if (block_until_ready())
995 return ;
997 logf("remove: %s", name);
999 entry = dircache_get_entry(name, false, false);
1001 if (entry == NULL)
1003 logf("not found!");
1004 dircache_initialized = false;
1005 return ;
1008 entry->name_len = 0;
1011 void dircache_rename(const char *oldpath, const char *newpath)
1012 { /* Test ok. */
1013 struct dircache_entry *entry, *newentry;
1014 struct dircache_entry oldentry;
1015 char absolute_path[MAX_PATH*2];
1016 char *p;
1018 if (block_until_ready())
1019 return ;
1021 logf("rename: %s->%s", oldpath, newpath);
1023 entry = dircache_get_entry(oldpath, true, false);
1024 if (entry == NULL)
1026 logf("not found!");
1027 dircache_initialized = false;
1028 return ;
1031 /* Delete the old entry. */
1032 entry->name_len = 0;
1034 /** If we rename the same filename twice in a row, we need to
1035 * save the data, because the entry will be re-used. */
1036 oldentry = *entry;
1038 /* Generate the absolute path for destination if necessary. */
1039 if (newpath[0] != '/')
1041 strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
1042 p = strrchr(absolute_path, '/');
1043 if (!p)
1045 logf("Invalid path");
1046 dircache_initialized = false;
1047 return ;
1050 *p = '\0';
1051 strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
1052 newpath = absolute_path;
1055 newentry = dircache_new_entry(newpath, entry->attribute);
1056 if (newentry == NULL)
1058 dircache_initialized = false;
1059 return ;
1062 newentry->down = oldentry.down;
1063 newentry->size = oldentry.size;
1064 newentry->startcluster = oldentry.startcluster;
1065 newentry->wrttime = oldentry.wrttime;
1066 newentry->wrtdate = oldentry.wrtdate;
1069 void dircache_add_file(const char *path, long startcluster)
1071 struct dircache_entry *entry;
1073 if (block_until_ready())
1074 return ;
1076 logf("add file: %s", path);
1077 entry = dircache_new_entry(path, 0);
1078 if (entry == NULL)
1079 return ;
1081 entry->startcluster = startcluster;
1084 DIRCACHED* opendir_cached(const char* name)
1086 struct dircache_entry *cache_entry;
1087 int dd;
1088 DIRCACHED* pdir = opendirs;
1090 if ( name[0] != '/' )
1092 DEBUGF("Only absolute paths supported right now\n");
1093 return NULL;
1096 /* find a free dir descriptor */
1097 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
1098 if ( !pdir->busy )
1099 break;
1101 if ( dd == MAX_OPEN_DIRS )
1103 DEBUGF("Too many dirs open\n");
1104 errno = EMFILE;
1105 return NULL;
1108 if (!dircache_initialized)
1110 pdir->regulardir = opendir(name);
1111 if (!pdir->regulardir)
1112 return NULL;
1114 pdir->busy = true;
1115 return pdir;
1118 pdir->busy = true;
1119 pdir->regulardir = NULL;
1120 cache_entry = dircache_get_entry(name, false, true);
1121 pdir->entry = cache_entry;
1123 if (cache_entry == NULL)
1125 pdir->busy = false;
1126 return NULL;
1129 return pdir;
1132 struct dircache_entry* readdir_cached(DIRCACHED* dir)
1134 struct dirent *regentry;
1135 struct dircache_entry *ce;
1137 if (!dir->busy)
1138 return NULL;
1140 if (dir->regulardir != NULL)
1142 regentry = readdir(dir->regulardir);
1143 if (regentry == NULL)
1144 return NULL;
1146 strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
1147 dir->secondary_entry.size = regentry->size;
1148 dir->secondary_entry.startcluster = regentry->startcluster;
1149 dir->secondary_entry.attribute = regentry->attribute;
1150 dir->secondary_entry.wrttime = regentry->wrttime;
1151 dir->secondary_entry.wrtdate = regentry->wrtdate;
1152 dir->secondary_entry.next = NULL;
1154 return &dir->secondary_entry;
1157 do {
1158 if (dir->entry == NULL)
1159 return NULL;
1161 ce = dir->entry;
1162 if (ce->name_len == 0)
1163 dir->entry = ce->next;
1164 } while (ce->name_len == 0) ;
1166 dir->entry = ce->next;
1168 strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
1169 /* Can't do `dir->secondary_entry = *ce`
1170 because that modifies the d_name pointer. */
1171 dir->secondary_entry.size = ce->size;
1172 dir->secondary_entry.startcluster = ce->startcluster;
1173 dir->secondary_entry.attribute = ce->attribute;
1174 dir->secondary_entry.wrttime = ce->wrttime;
1175 dir->secondary_entry.wrtdate = ce->wrtdate;
1176 dir->secondary_entry.next = NULL;
1177 dir->internal_entry = ce;
1179 //logf("-> %s", ce->name);
1180 return &dir->secondary_entry;
1183 int closedir_cached(DIRCACHED* dir)
1185 if (!dir->busy)
1186 return -1;
1188 dir->busy=false;
1189 if (dir->regulardir != NULL)
1190 return closedir(dir->regulardir);
1192 return 0;