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.
45 #include "timefuncs.h"
49 #define DIRCACHE_BUILD 1
50 #define DIRCACHE_STOP 2
52 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
53 #define MAX_OPEN_DIRS 12
55 #define MAX_OPEN_DIRS 8
57 static DIR_CACHED opendirs
[MAX_OPEN_DIRS
];
59 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
60 static struct dircache_entry
*dircache_root
;
61 #ifdef HAVE_MULTIVOLUME
62 static struct dircache_entry
*append_position
;
65 static bool dircache_initialized
= false;
66 static bool dircache_initializing
= false;
67 static bool thread_enabled
= false;
68 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
69 static unsigned long dircache_size
= 0;
70 static unsigned long entry_count
= 0;
71 static unsigned long reserve_used
= 0;
72 static unsigned int cache_build_ticks
= 0;
73 static unsigned long appflags
= 0;
75 static struct event_queue dircache_queue
;
76 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x200)/sizeof(long)];
77 static const char dircache_thread_name
[] = "dircache";
79 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
80 static int fdbind_idx
= 0;
82 /* --- Internal cache structure control functions --- */
85 * Internal function to allocate a new dircache_entry from memory.
87 static struct dircache_entry
* allocate_entry(void)
89 struct dircache_entry
*next_entry
;
91 if (dircache_size
> allocated_size
- MAX_PATH
*2)
93 logf("size limit reached");
97 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
98 #ifdef ROCKBOX_STRICT_ALIGN
99 /* Make sure the entry is long aligned. */
100 if ((long)next_entry
& 0x03)
102 dircache_size
+= 4 - ((long)next_entry
& 0x03);
103 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
106 next_entry
->name_len
= 0;
107 next_entry
->d_name
= NULL
;
108 next_entry
->up
= NULL
;
109 next_entry
->down
= NULL
;
110 next_entry
->next
= NULL
;
112 dircache_size
+= sizeof(struct dircache_entry
);
118 * Internal function to allocate a dircache_entry and set
119 * ->next entry pointers.
121 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
123 struct dircache_entry
*next_entry
;
125 if ( (next_entry
= allocate_entry()) == NULL
)
127 next_entry
->up
= ce
->up
;
128 ce
->next
= next_entry
;
134 * Internal function to allocate a dircache_entry and set
135 * ->down entry pointers.
137 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
139 struct dircache_entry
*next_entry
;
141 if ( (next_entry
= allocate_entry()) == NULL
)
144 ce
->down
= next_entry
;
150 * Returns true if there is an event waiting in the queue
151 * that requires the current operation to be aborted.
153 static bool check_event_queue(void)
155 struct queue_event ev
;
157 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
161 case SYS_USB_CONNECTED
:
165 /* Put the event back into the queue. */
166 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
174 /* scan and build static data (avoid redundancy on stack) */
177 #ifdef HAVE_MULTIVOLUME
181 struct fat_direntry
*direntry
;
184 static int sab_process_dir(unsigned long startcluster
, struct dircache_entry
*ce
)
186 /* normally, opendir expects a full fat_dir as parent but in our case,
187 * it's completely useless because we don't modify anything
188 * WARNING: this heavily relies on current FAT implementation ! */
190 /* those field are necessary to update the FAT entry in case of modification
191 here we don't touch anything so we put dummy values */
193 sab
.dir
->entrycount
= 0;
194 sab
.dir
->file
.firstcluster
= 0;
196 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
199 logf("fat_opendir failed: %d", rc
);
203 /* first pass : read dir */
204 struct dircache_entry
*first_ce
= ce
;
206 /* read through directory */
207 while((rc
= fat_getnext(sab
.dir
, sab
.direntry
)) >= 0 && sab
.direntry
->name
[0])
209 if(!strcmp(".", sab
.direntry
->name
) ||
210 !strcmp("..", sab
.direntry
->name
))
213 ce
->attribute
= sab
.direntry
->attr
;
214 ce
->name_len
= strlen(sab
.direntry
->name
) + 1;
215 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
216 ce
->startcluster
= sab
.direntry
->firstcluster
;
217 ce
->size
= sab
.direntry
->filesize
;
218 ce
->wrtdate
= sab
.direntry
->wrtdate
;
219 ce
->wrttime
= sab
.direntry
->wrttime
;
220 memcpy(ce
->d_name
, sab
.direntry
->name
, ce
->name_len
);
222 dircache_size
+= ce
->name_len
;
225 if(ce
->attribute
& FAT_ATTR_DIRECTORY
)
226 dircache_gen_down(ce
);
228 ce
= dircache_gen_next(ce
);
232 /* When simulator is used, it's only safe to yield here. */
235 /* Stop if we got an external signal. */
236 if(check_event_queue())
242 /* add "." and ".." */
245 ce
->attribute
= FAT_ATTR_DIRECTORY
;
246 ce
->startcluster
= startcluster
;
250 ce
= dircache_gen_next(ce
);
254 ce
->attribute
= FAT_ATTR_DIRECTORY
;
255 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
257 ce
->down
= first_ce
->up
;
259 /* second pass: recurse ! */
264 if(ce
->name_len
!= 0 && ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".") && strcmp(ce
->d_name
, ".."))
265 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
273 /* used during the generation */
274 static struct fat_dir sab_fat_dir
;
276 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
278 memset(ce
, 0, sizeof(struct dircache_entry
));
280 #ifdef HAVE_MULTIVOLUME
283 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
284 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
285 ce
->name_len
= VOL_ENUM_POS
+ 3;
286 dircache_size
+= ce
->name_len
;
287 ce
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
289 append_position
= dircache_gen_next(ce
);
290 ce
= dircache_gen_down(ce
);
294 struct fat_direntry direntry
; /* ditto */
295 #ifdef HAVE_MULTIVOLUME
298 sab
.dir
= &sab_fat_dir
;
299 sab
.direntry
= &direntry
;
301 return sab_process_dir(0, ce
);
303 #else /* !SIMULATOR */
304 static char sab_path
[MAX_PATH
];
306 static int sab_process_dir(struct dircache_entry
*ce
)
308 struct dirent_uncached
*entry
;
309 struct dircache_entry
*first_ce
= ce
;
310 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
313 logf("Failed to opendir_uncached(%s)", sab_path
);
317 while((entry
= readdir_uncached(dir
)))
319 if(!strcmp(".", entry
->d_name
) ||
320 !strcmp("..", entry
->d_name
))
323 ce
->attribute
= entry
->attribute
;
324 ce
->name_len
= strlen(entry
->d_name
) + 1;
325 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
326 ce
->size
= entry
->size
;
327 ce
->wrtdate
= entry
->wrtdate
;
328 ce
->wrttime
= entry
->wrttime
;
329 memcpy(ce
->d_name
, entry
->d_name
, ce
->name_len
);
331 dircache_size
+= ce
->name_len
;
334 if(entry
->attribute
& ATTR_DIRECTORY
)
336 dircache_gen_down(ce
);
339 closedir_uncached(dir
);
342 /* save current paths size */
343 int pathpos
= strlen(sab_path
);
345 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
346 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
348 int rc
= sab_process_dir(ce
->down
);
350 sab_path
[pathpos
] = '\0';
354 closedir_uncached(dir
);
359 ce
= dircache_gen_next(ce
);
363 /* When simulator is used, it's only safe to yield here. */
366 /* Stop if we got an external signal. */
367 if(check_event_queue())
373 /* add "." and ".." */
376 ce
->attribute
= ATTR_DIRECTORY
;
380 ce
= dircache_gen_next(ce
);
384 ce
->attribute
= ATTR_DIRECTORY
;
386 ce
->down
= first_ce
->up
;
388 closedir_uncached(dir
);
392 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
394 #ifdef HAVE_MULTIVOLUME
397 memset(ce
, 0, sizeof(struct dircache_entry
));
399 strlcpy(sab_path
, "/", sizeof sab_path
);
400 return sab_process_dir(ce
);
402 #endif /* SIMULATOR */
405 * Internal function to get a pointer to dircache_entry for a given filename.
406 * path: Absolute path to a file or directory (see comment)
407 * go_down: Returns the first entry of the directory given by the path (see comment)
409 * As a a special case, accept path="" as an alias for "/".
410 * Also if the path omits the first '/', it will be accepted.
412 * * If get_down=true:
413 * If path="/", the returned entry is the first of root directory (ie dircache_root)
414 * Otherwise, if 'entry' is the returned value when get_down=false,
415 * the functions returns entry->down (which can be NULL)
417 * * If get_down=false:
418 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
419 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
421 * dircache_get_entry(path)->d_name == chunk_n
423 * If path="/", the returned entry is NULL.
424 * If the entry doesn't exist, return NULL
426 * NOTE: this functions silently handles double '/'
428 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
430 char namecopy
[MAX_PATH
];
435 struct dircache_entry
*cache_entry
= dircache_root
;
437 strlcpy(namecopy
, path
, sizeof(namecopy
));
439 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
441 /* If request another chunk, the current entry has to be directory
442 * and so cache_entry->down has to be non-NULL/
443 * Special case of root because it's already the first entry of the root directory
445 * NOTE: this is safe even if cache_entry->down is NULL */
447 cache_entry
= cache_entry
->down
;
451 /* scan dir for name */
452 while(cache_entry
!= NULL
)
454 /* skip unused entries */
455 if(cache_entry
->name_len
== 0)
457 cache_entry
= cache_entry
->next
;
461 if(!strcasecmp(part
, cache_entry
->d_name
))
463 /* go to next entry */
464 cache_entry
= cache_entry
->next
;
467 /* handle not found case */
468 if(cache_entry
== NULL
)
472 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
474 return at_root
? cache_entry
: cache_entry
->down
;
476 return at_root
? NULL
: cache_entry
;
479 #ifdef HAVE_EEPROM_SETTINGS
481 * Function to load the internal cache structure from disk to initialize
482 * the dircache really fast and little disk access.
484 int dircache_load(void)
486 struct dircache_maindata maindata
;
490 if (dircache_initialized
)
493 logf("Loading directory cache");
496 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
500 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
501 if (bytes_read
!= sizeof(struct dircache_maindata
)
502 || maindata
.size
<= 0)
504 logf("Dircache file header error");
506 remove(DIRCACHE_FILE
);
510 dircache_root
= buffer_alloc(0);
511 if ((long)maindata
.root_entry
!= (long)dircache_root
)
513 logf("Position missmatch");
515 remove(DIRCACHE_FILE
);
519 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
520 entry_count
= maindata
.entry_count
;
521 appflags
= maindata
.appflags
;
522 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
524 remove(DIRCACHE_FILE
);
526 if (bytes_read
!= maindata
.size
)
528 logf("Dircache read failed");
532 /* Cache successfully loaded. */
533 dircache_size
= maindata
.size
;
534 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
536 logf("Done, %ld KiB used", dircache_size
/ 1024);
537 dircache_initialized
= true;
538 memset(fd_bindings
, 0, sizeof(fd_bindings
));
544 * Function to save the internal cache stucture to disk for fast loading
547 int dircache_save(void)
549 struct dircache_maindata maindata
;
551 unsigned long bytes_written
;
553 remove(DIRCACHE_FILE
);
555 if (!dircache_initialized
)
558 logf("Saving directory cache");
559 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
561 maindata
.magic
= DIRCACHE_MAGIC
;
562 maindata
.size
= dircache_size
;
563 maindata
.root_entry
= dircache_root
;
564 maindata
.entry_count
= entry_count
;
565 maindata
.appflags
= appflags
;
567 /* Save the info structure */
568 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
569 if (bytes_written
!= sizeof(struct dircache_maindata
))
572 logf("dircache: write failed #1");
576 /* Dump whole directory cache to disk */
577 bytes_written
= write(fd
, dircache_root
, dircache_size
);
579 if (bytes_written
!= dircache_size
)
581 logf("dircache: write failed #2");
590 * Internal function which scans the disk and creates the dircache structure.
592 static int dircache_do_rebuild(void)
594 unsigned int start_tick
;
597 /* Measure how long it takes build the cache. */
598 start_tick
= current_tick
;
599 dircache_initializing
= true;
603 dircache_size
= sizeof(struct dircache_entry
);
605 #ifdef HAVE_MULTIVOLUME
606 append_position
= dircache_root
;
608 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
610 if (fat_ismounted(i
))
614 #ifdef HAVE_MULTIVOLUME
615 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
617 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
618 #endif /* HAVE_MULTIVOLUME */
620 logf("dircache_scan_and_build failed");
623 dircache_initializing
= false;
627 #ifdef HAVE_MULTIVOLUME
632 logf("Done, %ld KiB used", dircache_size
/ 1024);
634 dircache_initialized
= true;
635 dircache_initializing
= false;
636 cache_build_ticks
= current_tick
- start_tick
;
638 /* Initialized fd bindings. */
639 memset(fd_bindings
, 0, sizeof(fd_bindings
));
640 for (i
= 0; i
< fdbind_idx
; i
++)
641 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
646 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
647 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
651 /* We have to long align the audiobuf to keep the buffer access fast. */
652 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
653 audiobuf
+= DIRCACHE_RESERVE
;
654 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
662 * Internal thread that controls transparent cache building.
664 static void dircache_thread(void)
666 struct queue_event ev
;
670 queue_wait(&dircache_queue
, &ev
);
676 if (!dircache_initialized
)
678 dircache_initialized
= false;
681 thread_enabled
= true;
682 dircache_do_rebuild();
683 thread_enabled
= false;
687 logf("Stopped the rebuilding.");
688 dircache_initialized
= false;
692 case SYS_USB_CONNECTED
:
693 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
694 usb_wait_for_disconnect(&dircache_queue
);
702 * Start scanning the disk to build the dircache.
703 * Either transparent or non-transparent build method is used.
705 int dircache_build(int last_size
)
707 if (dircache_initialized
|| thread_enabled
)
710 logf("Building directory cache");
711 remove(DIRCACHE_FILE
);
713 /* Background build, dircache has been previously allocated */
714 if (dircache_size
> 0)
716 thread_enabled
= true;
717 dircache_initializing
= true;
718 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
722 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
724 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
725 dircache_root
= buffer_alloc(allocated_size
);
726 thread_enabled
= true;
728 /* Start a transparent rebuild. */
729 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
733 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
735 /* Start a non-transparent rebuild. */
736 return dircache_do_rebuild();
740 * Steal the allocated dircache buffer and disable dircache.
742 void* dircache_steal_buffer(long *size
)
745 if (dircache_size
== 0)
751 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
753 return dircache_root
;
757 * Main initialization function that must be called before any other
758 * operations within the dircache.
760 void dircache_init(void)
764 dircache_initialized
= false;
765 dircache_initializing
= false;
767 memset(opendirs
, 0, sizeof(opendirs
));
768 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
770 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
773 queue_init(&dircache_queue
, true);
774 create_thread(dircache_thread
, dircache_stack
,
775 sizeof(dircache_stack
), 0, dircache_thread_name
776 IF_PRIO(, PRIORITY_BACKGROUND
)
781 * Returns true if dircache has been initialized and is ready to be used.
783 bool dircache_is_enabled(void)
785 return dircache_initialized
;
789 * Returns true if dircache is being initialized.
791 bool dircache_is_initializing(void)
793 return dircache_initializing
|| thread_enabled
;
797 * Set application flags used to determine if dircache is still intact.
799 void dircache_set_appflag(long mask
)
805 * Get application flags used to determine if dircache is still intact.
807 bool dircache_get_appflag(long mask
)
809 return dircache_is_enabled() && (appflags
& mask
);
813 * Returns the current number of entries (directories and files) in the cache.
815 int dircache_get_entry_count(void)
821 * Returns the allocated space for dircache (without reserve space).
823 int dircache_get_cache_size(void)
825 return dircache_is_enabled() ? dircache_size
: 0;
829 * Returns how many bytes of the reserve allocation for live cache
830 * updates have been used.
832 int dircache_get_reserve_used(void)
834 return dircache_is_enabled() ? reserve_used
: 0;
838 * Returns the time in kernel ticks that took to build the cache.
840 int dircache_get_build_ticks(void)
842 return dircache_is_enabled() ? cache_build_ticks
: 0;
846 * Disables the dircache. Usually called on shutdown or when
847 * accepting a usb connection.
849 void dircache_disable(void)
855 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
857 while (thread_enabled
)
859 dircache_initialized
= false;
861 logf("Waiting for cached dirs to release");
863 cache_in_use
= false;
864 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
865 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
872 } while (cache_in_use
) ;
874 logf("Cache released");
879 * Usermode function to return dircache_entry pointer to the given path.
881 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
883 if (!dircache_initialized
|| filename
== NULL
)
886 return dircache_get_entry(filename
, false);
890 * Function to copy the full absolute path from dircache to the given buffer
891 * using the given dircache_entry pointer.
893 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
897 const struct dircache_entry
*temp
= entry
;
902 /* first compute the necessary size */
905 path_size
+= temp
->name_len
; /* '/' + d_name */
909 /* now copy the path */
910 /* doesn't matter with trailing 0 because it's added later */
914 idx
-= entry
->name_len
;
916 int rem
= size
- idx
;
921 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
927 buf
[MIN(path_size
, size
-1)] = 0;
930 /* --- Directory cache live updating functions --- */
931 static int block_until_ready(void)
933 /* Block until dircache has been built. */
934 while (!dircache_initialized
&& dircache_is_initializing())
937 if (!dircache_initialized
)
943 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
945 struct dircache_entry
*entry
;
946 char basedir
[MAX_PATH
*2];
948 long last_cache_size
= dircache_size
;
950 strlcpy(basedir
, path
, sizeof(basedir
));
951 new = strrchr(basedir
, '/');
954 logf("error occurred");
955 dircache_initialized
= false;
962 entry
= dircache_get_entry(basedir
, true);
965 logf("basedir not found!");
967 dircache_initialized
= false;
971 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
974 logf("not enough space");
975 dircache_initialized
= false;
979 while (entry
->next
!= NULL
)
982 if (entry
->name_len
> 0)
983 entry
= dircache_gen_next(entry
);
987 dircache_initialized
= false;
991 entry
->attribute
= attribute
;
992 entry
->name_len
= MIN(254, strlen(new)) + 1;
993 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
994 entry
->startcluster
= 0;
998 memcpy(entry
->d_name
, new, entry
->name_len
);
999 dircache_size
+= entry
->name_len
;
1001 if (attribute
& ATTR_DIRECTORY
)
1004 dircache_gen_down(entry
);
1007 reserve_used
+= dircache_size
- last_cache_size
;
1012 void dircache_bind(int fd
, const char *path
)
1014 struct dircache_entry
*entry
;
1016 /* Queue requests until dircache has been built. */
1017 if (!dircache_initialized
&& dircache_is_initializing())
1019 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1021 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1022 sizeof(fdbind_cache
[fdbind_idx
].path
));
1023 fdbind_cache
[fdbind_idx
].fd
= fd
;
1028 if (!dircache_initialized
)
1031 logf("bind: %d/%s", fd
, path
);
1032 entry
= dircache_get_entry(path
, false);
1036 dircache_initialized
= false;
1040 fd_bindings
[fd
] = entry
;
1043 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1045 if (!dircache_initialized
|| fd
< 0)
1048 if (fd_bindings
[fd
] == NULL
)
1050 logf("dircache fd(%d) access error", fd
);
1051 dircache_initialized
= false;
1055 fd_bindings
[fd
]->size
= newsize
;
1056 fd_bindings
[fd
]->startcluster
= startcluster
;
1058 void dircache_update_filetime(int fd
)
1064 struct tm
*now
= get_time();
1065 if (!dircache_initialized
|| fd
< 0)
1068 if (fd_bindings
[fd
] == NULL
)
1070 logf("dircache fd access error");
1071 dircache_initialized
= false;
1074 year
= now
->tm_year
+1900-1980;
1075 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1076 (((now
->tm_mon
+1)&0xf)<<5) |
1077 (((now
->tm_mday
)&0x1f));
1078 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1079 (((now
->tm_min
)&0x3f)<<5) |
1080 (((now
->tm_sec
/2)&0x1f));
1084 void dircache_mkdir(const char *path
)
1086 if (block_until_ready())
1089 logf("mkdir: %s", path
);
1090 dircache_new_entry(path
, ATTR_DIRECTORY
);
1093 void dircache_rmdir(const char *path
)
1095 struct dircache_entry
*entry
;
1097 if (block_until_ready())
1100 logf("rmdir: %s", path
);
1101 entry
= dircache_get_entry(path
, false);
1102 if (entry
== NULL
|| entry
->down
== NULL
)
1104 logf("not found or not a directory!");
1105 dircache_initialized
= false;
1110 entry
->name_len
= 0;
1113 /* Remove a file from cache */
1114 void dircache_remove(const char *name
)
1116 struct dircache_entry
*entry
;
1118 if (block_until_ready())
1121 logf("remove: %s", name
);
1123 entry
= dircache_get_entry(name
, false);
1128 dircache_initialized
= false;
1132 entry
->name_len
= 0;
1135 void dircache_rename(const char *oldpath
, const char *newpath
)
1137 struct dircache_entry
*entry
, *newentry
;
1138 struct dircache_entry oldentry
;
1139 char absolute_path
[MAX_PATH
*2];
1142 if (block_until_ready())
1145 logf("rename: %s->%s", oldpath
, newpath
);
1147 entry
= dircache_get_entry(oldpath
, false);
1151 dircache_initialized
= false;
1155 /* Delete the old entry. */
1156 entry
->name_len
= 0;
1158 /** If we rename the same filename twice in a row, we need to
1159 * save the data, because the entry will be re-used. */
1162 /* Generate the absolute path for destination if necessary. */
1163 if (newpath
[0] != '/')
1165 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1166 p
= strrchr(absolute_path
, '/');
1169 logf("Invalid path");
1170 dircache_initialized
= false;
1175 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1176 newpath
= absolute_path
;
1179 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1180 if (newentry
== NULL
)
1182 dircache_initialized
= false;
1186 newentry
->down
= oldentry
.down
;
1187 newentry
->size
= oldentry
.size
;
1188 newentry
->startcluster
= oldentry
.startcluster
;
1189 newentry
->wrttime
= oldentry
.wrttime
;
1190 newentry
->wrtdate
= oldentry
.wrtdate
;
1193 void dircache_add_file(const char *path
, long startcluster
)
1195 struct dircache_entry
*entry
;
1197 if (block_until_ready())
1200 logf("add file: %s", path
);
1201 entry
= dircache_new_entry(path
, 0);
1205 entry
->startcluster
= startcluster
;
1208 DIR_CACHED
* opendir_cached(const char* name
)
1211 DIR_CACHED
* pdir
= opendirs
;
1213 if ( name
[0] != '/' )
1215 DEBUGF("Only absolute paths supported right now\n");
1219 /* find a free dir descriptor */
1220 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1224 if ( dd
== MAX_OPEN_DIRS
)
1226 DEBUGF("Too many dirs open\n");
1233 if (!dircache_initialized
)
1235 pdir
->internal_entry
= NULL
;
1236 pdir
->regulardir
= opendir_uncached(name
);
1240 pdir
->regulardir
= NULL
;
1241 pdir
->internal_entry
= dircache_get_entry(name
, true);
1242 pdir
->theent
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1245 if (pdir
->internal_entry
== NULL
&& pdir
->regulardir
== NULL
)
1254 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1256 struct dircache_entry
*ce
= dir
->internal_entry
;
1257 struct dirent_uncached
*regentry
;
1262 if (dir
->regulardir
!= NULL
)
1264 regentry
= readdir_uncached(dir
->regulardir
);
1265 if (regentry
== NULL
)
1268 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1269 dir
->theent
.size
= regentry
->size
;
1270 dir
->theent
.startcluster
= regentry
->startcluster
;
1271 dir
->theent
.attribute
= regentry
->attribute
;
1272 dir
->theent
.wrttime
= regentry
->wrttime
;
1273 dir
->theent
.wrtdate
= regentry
->wrtdate
;
1275 return &dir
->theent
;
1278 /* if theent.attribute=-1 then this is the first call */
1279 /* otherwise, this is is not so we first take the entry's ->next */
1280 /* NOTE: normal file can't have attribute=-1 */
1281 if(dir
->theent
.attribute
!= -1)
1283 /* skip unused entries */
1284 while(ce
!= NULL
&& ce
->name_len
== 0)
1290 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1291 /* Can't do `dir->theent = *ce`
1292 because that modifies the d_name pointer. */
1293 dir
->theent
.size
= ce
->size
;
1294 dir
->theent
.startcluster
= ce
->startcluster
;
1295 dir
->theent
.attribute
= ce
->attribute
;
1296 dir
->theent
.wrttime
= ce
->wrttime
;
1297 dir
->theent
.wrtdate
= ce
->wrtdate
;
1298 dir
->internal_entry
= ce
;
1300 //logf("-> %s", ce->name);
1301 return &dir
->theent
;
1304 int closedir_cached(DIR_CACHED
* dir
)
1310 if (dir
->regulardir
!= NULL
)
1311 return closedir_uncached(dir
->regulardir
);
1316 int mkdir_cached(const char *name
)
1318 int rc
=mkdir_uncached(name
);
1320 dircache_mkdir(name
);
1324 int rmdir_cached(const char* name
)
1326 int rc
=rmdir_uncached(name
);
1328 dircache_rmdir(name
);