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"
52 #define DIRCACHE_BUILD 1
53 #define DIRCACHE_STOP 2
56 #define MAX_OPEN_DIRS 12
58 #define MAX_OPEN_DIRS 8
61 #define MAX_PENDING_BINDINGS 2
67 /* Exported structures. */
68 struct dircache_entry
{
70 struct dircache_entry
*next
;
71 struct dircache_entry
*up
;
72 struct dircache_entry
*down
;
79 * x - array of struct dircache_entry
81 * d - name buffer for the d_name entry of the struct dircache_entry
82 * |xxxxxx|rrrrrrrrr|dddddd|
84 * subsequent x are allocated from the front, d are allocated from the back,
85 * using the reserve buffer for entries added after initial scan
87 * after a while the cache may look like:
88 * |xxxxxxxx|rrrrr|dddddddd|
90 * after a reboot, the reserve buffer is restored in it's size, so that the
91 * total allocation size grows
92 * |xxxxxxxx|rrrrrrrrr|dddddddd|
94 /* this points to the beginnging of the buffer and the first entry */
95 static struct dircache_entry
*dircache_root
;
96 /* these point to the start and end of the name buffer (d above) */
97 static char *d_names_start
, *d_names_end
;
98 /* put "." and ".." into the d_names buffer to enable easy pointer logic */
99 static char *dot
, *dotdot
;
100 #ifdef HAVE_MULTIVOLUME
101 static struct dircache_entry
*append_position
;
104 static DIR_CACHED opendirs
[MAX_OPEN_DIRS
];
105 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
107 static bool dircache_initialized
= false;
108 static bool dircache_initializing
= false;
109 static bool thread_enabled
= false;
110 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
111 static unsigned long dircache_size
= 0;
112 static unsigned long entry_count
= 0;
113 static unsigned long reserve_used
= 0;
114 static unsigned int cache_build_ticks
= 0;
115 static unsigned long appflags
= 0;
117 static struct event_queue dircache_queue SHAREDBSS_ATTR
;
118 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x400)/sizeof(long)];
119 static const char dircache_thread_name
[] = "dircache";
121 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
122 static int fdbind_idx
= 0;
124 /* --- Internal cache structure control functions --- */
126 static inline struct dircache_entry
* get_entry(int id
)
128 return &dircache_root
[id
];
131 #ifdef HAVE_EEPROM_SETTINGS
133 * Open the dircache file to save a snapshot on disk
135 static int open_dircache_file(unsigned flags
, int permissions
)
137 if (permissions
!= 0)
138 return open(DIRCACHE_FILE
, flags
, permissions
);
140 return open(DIRCACHE_FILE
, flags
);
144 * Remove the snapshot file
146 static int remove_dircache_file(void)
148 return remove(DIRCACHE_FILE
);
152 * Internal function to allocate a new dircache_entry from memory.
154 static struct dircache_entry
* allocate_entry(void)
156 struct dircache_entry
*next_entry
;
158 if (dircache_size
> allocated_size
- MAX_PATH
*2)
160 logf("size limit reached");
164 next_entry
= &dircache_root
[entry_count
++];
165 next_entry
->d_name
= NULL
;
166 next_entry
->up
= NULL
;
167 next_entry
->down
= NULL
;
168 next_entry
->next
= NULL
;
170 dircache_size
+= sizeof(struct dircache_entry
);
176 * Internal function to allocate a dircache_entry and set
177 * ->next entry pointers.
179 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
181 struct dircache_entry
*next_entry
;
183 if ( (next_entry
= allocate_entry()) == NULL
)
185 next_entry
->up
= ce
->up
;
186 ce
->next
= next_entry
;
192 * Internal function to allocate a dircache_entry and set
193 * ->down entry pointers.
195 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
197 struct dircache_entry
*next_entry
;
199 if ( (next_entry
= allocate_entry()) == NULL
)
202 ce
->down
= next_entry
;
208 * Returns true if there is an event waiting in the queue
209 * that requires the current operation to be aborted.
211 static bool check_event_queue(void)
213 struct queue_event ev
;
215 if(!queue_peek(&dircache_queue
, &ev
))
221 case SYS_USB_CONNECTED
:
231 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
232 /* scan and build static data (avoid redundancy on stack) */
235 #ifdef HAVE_MULTIVOLUME
239 struct fat_direntry
*direntry
;
242 static int sab_process_dir(unsigned long startcluster
, struct dircache_entry
*ce
)
244 /* normally, opendir expects a full fat_dir as parent but in our case,
245 * it's completely useless because we don't modify anything
246 * WARNING: this heavily relies on current FAT implementation ! */
248 /* those field are necessary to update the FAT entry in case of modification
249 here we don't touch anything so we put dummy values */
251 sab
.dir
->entrycount
= 0;
252 sab
.dir
->file
.firstcluster
= 0;
254 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
257 logf("fat_opendir failed: %d", rc
);
261 /* first pass : read dir */
262 struct dircache_entry
*first_ce
= ce
;
264 /* read through directory */
265 while((rc
= fat_getnext(sab
.dir
, sab
.direntry
)) >= 0 && sab
.direntry
->name
[0])
267 if(!strcmp(".", sab
.direntry
->name
) ||
268 !strcmp("..", sab
.direntry
->name
))
271 size_t size
= strlen(sab
.direntry
->name
) + 1;
272 ce
->d_name
= (d_names_start
-= size
);
273 ce
->startcluster
= sab
.direntry
->firstcluster
;
274 ce
->info
.size
= sab
.direntry
->filesize
;
275 ce
->info
.attribute
= sab
.direntry
->attr
;
276 ce
->info
.wrtdate
= sab
.direntry
->wrtdate
;
277 ce
->info
.wrttime
= sab
.direntry
->wrttime
;
279 strcpy(ce
->d_name
, sab
.direntry
->name
);
280 dircache_size
+= size
;
282 if(ce
->info
.attribute
& FAT_ATTR_DIRECTORY
)
283 dircache_gen_down(ce
);
285 ce
= dircache_gen_next(ce
);
289 /* When simulator is used, it's only safe to yield here. */
292 /* Stop if we got an external signal. */
293 if(check_event_queue())
299 /* add "." and ".." */
301 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
302 ce
->startcluster
= startcluster
;
306 ce
= dircache_gen_next(ce
);
309 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
310 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
312 ce
->down
= first_ce
->up
;
314 /* second pass: recurse ! */
319 if(ce
->d_name
!= NULL
&& ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".")
320 && strcmp(ce
->d_name
, ".."))
321 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
329 /* used during the generation */
330 static struct fat_dir sab_fat_dir
;
332 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
334 memset(ce
, 0, sizeof(struct dircache_entry
));
336 #ifdef HAVE_MULTIVOLUME
339 ce
->d_name
= (d_names_start
-= sizeof(VOL_NAMES
));
340 size_t len
= snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
)+1;
341 dircache_size
+= len
;
342 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
344 append_position
= dircache_gen_next(ce
);
345 ce
= dircache_gen_down(ce
);
349 struct fat_direntry direntry
; /* ditto */
350 #ifdef HAVE_MULTIVOLUME
353 sab
.dir
= &sab_fat_dir
;
354 sab
.direntry
= &direntry
;
356 return sab_process_dir(0, ce
);
358 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
359 static char sab_path
[MAX_PATH
];
361 static int sab_process_dir(struct dircache_entry
*ce
)
363 struct dirent_uncached
*entry
;
364 struct dircache_entry
*first_ce
= ce
;
365 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
368 logf("Failed to opendir_uncached(%s)", sab_path
);
372 while((entry
= readdir_uncached(dir
)))
374 if(!strcmp(".", entry
->d_name
) ||
375 !strcmp("..", entry
->d_name
))
378 size_t size
= strlen(entry
->d_name
) + 1;
379 ce
->d_name
= (d_names_start
-= size
);
380 ce
->info
= entry
->info
;
382 strcpy(ce
->d_name
, entry
->d_name
);
383 dircache_size
+= size
;
385 if(entry
->info
.attribute
& ATTR_DIRECTORY
)
387 dircache_gen_down(ce
);
390 closedir_uncached(dir
);
393 /* save current paths size */
394 int pathpos
= strlen(sab_path
);
396 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
397 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
399 int rc
= sab_process_dir(ce
->down
);
401 sab_path
[pathpos
] = '\0';
405 closedir_uncached(dir
);
410 ce
= dircache_gen_next(ce
);
414 /* When simulator is used, it's only safe to yield here. */
417 /* Stop if we got an external signal. */
418 if(check_event_queue())
424 /* add "." and ".." */
426 ce
->info
.attribute
= ATTR_DIRECTORY
;
430 ce
= dircache_gen_next(ce
);
433 ce
->info
.attribute
= ATTR_DIRECTORY
;
435 ce
->down
= first_ce
->up
;
437 closedir_uncached(dir
);
441 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
443 #ifdef HAVE_MULTIVOLUME
446 memset(ce
, 0, sizeof(struct dircache_entry
));
448 strlcpy(sab_path
, "/", sizeof sab_path
);
449 return sab_process_dir(ce
);
451 #endif /* PLATFORM_NATIVE */
454 * Internal function to get a pointer to dircache_entry for a given filename.
455 * path: Absolute path to a file or directory (see comment)
456 * go_down: Returns the first entry of the directory given by the path (see comment)
458 * As a a special case, accept path="" as an alias for "/".
459 * Also if the path omits the first '/', it will be accepted.
461 * * If get_down=true:
462 * If path="/", the returned entry is the first of root directory (ie dircache_root)
463 * Otherwise, if 'entry' is the returned value when get_down=false,
464 * the functions returns entry->down (which can be NULL)
466 * * If get_down=false:
467 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
468 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
470 * dircache_get_entry(path)->d_name == chunk_n
472 * If path="/", the returned entry is NULL.
473 * If the entry doesn't exist, return NULL
475 * NOTE: this functions silently handles double '/'
477 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
479 char namecopy
[MAX_PATH
];
484 struct dircache_entry
*cache_entry
= dircache_root
;
486 strlcpy(namecopy
, path
, sizeof(namecopy
));
488 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
490 /* If request another chunk, the current entry has to be directory
491 * and so cache_entry->down has to be non-NULL/
492 * Special case of root because it's already the first entry of the root directory
494 * NOTE: this is safe even if cache_entry->down is NULL */
496 cache_entry
= cache_entry
->down
;
500 /* scan dir for name */
501 while(cache_entry
!= NULL
)
503 /* skip unused entries */
504 if(cache_entry
->d_name
== NULL
)
506 cache_entry
= cache_entry
->next
;
510 if(!strcasecmp(part
, cache_entry
->d_name
))
512 /* go to next entry */
513 cache_entry
= cache_entry
->next
;
516 /* handle not found case */
517 if(cache_entry
== NULL
)
521 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
523 return at_root
? cache_entry
: cache_entry
->down
;
525 return at_root
? NULL
: cache_entry
;
528 #ifdef HAVE_EEPROM_SETTINGS
530 #define DIRCACHE_MAGIC 0x00d0c0a1
531 struct dircache_maindata
{
536 struct dircache_entry
*root_entry
;
541 * Function to load the internal cache structure from disk to initialize
542 * the dircache really fast and little disk access.
544 int dircache_load(void)
546 struct dircache_maindata maindata
;
550 if (dircache_initialized
)
553 logf("Loading directory cache");
556 fd
= open_dircache_file(O_RDONLY
, 0);
560 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
561 if (bytes_read
!= sizeof(struct dircache_maindata
)
562 || maindata
.magic
!= DIRCACHE_MAGIC
|| maindata
.size
<= 0)
564 logf("Dircache file header error");
566 remove_dircache_file();
570 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
571 entry_count
= maindata
.entry_count
;
572 appflags
= maindata
.appflags
;
574 /* read the dircache file into memory,
575 * start with the struct dircache_entries */
576 ssize_t bytes_to_read
= entry_count
*sizeof(struct dircache_entry
);
577 bytes_read
= read(fd
, dircache_root
, bytes_to_read
);
579 if (bytes_read
!= bytes_to_read
)
581 logf("Dircache read failed #1");
585 /* continue with the d_names. Fix up pointers to them if needed */
586 d_names_start
= ((char*)&dircache_root
[entry_count
] + DIRCACHE_RESERVE
);
587 bytes_to_read
= maindata
.size
- bytes_to_read
;
588 bytes_read
= read(fd
, d_names_start
, bytes_to_read
);
590 remove_dircache_file();
591 if (bytes_read
!= bytes_to_read
)
593 logf("Dircache read failed #2");
597 d_names_end
= &d_names_start
[bytes_read
];
598 dot
= d_names_end
- sizeof(".");
599 dotdot
= dot
- sizeof("..");
601 /* d_names are in reverse order, so the last entry points to the first string */
602 ptrdiff_t offset_d_names
= maindata
.d_names_start
- d_names_start
,
603 offset_entries
= maindata
.root_entry
- dircache_root
;
605 /* offset_entries is less likely to differ, so check if it's 0 in the loop
606 * offset_d_names however is almost always non-zero, since dircache_save()
607 * creates a file which causes the reserve buffer to be used. since
608 * we allocate a new, empty DIRCACHE_RESERVE here, the strings are
610 if (offset_entries
!= 0 || offset_d_names
!= 0)
612 for(unsigned i
= 0; i
< entry_count
; i
++)
614 if (dircache_root
[i
].d_name
)
615 dircache_root
[i
].d_name
-= offset_d_names
;
617 if (offset_entries
== 0)
619 if (dircache_root
[i
].next
)
620 dircache_root
[i
].next
-= offset_entries
;
621 if (dircache_root
[i
].up
)
622 dircache_root
[i
].up
-= offset_entries
;
623 if (dircache_root
[i
].down
)
624 dircache_root
[i
].down
-= offset_entries
;
628 /* Cache successfully loaded. */
629 dircache_size
= maindata
.size
;
630 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
632 logf("Done, %ld KiB used", dircache_size
/ 1024);
633 dircache_initialized
= true;
634 memset(fd_bindings
, 0, sizeof(fd_bindings
));
640 * Function to save the internal cache stucture to disk for fast loading
643 int dircache_save(void)
645 struct dircache_maindata maindata
;
647 unsigned long bytes_written
;
649 remove_dircache_file();
651 if (!dircache_initialized
)
654 logf("Saving directory cache");
655 fd
= open_dircache_file(O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
657 maindata
.magic
= DIRCACHE_MAGIC
;
658 maindata
.size
= dircache_size
;
659 maindata
.root_entry
= dircache_root
;
660 maindata
.d_names_start
= d_names_start
;
661 maindata
.entry_count
= entry_count
;
662 maindata
.appflags
= appflags
;
664 /* Save the info structure */
665 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
666 if (bytes_written
!= sizeof(struct dircache_maindata
))
669 logf("dircache: write failed #1");
673 /* Dump whole directory cache to disk
674 * start by writing the dircache_entries */
675 size_t bytes_to_write
= entry_count
*sizeof(struct dircache_entry
);
676 bytes_written
= write(fd
, dircache_root
, bytes_to_write
);
677 if (bytes_written
!= bytes_to_write
)
679 logf("dircache: write failed #2");
683 /* continue with the d_names */
684 bytes_to_write
= d_names_end
- d_names_start
;
685 bytes_written
= write(fd
, d_names_start
, bytes_to_write
);
687 if (bytes_written
!= bytes_to_write
)
689 logf("dircache: write failed #3");
696 #endif /* HAVE_EEPROM_SETTINGS */
699 * Internal function which scans the disk and creates the dircache structure.
701 static int dircache_do_rebuild(void)
703 struct dircache_entry
* root_entry
;
704 unsigned int start_tick
;
707 /* Measure how long it takes build the cache. */
708 start_tick
= current_tick
;
709 dircache_initializing
= true;
712 /* reset dircache and alloc root entry */
714 root_entry
= allocate_entry();
716 #ifdef HAVE_MULTIVOLUME
717 append_position
= root_entry
;
719 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
721 if (fat_ismounted(i
))
725 #ifdef HAVE_MULTIVOLUME
726 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
728 if (dircache_scan_and_build(IF_MV2(0,) root_entry
) < 0)
729 #endif /* HAVE_MULTIVOLUME */
731 logf("dircache_scan_and_build failed");
734 dircache_initializing
= false;
738 #ifdef HAVE_MULTIVOLUME
743 logf("Done, %ld KiB used", dircache_size
/ 1024);
745 dircache_initialized
= true;
746 dircache_initializing
= false;
747 cache_build_ticks
= current_tick
- start_tick
;
749 /* Initialized fd bindings. */
750 memset(fd_bindings
, 0, sizeof(fd_bindings
));
751 for (i
= 0; i
< fdbind_idx
; i
++)
752 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
757 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
758 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
765 * Internal thread that controls transparent cache building.
767 static void dircache_thread(void)
769 struct queue_event ev
;
773 queue_wait(&dircache_queue
, &ev
);
779 if (!dircache_initialized
)
781 dircache_initialized
= false;
784 thread_enabled
= true;
785 dircache_do_rebuild();
786 thread_enabled
= false;
790 logf("Stopped the rebuilding.");
791 dircache_initialized
= false;
794 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
795 case SYS_USB_CONNECTED
:
796 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
797 usb_wait_for_disconnect(&dircache_queue
);
804 static void generate_dot_d_names(void)
806 dot
= (d_names_start
-= sizeof("."));
807 dotdot
= (d_names_start
-= sizeof(".."));
808 dircache_size
+= sizeof(".") + sizeof("..");
810 strcpy(dotdot
, "..");
813 * Start scanning the disk to build the dircache.
814 * Either transparent or non-transparent build method is used.
816 int dircache_build(int last_size
)
818 if (dircache_initialized
|| thread_enabled
)
821 logf("Building directory cache");
822 #ifdef HAVE_EEPROM_SETTINGS
823 remove_dircache_file();
826 /* Background build, dircache has been previously allocated */
827 if (dircache_size
> 0)
829 thread_enabled
= true;
830 dircache_initializing
= true;
831 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
834 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
836 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
837 dircache_root
= buffer_alloc(allocated_size
);
839 d_names_end
= ((char*)dircache_root
)+allocated_size
-1;
841 thread_enabled
= true;
842 generate_dot_d_names();
844 /* Start a transparent rebuild. */
845 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
849 /* We'll use the entire audiobuf to allocate the dircache
850 * struct dircache_entrys are allocated from the beginning
851 * and their corresponding d_name from the end
852 * after generation the buffer will be compacted with DIRCACHE_RESERVE
853 * free bytes inbetween */
854 audiobuf
= (char*)(((intptr_t)audiobuf
& ~0x03) + 0x04);
855 dircache_root
= (struct dircache_entry
*)audiobuf
;
856 d_names_start
= d_names_end
= audiobufend
- 1;
858 generate_dot_d_names();
860 /* Start a non-transparent rebuild. */
861 int res
= dircache_do_rebuild();
865 /* now compact the dircache buffer */
866 char* dst
= ((char*)&dircache_root
[entry_count
] + DIRCACHE_RESERVE
);
867 ptrdiff_t offset
= d_names_start
- dst
, size_to_move
;
868 if (offset
<= 0) /* something went wrong */
871 /* memmove d_names down, there's a possibility of overlap */
872 size_to_move
= dircache_size
- entry_count
*sizeof(struct dircache_entry
);
873 memmove(dst
, d_names_start
, size_to_move
);
875 /* fix up pointers to the d_names */
876 for(unsigned i
= 0; i
< entry_count
; i
++)
877 dircache_root
[i
].d_name
-= offset
;
879 d_names_end
-= offset
;
880 /* equivalent to dircache_size + DIRCACHE_RESERVE */
881 allocated_size
= (d_names_end
- (char*)dircache_root
);
883 audiobuf
+= allocated_size
;
889 * Steal the allocated dircache buffer and disable dircache.
891 void* dircache_steal_buffer(long *size
)
894 if (dircache_size
== 0)
900 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
902 return dircache_root
;
906 * Main initialization function that must be called before any other
907 * operations within the dircache.
909 void dircache_init(void)
912 int thread_id
__attribute__((unused
));
914 dircache_initialized
= false;
915 dircache_initializing
= false;
917 memset(opendirs
, 0, sizeof(opendirs
));
918 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
920 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
923 queue_init(&dircache_queue
, true);
924 thread_id
= create_thread(dircache_thread
, dircache_stack
,
925 sizeof(dircache_stack
), 0, dircache_thread_name
926 IF_PRIO(, PRIORITY_BACKGROUND
)
928 #ifdef HAVE_IO_PRIORITY
929 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
935 * Returns true if dircache has been initialized and is ready to be used.
937 bool dircache_is_enabled(void)
939 return dircache_initialized
;
943 * Returns true if dircache is being initialized.
945 bool dircache_is_initializing(void)
947 return dircache_initializing
|| thread_enabled
;
951 * Set application flags used to determine if dircache is still intact.
953 void dircache_set_appflag(long mask
)
959 * Get application flags used to determine if dircache is still intact.
961 bool dircache_get_appflag(long mask
)
963 return dircache_is_enabled() && (appflags
& mask
);
967 * Returns the current number of entries (directories and files) in the cache.
969 int dircache_get_entry_count(void)
975 * Returns the allocated space for dircache (without reserve space).
977 int dircache_get_cache_size(void)
979 return dircache_is_enabled() ? dircache_size
: 0;
983 * Returns how many bytes of the reserve allocation for live cache
984 * updates have been used.
986 int dircache_get_reserve_used(void)
988 return dircache_is_enabled() ? reserve_used
: 0;
992 * Returns the time in kernel ticks that took to build the cache.
994 int dircache_get_build_ticks(void)
996 return dircache_is_enabled() ? cache_build_ticks
: 0;
1000 * Disables the dircache. Usually called on shutdown or when
1001 * accepting a usb connection.
1003 void dircache_disable(void)
1009 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
1011 while (thread_enabled
)
1013 dircache_initialized
= false;
1015 logf("Waiting for cached dirs to release");
1017 cache_in_use
= false;
1018 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
1019 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
1021 cache_in_use
= true;
1026 } while (cache_in_use
) ;
1028 logf("Cache released");
1033 * Usermode function to return dircache_entry index to the given path.
1035 static int dircache_get_entry_id_ex(const char *filename
, bool go_down
)
1037 if (!dircache_initialized
|| filename
== NULL
)
1040 struct dircache_entry
* res
= dircache_get_entry(filename
, go_down
);
1041 return res
? res
- dircache_root
: -1;
1044 int dircache_get_entry_id(const char* filename
)
1046 return dircache_get_entry_id_ex(filename
, false);
1050 * Internal: Get the startcluster for the index
1052 long _dircache_get_entry_startcluster(int id
)
1054 return get_entry(id
)->startcluster
;
1058 * Internal: Get the struct dirinfo for the index
1060 struct dirinfo
* _dircache_get_entry_dirinfo(int id
)
1062 return &get_entry(id
)->info
;
1066 * build a path from an entry upto the root using recursion
1068 * it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
1069 * and it will leave a trailing '/'
1071 * returns the position of that trailing '/' so it can be deleted afterwards
1072 * (or, in case of truncation, the position of the nul byte */
1073 static size_t copy_path_helper(const struct dircache_entry
*entry
, char *buf
, size_t size
)
1078 offset
+= copy_path_helper(entry
->up
, buf
, size
);
1080 size_t len
= strlcpy(buf
+offset
, entry
->d_name
, size
- offset
) + offset
;
1089 * Function to copy the full absolute path from dircache to the given buffer
1090 * using the given dircache_entry pointer.
1092 * Returns the size of the resulting string, or 0 if an error occured
1094 size_t dircache_copy_path(int index
, char *buf
, size_t size
)
1096 if (!size
|| !buf
|| index
< 0)
1100 size_t res
= copy_path_helper(&dircache_root
[index
], buf
, size
- 1);
1101 /* fixup trailing '/' */
1106 /* --- Directory cache live updating functions --- */
1107 static int block_until_ready(void)
1109 /* Block until dircache has been built. */
1110 while (!dircache_initialized
&& dircache_is_initializing())
1113 if (!dircache_initialized
)
1119 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
1121 struct dircache_entry
*entry
;
1122 char basedir
[MAX_PATH
*2];
1124 long last_cache_size
= dircache_size
;
1126 strlcpy(basedir
, path
, sizeof(basedir
));
1127 new = strrchr(basedir
, '/');
1130 logf("error occurred");
1131 dircache_initialized
= false;
1138 entry
= dircache_get_entry(basedir
, true);
1141 logf("basedir not found!");
1142 logf("%s", basedir
);
1143 dircache_initialized
= false;
1147 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
1148 >= DIRCACHE_RESERVE
)
1150 logf("not enough space");
1151 dircache_initialized
= false;
1155 while (entry
->next
!= NULL
)
1156 entry
= entry
->next
;
1158 if (entry
->d_name
!= NULL
)
1160 entry
= dircache_gen_next(entry
);
1163 dircache_initialized
= false;
1168 size_t size
= strlen(new) + 1;
1169 entry
->d_name
= (d_names_start
-= size
);
1170 entry
->startcluster
= 0;
1171 memset(&entry
->info
, 0, sizeof(entry
->info
));
1172 entry
->info
.attribute
= attribute
;
1174 strcpy(entry
->d_name
, new);
1175 dircache_size
+= size
;
1177 if (attribute
& ATTR_DIRECTORY
)
1180 dircache_gen_down(entry
);
1183 reserve_used
+= dircache_size
- last_cache_size
;
1188 void dircache_bind(int fd
, const char *path
)
1190 struct dircache_entry
*entry
;
1192 /* Queue requests until dircache has been built. */
1193 if (!dircache_initialized
&& dircache_is_initializing())
1195 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1197 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1198 sizeof(fdbind_cache
[fdbind_idx
].path
));
1199 fdbind_cache
[fdbind_idx
].fd
= fd
;
1204 if (!dircache_initialized
)
1207 logf("bind: %d/%s", fd
, path
);
1208 entry
= dircache_get_entry(path
, false);
1212 dircache_initialized
= false;
1216 fd_bindings
[fd
] = entry
;
1219 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1221 if (!dircache_initialized
|| fd
< 0)
1224 if (fd_bindings
[fd
] == NULL
)
1226 logf("dircache fd(%d) access error", fd
);
1227 dircache_initialized
= false;
1231 fd_bindings
[fd
]->info
.size
= newsize
;
1232 fd_bindings
[fd
]->startcluster
= startcluster
;
1234 void dircache_update_filetime(int fd
)
1240 struct tm
*now
= get_time();
1241 if (!dircache_initialized
|| fd
< 0)
1244 if (fd_bindings
[fd
] == NULL
)
1246 logf("dircache fd access error");
1247 dircache_initialized
= false;
1250 year
= now
->tm_year
+1900-1980;
1251 fd_bindings
[fd
]->info
.wrtdate
= (((year
)&0x7f)<<9) |
1252 (((now
->tm_mon
+1)&0xf)<<5) |
1253 (((now
->tm_mday
)&0x1f));
1254 fd_bindings
[fd
]->info
.wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1255 (((now
->tm_min
)&0x3f)<<5) |
1256 (((now
->tm_sec
/2)&0x1f));
1260 void dircache_mkdir(const char *path
)
1262 if (block_until_ready())
1266 logf("mkdir: %s", path
);
1267 dircache_new_entry(path
, ATTR_DIRECTORY
);
1270 void dircache_rmdir(const char *path
)
1272 struct dircache_entry
*entry
;
1274 if (block_until_ready())
1277 logf("rmdir: %s", path
);
1278 entry
= dircache_get_entry(path
, false);
1279 if (entry
== NULL
|| entry
->down
== NULL
)
1281 logf("not found or not a directory!");
1282 dircache_initialized
= false;
1287 entry
->d_name
= NULL
;
1290 /* Remove a file from cache */
1291 void dircache_remove(const char *name
)
1293 struct dircache_entry
*entry
;
1295 if (block_until_ready())
1298 logf("remove: %s", name
);
1300 entry
= dircache_get_entry(name
, false);
1305 dircache_initialized
= false;
1309 entry
->d_name
= NULL
;
1312 void dircache_rename(const char *oldpath
, const char *newpath
)
1314 struct dircache_entry
*entry
, *newentry
;
1315 struct dircache_entry oldentry
;
1316 char absolute_path
[MAX_PATH
*2];
1319 if (block_until_ready())
1322 logf("rename: %s->%s", oldpath
, newpath
);
1324 entry
= dircache_get_entry(oldpath
, false);
1328 dircache_initialized
= false;
1332 /* Delete the old entry. */
1333 entry
->d_name
= NULL
;
1335 /** If we rename the same filename twice in a row, we need to
1336 * save the data, because the entry will be re-used. */
1339 /* Generate the absolute path for destination if necessary. */
1340 if (newpath
[0] != '/')
1342 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1343 p
= strrchr(absolute_path
, '/');
1346 logf("Invalid path");
1347 dircache_initialized
= false;
1352 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1353 newpath
= absolute_path
;
1356 newentry
= dircache_new_entry(newpath
, entry
->info
.attribute
);
1357 if (newentry
== NULL
)
1359 dircache_initialized
= false;
1363 newentry
->down
= oldentry
.down
;
1364 newentry
->startcluster
= oldentry
.startcluster
;
1365 newentry
->info
.size
= oldentry
.info
.size
;
1366 newentry
->info
.wrtdate
= oldentry
.info
.wrtdate
;
1367 newentry
->info
.wrttime
= oldentry
.info
.wrttime
;
1370 void dircache_add_file(const char *path
, long startcluster
)
1372 struct dircache_entry
*entry
;
1374 if (block_until_ready())
1377 logf("add file: %s", path
);
1378 entry
= dircache_new_entry(path
, 0);
1382 entry
->startcluster
= startcluster
;
1385 static bool is_disable_msg_pending(void)
1387 return check_event_queue();
1390 DIR_CACHED
* opendir_cached(const char* name
)
1393 DIR_CACHED
* pdir
= opendirs
;
1395 if ( name
[0] != '/' )
1397 DEBUGF("Only absolute paths supported right now\n");
1401 /* find a free dir descriptor */
1402 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1406 if ( dd
== MAX_OPEN_DIRS
)
1408 DEBUGF("Too many dirs open\n");
1415 if (!dircache_initialized
|| is_disable_msg_pending())
1417 pdir
->internal_entry
= -1;
1418 pdir
->regulardir
= opendir_uncached(name
);
1422 pdir
->regulardir
= NULL
;
1423 pdir
->internal_entry
= dircache_get_entry_id_ex(name
, true);
1424 pdir
->theent
.info
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1427 if (pdir
->internal_entry
== -1 && pdir
->regulardir
== NULL
)
1436 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1438 struct dircache_entry
*ce
= get_entry(dir
->internal_entry
);
1439 struct dirent_uncached
*regentry
;
1444 if (dir
->regulardir
!= NULL
)
1446 regentry
= readdir_uncached(dir
->regulardir
);
1447 if (regentry
== NULL
)
1450 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1451 dir
->theent
.startcluster
= regentry
->startcluster
;
1452 dir
->theent
.info
= regentry
->info
;
1454 return &dir
->theent
;
1457 /* if theent.attribute=-1 then this is the first call */
1458 /* otherwise, this is is not so we first take the entry's ->next */
1459 /* NOTE: normal file can't have attribute=-1 */
1460 if(dir
->theent
.info
.attribute
!= -1)
1462 /* skip unused entries */
1463 while(ce
!= NULL
&& ce
->d_name
== NULL
)
1469 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1470 /* Can't do `dir->theent = *ce`
1471 because that modifies the d_name pointer. */
1472 dir
->theent
.startcluster
= ce
->startcluster
;
1473 dir
->theent
.info
= ce
->info
;
1474 dir
->internal_entry
= ce
- dircache_root
;
1476 //logf("-> %s", ce->d_name);
1477 return &dir
->theent
;
1480 int closedir_cached(DIR_CACHED
* dir
)
1486 if (dir
->regulardir
!= NULL
)
1487 return closedir_uncached(dir
->regulardir
);
1492 int mkdir_cached(const char *name
)
1494 int rc
=mkdir_uncached(name
);
1496 dircache_mkdir(name
);
1500 int rmdir_cached(const char* name
)
1502 int rc
=rmdir_uncached(name
);
1504 dircache_rmdir(name
);