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"
51 #define DIRCACHE_BUILD 1
52 #define DIRCACHE_STOP 2
55 #define MAX_OPEN_DIRS 12
57 #define MAX_OPEN_DIRS 8
59 static DIR_CACHED opendirs
[MAX_OPEN_DIRS
];
61 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
62 static struct dircache_entry
*dircache_root
;
63 #ifdef HAVE_MULTIVOLUME
64 static struct dircache_entry
*append_position
;
67 static bool dircache_initialized
= false;
68 static bool dircache_initializing
= false;
69 static bool thread_enabled
= false;
70 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
71 static unsigned long dircache_size
= 0;
72 static unsigned long entry_count
= 0;
73 static unsigned long reserve_used
= 0;
74 static unsigned int cache_build_ticks
= 0;
75 static unsigned long appflags
= 0;
77 static struct event_queue dircache_queue SHAREDBSS_ATTR
;
78 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x400)/sizeof(long)];
79 static const char dircache_thread_name
[] = "dircache";
81 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
82 static int fdbind_idx
= 0;
84 /* --- Internal cache structure control functions --- */
86 #ifdef HAVE_EEPROM_SETTINGS
88 * Open the dircache file to save a snapshot on disk
90 static int open_dircache_file(unsigned flags
, int permissions
)
93 return open(DIRCACHE_FILE
, flags
, permissions
);
95 return open(DIRCACHE_FILE
, flags
);
99 * Remove the snapshot file
101 static int remove_dircache_file(void)
103 return remove(DIRCACHE_FILE
);
107 * Internal function to allocate a new dircache_entry from memory.
109 static struct dircache_entry
* allocate_entry(void)
111 struct dircache_entry
*next_entry
;
113 if (dircache_size
> allocated_size
- MAX_PATH
*2)
115 logf("size limit reached");
119 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
120 #ifdef ROCKBOX_STRICT_ALIGN
121 /* Make sure the entry is long aligned. */
122 if ((long)next_entry
& 0x03)
124 dircache_size
+= 4 - ((long)next_entry
& 0x03);
125 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
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
->d_name
= ((char *)dircache_root
+ dircache_size
);
235 ce
->startcluster
= sab
.direntry
->firstcluster
;
236 ce
->info
.size
= sab
.direntry
->filesize
;
237 ce
->info
.attribute
= sab
.direntry
->attr
;
238 ce
->info
.wrtdate
= sab
.direntry
->wrtdate
;
239 ce
->info
.wrttime
= sab
.direntry
->wrttime
;
241 strcpy(ce
->d_name
, sab
.direntry
->name
);
242 dircache_size
+= strlen(ce
->d_name
) + 1;
246 if(ce
->info
.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 ".." */
265 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
266 ce
->startcluster
= startcluster
;
270 ce
= dircache_gen_next(ce
);
273 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
274 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
276 ce
->down
= first_ce
->up
;
278 /* second pass: recurse ! */
283 if(ce
->d_name
!= NULL
&& ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".")
284 && strcmp(ce
->d_name
, ".."))
285 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
293 /* used during the generation */
294 static struct fat_dir sab_fat_dir
;
296 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
298 memset(ce
, 0, sizeof(struct dircache_entry
));
300 #ifdef HAVE_MULTIVOLUME
303 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
304 size_t len
= snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
)+1;
305 dircache_size
+= len
;
306 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
308 append_position
= dircache_gen_next(ce
);
309 ce
= dircache_gen_down(ce
);
313 struct fat_direntry direntry
; /* ditto */
314 #ifdef HAVE_MULTIVOLUME
317 sab
.dir
= &sab_fat_dir
;
318 sab
.direntry
= &direntry
;
320 return sab_process_dir(0, ce
);
322 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
323 static char sab_path
[MAX_PATH
];
325 static int sab_process_dir(struct dircache_entry
*ce
)
327 struct dirent_uncached
*entry
;
328 struct dircache_entry
*first_ce
= ce
;
329 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
332 logf("Failed to opendir_uncached(%s)", sab_path
);
336 while((entry
= readdir_uncached(dir
)))
338 if(!strcmp(".", entry
->d_name
) ||
339 !strcmp("..", entry
->d_name
))
342 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
343 ce
->info
= entry
->info
;
345 strcpy(ce
->d_name
, entry
->d_name
);
346 dircache_size
+= strlen(entry
->d_name
) + 1;
350 if(entry
->info
.attribute
& ATTR_DIRECTORY
)
352 dircache_gen_down(ce
);
355 closedir_uncached(dir
);
358 /* save current paths size */
359 int pathpos
= strlen(sab_path
);
361 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
362 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
364 int rc
= sab_process_dir(ce
->down
);
366 sab_path
[pathpos
] = '\0';
370 closedir_uncached(dir
);
375 ce
= dircache_gen_next(ce
);
379 /* When simulator is used, it's only safe to yield here. */
382 /* Stop if we got an external signal. */
383 if(check_event_queue())
389 /* add "." and ".." */
391 ce
->info
.attribute
= ATTR_DIRECTORY
;
395 ce
= dircache_gen_next(ce
);
398 ce
->info
.attribute
= ATTR_DIRECTORY
;
400 ce
->down
= first_ce
->up
;
402 closedir_uncached(dir
);
406 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
408 #ifdef HAVE_MULTIVOLUME
411 memset(ce
, 0, sizeof(struct dircache_entry
));
413 strlcpy(sab_path
, "/", sizeof sab_path
);
414 return sab_process_dir(ce
);
416 #endif /* PLATFORM_NATIVE */
419 * Internal function to get a pointer to dircache_entry for a given filename.
420 * path: Absolute path to a file or directory (see comment)
421 * go_down: Returns the first entry of the directory given by the path (see comment)
423 * As a a special case, accept path="" as an alias for "/".
424 * Also if the path omits the first '/', it will be accepted.
426 * * If get_down=true:
427 * If path="/", the returned entry is the first of root directory (ie dircache_root)
428 * Otherwise, if 'entry' is the returned value when get_down=false,
429 * the functions returns entry->down (which can be NULL)
431 * * If get_down=false:
432 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
433 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
435 * dircache_get_entry(path)->d_name == chunk_n
437 * If path="/", the returned entry is NULL.
438 * If the entry doesn't exist, return NULL
440 * NOTE: this functions silently handles double '/'
442 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
444 char namecopy
[MAX_PATH
];
449 struct dircache_entry
*cache_entry
= dircache_root
;
451 strlcpy(namecopy
, path
, sizeof(namecopy
));
453 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
455 /* If request another chunk, the current entry has to be directory
456 * and so cache_entry->down has to be non-NULL/
457 * Special case of root because it's already the first entry of the root directory
459 * NOTE: this is safe even if cache_entry->down is NULL */
461 cache_entry
= cache_entry
->down
;
465 /* scan dir for name */
466 while(cache_entry
!= NULL
)
468 /* skip unused entries */
469 if(cache_entry
->d_name
== NULL
)
471 cache_entry
= cache_entry
->next
;
475 if(!strcasecmp(part
, cache_entry
->d_name
))
477 /* go to next entry */
478 cache_entry
= cache_entry
->next
;
481 /* handle not found case */
482 if(cache_entry
== NULL
)
486 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
488 return at_root
? cache_entry
: cache_entry
->down
;
490 return at_root
? NULL
: cache_entry
;
493 #ifdef HAVE_EEPROM_SETTINGS
495 * Function to load the internal cache structure from disk to initialize
496 * the dircache really fast and little disk access.
498 int dircache_load(void)
500 struct dircache_maindata maindata
;
504 if (dircache_initialized
)
507 logf("Loading directory cache");
510 fd
= open_dircache_file(O_RDONLY
, 0);
514 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
515 if (bytes_read
!= sizeof(struct dircache_maindata
)
516 || maindata
.size
<= 0)
518 logf("Dircache file header error");
520 remove_dircache_file();
524 dircache_root
= buffer_alloc(0);
525 if ((long)maindata
.root_entry
!= (long)dircache_root
)
527 logf("Position missmatch");
529 remove_dircache_file();
533 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
534 entry_count
= maindata
.entry_count
;
535 appflags
= maindata
.appflags
;
536 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
538 remove_dircache_file();
540 if (bytes_read
!= maindata
.size
)
542 logf("Dircache read failed");
546 /* Cache successfully loaded. */
547 dircache_size
= maindata
.size
;
548 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
550 logf("Done, %ld KiB used", dircache_size
/ 1024);
551 dircache_initialized
= true;
552 memset(fd_bindings
, 0, sizeof(fd_bindings
));
558 * Function to save the internal cache stucture to disk for fast loading
561 int dircache_save(void)
563 struct dircache_maindata maindata
;
565 unsigned long bytes_written
;
567 remove_dircache_file();
569 if (!dircache_initialized
)
572 logf("Saving directory cache");
573 fd
= open_dircache_file(O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
575 maindata
.magic
= DIRCACHE_MAGIC
;
576 maindata
.size
= dircache_size
;
577 maindata
.root_entry
= dircache_root
;
578 maindata
.entry_count
= entry_count
;
579 maindata
.appflags
= appflags
;
581 /* Save the info structure */
582 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
583 if (bytes_written
!= sizeof(struct dircache_maindata
))
586 logf("dircache: write failed #1");
590 /* Dump whole directory cache to disk */
591 bytes_written
= write(fd
, dircache_root
, dircache_size
);
593 if (bytes_written
!= dircache_size
)
595 logf("dircache: write failed #2");
601 #endif /* HAVE_EEPROM_SETTINGS */
604 * Internal function which scans the disk and creates the dircache structure.
606 static int dircache_do_rebuild(void)
608 unsigned int start_tick
;
611 /* Measure how long it takes build the cache. */
612 start_tick
= current_tick
;
613 dircache_initializing
= true;
617 dircache_size
= sizeof(struct dircache_entry
);
619 #ifdef HAVE_MULTIVOLUME
620 append_position
= dircache_root
;
622 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
624 if (fat_ismounted(i
))
628 #ifdef HAVE_MULTIVOLUME
629 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
631 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
632 #endif /* HAVE_MULTIVOLUME */
634 logf("dircache_scan_and_build failed");
637 dircache_initializing
= false;
641 #ifdef HAVE_MULTIVOLUME
646 logf("Done, %ld KiB used", dircache_size
/ 1024);
648 dircache_initialized
= true;
649 dircache_initializing
= false;
650 cache_build_ticks
= current_tick
- start_tick
;
652 /* Initialized fd bindings. */
653 memset(fd_bindings
, 0, sizeof(fd_bindings
));
654 for (i
= 0; i
< fdbind_idx
; i
++)
655 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
660 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
661 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
665 /* We have to long align the audiobuf to keep the buffer access fast. */
666 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
667 audiobuf
+= DIRCACHE_RESERVE
;
668 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
676 * Internal thread that controls transparent cache building.
678 static void dircache_thread(void)
680 struct queue_event ev
;
684 queue_wait(&dircache_queue
, &ev
);
690 if (!dircache_initialized
)
692 dircache_initialized
= false;
695 thread_enabled
= true;
696 dircache_do_rebuild();
697 thread_enabled
= false;
701 logf("Stopped the rebuilding.");
702 dircache_initialized
= false;
705 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
706 case SYS_USB_CONNECTED
:
707 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
708 usb_wait_for_disconnect(&dircache_queue
);
716 * Start scanning the disk to build the dircache.
717 * Either transparent or non-transparent build method is used.
719 int dircache_build(int last_size
)
721 if (dircache_initialized
|| thread_enabled
)
724 logf("Building directory cache");
725 #ifdef HAVE_EEPROM_SETTINGS
726 remove_dircache_file();
729 /* Background build, dircache has been previously allocated */
730 if (dircache_size
> 0)
732 thread_enabled
= true;
733 dircache_initializing
= true;
734 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
738 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
740 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
741 dircache_root
= buffer_alloc(allocated_size
);
742 thread_enabled
= true;
744 /* Start a transparent rebuild. */
745 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
749 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
751 /* Start a non-transparent rebuild. */
752 return dircache_do_rebuild();
756 * Steal the allocated dircache buffer and disable dircache.
758 void* dircache_steal_buffer(long *size
)
761 if (dircache_size
== 0)
767 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
769 return dircache_root
;
773 * Main initialization function that must be called before any other
774 * operations within the dircache.
776 void dircache_init(void)
779 int thread_id
__attribute__((unused
));
781 dircache_initialized
= false;
782 dircache_initializing
= false;
784 memset(opendirs
, 0, sizeof(opendirs
));
785 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
787 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
790 queue_init(&dircache_queue
, true);
791 thread_id
= create_thread(dircache_thread
, dircache_stack
,
792 sizeof(dircache_stack
), 0, dircache_thread_name
793 IF_PRIO(, PRIORITY_BACKGROUND
)
795 #ifdef HAVE_IO_PRIORITY
796 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
802 * Returns true if dircache has been initialized and is ready to be used.
804 bool dircache_is_enabled(void)
806 return dircache_initialized
;
810 * Returns true if dircache is being initialized.
812 bool dircache_is_initializing(void)
814 return dircache_initializing
|| thread_enabled
;
818 * Set application flags used to determine if dircache is still intact.
820 void dircache_set_appflag(long mask
)
826 * Get application flags used to determine if dircache is still intact.
828 bool dircache_get_appflag(long mask
)
830 return dircache_is_enabled() && (appflags
& mask
);
834 * Returns the current number of entries (directories and files) in the cache.
836 int dircache_get_entry_count(void)
842 * Returns the allocated space for dircache (without reserve space).
844 int dircache_get_cache_size(void)
846 return dircache_is_enabled() ? dircache_size
: 0;
850 * Returns how many bytes of the reserve allocation for live cache
851 * updates have been used.
853 int dircache_get_reserve_used(void)
855 return dircache_is_enabled() ? reserve_used
: 0;
859 * Returns the time in kernel ticks that took to build the cache.
861 int dircache_get_build_ticks(void)
863 return dircache_is_enabled() ? cache_build_ticks
: 0;
867 * Disables the dircache. Usually called on shutdown or when
868 * accepting a usb connection.
870 void dircache_disable(void)
876 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
878 while (thread_enabled
)
880 dircache_initialized
= false;
882 logf("Waiting for cached dirs to release");
884 cache_in_use
= false;
885 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
886 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
893 } while (cache_in_use
) ;
895 logf("Cache released");
900 * Usermode function to return dircache_entry pointer to the given path.
902 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
904 if (!dircache_initialized
|| filename
== NULL
)
907 return dircache_get_entry(filename
, false);
911 * build a path from an entry upto the root using recursion
913 * it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
914 * and it will leave a trailing '/'
916 * returns the position of that trailing '/' so it can be deleted afterwards
917 * (or, in case of truncation, the position of the nul byte */
918 static size_t copy_path_helper(const struct dircache_entry
*entry
, char *buf
, size_t size
)
922 copy_path_helper(entry
->up
, buf
, size
);
924 size_t len
= strlcat(buf
, entry
->d_name
, size
);
933 * Function to copy the full absolute path from dircache to the given buffer
934 * using the given dircache_entry pointer.
936 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
938 if (size
<= 0 || !buf
)
942 size_t res
= copy_path_helper(entry
, buf
, size
);
943 /* fixup trailing '/' */
947 /* --- Directory cache live updating functions --- */
948 static int block_until_ready(void)
950 /* Block until dircache has been built. */
951 while (!dircache_initialized
&& dircache_is_initializing())
954 if (!dircache_initialized
)
960 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
962 struct dircache_entry
*entry
;
963 char basedir
[MAX_PATH
*2];
965 long last_cache_size
= dircache_size
;
967 strlcpy(basedir
, path
, sizeof(basedir
));
968 new = strrchr(basedir
, '/');
971 logf("error occurred");
972 dircache_initialized
= false;
979 entry
= dircache_get_entry(basedir
, true);
982 logf("basedir not found!");
984 dircache_initialized
= false;
988 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
991 logf("not enough space");
992 dircache_initialized
= false;
996 while (entry
->next
!= NULL
)
999 if (entry
->d_name
!= NULL
)
1001 entry
= dircache_gen_next(entry
);
1004 dircache_initialized
= false;
1009 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
1010 entry
->startcluster
= 0;
1011 memset(&entry
->info
, 0, sizeof(entry
->info
));
1012 entry
->info
.attribute
= attribute
;
1014 strcpy(entry
->d_name
, new);
1015 dircache_size
+= strlen(entry
->d_name
);
1017 if (attribute
& ATTR_DIRECTORY
)
1020 dircache_gen_down(entry
);
1023 reserve_used
+= dircache_size
- last_cache_size
;
1028 void dircache_bind(int fd
, const char *path
)
1030 struct dircache_entry
*entry
;
1032 /* Queue requests until dircache has been built. */
1033 if (!dircache_initialized
&& dircache_is_initializing())
1035 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1037 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1038 sizeof(fdbind_cache
[fdbind_idx
].path
));
1039 fdbind_cache
[fdbind_idx
].fd
= fd
;
1044 if (!dircache_initialized
)
1047 logf("bind: %d/%s", fd
, path
);
1048 entry
= dircache_get_entry(path
, false);
1052 dircache_initialized
= false;
1056 fd_bindings
[fd
] = entry
;
1059 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1061 if (!dircache_initialized
|| fd
< 0)
1064 if (fd_bindings
[fd
] == NULL
)
1066 logf("dircache fd(%d) access error", fd
);
1067 dircache_initialized
= false;
1071 fd_bindings
[fd
]->info
.size
= newsize
;
1072 fd_bindings
[fd
]->startcluster
= startcluster
;
1074 void dircache_update_filetime(int fd
)
1080 struct tm
*now
= get_time();
1081 if (!dircache_initialized
|| fd
< 0)
1084 if (fd_bindings
[fd
] == NULL
)
1086 logf("dircache fd access error");
1087 dircache_initialized
= false;
1090 year
= now
->tm_year
+1900-1980;
1091 fd_bindings
[fd
]->info
.wrtdate
= (((year
)&0x7f)<<9) |
1092 (((now
->tm_mon
+1)&0xf)<<5) |
1093 (((now
->tm_mday
)&0x1f));
1094 fd_bindings
[fd
]->info
.wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1095 (((now
->tm_min
)&0x3f)<<5) |
1096 (((now
->tm_sec
/2)&0x1f));
1100 void dircache_mkdir(const char *path
)
1102 if (block_until_ready())
1106 logf("mkdir: %s", path
);
1107 dircache_new_entry(path
, ATTR_DIRECTORY
);
1110 void dircache_rmdir(const char *path
)
1112 struct dircache_entry
*entry
;
1114 if (block_until_ready())
1117 logf("rmdir: %s", path
);
1118 entry
= dircache_get_entry(path
, false);
1119 if (entry
== NULL
|| entry
->down
== NULL
)
1121 logf("not found or not a directory!");
1122 dircache_initialized
= false;
1127 entry
->d_name
= NULL
;
1130 /* Remove a file from cache */
1131 void dircache_remove(const char *name
)
1133 struct dircache_entry
*entry
;
1135 if (block_until_ready())
1138 logf("remove: %s", name
);
1140 entry
= dircache_get_entry(name
, false);
1145 dircache_initialized
= false;
1149 entry
->d_name
= NULL
;
1152 void dircache_rename(const char *oldpath
, const char *newpath
)
1154 struct dircache_entry
*entry
, *newentry
;
1155 struct dircache_entry oldentry
;
1156 char absolute_path
[MAX_PATH
*2];
1159 if (block_until_ready())
1162 logf("rename: %s->%s", oldpath
, newpath
);
1164 entry
= dircache_get_entry(oldpath
, false);
1168 dircache_initialized
= false;
1172 /* Delete the old entry. */
1173 entry
->d_name
= NULL
;
1175 /** If we rename the same filename twice in a row, we need to
1176 * save the data, because the entry will be re-used. */
1179 /* Generate the absolute path for destination if necessary. */
1180 if (newpath
[0] != '/')
1182 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1183 p
= strrchr(absolute_path
, '/');
1186 logf("Invalid path");
1187 dircache_initialized
= false;
1192 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1193 newpath
= absolute_path
;
1196 newentry
= dircache_new_entry(newpath
, entry
->info
.attribute
);
1197 if (newentry
== NULL
)
1199 dircache_initialized
= false;
1203 newentry
->down
= oldentry
.down
;
1204 newentry
->startcluster
= oldentry
.startcluster
;
1205 newentry
->info
.size
= oldentry
.info
.size
;
1206 newentry
->info
.wrtdate
= oldentry
.info
.wrtdate
;
1207 newentry
->info
.wrttime
= oldentry
.info
.wrttime
;
1210 void dircache_add_file(const char *path
, long startcluster
)
1212 struct dircache_entry
*entry
;
1214 if (block_until_ready())
1217 logf("add file: %s", path
);
1218 entry
= dircache_new_entry(path
, 0);
1222 entry
->startcluster
= startcluster
;
1225 static bool is_disable_msg_pending(void)
1227 return check_event_queue();
1230 DIR_CACHED
* opendir_cached(const char* name
)
1233 DIR_CACHED
* pdir
= opendirs
;
1235 if ( name
[0] != '/' )
1237 DEBUGF("Only absolute paths supported right now\n");
1241 /* find a free dir descriptor */
1242 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1246 if ( dd
== MAX_OPEN_DIRS
)
1248 DEBUGF("Too many dirs open\n");
1255 if (!dircache_initialized
|| is_disable_msg_pending())
1257 pdir
->internal_entry
= NULL
;
1258 pdir
->regulardir
= opendir_uncached(name
);
1262 pdir
->regulardir
= NULL
;
1263 pdir
->internal_entry
= dircache_get_entry(name
, true);
1264 pdir
->theent
.info
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1267 if (pdir
->internal_entry
== NULL
&& pdir
->regulardir
== NULL
)
1276 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1278 struct dircache_entry
*ce
= dir
->internal_entry
;
1279 struct dirent_uncached
*regentry
;
1284 if (dir
->regulardir
!= NULL
)
1286 regentry
= readdir_uncached(dir
->regulardir
);
1287 if (regentry
== NULL
)
1290 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1291 dir
->theent
.startcluster
= regentry
->startcluster
;
1292 dir
->theent
.info
= regentry
->info
;
1294 return &dir
->theent
;
1297 /* if theent.attribute=-1 then this is the first call */
1298 /* otherwise, this is is not so we first take the entry's ->next */
1299 /* NOTE: normal file can't have attribute=-1 */
1300 if(dir
->theent
.info
.attribute
!= -1)
1302 /* skip unused entries */
1303 while(ce
!= NULL
&& ce
->d_name
== NULL
)
1309 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1310 /* Can't do `dir->theent = *ce`
1311 because that modifies the d_name pointer. */
1312 dir
->theent
.startcluster
= ce
->startcluster
;
1313 dir
->theent
.info
= ce
->info
;
1314 dir
->internal_entry
= ce
;
1316 //logf("-> %s", ce->d_name);
1317 return &dir
->theent
;
1320 int closedir_cached(DIR_CACHED
* dir
)
1326 if (dir
->regulardir
!= NULL
)
1327 return closedir_uncached(dir
->regulardir
);
1332 int mkdir_cached(const char *name
)
1334 int rc
=mkdir_uncached(name
);
1336 dircache_mkdir(name
);
1340 int rmdir_cached(const char* name
)
1342 int rc
=rmdir_uncached(name
);
1344 dircache_rmdir(name
);