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 --- */
86 * Internal function to allocate a new dircache_entry from memory.
88 static struct dircache_entry
* allocate_entry(void)
90 struct dircache_entry
*next_entry
;
92 if (dircache_size
> allocated_size
- MAX_PATH
*2)
94 logf("size limit reached");
98 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
99 #ifdef ROCKBOX_STRICT_ALIGN
100 /* Make sure the entry is long aligned. */
101 if ((long)next_entry
& 0x03)
103 dircache_size
+= 4 - ((long)next_entry
& 0x03);
104 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
107 next_entry
->name_len
= 0;
108 next_entry
->d_name
= NULL
;
109 next_entry
->up
= NULL
;
110 next_entry
->down
= NULL
;
111 next_entry
->next
= NULL
;
113 dircache_size
+= sizeof(struct dircache_entry
);
119 * Internal function to allocate a dircache_entry and set
120 * ->next entry pointers.
122 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
124 struct dircache_entry
*next_entry
;
126 if ( (next_entry
= allocate_entry()) == NULL
)
128 next_entry
->up
= ce
->up
;
129 ce
->next
= next_entry
;
135 * Internal function to allocate a dircache_entry and set
136 * ->down entry pointers.
138 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
140 struct dircache_entry
*next_entry
;
142 if ( (next_entry
= allocate_entry()) == NULL
)
145 ce
->down
= next_entry
;
151 * Returns true if there is an event waiting in the queue
152 * that requires the current operation to be aborted.
154 static bool check_event_queue(void)
156 struct queue_event ev
;
158 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
162 case SYS_USB_CONNECTED
:
166 /* Put the event back into the queue. */
167 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
175 /* scan and build static data (avoid redundancy on stack) */
178 #ifdef HAVE_MULTIVOLUME
182 struct fat_direntry
*direntry
;
185 static int sab_process_dir(unsigned long startcluster
, struct dircache_entry
*ce
)
187 /* normally, opendir expects a full fat_dir as parent but in our case,
188 * it's completely useless because we don't modify anything
189 * WARNING: this heavily relies on current FAT implementation ! */
191 /* those field are necessary to update the FAT entry in case of modification
192 here we don't touch anything so we put dummy values */
194 sab
.dir
->entrycount
= 0;
195 sab
.dir
->file
.firstcluster
= 0;
197 int rc
= fat_opendir(IF_MV2(sab
.volume
,) sab
.dir
, startcluster
, sab
.dir
);
200 logf("fat_opendir failed: %d", rc
);
204 /* first pass : read dir */
205 struct dircache_entry
*first_ce
= ce
;
207 /* read through directory */
208 while((rc
= fat_getnext(sab
.dir
, sab
.direntry
)) >= 0 && sab
.direntry
->name
[0])
210 if(!strcmp(".", sab
.direntry
->name
) ||
211 !strcmp("..", sab
.direntry
->name
))
214 ce
->attribute
= sab
.direntry
->attr
;
215 ce
->name_len
= strlen(sab
.direntry
->name
) + 1;
216 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
217 ce
->startcluster
= sab
.direntry
->firstcluster
;
218 ce
->size
= sab
.direntry
->filesize
;
219 ce
->wrtdate
= sab
.direntry
->wrtdate
;
220 ce
->wrttime
= sab
.direntry
->wrttime
;
221 memcpy(ce
->d_name
, sab
.direntry
->name
, ce
->name_len
);
223 dircache_size
+= ce
->name_len
;
226 if(ce
->attribute
& FAT_ATTR_DIRECTORY
)
227 dircache_gen_down(ce
);
229 ce
= dircache_gen_next(ce
);
233 /* When simulator is used, it's only safe to yield here. */
236 /* Stop if we got an external signal. */
237 if(check_event_queue())
243 /* add "." and ".." */
246 ce
->attribute
= FAT_ATTR_DIRECTORY
;
247 ce
->startcluster
= startcluster
;
251 ce
= dircache_gen_next(ce
);
255 ce
->attribute
= FAT_ATTR_DIRECTORY
;
256 ce
->startcluster
= (first_ce
->up
? first_ce
->up
->startcluster
: 0);
258 ce
->down
= first_ce
->up
;
260 /* second pass: recurse ! */
265 if(ce
->name_len
!= 0 && ce
->down
!= NULL
&& strcmp(ce
->d_name
, ".") && strcmp(ce
->d_name
, ".."))
266 rc
= sab_process_dir(ce
->startcluster
, ce
->down
);
274 /* used during the generation */
275 static struct fat_dir sab_fat_dir
;
277 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
279 memset(ce
, 0, sizeof(struct dircache_entry
));
281 #ifdef HAVE_MULTIVOLUME
284 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
285 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
286 ce
->name_len
= VOL_ENUM_POS
+ 3;
287 dircache_size
+= ce
->name_len
;
288 ce
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
290 append_position
= dircache_gen_next(ce
);
291 ce
= dircache_gen_down(ce
);
295 struct fat_direntry direntry
; /* ditto */
296 #ifdef HAVE_MULTIVOLUME
299 sab
.dir
= &sab_fat_dir
;
300 sab
.direntry
= &direntry
;
302 return sab_process_dir(0, ce
);
304 #else /* !SIMULATOR */
305 static char sab_path
[MAX_PATH
];
307 static int sab_process_dir(struct dircache_entry
*ce
)
309 struct dirent_uncached
*entry
;
310 struct dircache_entry
*first_ce
= ce
;
311 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
314 logf("Failed to opendir_uncached(%s)", sab_path
);
318 while((entry
= readdir_uncached(dir
)))
320 if(!strcmp(".", entry
->d_name
) ||
321 !strcmp("..", entry
->d_name
))
324 ce
->attribute
= entry
->attribute
;
325 ce
->name_len
= strlen(entry
->d_name
) + 1;
326 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
327 ce
->size
= entry
->size
;
328 ce
->wrtdate
= entry
->wrtdate
;
329 ce
->wrttime
= entry
->wrttime
;
330 memcpy(ce
->d_name
, entry
->d_name
, ce
->name_len
);
332 dircache_size
+= ce
->name_len
;
335 if(entry
->attribute
& ATTR_DIRECTORY
)
337 dircache_gen_down(ce
);
340 closedir_uncached(dir
);
343 /* save current paths size */
344 int pathpos
= strlen(sab_path
);
346 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
347 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
349 int rc
= sab_process_dir(ce
->down
);
351 sab_path
[pathpos
] = '\0';
355 closedir_uncached(dir
);
360 ce
= dircache_gen_next(ce
);
364 /* When simulator is used, it's only safe to yield here. */
367 /* Stop if we got an external signal. */
368 if(check_event_queue())
374 /* add "." and ".." */
377 ce
->attribute
= ATTR_DIRECTORY
;
381 ce
= dircache_gen_next(ce
);
385 ce
->attribute
= ATTR_DIRECTORY
;
387 ce
->down
= first_ce
->up
;
389 closedir_uncached(dir
);
393 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
395 #ifdef HAVE_MULTIVOLUME
398 memset(ce
, 0, sizeof(struct dircache_entry
));
400 strlcpy(sab_path
, "/", sizeof sab_path
);
401 return sab_process_dir(ce
);
403 #endif /* SIMULATOR */
406 * Internal function to get a pointer to dircache_entry for a given filename.
407 * path: Absolute path to a file or directory (see comment)
408 * go_down: Returns the first entry of the directory given by the path (see comment)
410 * As a a special case, accept path="" as an alias for "/".
411 * Also if the path omits the first '/', it will be accepted.
413 * * If get_down=true:
414 * If path="/", the returned entry is the first of root directory (ie dircache_root)
415 * Otherwise, if 'entry' is the returned value when get_down=false,
416 * the functions returns entry->down (which can be NULL)
418 * * If get_down=false:
419 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
420 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
422 * dircache_get_entry(path)->d_name == chunk_n
424 * If path="/", the returned entry is NULL.
425 * If the entry doesn't exist, return NULL
427 * NOTE: this functions silently handles double '/'
429 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
431 char namecopy
[MAX_PATH
];
436 struct dircache_entry
*cache_entry
= dircache_root
;
438 strlcpy(namecopy
, path
, sizeof(namecopy
));
440 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
442 /* If request another chunk, the current entry has to be directory
443 * and so cache_entry->down has to be non-NULL/
444 * Special case of root because it's already the first entry of the root directory
446 * NOTE: this is safe even if cache_entry->down is NULL */
448 cache_entry
= cache_entry
->down
;
452 /* scan dir for name */
453 while(cache_entry
!= NULL
)
455 /* skip unused entries */
456 if(cache_entry
->name_len
== 0)
458 cache_entry
= cache_entry
->next
;
462 if(!strcasecmp(part
, cache_entry
->d_name
))
464 /* go to next entry */
465 cache_entry
= cache_entry
->next
;
468 /* handle not found case */
469 if(cache_entry
== NULL
)
473 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
475 return at_root
? cache_entry
: cache_entry
->down
;
477 return at_root
? NULL
: cache_entry
;
480 #ifdef HAVE_EEPROM_SETTINGS
482 * Function to load the internal cache structure from disk to initialize
483 * the dircache really fast and little disk access.
485 int dircache_load(void)
487 struct dircache_maindata maindata
;
491 if (dircache_initialized
)
494 logf("Loading directory cache");
497 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
501 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
502 if (bytes_read
!= sizeof(struct dircache_maindata
)
503 || maindata
.size
<= 0)
505 logf("Dircache file header error");
507 remove(DIRCACHE_FILE
);
511 dircache_root
= buffer_alloc(0);
512 if ((long)maindata
.root_entry
!= (long)dircache_root
)
514 logf("Position missmatch");
516 remove(DIRCACHE_FILE
);
520 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
521 entry_count
= maindata
.entry_count
;
522 appflags
= maindata
.appflags
;
523 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
525 remove(DIRCACHE_FILE
);
527 if (bytes_read
!= maindata
.size
)
529 logf("Dircache read failed");
533 /* Cache successfully loaded. */
534 dircache_size
= maindata
.size
;
535 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
537 logf("Done, %ld KiB used", dircache_size
/ 1024);
538 dircache_initialized
= true;
539 memset(fd_bindings
, 0, sizeof(fd_bindings
));
545 * Function to save the internal cache stucture to disk for fast loading
548 int dircache_save(void)
550 struct dircache_maindata maindata
;
552 unsigned long bytes_written
;
554 remove(DIRCACHE_FILE
);
556 if (!dircache_initialized
)
559 logf("Saving directory cache");
560 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
562 maindata
.magic
= DIRCACHE_MAGIC
;
563 maindata
.size
= dircache_size
;
564 maindata
.root_entry
= dircache_root
;
565 maindata
.entry_count
= entry_count
;
566 maindata
.appflags
= appflags
;
568 /* Save the info structure */
569 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
570 if (bytes_written
!= sizeof(struct dircache_maindata
))
573 logf("dircache: write failed #1");
577 /* Dump whole directory cache to disk */
578 bytes_written
= write(fd
, dircache_root
, dircache_size
);
580 if (bytes_written
!= dircache_size
)
582 logf("dircache: write failed #2");
591 * Internal function which scans the disk and creates the dircache structure.
593 static int dircache_do_rebuild(void)
595 unsigned int start_tick
;
598 /* Measure how long it takes build the cache. */
599 start_tick
= current_tick
;
600 dircache_initializing
= true;
604 dircache_size
= sizeof(struct dircache_entry
);
606 #ifdef HAVE_MULTIVOLUME
607 append_position
= dircache_root
;
609 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
611 if (fat_ismounted(i
))
615 #ifdef HAVE_MULTIVOLUME
616 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
618 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
619 #endif /* HAVE_MULTIVOLUME */
621 logf("dircache_scan_and_build failed");
624 dircache_initializing
= false;
628 #ifdef HAVE_MULTIVOLUME
633 logf("Done, %ld KiB used", dircache_size
/ 1024);
635 dircache_initialized
= true;
636 dircache_initializing
= false;
637 cache_build_ticks
= current_tick
- start_tick
;
639 /* Initialized fd bindings. */
640 memset(fd_bindings
, 0, sizeof(fd_bindings
));
641 for (i
= 0; i
< fdbind_idx
; i
++)
642 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
647 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
648 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
652 /* We have to long align the audiobuf to keep the buffer access fast. */
653 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
654 audiobuf
+= DIRCACHE_RESERVE
;
655 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
663 * Internal thread that controls transparent cache building.
665 static void dircache_thread(void)
667 struct queue_event ev
;
671 queue_wait(&dircache_queue
, &ev
);
677 if (!dircache_initialized
)
679 dircache_initialized
= false;
682 thread_enabled
= true;
683 dircache_do_rebuild();
684 thread_enabled
= false;
688 logf("Stopped the rebuilding.");
689 dircache_initialized
= false;
693 case SYS_USB_CONNECTED
:
694 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
695 usb_wait_for_disconnect(&dircache_queue
);
703 * Start scanning the disk to build the dircache.
704 * Either transparent or non-transparent build method is used.
706 int dircache_build(int last_size
)
708 if (dircache_initialized
|| thread_enabled
)
711 logf("Building directory cache");
712 remove(DIRCACHE_FILE
);
714 /* Background build, dircache has been previously allocated */
715 if (dircache_size
> 0)
717 thread_enabled
= true;
718 dircache_initializing
= true;
719 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
723 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
725 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
726 dircache_root
= buffer_alloc(allocated_size
);
727 thread_enabled
= true;
729 /* Start a transparent rebuild. */
730 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
734 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
736 /* Start a non-transparent rebuild. */
737 return dircache_do_rebuild();
741 * Steal the allocated dircache buffer and disable dircache.
743 void* dircache_steal_buffer(long *size
)
746 if (dircache_size
== 0)
752 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
754 return dircache_root
;
758 * Main initialization function that must be called before any other
759 * operations within the dircache.
761 void dircache_init(void)
766 dircache_initialized
= false;
767 dircache_initializing
= false;
769 memset(opendirs
, 0, sizeof(opendirs
));
770 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
772 opendirs
[i
].theent
.d_name
= buffer_alloc(MAX_PATH
);
775 queue_init(&dircache_queue
, true);
776 thread_id
= create_thread(dircache_thread
, dircache_stack
,
777 sizeof(dircache_stack
), 0, dircache_thread_name
778 IF_PRIO(, PRIORITY_BACKGROUND
)
780 #ifdef HAVE_IO_PRIORITY
781 thread_set_io_priority(thread_id
,IO_PRIORITY_BACKGROUND
);
787 * Returns true if dircache has been initialized and is ready to be used.
789 bool dircache_is_enabled(void)
791 return dircache_initialized
;
795 * Returns true if dircache is being initialized.
797 bool dircache_is_initializing(void)
799 return dircache_initializing
|| thread_enabled
;
803 * Set application flags used to determine if dircache is still intact.
805 void dircache_set_appflag(long mask
)
811 * Get application flags used to determine if dircache is still intact.
813 bool dircache_get_appflag(long mask
)
815 return dircache_is_enabled() && (appflags
& mask
);
819 * Returns the current number of entries (directories and files) in the cache.
821 int dircache_get_entry_count(void)
827 * Returns the allocated space for dircache (without reserve space).
829 int dircache_get_cache_size(void)
831 return dircache_is_enabled() ? dircache_size
: 0;
835 * Returns how many bytes of the reserve allocation for live cache
836 * updates have been used.
838 int dircache_get_reserve_used(void)
840 return dircache_is_enabled() ? reserve_used
: 0;
844 * Returns the time in kernel ticks that took to build the cache.
846 int dircache_get_build_ticks(void)
848 return dircache_is_enabled() ? cache_build_ticks
: 0;
852 * Disables the dircache. Usually called on shutdown or when
853 * accepting a usb connection.
855 void dircache_disable(void)
861 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
863 while (thread_enabled
)
865 dircache_initialized
= false;
867 logf("Waiting for cached dirs to release");
869 cache_in_use
= false;
870 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
871 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
878 } while (cache_in_use
) ;
880 logf("Cache released");
885 * Usermode function to return dircache_entry pointer to the given path.
887 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
889 if (!dircache_initialized
|| filename
== NULL
)
892 return dircache_get_entry(filename
, false);
896 * Function to copy the full absolute path from dircache to the given buffer
897 * using the given dircache_entry pointer.
899 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
903 const struct dircache_entry
*temp
= entry
;
908 /* first compute the necessary size */
911 path_size
+= temp
->name_len
; /* '/' + d_name */
915 /* now copy the path */
916 /* doesn't matter with trailing 0 because it's added later */
920 idx
-= entry
->name_len
;
922 int rem
= size
- idx
;
927 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
933 buf
[MIN(path_size
, size
-1)] = 0;
936 /* --- Directory cache live updating functions --- */
937 static int block_until_ready(void)
939 /* Block until dircache has been built. */
940 while (!dircache_initialized
&& dircache_is_initializing())
943 if (!dircache_initialized
)
949 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
951 struct dircache_entry
*entry
;
952 char basedir
[MAX_PATH
*2];
954 long last_cache_size
= dircache_size
;
956 strlcpy(basedir
, path
, sizeof(basedir
));
957 new = strrchr(basedir
, '/');
960 logf("error occurred");
961 dircache_initialized
= false;
968 entry
= dircache_get_entry(basedir
, true);
971 logf("basedir not found!");
973 dircache_initialized
= false;
977 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
980 logf("not enough space");
981 dircache_initialized
= false;
985 while (entry
->next
!= NULL
)
988 if (entry
->name_len
> 0)
989 entry
= dircache_gen_next(entry
);
993 dircache_initialized
= false;
997 entry
->attribute
= attribute
;
998 entry
->name_len
= MIN(254, strlen(new)) + 1;
999 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
1000 entry
->startcluster
= 0;
1004 memcpy(entry
->d_name
, new, entry
->name_len
);
1005 dircache_size
+= entry
->name_len
;
1007 if (attribute
& ATTR_DIRECTORY
)
1010 dircache_gen_down(entry
);
1013 reserve_used
+= dircache_size
- last_cache_size
;
1018 void dircache_bind(int fd
, const char *path
)
1020 struct dircache_entry
*entry
;
1022 /* Queue requests until dircache has been built. */
1023 if (!dircache_initialized
&& dircache_is_initializing())
1025 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1027 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1028 sizeof(fdbind_cache
[fdbind_idx
].path
));
1029 fdbind_cache
[fdbind_idx
].fd
= fd
;
1034 if (!dircache_initialized
)
1037 logf("bind: %d/%s", fd
, path
);
1038 entry
= dircache_get_entry(path
, false);
1042 dircache_initialized
= false;
1046 fd_bindings
[fd
] = entry
;
1049 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1051 if (!dircache_initialized
|| fd
< 0)
1054 if (fd_bindings
[fd
] == NULL
)
1056 logf("dircache fd(%d) access error", fd
);
1057 dircache_initialized
= false;
1061 fd_bindings
[fd
]->size
= newsize
;
1062 fd_bindings
[fd
]->startcluster
= startcluster
;
1064 void dircache_update_filetime(int fd
)
1070 struct tm
*now
= get_time();
1071 if (!dircache_initialized
|| fd
< 0)
1074 if (fd_bindings
[fd
] == NULL
)
1076 logf("dircache fd access error");
1077 dircache_initialized
= false;
1080 year
= now
->tm_year
+1900-1980;
1081 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1082 (((now
->tm_mon
+1)&0xf)<<5) |
1083 (((now
->tm_mday
)&0x1f));
1084 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1085 (((now
->tm_min
)&0x3f)<<5) |
1086 (((now
->tm_sec
/2)&0x1f));
1090 void dircache_mkdir(const char *path
)
1092 if (block_until_ready())
1095 logf("mkdir: %s", path
);
1096 dircache_new_entry(path
, ATTR_DIRECTORY
);
1099 void dircache_rmdir(const char *path
)
1101 struct dircache_entry
*entry
;
1103 if (block_until_ready())
1106 logf("rmdir: %s", path
);
1107 entry
= dircache_get_entry(path
, false);
1108 if (entry
== NULL
|| entry
->down
== NULL
)
1110 logf("not found or not a directory!");
1111 dircache_initialized
= false;
1116 entry
->name_len
= 0;
1119 /* Remove a file from cache */
1120 void dircache_remove(const char *name
)
1122 struct dircache_entry
*entry
;
1124 if (block_until_ready())
1127 logf("remove: %s", name
);
1129 entry
= dircache_get_entry(name
, false);
1134 dircache_initialized
= false;
1138 entry
->name_len
= 0;
1141 void dircache_rename(const char *oldpath
, const char *newpath
)
1143 struct dircache_entry
*entry
, *newentry
;
1144 struct dircache_entry oldentry
;
1145 char absolute_path
[MAX_PATH
*2];
1148 if (block_until_ready())
1151 logf("rename: %s->%s", oldpath
, newpath
);
1153 entry
= dircache_get_entry(oldpath
, false);
1157 dircache_initialized
= false;
1161 /* Delete the old entry. */
1162 entry
->name_len
= 0;
1164 /** If we rename the same filename twice in a row, we need to
1165 * save the data, because the entry will be re-used. */
1168 /* Generate the absolute path for destination if necessary. */
1169 if (newpath
[0] != '/')
1171 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1172 p
= strrchr(absolute_path
, '/');
1175 logf("Invalid path");
1176 dircache_initialized
= false;
1181 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1182 newpath
= absolute_path
;
1185 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1186 if (newentry
== NULL
)
1188 dircache_initialized
= false;
1192 newentry
->down
= oldentry
.down
;
1193 newentry
->size
= oldentry
.size
;
1194 newentry
->startcluster
= oldentry
.startcluster
;
1195 newentry
->wrttime
= oldentry
.wrttime
;
1196 newentry
->wrtdate
= oldentry
.wrtdate
;
1199 void dircache_add_file(const char *path
, long startcluster
)
1201 struct dircache_entry
*entry
;
1203 if (block_until_ready())
1206 logf("add file: %s", path
);
1207 entry
= dircache_new_entry(path
, 0);
1211 entry
->startcluster
= startcluster
;
1214 DIR_CACHED
* opendir_cached(const char* name
)
1217 DIR_CACHED
* pdir
= opendirs
;
1219 if ( name
[0] != '/' )
1221 DEBUGF("Only absolute paths supported right now\n");
1225 /* find a free dir descriptor */
1226 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1230 if ( dd
== MAX_OPEN_DIRS
)
1232 DEBUGF("Too many dirs open\n");
1239 if (!dircache_initialized
)
1241 pdir
->internal_entry
= NULL
;
1242 pdir
->regulardir
= opendir_uncached(name
);
1246 pdir
->regulardir
= NULL
;
1247 pdir
->internal_entry
= dircache_get_entry(name
, true);
1248 pdir
->theent
.attribute
= -1; /* used to make readdir_cached aware of the first call */
1251 if (pdir
->internal_entry
== NULL
&& pdir
->regulardir
== NULL
)
1260 struct dirent_cached
* readdir_cached(DIR_CACHED
* dir
)
1262 struct dircache_entry
*ce
= dir
->internal_entry
;
1263 struct dirent_uncached
*regentry
;
1268 if (dir
->regulardir
!= NULL
)
1270 regentry
= readdir_uncached(dir
->regulardir
);
1271 if (regentry
== NULL
)
1274 strlcpy(dir
->theent
.d_name
, regentry
->d_name
, MAX_PATH
);
1275 dir
->theent
.size
= regentry
->size
;
1276 dir
->theent
.startcluster
= regentry
->startcluster
;
1277 dir
->theent
.attribute
= regentry
->attribute
;
1278 dir
->theent
.wrttime
= regentry
->wrttime
;
1279 dir
->theent
.wrtdate
= regentry
->wrtdate
;
1281 return &dir
->theent
;
1284 /* if theent.attribute=-1 then this is the first call */
1285 /* otherwise, this is is not so we first take the entry's ->next */
1286 /* NOTE: normal file can't have attribute=-1 */
1287 if(dir
->theent
.attribute
!= -1)
1289 /* skip unused entries */
1290 while(ce
!= NULL
&& ce
->name_len
== 0)
1296 strlcpy(dir
->theent
.d_name
, ce
->d_name
, MAX_PATH
);
1297 /* Can't do `dir->theent = *ce`
1298 because that modifies the d_name pointer. */
1299 dir
->theent
.size
= ce
->size
;
1300 dir
->theent
.startcluster
= ce
->startcluster
;
1301 dir
->theent
.attribute
= ce
->attribute
;
1302 dir
->theent
.wrttime
= ce
->wrttime
;
1303 dir
->theent
.wrtdate
= ce
->wrtdate
;
1304 dir
->internal_entry
= ce
;
1306 //logf("-> %s", ce->name);
1307 return &dir
->theent
;
1310 int closedir_cached(DIR_CACHED
* dir
)
1316 if (dir
->regulardir
!= NULL
)
1317 return closedir_uncached(dir
->regulardir
);
1322 int mkdir_cached(const char *name
)
1324 int rc
=mkdir_uncached(name
);
1326 dircache_mkdir(name
);
1330 int rmdir_cached(const char* name
)
1332 int rc
=rmdir_uncached(name
);
1334 dircache_rmdir(name
);