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;
74 static char dircache_cur_path
[MAX_PATH
*2];
76 static struct event_queue dircache_queue
;
77 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x900)/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
;
150 /* This will eat ~30 KiB of memory!
151 * We should probably use that as additional reserve buffer in future. */
152 #define MAX_SCAN_DEPTH 16
153 static struct travel_data dir_recursion
[MAX_SCAN_DEPTH
];
156 * Returns true if there is an event waiting in the queue
157 * that requires the current operation to be aborted.
159 static bool check_event_queue(void)
161 struct queue_event ev
;
163 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
167 case SYS_USB_CONNECTED
:
171 /* Put the event back into the queue. */
172 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
180 * Internal function to iterate a path.
182 static int dircache_scan(IF_MV2(int volume
,) struct travel_data
*td
)
185 #ifdef HAVE_MULTIVOLUME
188 while ( ( td
->entry
= readdir_uncached(td
->dir
) ) )
190 while ( (fat_getnext(td
->dir
, &td
->entry
) >= 0) && (td
->entry
.name
[0]))
194 if (!strcmp(".", td
->entry
->d_name
) ||
195 !strcmp("..", td
->entry
->d_name
))
200 td
->ce
->attribute
= td
->entry
->attribute
;
201 td
->ce
->name_len
= strlen(td
->entry
->d_name
) + 1;
202 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
203 td
->ce
->size
= td
->entry
->size
;
204 td
->ce
->wrtdate
= td
->entry
->wrtdate
;
205 td
->ce
->wrttime
= td
->entry
->wrttime
;
206 memcpy(td
->ce
->d_name
, td
->entry
->d_name
, td
->ce
->name_len
);
208 if (!strcmp(".", td
->entry
.name
) ||
209 !strcmp("..", td
->entry
.name
))
214 td
->ce
->attribute
= td
->entry
.attr
;
215 td
->ce
->name_len
= strlen(td
->entry
.name
) + 1;
216 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
217 td
->ce
->startcluster
= td
->entry
.firstcluster
;
218 td
->ce
->size
= td
->entry
.filesize
;
219 td
->ce
->wrtdate
= td
->entry
.wrtdate
;
220 td
->ce
->wrttime
= td
->entry
.wrttime
;
221 memcpy(td
->ce
->d_name
, td
->entry
.name
, td
->ce
->name_len
);
223 dircache_size
+= td
->ce
->name_len
;
227 if (td
->entry
->attribute
& ATTR_DIRECTORY
)
229 if (td
->entry
.attr
& FAT_ATTR_DIRECTORY
)
233 td
->down_entry
= dircache_gen_down(td
->ce
);
234 if (td
->down_entry
== NULL
)
237 td
->pathpos
= strlen(dircache_cur_path
);
238 strlcpy(&dircache_cur_path
[td
->pathpos
], "/",
239 sizeof(dircache_cur_path
) - td
->pathpos
);
241 strlcpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
->d_name
,
242 sizeof(dircache_cur_path
) - td
->pathpos
- 1);
244 td
->newdir
= opendir_uncached(dircache_cur_path
);
245 if (td
->newdir
== NULL
)
247 logf("Failed to opendir_uncached(): %s", dircache_cur_path
);
251 strlcpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
.name
,
252 sizeof(dircache_cur_path
) - td
->pathpos
- 1);
254 td
->newdir
= *td
->dir
;
255 if (fat_opendir(IF_MV2(volume
,) &td
->newdir
,
256 td
->entry
.firstcluster
, td
->dir
) < 0 )
262 td
->ce
= dircache_gen_next(td
->ce
);
270 td
->ce
= dircache_gen_next(td
->ce
);
274 /* When simulator is used, it's only safe to yield here. */
277 /* Stop if we got an external signal. */
278 if (check_event_queue())
289 * Recursively scan the hard disk and build the cache.
292 static int dircache_travel(IF_MV2(int volume
,) DIR_UNCACHED
*dir
, struct dircache_entry
*ce
)
294 static int dircache_travel(IF_MV2(int volume
,) struct fat_dir
*dir
, struct dircache_entry
*ce
)
300 memset(ce
, 0, sizeof(struct dircache_entry
));
302 #if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR)
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
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
311 append_position
= dircache_gen_next(ce
);
312 ce
= dircache_gen_down(ce
);
316 dir_recursion
[0].dir
= dir
;
317 dir_recursion
[0].ce
= ce
;
318 dir_recursion
[0].first
= ce
;
321 //logf("=> %s", dircache_cur_path);
322 result
= dircache_scan(IF_MV2(volume
,) &dir_recursion
[depth
]);
324 case 0: /* Leaving the current directory. */
325 /* Add the standard . and .. entries. */
326 ce
= dir_recursion
[depth
].ce
;
330 closedir_uncached(dir_recursion
[depth
].dir
);
331 ce
->attribute
= ATTR_DIRECTORY
;
333 ce
->attribute
= FAT_ATTR_DIRECTORY
;
334 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
337 ce
->down
= dir_recursion
[depth
].first
;
343 dircache_cur_path
[dir_recursion
[depth
].pathpos
] = '\0';
345 ce
= dircache_gen_next(ce
);
348 logf("memory allocation error");
352 ce
->attribute
= ATTR_DIRECTORY
;
354 ce
->attribute
= FAT_ATTR_DIRECTORY
;
355 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
360 ce
->down
= dir_recursion
[depth
].first
;
364 case 1: /* Going down in the directory tree. */
366 if (depth
>= MAX_SCAN_DEPTH
)
368 logf("Too deep directory structure");
373 dir_recursion
[depth
].dir
= dir_recursion
[depth
-1].newdir
;
375 dir_recursion
[depth
].dir
= &dir_recursion
[depth
-1].newdir
;
377 dir_recursion
[depth
].first
= dir_recursion
[depth
-1].down_entry
;
378 dir_recursion
[depth
].ce
= dir_recursion
[depth
-1].down_entry
;
383 logf("-> %s", dircache_cur_path
);
386 } while (depth
>= 0) ;
392 * Internal function to get a pointer to dircache_entry for a given filename.
393 * path: Absolute path to a file or directory (see comment)
394 * go_down: Returns the first entry of the directory given by the path (see comment)
396 * As a a special case to handle buggy code, accept path="" as an alias for "/"
398 * * If get_down=true:
399 * If path="/", the returned entry is the first of root directory (ie dircache_root)
400 * Otherwise, if 'entry' is the returned value when get_down=false,
401 * the functions returns entry->down (which can be NULL)
403 * * If get_down=false:
404 * If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
405 * root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
407 * dircache_get_entry(path)->d_name == chunk_n
409 * If path="/", the returned entry is NULL.
410 * If the entry doesn't exist, return NULL
412 * NOTE: this functions silently handles double '/'
414 static struct dircache_entry
* dircache_get_entry(const char *path
, bool go_down
)
416 char namecopy
[MAX_PATH
];
421 struct dircache_entry
*cache_entry
= dircache_root
;
423 /* check that the path is absolute (accept empty path also) */
424 if(path
[0] != '/' && path
[0] != 0)
427 /* make a copy of the path because strok_r modifies the path */
428 /* also handle the weird "" alias for "/" */
430 strlcpy(namecopy
, path
, sizeof(namecopy
));
432 strlcpy(namecopy
, "/", sizeof(namecopy
));
434 for(part
= strtok_r(namecopy
, "/", &end
); part
; part
= strtok_r(NULL
, "/", &end
))
436 /* If request another chunk, the current entry has to be directory
437 * and so cache_entry->down has to be non-NULL/
438 * Special case of root because it's already the first entry of the root directory
440 * NOTE: this is safe even if cache_entry->down is NULL */
442 cache_entry
= cache_entry
->down
;
446 /* scan dir for name */
447 while(cache_entry
!= NULL
)
449 /* skip unused entries */
450 if(cache_entry
->name_len
== 0)
452 cache_entry
= cache_entry
->next
;
456 if(!strcasecmp(part
, cache_entry
->d_name
))
458 /* go to next entry */
459 cache_entry
= cache_entry
->next
;
462 /* handle not found case */
463 if(cache_entry
== NULL
)
467 /* NOTE: here cache_entry!=NULL so taking ->down is safe */
469 return at_root
? cache_entry
: cache_entry
->down
;
471 return at_root
? NULL
: cache_entry
;
474 #ifdef HAVE_EEPROM_SETTINGS
476 * Function to load the internal cache structure from disk to initialize
477 * the dircache really fast and little disk access.
479 int dircache_load(void)
481 struct dircache_maindata maindata
;
485 if (dircache_initialized
)
488 logf("Loading directory cache");
491 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
495 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
496 if (bytes_read
!= sizeof(struct dircache_maindata
)
497 || maindata
.size
<= 0)
499 logf("Dircache file header error");
501 remove(DIRCACHE_FILE
);
505 dircache_root
= buffer_alloc(0);
506 if ((long)maindata
.root_entry
!= (long)dircache_root
)
508 logf("Position missmatch");
510 remove(DIRCACHE_FILE
);
514 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
515 entry_count
= maindata
.entry_count
;
516 appflags
= maindata
.appflags
;
517 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
519 remove(DIRCACHE_FILE
);
521 if (bytes_read
!= maindata
.size
)
523 logf("Dircache read failed");
527 /* Cache successfully loaded. */
528 dircache_size
= maindata
.size
;
529 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
531 logf("Done, %ld KiB used", dircache_size
/ 1024);
532 dircache_initialized
= true;
533 memset(fd_bindings
, 0, sizeof(fd_bindings
));
539 * Function to save the internal cache stucture to disk for fast loading
542 int dircache_save(void)
544 struct dircache_maindata maindata
;
546 unsigned long bytes_written
;
548 remove(DIRCACHE_FILE
);
550 if (!dircache_initialized
)
553 logf("Saving directory cache");
554 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
556 maindata
.magic
= DIRCACHE_MAGIC
;
557 maindata
.size
= dircache_size
;
558 maindata
.root_entry
= dircache_root
;
559 maindata
.entry_count
= entry_count
;
560 maindata
.appflags
= appflags
;
562 /* Save the info structure */
563 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
564 if (bytes_written
!= sizeof(struct dircache_maindata
))
567 logf("dircache: write failed #1");
571 /* Dump whole directory cache to disk */
572 bytes_written
= write(fd
, dircache_root
, dircache_size
);
574 if (bytes_written
!= dircache_size
)
576 logf("dircache: write failed #2");
585 * Internal function which scans the disk and creates the dircache structure.
587 static int dircache_do_rebuild(void)
592 struct fat_dir dir
, *pdir
;
594 unsigned int start_tick
;
597 /* Measure how long it takes build the cache. */
598 start_tick
= current_tick
;
599 dircache_initializing
= true;
603 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
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 pdir
= opendir_uncached("/");
618 logf("Failed to open rootdir");
619 dircache_initializing
= false;
623 #ifdef HAVE_MULTIVOLUME
624 if ( fat_opendir(IF_MV2(i
,) &dir
, 0, NULL
) < 0 ) {
626 if ( fat_opendir(IF_MV2(0,) &dir
, 0, NULL
) < 0 ) {
627 #endif /* HAVE_MULTIVOLUME */
628 logf("Failed opening root dir");
629 dircache_initializing
= false;
635 #ifdef HAVE_MULTIVOLUME
636 if (dircache_travel(IF_MV2(i
,) pdir
, append_position
) < 0)
638 if (dircache_travel(IF_MV2(0,) pdir
, dircache_root
) < 0)
639 #endif /* HAVE_MULTIVOLUME */
641 logf("dircache_travel failed");
644 dircache_initializing
= false;
648 #ifdef HAVE_MULTIVOLUME
653 logf("Done, %ld KiB used", dircache_size
/ 1024);
655 dircache_initialized
= true;
656 dircache_initializing
= false;
657 cache_build_ticks
= current_tick
- start_tick
;
659 /* Initialized fd bindings. */
660 memset(fd_bindings
, 0, sizeof(fd_bindings
));
661 for (i
= 0; i
< fdbind_idx
; i
++)
662 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
667 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
668 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
672 /* We have to long align the audiobuf to keep the buffer access fast. */
673 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
674 audiobuf
+= DIRCACHE_RESERVE
;
675 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
683 * Internal thread that controls transparent cache building.
685 static void dircache_thread(void)
687 struct queue_event ev
;
691 queue_wait(&dircache_queue
, &ev
);
697 if (!dircache_initialized
)
699 dircache_initialized
= false;
702 thread_enabled
= true;
703 dircache_do_rebuild();
704 thread_enabled
= false;
708 logf("Stopped the rebuilding.");
709 dircache_initialized
= false;
713 case SYS_USB_CONNECTED
:
714 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
715 usb_wait_for_disconnect(&dircache_queue
);
723 * Start scanning the disk to build the dircache.
724 * Either transparent or non-transparent build method is used.
726 int dircache_build(int last_size
)
728 if (dircache_initialized
|| thread_enabled
)
731 logf("Building directory cache");
732 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)
785 dircache_initialized
= false;
786 dircache_initializing
= false;
788 memset(opendirs
, 0, sizeof(opendirs
));
789 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
791 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
794 queue_init(&dircache_queue
, true);
795 create_thread(dircache_thread
, dircache_stack
,
796 sizeof(dircache_stack
), 0, dircache_thread_name
797 IF_PRIO(, PRIORITY_BACKGROUND
)
802 * Returns true if dircache has been initialized and is ready to be used.
804 bool dircache_is_enabled(void)
806 return dircache_initialized
;
810 * Returns true if dircache is being initialized.
812 bool dircache_is_initializing(void)
814 return dircache_initializing
|| thread_enabled
;
818 * Set application flags used to determine if dircache is still intact.
820 void dircache_set_appflag(long mask
)
826 * Get application flags used to determine if dircache is still intact.
828 bool dircache_get_appflag(long mask
)
830 return dircache_is_enabled() && (appflags
& mask
);
834 * Returns the current number of entries (directories and files) in the cache.
836 int dircache_get_entry_count(void)
842 * Returns the allocated space for dircache (without reserve space).
844 int dircache_get_cache_size(void)
846 return dircache_is_enabled() ? dircache_size
: 0;
850 * Returns how many bytes of the reserve allocation for live cache
851 * updates have been used.
853 int dircache_get_reserve_used(void)
855 return dircache_is_enabled() ? reserve_used
: 0;
859 * Returns the time in kernel ticks that took to build the cache.
861 int dircache_get_build_ticks(void)
863 return dircache_is_enabled() ? cache_build_ticks
: 0;
867 * Disables the dircache. Usually called on shutdown or when
868 * accepting a usb connection.
870 void dircache_disable(void)
876 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
878 while (thread_enabled
)
880 dircache_initialized
= false;
882 logf("Waiting for cached dirs to release");
884 cache_in_use
= false;
885 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
886 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
893 } while (cache_in_use
) ;
895 logf("Cache released");
900 * Usermode function to return dircache_entry pointer to the given path.
902 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
904 if (!dircache_initialized
|| filename
== NULL
)
907 return dircache_get_entry(filename
, false);
911 * Function to copy the full absolute path from dircache to the given buffer
912 * using the given dircache_entry pointer.
914 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
916 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
931 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
935 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
936 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
937 size
-= down
[depth
]->name_len
;
943 /* --- Directory cache live updating functions --- */
944 static int block_until_ready(void)
946 /* Block until dircache has been built. */
947 while (!dircache_initialized
&& dircache_is_initializing())
950 if (!dircache_initialized
)
956 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
958 struct dircache_entry
*entry
;
959 char basedir
[MAX_PATH
*2];
961 long last_cache_size
= dircache_size
;
963 strlcpy(basedir
, path
, sizeof(basedir
));
964 new = strrchr(basedir
, '/');
967 logf("error occurred");
968 dircache_initialized
= false;
975 entry
= dircache_get_entry(basedir
, true);
978 logf("basedir not found!");
980 dircache_initialized
= false;
984 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
987 logf("not enough space");
988 dircache_initialized
= false;
992 while (entry
->next
!= NULL
)
995 if (entry
->name_len
> 0)
996 entry
= dircache_gen_next(entry
);
1000 dircache_initialized
= false;
1004 entry
->attribute
= attribute
;
1005 entry
->name_len
= MIN(254, strlen(new)) + 1;
1006 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
1007 entry
->startcluster
= 0;
1011 memcpy(entry
->d_name
, new, entry
->name_len
);
1012 dircache_size
+= entry
->name_len
;
1014 if (attribute
& ATTR_DIRECTORY
)
1017 dircache_gen_down(entry
);
1020 reserve_used
+= dircache_size
- last_cache_size
;
1025 void dircache_bind(int fd
, const char *path
)
1027 struct dircache_entry
*entry
;
1029 /* Queue requests until dircache has been built. */
1030 if (!dircache_initialized
&& dircache_is_initializing())
1032 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1034 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1035 sizeof(fdbind_cache
[fdbind_idx
].path
));
1036 fdbind_cache
[fdbind_idx
].fd
= fd
;
1041 if (!dircache_initialized
)
1044 logf("bind: %d/%s", fd
, path
);
1045 entry
= dircache_get_entry(path
, false);
1049 dircache_initialized
= false;
1053 fd_bindings
[fd
] = entry
;
1056 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1058 if (!dircache_initialized
|| fd
< 0)
1061 if (fd_bindings
[fd
] == NULL
)
1063 logf("dircache fd access error");
1064 dircache_initialized
= false;
1068 fd_bindings
[fd
]->size
= newsize
;
1069 fd_bindings
[fd
]->startcluster
= startcluster
;
1071 void dircache_update_filetime(int fd
)
1077 struct tm
*now
= get_time();
1078 if (!dircache_initialized
|| fd
< 0)
1081 if (fd_bindings
[fd
] == NULL
)
1083 logf("dircache fd access error");
1084 dircache_initialized
= false;
1087 year
= now
->tm_year
+1900-1980;
1088 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1089 (((now
->tm_mon
+1)&0xf)<<5) |
1090 (((now
->tm_mday
)&0x1f));
1091 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1092 (((now
->tm_min
)&0x3f)<<5) |
1093 (((now
->tm_sec
/2)&0x1f));
1097 void dircache_mkdir(const char *path
)
1099 if (block_until_ready())
1102 logf("mkdir: %s", path
);
1103 dircache_new_entry(path
, ATTR_DIRECTORY
);
1106 void dircache_rmdir(const char *path
)
1108 struct dircache_entry
*entry
;
1110 if (block_until_ready())
1113 logf("rmdir: %s", path
);
1114 entry
= dircache_get_entry(path
, false);
1115 if (entry
== NULL
|| entry
->down
== NULL
)
1117 logf("not found or not a directory!");
1118 dircache_initialized
= false;
1123 entry
->name_len
= 0;
1126 /* Remove a file from cache */
1127 void dircache_remove(const char *name
)
1129 struct dircache_entry
*entry
;
1131 if (block_until_ready())
1134 logf("remove: %s", name
);
1136 entry
= dircache_get_entry(name
, false);
1141 dircache_initialized
= false;
1145 entry
->name_len
= 0;
1148 void dircache_rename(const char *oldpath
, const char *newpath
)
1150 struct dircache_entry
*entry
, *newentry
;
1151 struct dircache_entry oldentry
;
1152 char absolute_path
[MAX_PATH
*2];
1155 if (block_until_ready())
1158 logf("rename: %s->%s", oldpath
, newpath
);
1160 entry
= dircache_get_entry(oldpath
, false);
1164 dircache_initialized
= false;
1168 /* Delete the old entry. */
1169 entry
->name_len
= 0;
1171 /** If we rename the same filename twice in a row, we need to
1172 * save the data, because the entry will be re-used. */
1175 /* Generate the absolute path for destination if necessary. */
1176 if (newpath
[0] != '/')
1178 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1179 p
= strrchr(absolute_path
, '/');
1182 logf("Invalid path");
1183 dircache_initialized
= false;
1188 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1189 newpath
= absolute_path
;
1192 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1193 if (newentry
== NULL
)
1195 dircache_initialized
= false;
1199 newentry
->down
= oldentry
.down
;
1200 newentry
->size
= oldentry
.size
;
1201 newentry
->startcluster
= oldentry
.startcluster
;
1202 newentry
->wrttime
= oldentry
.wrttime
;
1203 newentry
->wrtdate
= oldentry
.wrtdate
;
1206 void dircache_add_file(const char *path
, long startcluster
)
1208 struct dircache_entry
*entry
;
1210 if (block_until_ready())
1213 logf("add file: %s", path
);
1214 entry
= dircache_new_entry(path
, 0);
1218 entry
->startcluster
= startcluster
;
1221 DIR_CACHED
* opendir_cached(const char* name
)
1223 struct dircache_entry
*cache_entry
;
1225 DIR_CACHED
* pdir
= opendirs
;
1227 if ( name
[0] != '/' )
1229 DEBUGF("Only absolute paths supported right now\n");
1233 /* find a free dir descriptor */
1234 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1238 if ( dd
== MAX_OPEN_DIRS
)
1240 DEBUGF("Too many dirs open\n");
1245 if (!dircache_initialized
)
1247 pdir
->regulardir
= opendir_uncached(name
);
1248 if (!pdir
->regulardir
)
1256 pdir
->regulardir
= NULL
;
1257 cache_entry
= dircache_get_entry(name
, true);
1258 pdir
->entry
= cache_entry
;
1260 if (cache_entry
== NULL
)
1269 struct dircache_entry
* readdir_cached(DIR_CACHED
* dir
)
1271 struct dirent_uncached
*regentry
;
1272 struct dircache_entry
*ce
;
1277 if (dir
->regulardir
!= NULL
)
1279 regentry
= readdir_uncached(dir
->regulardir
);
1280 if (regentry
== NULL
)
1283 strlcpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
);
1284 dir
->secondary_entry
.size
= regentry
->size
;
1285 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1286 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1287 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1288 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1289 dir
->secondary_entry
.next
= NULL
;
1291 return &dir
->secondary_entry
;
1295 if (dir
->entry
== NULL
)
1299 if (ce
->name_len
== 0)
1300 dir
->entry
= ce
->next
;
1301 } while (ce
->name_len
== 0) ;
1303 dir
->entry
= ce
->next
;
1305 strlcpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
);
1306 /* Can't do `dir->secondary_entry = *ce`
1307 because that modifies the d_name pointer. */
1308 dir
->secondary_entry
.size
= ce
->size
;
1309 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1310 dir
->secondary_entry
.attribute
= ce
->attribute
;
1311 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1312 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1313 dir
->secondary_entry
.next
= NULL
;
1314 dir
->internal_entry
= ce
;
1316 //logf("-> %s", ce->name);
1317 return &dir
->secondary_entry
;
1320 int closedir_cached(DIR_CACHED
* dir
)
1326 if (dir
->regulardir
!= NULL
)
1327 return closedir_uncached(dir
->regulardir
);
1332 int mkdir_cached(const char *name
)
1334 int rc
=mkdir_uncached(name
);
1336 dircache_mkdir(name
);
1340 int rmdir_cached(const char* name
)
1342 int rc
=rmdir_uncached(name
);
1344 dircache_rmdir(name
);