1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 - Allow cache live updating while transparent rebuild is running.
30 #include "string-extra.h"
46 #include "timefuncs.h"
50 #define DIRCACHE_BUILD 1
51 #define DIRCACHE_STOP 2
53 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
54 #define MAX_OPEN_DIRS 12
56 #define MAX_OPEN_DIRS 8
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
;
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
87 * Open the dircache file to save a snapshot on disk
89 static int open_dircache_file(unsigned flags
, int permissions
)
92 return open(DIRCACHE_FILE
, flags
, permissions
);
94 return open(DIRCACHE_FILE
, flags
);
98 * Remove the snapshot file
100 static int remove_dircache_file(void)
102 return remove(DIRCACHE_FILE
);
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");
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);
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
);
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
)
148 next_entry
->up
= ce
->up
;
149 ce
->next
= 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
)
165 ce
->down
= 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
))
184 case SYS_USB_CONNECTED
:
194 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
195 /* scan and build static data (avoid redundancy on stack) */
198 #ifdef HAVE_MULTIVOLUME
202 struct fat_direntry
*direntry
;
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 */
214 sab
.dir
->entrycount
= 0;
215 sab
.dir
->file
.firstcluster
= 0;
217 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
220 logf("fat_opendir failed: %d", 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
))
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
;
246 if(ce
->attribute
& FAT_ATTR_DIRECTORY
)
247 dircache_gen_down(ce
);
249 ce
= dircache_gen_next(ce
);
253 /* When simulator is used, it's only safe to yield here. */
256 /* Stop if we got an external signal. */
257 if(check_event_queue())
263 /* add "." and ".." */
266 ce
->attribute
= FAT_ATTR_DIRECTORY
;
267 ce
->startcluster
= startcluster
;
271 ce
= dircache_gen_next(ce
);
275 ce
->attribute
= FAT_ATTR_DIRECTORY
;
276 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
278 ce
->down
= first_ce
->up
;
280 /* second pass: recurse ! */
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
);
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
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
;
310 append_position
= dircache_gen_next(ce
);
311 ce
= dircache_gen_down(ce
);
315 struct fat_direntry direntry
; /* ditto */
316 #ifdef HAVE_MULTIVOLUME
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
);
334 logf("Failed to opendir_uncached(%s)", sab_path
);
338 while((entry
= readdir_uncached(dir
)))
340 if(!strcmp(".", entry
->d_name
) ||
341 !strcmp("..", entry
->d_name
))
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
;
355 if(entry
->attribute
& ATTR_DIRECTORY
)
357 dircache_gen_down(ce
);
360 closedir_uncached(dir
);
363 /* save current paths size */
364 int pathpos
= strlen(sab_path
);
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
);
371 sab_path
[pathpos
] = '\0';
375 closedir_uncached(dir
);
380 ce
= dircache_gen_next(ce
);
384 /* When simulator is used, it's only safe to yield here. */
387 /* Stop if we got an external signal. */
388 if(check_event_queue())
394 /* add "." and ".." */
397 ce
->attribute
= ATTR_DIRECTORY
;
401 ce
= dircache_gen_next(ce
);
405 ce
->attribute
= ATTR_DIRECTORY
;
407 ce
->down
= first_ce
->up
;
409 closedir_uncached(dir
);
413 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
415 #ifdef HAVE_MULTIVOLUME
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)
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
];
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 */
468 cache_entry
= cache_entry
->down
;
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
;
482 if(!strcasecmp(part
, cache_entry
->d_name
))
484 /* go to next entry */
485 cache_entry
= cache_entry
->next
;
488 /* handle not found case */
489 if(cache_entry
== NULL
)
493 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
495 return at_root
? cache_entry
: cache_entry
->down
;
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
;
511 if (dircache_initialized
)
514 logf("Loading directory cache");
517 fd
= open_dircache_file(O_RDONLY
, 0);
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");
527 remove_dircache_file();
531 dircache_root
= buffer_alloc(0);
532 if ((long)maindata
.root_entry
!= (long)dircache_root
)
534 logf("Position missmatch");
536 remove_dircache_file();
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
));
545 remove_dircache_file();
547 if (bytes_read
!= maindata
.size
)
549 logf("Dircache read failed");
553 /* Cache successfully loaded. */
554 dircache_size
= maindata
.size
;
555 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
557 logf("Done, %ld KiB used", dircache_size
/ 1024);
558 dircache_initialized
= true;
559 memset(fd_bindings
, 0, sizeof(fd_bindings
));
565 * Function to save the internal cache stucture to disk for fast loading
568 int dircache_save(void)
570 struct dircache_maindata maindata
;
572 unsigned long bytes_written
;
574 remove_dircache_file();
576 if (!dircache_initialized
)
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
))
593 logf("dircache: write failed #1");
597 /* Dump whole directory cache to disk */
598 bytes_written
= write(fd
, dircache_root
, dircache_size
);
600 if (bytes_written
!= dircache_size
)
602 logf("dircache: write failed #2");
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
;
618 /* Measure how long it takes build the cache. */
619 start_tick
= current_tick
;
620 dircache_initializing
= true;
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
))
635 #ifdef HAVE_MULTIVOLUME
636 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
638 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
639 #endif /* HAVE_MULTIVOLUME */
641 logf("dircache_scan_and_build failed");
644 dircache_initializing
= false;
648 #ifdef HAVE_MULTIVOLUME
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
);
667 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
668 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
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
;
683 * Internal thread that controls transparent cache building.
685 static void dircache_thread(void)
687 struct queue_event ev
;
691 queue_wait(&dircache_queue
, &ev
);
697 if (!dircache_initialized
)
699 dircache_initialized
= false;
702 thread_enabled
= true;
703 dircache_do_rebuild();
704 thread_enabled
= false;
708 logf("Stopped the rebuilding.");
709 dircache_initialized
= false;
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
);
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
)
731 logf("Building directory cache");
732 #ifdef HAVE_EEPROM_SETTINGS
733 remove_dircache_file();
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);
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);
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
)
768 if (dircache_size
== 0)
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)
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
)
802 #ifdef HAVE_IO_PRIORITY
803 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
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
)
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)
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)
883 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
885 while (thread_enabled
)
887 dircache_initialized
= false;
889 logf("Waiting for cached dirs to release");
891 cache_in_use
= false;
892 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
893 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
900 } while (cache_in_use
) ;
902 logf("Cache released");
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
)
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
)
925 const struct dircache_entry
*temp
= entry
;
930 /* first compute the necessary size */
933 path_size
+= temp
->name_len
; /* '/' + d_name */
937 /* now copy the path */
938 /* doesn't matter with trailing 0 because it's added later */
942 idx
-= entry
->name_len
;
944 int rem
= size
- idx
;
949 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
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())
965 if (!dircache_initialized
)
971 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
973 struct dircache_entry
*entry
;
974 char basedir
[MAX_PATH
*2];
976 long last_cache_size
= dircache_size
;
978 strlcpy(basedir
, path
, sizeof(basedir
));
979 new = strrchr(basedir
, '/');
982 logf("error occurred");
983 dircache_initialized
= false;
990 entry
= dircache_get_entry(basedir
, true);
993 logf("basedir not found!");
995 dircache_initialized
= false;
999 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
1000 >= DIRCACHE_RESERVE
)
1002 logf("not enough space");
1003 dircache_initialized
= false;
1007 while (entry
->next
!= NULL
)
1008 entry
= entry
->next
;
1010 if (entry
->name_len
> 0)
1011 entry
= dircache_gen_next(entry
);
1015 dircache_initialized
= false;
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;
1026 memcpy(entry
->d_name
, new, entry
->name_len
);
1027 dircache_size
+= entry
->name_len
;
1029 if (attribute
& ATTR_DIRECTORY
)
1032 dircache_gen_down(entry
);
1035 reserve_used
+= dircache_size
- last_cache_size
;
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
)
1049 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1050 sizeof(fdbind_cache
[fdbind_idx
].path
));
1051 fdbind_cache
[fdbind_idx
].fd
= fd
;
1056 if (!dircache_initialized
)
1059 logf("bind: %d/%s", fd
, path
);
1060 entry
= dircache_get_entry(path
, false);
1064 dircache_initialized
= false;
1068 fd_bindings
[fd
] = entry
;
1071 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1073 if (!dircache_initialized
|| fd
< 0)
1076 if (fd_bindings
[fd
] == NULL
)
1078 logf("dircache fd(%d) access error", fd
);
1079 dircache_initialized
= false;
1083 fd_bindings
[fd
]->size
= newsize
;
1084 fd_bindings
[fd
]->startcluster
= startcluster
;
1086 void dircache_update_filetime(int fd
)
1092 struct tm
*now
= get_time();
1093 if (!dircache_initialized
|| fd
< 0)
1096 if (fd_bindings
[fd
] == NULL
)
1098 logf("dircache fd access error");
1099 dircache_initialized
= false;
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));
1112 void dircache_mkdir(const char *path
)
1114 if (block_until_ready())
1118 logf("mkdir: %s", path
);
1119 dircache_new_entry(path
, ATTR_DIRECTORY
);
1122 void dircache_rmdir(const char *path
)
1124 struct dircache_entry
*entry
;
1126 if (block_until_ready())
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;
1139 entry
->name_len
= 0;
1142 /* Remove a file from cache */
1143 void dircache_remove(const char *name
)
1145 struct dircache_entry
*entry
;
1147 if (block_until_ready())
1150 logf("remove: %s", name
);
1152 entry
= dircache_get_entry(name
, false);
1157 dircache_initialized
= false;
1161 entry
->name_len
= 0;
1164 void dircache_rename(const char *oldpath
, const char *newpath
)
1166 struct dircache_entry
*entry
, *newentry
;
1167 struct dircache_entry oldentry
;
1168 char absolute_path
[MAX_PATH
*2];
1171 if (block_until_ready())
1174 logf("rename: %s->%s", oldpath
, newpath
);
1176 entry
= dircache_get_entry(oldpath
, false);
1180 dircache_initialized
= false;
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. */
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
, '/');
1198 logf("Invalid path");
1199 dircache_initialized
= false;
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;
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())
1229 logf("add file: %s", path
);
1230 entry
= dircache_new_entry(path
, 0);
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
)
1245 DIR_CACHED
* pdir
= opendirs
;
1247 if ( name
[0] != '/' )
1249 DEBUGF("Only absolute paths supported right now\n");
1253 /* find a free dir descriptor */
1254 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1258 if ( dd
== MAX_OPEN_DIRS
)
1260 DEBUGF("Too many dirs open\n");
1267 if (!dircache_initialized
|| is_disable_msg_pending())
1269 pdir
->internal_entry
= NULL
;
1270 pdir
->regulardir
= opendir_uncached(name
);
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
)
1288 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1290 struct dircache_entry
*ce
= dir
->internal_entry
;
1291 struct dirent_uncached
*regentry
;
1296 if (dir
->regulardir
!= NULL
)
1298 regentry
= readdir_uncached(dir
->regulardir
);
1299 if (regentry
== 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)
1317 /* skip unused entries */
1318 while(ce
!= NULL
&& ce
->name_len
== 0)
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
)
1344 if (dir
->regulardir
!= NULL
)
1345 return closedir_uncached(dir
->regulardir
);
1350 int mkdir_cached(const char *name
)
1352 int rc
=mkdir_uncached(name
);
1354 dircache_mkdir(name
);
1358 int rmdir_cached(const char* name
)
1360 int rc
=rmdir_uncached(name
);
1362 dircache_rmdir(name
);