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 const char *file
= get_user_file_path(DIRCACHE_FILE
, IS_FILE
|NEED_WRITE
,
95 return open(file
, flags
, permissions
);
97 return open(file
, flags
);
101 * Remove the snapshot file
103 static int remove_dircache_file(void)
106 return remove(get_user_file_path(DIRCACHE_FILE
, IS_FILE
|NEED_WRITE
,
107 path
, sizeof(path
)));
111 * Internal function to allocate a new dircache_entry from memory.
113 static struct dircache_entry
* allocate_entry(void)
115 struct dircache_entry
*next_entry
;
117 if (dircache_size
> allocated_size
- MAX_PATH
*2)
119 logf("size limit reached");
123 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
124 #ifdef ROCKBOX_STRICT_ALIGN
125 /* Make sure the entry is long aligned. */
126 if ((long)next_entry
& 0x03)
128 dircache_size
+= 4 - ((long)next_entry
& 0x03);
129 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
132 next_entry
->name_len
= 0;
133 next_entry
->d_name
= NULL
;
134 next_entry
->up
= NULL
;
135 next_entry
->down
= NULL
;
136 next_entry
->next
= NULL
;
138 dircache_size
+= sizeof(struct dircache_entry
);
144 * Internal function to allocate a dircache_entry and set
145 * ->next entry pointers.
147 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
149 struct dircache_entry
*next_entry
;
151 if ( (next_entry
= allocate_entry()) == NULL
)
153 next_entry
->up
= ce
->up
;
154 ce
->next
= next_entry
;
160 * Internal function to allocate a dircache_entry and set
161 * ->down entry pointers.
163 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
165 struct dircache_entry
*next_entry
;
167 if ( (next_entry
= allocate_entry()) == NULL
)
170 ce
->down
= next_entry
;
176 * Returns true if there is an event waiting in the queue
177 * that requires the current operation to be aborted.
179 static bool check_event_queue(void)
181 struct queue_event ev
;
183 if(!queue_peek(&dircache_queue
, &ev
))
189 case SYS_USB_CONNECTED
:
199 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
200 /* scan and build static data (avoid redundancy on stack) */
203 #ifdef HAVE_MULTIVOLUME
207 struct fat_direntry
*direntry
;
210 static int sab_process_dir(unsigned long startcluster
, struct dircache_entry
*ce
)
212 /* normally, opendir expects a full fat_dir as parent but in our case,
213 * it's completely useless because we don't modify anything
214 * WARNING: this heavily relies on current FAT implementation ! */
216 /* those field are necessary to update the FAT entry in case of modification
217 here we don't touch anything so we put dummy values */
219 sab
.dir
->entrycount
= 0;
220 sab
.dir
->file
.firstcluster
= 0;
222 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
225 logf("fat_opendir failed: %d", rc
);
229 /* first pass : read dir */
230 struct dircache_entry
*first_ce
= ce
;
232 /* read through directory */
233 while((rc
= fat_getnext(sab
.dir
, sab
.direntry
)) >= 0 && sab
.direntry
->name
[0])
235 if(!strcmp(".", sab
.direntry
->name
) ||
236 !strcmp("..", sab
.direntry
->name
))
239 ce
->attribute
= sab
.direntry
->attr
;
240 ce
->name_len
= strlen(sab
.direntry
->name
) + 1;
241 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
242 ce
->startcluster
= sab
.direntry
->firstcluster
;
243 ce
->size
= sab
.direntry
->filesize
;
244 ce
->wrtdate
= sab
.direntry
->wrtdate
;
245 ce
->wrttime
= sab
.direntry
->wrttime
;
246 memcpy(ce
->d_name
, sab
.direntry
->name
, ce
->name_len
);
248 dircache_size
+= ce
->name_len
;
251 if(ce
->attribute
& FAT_ATTR_DIRECTORY
)
252 dircache_gen_down(ce
);
254 ce
= dircache_gen_next(ce
);
258 /* When simulator is used, it's only safe to yield here. */
261 /* Stop if we got an external signal. */
262 if(check_event_queue())
268 /* add "." and ".." */
271 ce
->attribute
= FAT_ATTR_DIRECTORY
;
272 ce
->startcluster
= startcluster
;
276 ce
= dircache_gen_next(ce
);
280 ce
->attribute
= FAT_ATTR_DIRECTORY
;
281 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
283 ce
->down
= first_ce
->up
;
285 /* second pass: recurse ! */
290 if(ce
->name_len
!= 0 && ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".") && strcmp(ce
->d_name
, ".."))
291 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
299 /* used during the generation */
300 static struct fat_dir sab_fat_dir
;
302 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
304 memset(ce
, 0, sizeof(struct dircache_entry
));
306 #ifdef HAVE_MULTIVOLUME
309 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
310 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
311 ce
->name_len
= VOL_ENUM_POS
+ 3;
312 dircache_size
+= ce
->name_len
;
313 ce
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
315 append_position
= dircache_gen_next(ce
);
316 ce
= dircache_gen_down(ce
);
320 struct fat_direntry direntry
; /* ditto */
321 #ifdef HAVE_MULTIVOLUME
324 sab
.dir
= &sab_fat_dir
;
325 sab
.direntry
= &direntry
;
327 return sab_process_dir(0, ce
);
329 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
330 static char sab_path
[MAX_PATH
];
332 static int sab_process_dir(struct dircache_entry
*ce
)
334 struct dirent_uncached
*entry
;
335 struct dircache_entry
*first_ce
= ce
;
336 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
339 logf("Failed to opendir_uncached(%s)", sab_path
);
343 while((entry
= readdir_uncached(dir
)))
345 if(!strcmp(".", entry
->d_name
) ||
346 !strcmp("..", entry
->d_name
))
349 ce
->attribute
= entry
->attribute
;
350 ce
->name_len
= strlen(entry
->d_name
) + 1;
351 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
352 ce
->size
= entry
->size
;
353 ce
->wrtdate
= entry
->wrtdate
;
354 ce
->wrttime
= entry
->wrttime
;
355 memcpy(ce
->d_name
, entry
->d_name
, ce
->name_len
);
357 dircache_size
+= ce
->name_len
;
360 if(entry
->attribute
& ATTR_DIRECTORY
)
362 dircache_gen_down(ce
);
365 closedir_uncached(dir
);
368 /* save current paths size */
369 int pathpos
= strlen(sab_path
);
371 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
372 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
374 int rc
= sab_process_dir(ce
->down
);
376 sab_path
[pathpos
] = '\0';
380 closedir_uncached(dir
);
385 ce
= dircache_gen_next(ce
);
389 /* When simulator is used, it's only safe to yield here. */
392 /* Stop if we got an external signal. */
393 if(check_event_queue())
399 /* add "." and ".." */
402 ce
->attribute
= ATTR_DIRECTORY
;
406 ce
= dircache_gen_next(ce
);
410 ce
->attribute
= ATTR_DIRECTORY
;
412 ce
->down
= first_ce
->up
;
414 closedir_uncached(dir
);
418 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
420 #ifdef HAVE_MULTIVOLUME
423 memset(ce
, 0, sizeof(struct dircache_entry
));
425 strlcpy(sab_path
, "/", sizeof sab_path
);
426 return sab_process_dir(ce
);
428 #endif /* PLATFORM_NATIVE */
431 * Internal function to get a pointer to dircache_entry for a given filename.
432 * path: Absolute path to a file or directory (see comment)
433 * go_down: Returns the first entry of the directory given by the path (see comment)
435 * As a a special case, accept path="" as an alias for "/".
436 * Also if the path omits the first '/', it will be accepted.
438 * * If get_down=true:
439 * If path="/", the returned entry is the first of root directory (ie dircache_root)
440 * Otherwise, if 'entry' is the returned value when get_down=false,
441 * the functions returns entry->down (which can be NULL)
443 * * If get_down=false:
444 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
445 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
447 * dircache_get_entry(path)->d_name == chunk_n
449 * If path="/", the returned entry is NULL.
450 * If the entry doesn't exist, return NULL
452 * NOTE: this functions silently handles double '/'
454 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
456 char namecopy
[MAX_PATH
];
461 struct dircache_entry
*cache_entry
= dircache_root
;
463 strlcpy(namecopy
, path
, sizeof(namecopy
));
465 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
467 /* If request another chunk, the current entry has to be directory
468 * and so cache_entry->down has to be non-NULL/
469 * Special case of root because it's already the first entry of the root directory
471 * NOTE: this is safe even if cache_entry->down is NULL */
473 cache_entry
= cache_entry
->down
;
477 /* scan dir for name */
478 while(cache_entry
!= NULL
)
480 /* skip unused entries */
481 if(cache_entry
->name_len
== 0)
483 cache_entry
= cache_entry
->next
;
487 if(!strcasecmp(part
, cache_entry
->d_name
))
489 /* go to next entry */
490 cache_entry
= cache_entry
->next
;
493 /* handle not found case */
494 if(cache_entry
== NULL
)
498 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
500 return at_root
? cache_entry
: cache_entry
->down
;
502 return at_root
? NULL
: cache_entry
;
505 #ifdef HAVE_EEPROM_SETTINGS
507 * Function to load the internal cache structure from disk to initialize
508 * the dircache really fast and little disk access.
510 int dircache_load(void)
512 struct dircache_maindata maindata
;
516 if (dircache_initialized
)
519 logf("Loading directory cache");
522 fd
= open_dircache_file(O_RDONLY
, 0);
526 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
527 if (bytes_read
!= sizeof(struct dircache_maindata
)
528 || maindata
.size
<= 0)
530 logf("Dircache file header error");
532 remove_dircache_file();
536 dircache_root
= buffer_alloc(0);
537 if ((long)maindata
.root_entry
!= (long)dircache_root
)
539 logf("Position missmatch");
541 remove_dircache_file();
545 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
546 entry_count
= maindata
.entry_count
;
547 appflags
= maindata
.appflags
;
548 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
550 remove_dircache_file();
552 if (bytes_read
!= maindata
.size
)
554 logf("Dircache read failed");
558 /* Cache successfully loaded. */
559 dircache_size
= maindata
.size
;
560 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
562 logf("Done, %ld KiB used", dircache_size
/ 1024);
563 dircache_initialized
= true;
564 memset(fd_bindings
, 0, sizeof(fd_bindings
));
570 * Function to save the internal cache stucture to disk for fast loading
573 int dircache_save(void)
575 struct dircache_maindata maindata
;
577 unsigned long bytes_written
;
579 remove_dircache_file();
581 if (!dircache_initialized
)
584 logf("Saving directory cache");
585 fd
= open_dircache_file(O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
587 maindata
.magic
= DIRCACHE_MAGIC
;
588 maindata
.size
= dircache_size
;
589 maindata
.root_entry
= dircache_root
;
590 maindata
.entry_count
= entry_count
;
591 maindata
.appflags
= appflags
;
593 /* Save the info structure */
594 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
595 if (bytes_written
!= sizeof(struct dircache_maindata
))
598 logf("dircache: write failed #1");
602 /* Dump whole directory cache to disk */
603 bytes_written
= write(fd
, dircache_root
, dircache_size
);
605 if (bytes_written
!= dircache_size
)
607 logf("dircache: write failed #2");
613 #endif /* HAVE_EEPROM_SETTINGS */
616 * Internal function which scans the disk and creates the dircache structure.
618 static int dircache_do_rebuild(void)
620 unsigned int start_tick
;
623 /* Measure how long it takes build the cache. */
624 start_tick
= current_tick
;
625 dircache_initializing
= true;
629 dircache_size
= sizeof(struct dircache_entry
);
631 #ifdef HAVE_MULTIVOLUME
632 append_position
= dircache_root
;
634 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
636 if (fat_ismounted(i
))
640 #ifdef HAVE_MULTIVOLUME
641 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
643 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
644 #endif /* HAVE_MULTIVOLUME */
646 logf("dircache_scan_and_build failed");
649 dircache_initializing
= false;
653 #ifdef HAVE_MULTIVOLUME
658 logf("Done, %ld KiB used", dircache_size
/ 1024);
660 dircache_initialized
= true;
661 dircache_initializing
= false;
662 cache_build_ticks
= current_tick
- start_tick
;
664 /* Initialized fd bindings. */
665 memset(fd_bindings
, 0, sizeof(fd_bindings
));
666 for (i
= 0; i
< fdbind_idx
; i
++)
667 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
672 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
673 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
677 /* We have to long align the audiobuf to keep the buffer access fast. */
678 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
679 audiobuf
+= DIRCACHE_RESERVE
;
680 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
688 * Internal thread that controls transparent cache building.
690 static void dircache_thread(void)
692 struct queue_event ev
;
696 queue_wait(&dircache_queue
, &ev
);
702 if (!dircache_initialized
)
704 dircache_initialized
= false;
707 thread_enabled
= true;
708 dircache_do_rebuild();
709 thread_enabled
= false;
713 logf("Stopped the rebuilding.");
714 dircache_initialized
= false;
717 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
718 case SYS_USB_CONNECTED
:
719 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
720 usb_wait_for_disconnect(&dircache_queue
);
728 * Start scanning the disk to build the dircache.
729 * Either transparent or non-transparent build method is used.
731 int dircache_build(int last_size
)
733 if (dircache_initialized
|| thread_enabled
)
736 logf("Building directory cache");
737 #ifdef HAVE_EEPROM_SETTINGS
738 remove_dircache_file();
741 /* Background build, dircache has been previously allocated */
742 if (dircache_size
> 0)
744 thread_enabled
= true;
745 dircache_initializing
= true;
746 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
750 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
752 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
753 dircache_root
= buffer_alloc(allocated_size
);
754 thread_enabled
= true;
756 /* Start a transparent rebuild. */
757 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
761 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
763 /* Start a non-transparent rebuild. */
764 return dircache_do_rebuild();
768 * Steal the allocated dircache buffer and disable dircache.
770 void* dircache_steal_buffer(long *size
)
773 if (dircache_size
== 0)
779 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
781 return dircache_root
;
785 * Main initialization function that must be called before any other
786 * operations within the dircache.
788 void dircache_init(void)
793 dircache_initialized
= false;
794 dircache_initializing
= false;
796 memset(opendirs
, 0, sizeof(opendirs
));
797 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
799 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
802 queue_init(&dircache_queue
, true);
803 thread_id
= create_thread(dircache_thread
, dircache_stack
,
804 sizeof(dircache_stack
), 0, dircache_thread_name
805 IF_PRIO(, PRIORITY_BACKGROUND
)
807 #ifdef HAVE_IO_PRIORITY
808 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
814 * Returns true if dircache has been initialized and is ready to be used.
816 bool dircache_is_enabled(void)
818 return dircache_initialized
;
822 * Returns true if dircache is being initialized.
824 bool dircache_is_initializing(void)
826 return dircache_initializing
|| thread_enabled
;
830 * Set application flags used to determine if dircache is still intact.
832 void dircache_set_appflag(long mask
)
838 * Get application flags used to determine if dircache is still intact.
840 bool dircache_get_appflag(long mask
)
842 return dircache_is_enabled() && (appflags
& mask
);
846 * Returns the current number of entries (directories and files) in the cache.
848 int dircache_get_entry_count(void)
854 * Returns the allocated space for dircache (without reserve space).
856 int dircache_get_cache_size(void)
858 return dircache_is_enabled() ? dircache_size
: 0;
862 * Returns how many bytes of the reserve allocation for live cache
863 * updates have been used.
865 int dircache_get_reserve_used(void)
867 return dircache_is_enabled() ? reserve_used
: 0;
871 * Returns the time in kernel ticks that took to build the cache.
873 int dircache_get_build_ticks(void)
875 return dircache_is_enabled() ? cache_build_ticks
: 0;
879 * Disables the dircache. Usually called on shutdown or when
880 * accepting a usb connection.
882 void dircache_disable(void)
888 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
890 while (thread_enabled
)
892 dircache_initialized
= false;
894 logf("Waiting for cached dirs to release");
896 cache_in_use
= false;
897 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
898 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
905 } while (cache_in_use
) ;
907 logf("Cache released");
912 * Usermode function to return dircache_entry pointer to the given path.
914 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
916 if (!dircache_initialized
|| filename
== NULL
)
919 return dircache_get_entry(filename
, false);
923 * Function to copy the full absolute path from dircache to the given buffer
924 * using the given dircache_entry pointer.
926 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
930 const struct dircache_entry
*temp
= entry
;
935 /* first compute the necessary size */
938 path_size
+= temp
->name_len
; /* '/' + d_name */
942 /* now copy the path */
943 /* doesn't matter with trailing 0 because it's added later */
947 idx
-= entry
->name_len
;
949 int rem
= size
- idx
;
954 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
960 buf
[MIN(path_size
, size
-1)] = 0;
963 /* --- Directory cache live updating functions --- */
964 static int block_until_ready(void)
966 /* Block until dircache has been built. */
967 while (!dircache_initialized
&& dircache_is_initializing())
970 if (!dircache_initialized
)
976 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
978 struct dircache_entry
*entry
;
979 char basedir
[MAX_PATH
*2];
981 long last_cache_size
= dircache_size
;
983 strlcpy(basedir
, path
, sizeof(basedir
));
984 new = strrchr(basedir
, '/');
987 logf("error occurred");
988 dircache_initialized
= false;
995 entry
= dircache_get_entry(basedir
, true);
998 logf("basedir not found!");
1000 dircache_initialized
= false;
1004 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
1005 >= DIRCACHE_RESERVE
)
1007 logf("not enough space");
1008 dircache_initialized
= false;
1012 while (entry
->next
!= NULL
)
1013 entry
= entry
->next
;
1015 if (entry
->name_len
> 0)
1016 entry
= dircache_gen_next(entry
);
1020 dircache_initialized
= false;
1024 entry
->attribute
= attribute
;
1025 entry
->name_len
= MIN(254, strlen(new)) + 1;
1026 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
1027 entry
->startcluster
= 0;
1031 memcpy(entry
->d_name
, new, entry
->name_len
);
1032 dircache_size
+= entry
->name_len
;
1034 if (attribute
& ATTR_DIRECTORY
)
1037 dircache_gen_down(entry
);
1040 reserve_used
+= dircache_size
- last_cache_size
;
1045 void dircache_bind(int fd
, const char *path
)
1047 struct dircache_entry
*entry
;
1049 /* Queue requests until dircache has been built. */
1050 if (!dircache_initialized
&& dircache_is_initializing())
1052 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1054 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1055 sizeof(fdbind_cache
[fdbind_idx
].path
));
1056 fdbind_cache
[fdbind_idx
].fd
= fd
;
1061 if (!dircache_initialized
)
1064 logf("bind: %d/%s", fd
, path
);
1065 entry
= dircache_get_entry(path
, false);
1069 dircache_initialized
= false;
1073 fd_bindings
[fd
] = entry
;
1076 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1078 if (!dircache_initialized
|| fd
< 0)
1081 if (fd_bindings
[fd
] == NULL
)
1083 logf("dircache fd(%d) access error", fd
);
1084 dircache_initialized
= false;
1088 fd_bindings
[fd
]->size
= newsize
;
1089 fd_bindings
[fd
]->startcluster
= startcluster
;
1091 void dircache_update_filetime(int fd
)
1097 struct tm
*now
= get_time();
1098 if (!dircache_initialized
|| fd
< 0)
1101 if (fd_bindings
[fd
] == NULL
)
1103 logf("dircache fd access error");
1104 dircache_initialized
= false;
1107 year
= now
->tm_year
+1900-1980;
1108 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1109 (((now
->tm_mon
+1)&0xf)<<5) |
1110 (((now
->tm_mday
)&0x1f));
1111 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1112 (((now
->tm_min
)&0x3f)<<5) |
1113 (((now
->tm_sec
/2)&0x1f));
1117 void dircache_mkdir(const char *path
)
1119 if (block_until_ready())
1123 logf("mkdir: %s", path
);
1124 dircache_new_entry(path
, ATTR_DIRECTORY
);
1127 void dircache_rmdir(const char *path
)
1129 struct dircache_entry
*entry
;
1131 if (block_until_ready())
1134 logf("rmdir: %s", path
);
1135 entry
= dircache_get_entry(path
, false);
1136 if (entry
== NULL
|| entry
->down
== NULL
)
1138 logf("not found or not a directory!");
1139 dircache_initialized
= false;
1144 entry
->name_len
= 0;
1147 /* Remove a file from cache */
1148 void dircache_remove(const char *name
)
1150 struct dircache_entry
*entry
;
1152 if (block_until_ready())
1155 logf("remove: %s", name
);
1157 entry
= dircache_get_entry(name
, false);
1162 dircache_initialized
= false;
1166 entry
->name_len
= 0;
1169 void dircache_rename(const char *oldpath
, const char *newpath
)
1171 struct dircache_entry
*entry
, *newentry
;
1172 struct dircache_entry oldentry
;
1173 char absolute_path
[MAX_PATH
*2];
1176 if (block_until_ready())
1179 logf("rename: %s->%s", oldpath
, newpath
);
1181 entry
= dircache_get_entry(oldpath
, false);
1185 dircache_initialized
= false;
1189 /* Delete the old entry. */
1190 entry
->name_len
= 0;
1192 /** If we rename the same filename twice in a row, we need to
1193 * save the data, because the entry will be re-used. */
1196 /* Generate the absolute path for destination if necessary. */
1197 if (newpath
[0] != '/')
1199 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1200 p
= strrchr(absolute_path
, '/');
1203 logf("Invalid path");
1204 dircache_initialized
= false;
1209 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1210 newpath
= absolute_path
;
1213 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1214 if (newentry
== NULL
)
1216 dircache_initialized
= false;
1220 newentry
->down
= oldentry
.down
;
1221 newentry
->size
= oldentry
.size
;
1222 newentry
->startcluster
= oldentry
.startcluster
;
1223 newentry
->wrttime
= oldentry
.wrttime
;
1224 newentry
->wrtdate
= oldentry
.wrtdate
;
1227 void dircache_add_file(const char *path
, long startcluster
)
1229 struct dircache_entry
*entry
;
1231 if (block_until_ready())
1234 logf("add file: %s", path
);
1235 entry
= dircache_new_entry(path
, 0);
1239 entry
->startcluster
= startcluster
;
1242 static bool is_disable_msg_pending(void)
1244 return check_event_queue();
1247 DIR_CACHED
* opendir_cached(const char* name
)
1250 DIR_CACHED
* pdir
= opendirs
;
1252 if ( name
[0] != '/' )
1254 DEBUGF("Only absolute paths supported right now\n");
1258 /* find a free dir descriptor */
1259 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1263 if ( dd
== MAX_OPEN_DIRS
)
1265 DEBUGF("Too many dirs open\n");
1272 if (!dircache_initialized
|| is_disable_msg_pending())
1274 pdir
->internal_entry
= NULL
;
1275 pdir
->regulardir
= opendir_uncached(name
);
1279 pdir
->regulardir
= NULL
;
1280 pdir
->internal_entry
= dircache_get_entry(name
, true);
1281 pdir
->theent
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1284 if (pdir
->internal_entry
== NULL
&& pdir
->regulardir
== NULL
)
1293 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1295 struct dircache_entry
*ce
= dir
->internal_entry
;
1296 struct dirent_uncached
*regentry
;
1301 if (dir
->regulardir
!= NULL
)
1303 regentry
= readdir_uncached(dir
->regulardir
);
1304 if (regentry
== NULL
)
1307 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1308 dir
->theent
.size
= regentry
->size
;
1309 dir
->theent
.startcluster
= regentry
->startcluster
;
1310 dir
->theent
.attribute
= regentry
->attribute
;
1311 dir
->theent
.wrttime
= regentry
->wrttime
;
1312 dir
->theent
.wrtdate
= regentry
->wrtdate
;
1314 return &dir
->theent
;
1317 /* if theent.attribute=-1 then this is the first call */
1318 /* otherwise, this is is not so we first take the entry's ->next */
1319 /* NOTE: normal file can't have attribute=-1 */
1320 if(dir
->theent
.attribute
!= -1)
1322 /* skip unused entries */
1323 while(ce
!= NULL
&& ce
->name_len
== 0)
1329 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1330 /* Can't do `dir->theent = *ce`
1331 because that modifies the d_name pointer. */
1332 dir
->theent
.size
= ce
->size
;
1333 dir
->theent
.startcluster
= ce
->startcluster
;
1334 dir
->theent
.attribute
= ce
->attribute
;
1335 dir
->theent
.wrttime
= ce
->wrttime
;
1336 dir
->theent
.wrtdate
= ce
->wrtdate
;
1337 dir
->internal_entry
= ce
;
1339 //logf("-> %s", ce->name);
1340 return &dir
->theent
;
1343 int closedir_cached(DIR_CACHED
* dir
)
1349 if (dir
->regulardir
!= NULL
)
1350 return closedir_uncached(dir
->regulardir
);
1355 int mkdir_cached(const char *name
)
1357 int rc
=mkdir_uncached(name
);
1359 dircache_mkdir(name
);
1363 int rmdir_cached(const char* name
)
1365 int rc
=rmdir_uncached(name
);
1367 dircache_rmdir(name
);