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
+ 0x900)/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
->startcluster
;
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 sab_process_dir(ce
->startcluster
, ce
->down
);
273 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
275 memset(ce
, 0, sizeof(struct dircache_entry
));
277 #ifdef HAVE_MULTIVOLUME
280 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
281 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
282 ce
->name_len
= VOL_ENUM_POS
+ 3;
283 dircache_size
+= ce
->name_len
;
284 ce
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
286 append_position
= dircache_gen_next(ce
);
287 ce
= dircache_gen_down(ce
);
291 struct fat_dir dir
; /* allocate on stack once for all scan */
292 struct fat_direntry direntry
; /* ditto */
293 #ifdef HAVE_MULTIVOLUME
297 sab
.direntry
= &direntry
;
299 return sab_process_dir(0, ce
);
301 #else /* !SIMULATOR */
302 static char sab_path
[MAX_PATH
];
304 static int sab_process_dir(struct dircache_entry
*ce
)
306 struct dirent_uncached
*entry
;
307 struct dircache_entry
*first_ce
= ce
;
308 DIR_UNCACHED
*dir
= opendir_uncached(sab_path
);
311 logf("Failed to opendir_uncached(%s)", sab_path
);
315 while((entry
= readdir_uncached(dir
)))
317 if(!strcmp(".", entry
->d_name
) ||
318 !strcmp("..", entry
->d_name
))
321 ce
->attribute
= entry
->attribute
;
322 ce
->name_len
= strlen(entry
->d_name
) + 1;
323 ce
->d_name
= ((char *)dircache_root
+ dircache_size
);
324 ce
->size
= entry
->size
;
325 ce
->wrtdate
= entry
->wrtdate
;
326 ce
->wrttime
= entry
->wrttime
;
327 memcpy(ce
->d_name
, entry
->d_name
, ce
->name_len
);
329 dircache_size
+= ce
->name_len
;
332 if(entry
->attribute
& ATTR_DIRECTORY
)
334 dircache_gen_down(ce
);
337 closedir_uncached(dir
);
340 /* save current paths size */
341 int pathpos
= strlen(sab_path
);
343 strlcpy(&sab_path
[pathpos
], "/", sizeof(sab_path
) - pathpos
);
344 strlcpy(&sab_path
[pathpos
+1], entry
->d_name
, sizeof(sab_path
) - pathpos
- 1);
346 int rc
= sab_process_dir(ce
->down
);
348 sab_path
[pathpos
] = '\0';
352 closedir_uncached(dir
);
357 ce
= dircache_gen_next(ce
);
361 /* When simulator is used, it's only safe to yield here. */
364 /* Stop if we got an external signal. */
365 if(check_event_queue())
371 /* add "." and ".." */
374 ce
->attribute
= ATTR_DIRECTORY
;
378 ce
= dircache_gen_next(ce
);
382 ce
->attribute
= ATTR_DIRECTORY
;
384 ce
->down
= first_ce
->up
;
386 closedir_uncached(dir
);
390 static int dircache_scan_and_build(IF_MV2(int volume
,) struct dircache_entry
*ce
)
392 #ifdef HAVE_MULTIVOLUME
395 memset(ce
, 0, sizeof(struct dircache_entry
));
397 strlcpy(sab_path
, "/", sizeof sab_path
);
398 return sab_process_dir(ce
);
400 #endif /* SIMULATOR */
403 * Internal function to get a pointer to dircache_entry for a given filename.
404 * path: Absolute path to a file or directory (see comment)
405 * go_down: Returns the first entry of the directory given by the path (see comment)
407 * As a a special case, accept path="" as an alias for "/".
408 * Also if the path omits the first '/', it will be accepted.
410 * * If get_down=true:
411 * If path="/", the returned entry is the first of root directory (ie dircache_root)
412 * Otherwise, if 'entry' is the returned value when get_down=false,
413 * the functions returns entry->down (which can be NULL)
415 * * If get_down=false:
416 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
417 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
419 * dircache_get_entry(path)->d_name == chunk_n
421 * If path="/", the returned entry is NULL.
422 * If the entry doesn't exist, return NULL
424 * NOTE: this functions silently handles double '/'
426 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
428 char namecopy
[MAX_PATH
];
433 struct dircache_entry
*cache_entry
= dircache_root
;
435 strlcpy(namecopy
, path
, sizeof(namecopy
));
437 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
439 /* If request another chunk, the current entry has to be directory
440 * and so cache_entry->down has to be non-NULL/
441 * Special case of root because it's already the first entry of the root directory
443 * NOTE: this is safe even if cache_entry->down is NULL */
445 cache_entry
= cache_entry
->down
;
449 /* scan dir for name */
450 while(cache_entry
!= NULL
)
452 /* skip unused entries */
453 if(cache_entry
->name_len
== 0)
455 cache_entry
= cache_entry
->next
;
459 if(!strcasecmp(part
, cache_entry
->d_name
))
461 /* go to next entry */
462 cache_entry
= cache_entry
->next
;
465 /* handle not found case */
466 if(cache_entry
== NULL
)
470 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
472 return at_root
? cache_entry
: cache_entry
->down
;
474 return at_root
? NULL
: cache_entry
;
477 #ifdef HAVE_EEPROM_SETTINGS
479 * Function to load the internal cache structure from disk to initialize
480 * the dircache really fast and little disk access.
482 int dircache_load(void)
484 struct dircache_maindata maindata
;
488 if (dircache_initialized
)
491 logf("Loading directory cache");
494 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
498 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
499 if (bytes_read
!= sizeof(struct dircache_maindata
)
500 || maindata
.size
<= 0)
502 logf("Dircache file header error");
504 remove(DIRCACHE_FILE
);
508 dircache_root
= buffer_alloc(0);
509 if ((long)maindata
.root_entry
!= (long)dircache_root
)
511 logf("Position missmatch");
513 remove(DIRCACHE_FILE
);
517 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
518 entry_count
= maindata
.entry_count
;
519 appflags
= maindata
.appflags
;
520 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
522 remove(DIRCACHE_FILE
);
524 if (bytes_read
!= maindata
.size
)
526 logf("Dircache read failed");
530 /* Cache successfully loaded. */
531 dircache_size
= maindata
.size
;
532 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
534 logf("Done, %ld KiB used", dircache_size
/ 1024);
535 dircache_initialized
= true;
536 memset(fd_bindings
, 0, sizeof(fd_bindings
));
542 * Function to save the internal cache stucture to disk for fast loading
545 int dircache_save(void)
547 struct dircache_maindata maindata
;
549 unsigned long bytes_written
;
551 remove(DIRCACHE_FILE
);
553 if (!dircache_initialized
)
556 logf("Saving directory cache");
557 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
559 maindata
.magic
= DIRCACHE_MAGIC
;
560 maindata
.size
= dircache_size
;
561 maindata
.root_entry
= dircache_root
;
562 maindata
.entry_count
= entry_count
;
563 maindata
.appflags
= appflags
;
565 /* Save the info structure */
566 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
567 if (bytes_written
!= sizeof(struct dircache_maindata
))
570 logf("dircache: write failed #1");
574 /* Dump whole directory cache to disk */
575 bytes_written
= write(fd
, dircache_root
, dircache_size
);
577 if (bytes_written
!= dircache_size
)
579 logf("dircache: write failed #2");
588 * Internal function which scans the disk and creates the dircache structure.
590 static int dircache_do_rebuild(void)
592 unsigned int start_tick
;
595 /* Measure how long it takes build the cache. */
596 start_tick
= current_tick
;
597 dircache_initializing
= true;
601 dircache_size
= sizeof(struct dircache_entry
);
603 #ifdef HAVE_MULTIVOLUME
604 append_position
= dircache_root
;
606 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
608 if (fat_ismounted(i
))
612 #ifdef HAVE_MULTIVOLUME
613 if (dircache_scan_and_build(IF_MV2(i
,) append_position
) < 0)
615 if (dircache_scan_and_build(IF_MV2(0,) dircache_root
) < 0)
616 #endif /* HAVE_MULTIVOLUME */
618 logf("dircache_travel failed");
621 dircache_initializing
= false;
625 #ifdef HAVE_MULTIVOLUME
630 logf("Done, %ld KiB used", dircache_size
/ 1024);
632 dircache_initialized
= true;
633 dircache_initializing
= false;
634 cache_build_ticks
= current_tick
- start_tick
;
636 /* Initialized fd bindings. */
637 memset(fd_bindings
, 0, sizeof(fd_bindings
));
638 for (i
= 0; i
< fdbind_idx
; i
++)
639 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
644 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
645 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
649 /* We have to long align the audiobuf to keep the buffer access fast. */
650 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
651 audiobuf
+= DIRCACHE_RESERVE
;
652 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
660 * Internal thread that controls transparent cache building.
662 static void dircache_thread(void)
664 struct queue_event ev
;
668 queue_wait(&dircache_queue
, &ev
);
674 if (!dircache_initialized
)
676 dircache_initialized
= false;
679 thread_enabled
= true;
680 dircache_do_rebuild();
681 thread_enabled
= false;
685 logf("Stopped the rebuilding.");
686 dircache_initialized
= false;
690 case SYS_USB_CONNECTED
:
691 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
692 usb_wait_for_disconnect(&dircache_queue
);
700 * Start scanning the disk to build the dircache.
701 * Either transparent or non-transparent build method is used.
703 int dircache_build(int last_size
)
705 if (dircache_initialized
|| thread_enabled
)
708 logf("Building directory cache");
709 remove(DIRCACHE_FILE
);
711 /* Background build, dircache has been previously allocated */
712 if (dircache_size
> 0)
714 thread_enabled
= true;
715 dircache_initializing
= true;
716 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
720 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
722 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
723 dircache_root
= buffer_alloc(allocated_size
);
724 thread_enabled
= true;
726 /* Start a transparent rebuild. */
727 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
731 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
733 /* Start a non-transparent rebuild. */
734 return dircache_do_rebuild();
738 * Steal the allocated dircache buffer and disable dircache.
740 void* dircache_steal_buffer(long *size
)
743 if (dircache_size
== 0)
749 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
751 return dircache_root
;
755 * Main initialization function that must be called before any other
756 * operations within the dircache.
758 void dircache_init(void)
762 dircache_initialized
= false;
763 dircache_initializing
= false;
765 memset(opendirs
, 0, sizeof(opendirs
));
766 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
768 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
771 queue_init(&dircache_queue
, true);
772 create_thread(dircache_thread
, dircache_stack
,
773 sizeof(dircache_stack
), 0, dircache_thread_name
774 IF_PRIO(, PRIORITY_BACKGROUND
)
779 * Returns true if dircache has been initialized and is ready to be used.
781 bool dircache_is_enabled(void)
783 return dircache_initialized
;
787 * Returns true if dircache is being initialized.
789 bool dircache_is_initializing(void)
791 return dircache_initializing
|| thread_enabled
;
795 * Set application flags used to determine if dircache is still intact.
797 void dircache_set_appflag(long mask
)
803 * Get application flags used to determine if dircache is still intact.
805 bool dircache_get_appflag(long mask
)
807 return dircache_is_enabled() && (appflags
& mask
);
811 * Returns the current number of entries (directories and files) in the cache.
813 int dircache_get_entry_count(void)
819 * Returns the allocated space for dircache (without reserve space).
821 int dircache_get_cache_size(void)
823 return dircache_is_enabled() ? dircache_size
: 0;
827 * Returns how many bytes of the reserve allocation for live cache
828 * updates have been used.
830 int dircache_get_reserve_used(void)
832 return dircache_is_enabled() ? reserve_used
: 0;
836 * Returns the time in kernel ticks that took to build the cache.
838 int dircache_get_build_ticks(void)
840 return dircache_is_enabled() ? cache_build_ticks
: 0;
844 * Disables the dircache. Usually called on shutdown or when
845 * accepting a usb connection.
847 void dircache_disable(void)
853 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
855 while (thread_enabled
)
857 dircache_initialized
= false;
859 logf("Waiting for cached dirs to release");
861 cache_in_use
= false;
862 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
863 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
870 } while (cache_in_use
) ;
872 logf("Cache released");
877 * Usermode function to return dircache_entry pointer to the given path.
879 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
881 if (!dircache_initialized
|| filename
== NULL
)
884 return dircache_get_entry(filename
, false);
888 * Function to copy the full absolute path from dircache to the given buffer
889 * using the given dircache_entry pointer.
891 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
895 const struct dircache_entry
*temp
= entry
;
900 /* first compute the necessary size */
903 path_size
+= temp
->name_len
; /* '/' + d_name */
907 /* now copy the path */
908 /* doesn't matter with trailing 0 because it's added later */
912 idx
-= entry
->name_len
;
914 int rem
= size
- idx
;
919 memcpy(buf
+ idx
+ 1, entry
->d_name
, MIN((size_t)(rem
- 1), (size_t)(entry
->name_len
- 1)));
925 buf
[MIN(path_size
, size
-1)] = 0;
928 /* --- Directory cache live updating functions --- */
929 static int block_until_ready(void)
931 /* Block until dircache has been built. */
932 while (!dircache_initialized
&& dircache_is_initializing())
935 if (!dircache_initialized
)
941 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
943 struct dircache_entry
*entry
;
944 char basedir
[MAX_PATH
*2];
946 long last_cache_size
= dircache_size
;
948 strlcpy(basedir
, path
, sizeof(basedir
));
949 new = strrchr(basedir
, '/');
952 logf("error occurred");
953 dircache_initialized
= false;
960 entry
= dircache_get_entry(basedir
, true);
963 logf("basedir not found!");
965 dircache_initialized
= false;
969 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
972 logf("not enough space");
973 dircache_initialized
= false;
977 while (entry
->next
!= NULL
)
980 if (entry
->name_len
> 0)
981 entry
= dircache_gen_next(entry
);
985 dircache_initialized
= false;
989 entry
->attribute
= attribute
;
990 entry
->name_len
= MIN(254, strlen(new)) + 1;
991 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
992 entry
->startcluster
= 0;
996 memcpy(entry
->d_name
, new, entry
->name_len
);
997 dircache_size
+= entry
->name_len
;
999 if (attribute
& ATTR_DIRECTORY
)
1002 dircache_gen_down(entry
);
1005 reserve_used
+= dircache_size
- last_cache_size
;
1010 void dircache_bind(int fd
, const char *path
)
1012 struct dircache_entry
*entry
;
1014 /* Queue requests until dircache has been built. */
1015 if (!dircache_initialized
&& dircache_is_initializing())
1017 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1019 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1020 sizeof(fdbind_cache
[fdbind_idx
].path
));
1021 fdbind_cache
[fdbind_idx
].fd
= fd
;
1026 if (!dircache_initialized
)
1029 logf("bind: %d/%s", fd
, path
);
1030 entry
= dircache_get_entry(path
, false);
1034 dircache_initialized
= false;
1038 fd_bindings
[fd
] = entry
;
1041 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1043 if (!dircache_initialized
|| fd
< 0)
1046 if (fd_bindings
[fd
] == NULL
)
1048 logf("dircache fd(%d) access error", fd
);
1049 dircache_initialized
= false;
1053 fd_bindings
[fd
]->size
= newsize
;
1054 fd_bindings
[fd
]->startcluster
= startcluster
;
1056 void dircache_update_filetime(int fd
)
1062 struct tm
*now
= get_time();
1063 if (!dircache_initialized
|| fd
< 0)
1066 if (fd_bindings
[fd
] == NULL
)
1068 logf("dircache fd access error");
1069 dircache_initialized
= false;
1072 year
= now
->tm_year
+1900-1980;
1073 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1074 (((now
->tm_mon
+1)&0xf)<<5) |
1075 (((now
->tm_mday
)&0x1f));
1076 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1077 (((now
->tm_min
)&0x3f)<<5) |
1078 (((now
->tm_sec
/2)&0x1f));
1082 void dircache_mkdir(const char *path
)
1084 if (block_until_ready())
1087 logf("mkdir: %s", path
);
1088 dircache_new_entry(path
, ATTR_DIRECTORY
);
1091 void dircache_rmdir(const char *path
)
1093 struct dircache_entry
*entry
;
1095 if (block_until_ready())
1098 logf("rmdir: %s", path
);
1099 entry
= dircache_get_entry(path
, false);
1100 if (entry
== NULL
|| entry
->down
== NULL
)
1102 logf("not found or not a directory!");
1103 dircache_initialized
= false;
1108 entry
->name_len
= 0;
1111 /* Remove a file from cache */
1112 void dircache_remove(const char *name
)
1114 struct dircache_entry
*entry
;
1116 if (block_until_ready())
1119 logf("remove: %s", name
);
1121 entry
= dircache_get_entry(name
, false);
1126 dircache_initialized
= false;
1130 entry
->name_len
= 0;
1133 void dircache_rename(const char *oldpath
, const char *newpath
)
1135 struct dircache_entry
*entry
, *newentry
;
1136 struct dircache_entry oldentry
;
1137 char absolute_path
[MAX_PATH
*2];
1140 if (block_until_ready())
1143 logf("rename: %s->%s", oldpath
, newpath
);
1145 entry
= dircache_get_entry(oldpath
, false);
1149 dircache_initialized
= false;
1153 /* Delete the old entry. */
1154 entry
->name_len
= 0;
1156 /** If we rename the same filename twice in a row, we need to
1157 * save the data, because the entry will be re-used. */
1160 /* Generate the absolute path for destination if necessary. */
1161 if (newpath
[0] != '/')
1163 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1164 p
= strrchr(absolute_path
, '/');
1167 logf("Invalid path");
1168 dircache_initialized
= false;
1173 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1174 newpath
= absolute_path
;
1177 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1178 if (newentry
== NULL
)
1180 dircache_initialized
= false;
1184 newentry
->down
= oldentry
.down
;
1185 newentry
->size
= oldentry
.size
;
1186 newentry
->startcluster
= oldentry
.startcluster
;
1187 newentry
->wrttime
= oldentry
.wrttime
;
1188 newentry
->wrtdate
= oldentry
.wrtdate
;
1191 void dircache_add_file(const char *path
, long startcluster
)
1193 struct dircache_entry
*entry
;
1195 if (block_until_ready())
1198 logf("add file: %s", path
);
1199 entry
= dircache_new_entry(path
, 0);
1203 entry
->startcluster
= startcluster
;
1206 DIR_CACHED
* opendir_cached(const char* name
)
1208 struct dircache_entry
*cache_entry
;
1210 DIR_CACHED
* pdir
= opendirs
;
1212 if ( name
[0] != '/' )
1214 DEBUGF("Only absolute paths supported right now\n");
1218 /* find a free dir descriptor */
1219 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1223 if ( dd
== MAX_OPEN_DIRS
)
1225 DEBUGF("Too many dirs open\n");
1230 if (!dircache_initialized
)
1232 pdir
->regulardir
= opendir_uncached(name
);
1233 if (!pdir
->regulardir
)
1241 pdir
->regulardir
= NULL
;
1242 cache_entry
= dircache_get_entry(name
, true);
1243 pdir
->entry
= cache_entry
;
1245 if (cache_entry
== NULL
)
1254 struct dircache_entry
* readdir_cached(DIR_CACHED
* dir
)
1256 struct dirent_uncached
*regentry
;
1257 struct dircache_entry
*ce
;
1262 if (dir
->regulardir
!= NULL
)
1264 regentry
= readdir_uncached(dir
->regulardir
);
1265 if (regentry
== NULL
)
1268 strlcpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
);
1269 dir
->secondary_entry
.size
= regentry
->size
;
1270 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1271 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1272 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1273 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1274 dir
->secondary_entry
.next
= NULL
;
1276 return &dir
->secondary_entry
;
1280 if (dir
->entry
== NULL
)
1284 if (ce
->name_len
== 0)
1285 dir
->entry
= ce
->next
;
1286 } while (ce
->name_len
== 0) ;
1288 dir
->entry
= ce
->next
;
1290 strlcpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
);
1291 /* Can't do `dir->secondary_entry = *ce`
1292 because that modifies the d_name pointer. */
1293 dir
->secondary_entry
.size
= ce
->size
;
1294 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1295 dir
->secondary_entry
.attribute
= ce
->attribute
;
1296 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1297 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1298 dir
->secondary_entry
.next
= NULL
;
1299 dir
->internal_entry
= ce
;
1301 //logf("-> %s", ce->name);
1302 return &dir
->secondary_entry
;
1305 int closedir_cached(DIR_CACHED
* dir
)
1311 if (dir
->regulardir
!= NULL
)
1312 return closedir_uncached(dir
->regulardir
);
1317 int mkdir_cached(const char *name
)
1319 int rc
=mkdir_uncached(name
);
1321 dircache_mkdir(name
);
1325 int rmdir_cached(const char* name
)
1327 int rc
=rmdir_uncached(name
);
1329 dircache_rmdir(name
);