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
->name_len
= 0;
129 next_entry
->d_name
= NULL
;
130 next_entry
->up
= NULL
;
131 next_entry
->down
= NULL
;
132 next_entry
->next
= NULL
;
134 dircache_size
+= sizeof(struct dircache_entry
);
140 * Internal function to allocate a dircache_entry and set
141 * ->next entry pointers.
143 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
145 struct dircache_entry
*next_entry
;
147 if ( (next_entry
= allocate_entry()) == NULL
)
149 next_entry
->up
= ce
->up
;
150 ce
->next
= next_entry
;
156 * Internal function to allocate a dircache_entry and set
157 * ->down entry pointers.
159 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
161 struct dircache_entry
*next_entry
;
163 if ( (next_entry
= allocate_entry()) == NULL
)
166 ce
->down
= next_entry
;
172 * Returns true if there is an event waiting in the queue
173 * that requires the current operation to be aborted.
175 static bool check_event_queue(void)
177 struct queue_event ev
;
179 if(!queue_peek(&dircache_queue
, &ev
))
185 case SYS_USB_CONNECTED
:
195 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
196 /* scan and build static data (avoid redundancy on stack) */
199 #ifdef HAVE_MULTIVOLUME
203 struct fat_direntry
*direntry
;
206 static int sab_process_dir(unsigned long startcluster
, struct dircache_entry
*ce
)
208 /* normally, opendir expects a full fat_dir as parent but in our case,
209 * it's completely useless because we don't modify anything
210 * WARNING: this heavily relies on current FAT implementation ! */
212 /* those field are necessary to update the FAT entry in case of modification
213 here we don't touch anything so we put dummy values */
215 sab
.dir
->entrycount
= 0;
216 sab
.dir
->file
.firstcluster
= 0;
218 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
221 logf("fat_opendir failed: %d", rc
);
225 /* first pass : read dir */
226 struct dircache_entry
*first_ce
= ce
;
228 /* read through directory */
229 while((rc
= fat_getnext(sab
.dir
, sab
.direntry
)) >= 0 && sab
.direntry
->name
[0])
231 if(!strcmp(".", sab
.direntry
->name
) ||
232 !strcmp("..", sab
.direntry
->name
))
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
->info
.size
= sab
.direntry
->filesize
;
239 ce
->info
.attribute
= sab
.direntry
->attr
;
240 ce
->info
.wrtdate
= sab
.direntry
->wrtdate
;
241 ce
->info
.wrttime
= sab
.direntry
->wrttime
;
242 memcpy(ce
->d_name
, sab
.direntry
->name
, ce
->name_len
);
244 dircache_size
+= ce
->name_len
;
247 if(ce
->info
.attribute
& FAT_ATTR_DIRECTORY
)
248 dircache_gen_down(ce
);
250 ce
= dircache_gen_next(ce
);
254 /* When simulator is used, it's only safe to yield here. */
257 /* Stop if we got an external signal. */
258 if(check_event_queue())
264 /* add "." and ".." */
267 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
268 ce
->startcluster
= startcluster
;
272 ce
= dircache_gen_next(ce
);
276 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
;
277 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
279 ce
->down
= first_ce
->up
;
281 /* second pass: recurse ! */
286 if(ce
->name_len
!= 0 && ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".") && strcmp(ce
->d_name
, ".."))
287 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
295 /* used during the generation */
296 static struct fat_dir sab_fat_dir
;
298 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
300 memset(ce
, 0, sizeof(struct dircache_entry
));
302 #ifdef HAVE_MULTIVOLUME
305 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
306 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
307 ce
->name_len
= VOL_ENUM_POS
+ 3;
308 dircache_size
+= ce
->name_len
;
309 ce
->info
.attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
311 append_position
= dircache_gen_next(ce
);
312 ce
= dircache_gen_down(ce
);
316 struct fat_direntry direntry
; /* ditto */
317 #ifdef HAVE_MULTIVOLUME
320 sab
.dir
= &sab_fat_dir
;
321 sab
.direntry
= &direntry
;
323 return sab_process_dir(0, ce
);
325 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
326 static char sab_path
[MAX_PATH
];
328 static int sab_process_dir(struct dircache_entry
*ce
)
330 struct dirent_uncached
*entry
;
331 struct dircache_entry
*first_ce
= ce
;
332 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
335 logf("Failed to opendir_uncached(%s)", sab_path
);
339 while((entry
= readdir_uncached(dir
)))
341 if(!strcmp(".", entry
->d_name
) ||
342 !strcmp("..", entry
->d_name
))
345 ce
->name_len
= strlen(entry
->d_name
) + 1;
346 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
347 ce
->info
= entry
->info
;
348 memcpy(ce
->d_name
, entry
->d_name
, ce
->name_len
);
350 dircache_size
+= ce
->name_len
;
353 if(entry
->info
.attribute
& ATTR_DIRECTORY
)
355 dircache_gen_down(ce
);
358 closedir_uncached(dir
);
361 /* save current paths size */
362 int pathpos
= strlen(sab_path
);
364 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
365 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
367 int rc
= sab_process_dir(ce
->down
);
369 sab_path
[pathpos
] = '\0';
373 closedir_uncached(dir
);
378 ce
= dircache_gen_next(ce
);
382 /* When simulator is used, it's only safe to yield here. */
385 /* Stop if we got an external signal. */
386 if(check_event_queue())
392 /* add "." and ".." */
395 ce
->info
.attribute
= ATTR_DIRECTORY
;
399 ce
= dircache_gen_next(ce
);
403 ce
->info
.attribute
= ATTR_DIRECTORY
;
405 ce
->down
= first_ce
->up
;
407 closedir_uncached(dir
);
411 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
413 #ifdef HAVE_MULTIVOLUME
416 memset(ce
, 0, sizeof(struct dircache_entry
));
418 strlcpy(sab_path
, "/", sizeof sab_path
);
419 return sab_process_dir(ce
);
421 #endif /* PLATFORM_NATIVE */
424 * Internal function to get a pointer to dircache_entry for a given filename.
425 * path: Absolute path to a file or directory (see comment)
426 * go_down: Returns the first entry of the directory given by the path (see comment)
428 * As a a special case, accept path="" as an alias for "/".
429 * Also if the path omits the first '/', it will be accepted.
431 * * If get_down=true:
432 * If path="/", the returned entry is the first of root directory (ie dircache_root)
433 * Otherwise, if 'entry' is the returned value when get_down=false,
434 * the functions returns entry->down (which can be NULL)
436 * * If get_down=false:
437 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
438 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
440 * dircache_get_entry(path)->d_name == chunk_n
442 * If path="/", the returned entry is NULL.
443 * If the entry doesn't exist, return NULL
445 * NOTE: this functions silently handles double '/'
447 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
449 char namecopy
[MAX_PATH
];
454 struct dircache_entry
*cache_entry
= dircache_root
;
456 strlcpy(namecopy
, path
, sizeof(namecopy
));
458 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
460 /* If request another chunk, the current entry has to be directory
461 * and so cache_entry->down has to be non-NULL/
462 * Special case of root because it's already the first entry of the root directory
464 * NOTE: this is safe even if cache_entry->down is NULL */
466 cache_entry
= cache_entry
->down
;
470 /* scan dir for name */
471 while(cache_entry
!= NULL
)
473 /* skip unused entries */
474 if(cache_entry
->name_len
== 0)
476 cache_entry
= cache_entry
->next
;
480 if(!strcasecmp(part
, cache_entry
->d_name
))
482 /* go to next entry */
483 cache_entry
= cache_entry
->next
;
486 /* handle not found case */
487 if(cache_entry
== NULL
)
491 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
493 return at_root
? cache_entry
: cache_entry
->down
;
495 return at_root
? NULL
: cache_entry
;
498 #ifdef HAVE_EEPROM_SETTINGS
500 * Function to load the internal cache structure from disk to initialize
501 * the dircache really fast and little disk access.
503 int dircache_load(void)
505 struct dircache_maindata maindata
;
509 if (dircache_initialized
)
512 logf("Loading directory cache");
515 fd
= open_dircache_file(O_RDONLY
, 0);
519 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
520 if (bytes_read
!= sizeof(struct dircache_maindata
)
521 || maindata
.size
<= 0)
523 logf("Dircache file header error");
525 remove_dircache_file();
529 dircache_root
= buffer_alloc(0);
530 if ((long)maindata
.root_entry
!= (long)dircache_root
)
532 logf("Position missmatch");
534 remove_dircache_file();
538 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
539 entry_count
= maindata
.entry_count
;
540 appflags
= maindata
.appflags
;
541 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
543 remove_dircache_file();
545 if (bytes_read
!= maindata
.size
)
547 logf("Dircache read failed");
551 /* Cache successfully loaded. */
552 dircache_size
= maindata
.size
;
553 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
555 logf("Done, %ld KiB used", dircache_size
/ 1024);
556 dircache_initialized
= true;
557 memset(fd_bindings
, 0, sizeof(fd_bindings
));
563 * Function to save the internal cache stucture to disk for fast loading
566 int dircache_save(void)
568 struct dircache_maindata maindata
;
570 unsigned long bytes_written
;
572 remove_dircache_file();
574 if (!dircache_initialized
)
577 logf("Saving directory cache");
578 fd
= open_dircache_file(O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
580 maindata
.magic
= DIRCACHE_MAGIC
;
581 maindata
.size
= dircache_size
;
582 maindata
.root_entry
= dircache_root
;
583 maindata
.entry_count
= entry_count
;
584 maindata
.appflags
= appflags
;
586 /* Save the info structure */
587 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
588 if (bytes_written
!= sizeof(struct dircache_maindata
))
591 logf("dircache: write failed #1");
595 /* Dump whole directory cache to disk */
596 bytes_written
= write(fd
, dircache_root
, dircache_size
);
598 if (bytes_written
!= dircache_size
)
600 logf("dircache: write failed #2");
606 #endif /* HAVE_EEPROM_SETTINGS */
609 * Internal function which scans the disk and creates the dircache structure.
611 static int dircache_do_rebuild(void)
613 unsigned int start_tick
;
616 /* Measure how long it takes build the cache. */
617 start_tick
= current_tick
;
618 dircache_initializing
= true;
622 dircache_size
= sizeof(struct dircache_entry
);
624 #ifdef HAVE_MULTIVOLUME
625 append_position
= dircache_root
;
627 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
629 if (fat_ismounted(i
))
633 #ifdef HAVE_MULTIVOLUME
634 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
636 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
637 #endif /* HAVE_MULTIVOLUME */
639 logf("dircache_scan_and_build failed");
642 dircache_initializing
= false;
646 #ifdef HAVE_MULTIVOLUME
651 logf("Done, %ld KiB used", dircache_size
/ 1024);
653 dircache_initialized
= true;
654 dircache_initializing
= false;
655 cache_build_ticks
= current_tick
- start_tick
;
657 /* Initialized fd bindings. */
658 memset(fd_bindings
, 0, sizeof(fd_bindings
));
659 for (i
= 0; i
< fdbind_idx
; i
++)
660 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
665 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
666 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
670 /* We have to long align the audiobuf to keep the buffer access fast. */
671 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
672 audiobuf
+= DIRCACHE_RESERVE
;
673 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
681 * Internal thread that controls transparent cache building.
683 static void dircache_thread(void)
685 struct queue_event ev
;
689 queue_wait(&dircache_queue
, &ev
);
695 if (!dircache_initialized
)
697 dircache_initialized
= false;
700 thread_enabled
= true;
701 dircache_do_rebuild();
702 thread_enabled
= false;
706 logf("Stopped the rebuilding.");
707 dircache_initialized
= false;
710 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
711 case SYS_USB_CONNECTED
:
712 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
713 usb_wait_for_disconnect(&dircache_queue
);
721 * Start scanning the disk to build the dircache.
722 * Either transparent or non-transparent build method is used.
724 int dircache_build(int last_size
)
726 if (dircache_initialized
|| thread_enabled
)
729 logf("Building directory cache");
730 #ifdef HAVE_EEPROM_SETTINGS
731 remove_dircache_file();
734 /* Background build, dircache has been previously allocated */
735 if (dircache_size
> 0)
737 thread_enabled
= true;
738 dircache_initializing
= true;
739 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
743 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
745 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
746 dircache_root
= buffer_alloc(allocated_size
);
747 thread_enabled
= true;
749 /* Start a transparent rebuild. */
750 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
754 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
756 /* Start a non-transparent rebuild. */
757 return dircache_do_rebuild();
761 * Steal the allocated dircache buffer and disable dircache.
763 void* dircache_steal_buffer(long *size
)
766 if (dircache_size
== 0)
772 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
774 return dircache_root
;
778 * Main initialization function that must be called before any other
779 * operations within the dircache.
781 void dircache_init(void)
786 dircache_initialized
= false;
787 dircache_initializing
= false;
789 memset(opendirs
, 0, sizeof(opendirs
));
790 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
792 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
795 queue_init(&dircache_queue
, true);
796 thread_id
= create_thread(dircache_thread
, dircache_stack
,
797 sizeof(dircache_stack
), 0, dircache_thread_name
798 IF_PRIO(, PRIORITY_BACKGROUND
)
800 #ifdef HAVE_IO_PRIORITY
801 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
807 * Returns true if dircache has been initialized and is ready to be used.
809 bool dircache_is_enabled(void)
811 return dircache_initialized
;
815 * Returns true if dircache is being initialized.
817 bool dircache_is_initializing(void)
819 return dircache_initializing
|| thread_enabled
;
823 * Set application flags used to determine if dircache is still intact.
825 void dircache_set_appflag(long mask
)
831 * Get application flags used to determine if dircache is still intact.
833 bool dircache_get_appflag(long mask
)
835 return dircache_is_enabled() && (appflags
& mask
);
839 * Returns the current number of entries (directories and files) in the cache.
841 int dircache_get_entry_count(void)
847 * Returns the allocated space for dircache (without reserve space).
849 int dircache_get_cache_size(void)
851 return dircache_is_enabled() ? dircache_size
: 0;
855 * Returns how many bytes of the reserve allocation for live cache
856 * updates have been used.
858 int dircache_get_reserve_used(void)
860 return dircache_is_enabled() ? reserve_used
: 0;
864 * Returns the time in kernel ticks that took to build the cache.
866 int dircache_get_build_ticks(void)
868 return dircache_is_enabled() ? cache_build_ticks
: 0;
872 * Disables the dircache. Usually called on shutdown or when
873 * accepting a usb connection.
875 void dircache_disable(void)
881 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
883 while (thread_enabled
)
885 dircache_initialized
= false;
887 logf("Waiting for cached dirs to release");
889 cache_in_use
= false;
890 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
891 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
898 } while (cache_in_use
) ;
900 logf("Cache released");
905 * Usermode function to return dircache_entry pointer to the given path.
907 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
909 if (!dircache_initialized
|| filename
== NULL
)
912 return dircache_get_entry(filename
, false);
916 * Function to copy the full absolute path from dircache to the given buffer
917 * using the given dircache_entry pointer.
919 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
923 const struct dircache_entry
*temp
= entry
;
928 /* first compute the necessary size */
931 path_size
+= temp
->name_len
; /* '/' + d_name */
935 /* now copy the path */
936 /* doesn't matter with trailing 0 because it's added later */
940 idx
-= entry
->name_len
;
942 int rem
= size
- idx
;
947 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
953 buf
[MIN(path_size
, size
-1)] = 0;
956 /* --- Directory cache live updating functions --- */
957 static int block_until_ready(void)
959 /* Block until dircache has been built. */
960 while (!dircache_initialized
&& dircache_is_initializing())
963 if (!dircache_initialized
)
969 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
971 struct dircache_entry
*entry
;
972 char basedir
[MAX_PATH
*2];
974 long last_cache_size
= dircache_size
;
976 strlcpy(basedir
, path
, sizeof(basedir
));
977 new = strrchr(basedir
, '/');
980 logf("error occurred");
981 dircache_initialized
= false;
988 entry
= dircache_get_entry(basedir
, true);
991 logf("basedir not found!");
993 dircache_initialized
= false;
997 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
1000 logf("not enough space");
1001 dircache_initialized
= false;
1005 while (entry
->next
!= NULL
)
1006 entry
= entry
->next
;
1008 if (entry
->name_len
> 0)
1009 entry
= dircache_gen_next(entry
);
1013 dircache_initialized
= false;
1017 entry
->name_len
= MIN(254, strlen(new)) + 1;
1018 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
1019 entry
->startcluster
= 0;
1020 memset(&entry
->info
, 0, sizeof(entry
->info
));
1021 entry
->info
.attribute
= attribute
;
1022 memcpy(entry
->d_name
, new, entry
->name_len
);
1023 dircache_size
+= entry
->name_len
;
1025 if (attribute
& ATTR_DIRECTORY
)
1028 dircache_gen_down(entry
);
1031 reserve_used
+= dircache_size
- last_cache_size
;
1036 void dircache_bind(int fd
, const char *path
)
1038 struct dircache_entry
*entry
;
1040 /* Queue requests until dircache has been built. */
1041 if (!dircache_initialized
&& dircache_is_initializing())
1043 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1045 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1046 sizeof(fdbind_cache
[fdbind_idx
].path
));
1047 fdbind_cache
[fdbind_idx
].fd
= fd
;
1052 if (!dircache_initialized
)
1055 logf("bind: %d/%s", fd
, path
);
1056 entry
= dircache_get_entry(path
, false);
1060 dircache_initialized
= false;
1064 fd_bindings
[fd
] = entry
;
1067 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1069 if (!dircache_initialized
|| fd
< 0)
1072 if (fd_bindings
[fd
] == NULL
)
1074 logf("dircache fd(%d) access error", fd
);
1075 dircache_initialized
= false;
1079 fd_bindings
[fd
]->info
.size
= newsize
;
1080 fd_bindings
[fd
]->startcluster
= startcluster
;
1082 void dircache_update_filetime(int fd
)
1088 struct tm
*now
= get_time();
1089 if (!dircache_initialized
|| fd
< 0)
1092 if (fd_bindings
[fd
] == NULL
)
1094 logf("dircache fd access error");
1095 dircache_initialized
= false;
1098 year
= now
->tm_year
+1900-1980;
1099 fd_bindings
[fd
]->info
.wrtdate
= (((year
)&0x7f)<<9) |
1100 (((now
->tm_mon
+1)&0xf)<<5) |
1101 (((now
->tm_mday
)&0x1f));
1102 fd_bindings
[fd
]->info
.wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1103 (((now
->tm_min
)&0x3f)<<5) |
1104 (((now
->tm_sec
/2)&0x1f));
1108 void dircache_mkdir(const char *path
)
1110 if (block_until_ready())
1114 logf("mkdir: %s", path
);
1115 dircache_new_entry(path
, ATTR_DIRECTORY
);
1118 void dircache_rmdir(const char *path
)
1120 struct dircache_entry
*entry
;
1122 if (block_until_ready())
1125 logf("rmdir: %s", path
);
1126 entry
= dircache_get_entry(path
, false);
1127 if (entry
== NULL
|| entry
->down
== NULL
)
1129 logf("not found or not a directory!");
1130 dircache_initialized
= false;
1135 entry
->name_len
= 0;
1138 /* Remove a file from cache */
1139 void dircache_remove(const char *name
)
1141 struct dircache_entry
*entry
;
1143 if (block_until_ready())
1146 logf("remove: %s", name
);
1148 entry
= dircache_get_entry(name
, false);
1153 dircache_initialized
= false;
1157 entry
->name_len
= 0;
1160 void dircache_rename(const char *oldpath
, const char *newpath
)
1162 struct dircache_entry
*entry
, *newentry
;
1163 struct dircache_entry oldentry
;
1164 char absolute_path
[MAX_PATH
*2];
1167 if (block_until_ready())
1170 logf("rename: %s->%s", oldpath
, newpath
);
1172 entry
= dircache_get_entry(oldpath
, false);
1176 dircache_initialized
= false;
1180 /* Delete the old entry. */
1181 entry
->name_len
= 0;
1183 /** If we rename the same filename twice in a row, we need to
1184 * save the data, because the entry will be re-used. */
1187 /* Generate the absolute path for destination if necessary. */
1188 if (newpath
[0] != '/')
1190 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1191 p
= strrchr(absolute_path
, '/');
1194 logf("Invalid path");
1195 dircache_initialized
= false;
1200 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1201 newpath
= absolute_path
;
1204 newentry
= dircache_new_entry(newpath
, entry
->info
.attribute
);
1205 if (newentry
== NULL
)
1207 dircache_initialized
= false;
1211 newentry
->down
= oldentry
.down
;
1212 newentry
->startcluster
= oldentry
.startcluster
;
1213 newentry
->info
.size
= oldentry
.info
.size
;
1214 newentry
->info
.wrtdate
= oldentry
.info
.wrtdate
;
1215 newentry
->info
.wrttime
= oldentry
.info
.wrttime
;
1218 void dircache_add_file(const char *path
, long startcluster
)
1220 struct dircache_entry
*entry
;
1222 if (block_until_ready())
1225 logf("add file: %s", path
);
1226 entry
= dircache_new_entry(path
, 0);
1230 entry
->startcluster
= startcluster
;
1233 static bool is_disable_msg_pending(void)
1235 return check_event_queue();
1238 DIR_CACHED
* opendir_cached(const char* name
)
1241 DIR_CACHED
* pdir
= opendirs
;
1243 if ( name
[0] != '/' )
1245 DEBUGF("Only absolute paths supported right now\n");
1249 /* find a free dir descriptor */
1250 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1254 if ( dd
== MAX_OPEN_DIRS
)
1256 DEBUGF("Too many dirs open\n");
1263 if (!dircache_initialized
|| is_disable_msg_pending())
1265 pdir
->internal_entry
= NULL
;
1266 pdir
->regulardir
= opendir_uncached(name
);
1270 pdir
->regulardir
= NULL
;
1271 pdir
->internal_entry
= dircache_get_entry(name
, true);
1272 pdir
->theent
.info
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1275 if (pdir
->internal_entry
== NULL
&& pdir
->regulardir
== NULL
)
1284 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1286 struct dircache_entry
*ce
= dir
->internal_entry
;
1287 struct dirent_uncached
*regentry
;
1292 if (dir
->regulardir
!= NULL
)
1294 regentry
= readdir_uncached(dir
->regulardir
);
1295 if (regentry
== NULL
)
1298 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1299 dir
->theent
.startcluster
= regentry
->startcluster
;
1300 dir
->theent
.info
= regentry
->info
;
1302 return &dir
->theent
;
1305 /* if theent.attribute=-1 then this is the first call */
1306 /* otherwise, this is is not so we first take the entry's ->next */
1307 /* NOTE: normal file can't have attribute=-1 */
1308 if(dir
->theent
.info
.attribute
!= -1)
1310 /* skip unused entries */
1311 while(ce
!= NULL
&& ce
->name_len
== 0)
1317 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1318 /* Can't do `dir->theent = *ce`
1319 because that modifies the d_name pointer. */
1320 dir
->theent
.startcluster
= ce
->startcluster
;
1321 dir
->theent
.info
= ce
->info
;
1322 dir
->internal_entry
= ce
;
1324 //logf("-> %s", ce->name);
1325 return &dir
->theent
;
1328 int closedir_cached(DIR_CACHED
* dir
)
1334 if (dir
->regulardir
!= NULL
)
1335 return closedir_uncached(dir
->regulardir
);
1340 int mkdir_cached(const char *name
)
1342 int rc
=mkdir_uncached(name
);
1344 dircache_mkdir(name
);
1348 int rmdir_cached(const char* name
)
1350 int rc
=rmdir_uncached(name
);
1352 dircache_rmdir(name
);