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.
394 * get_before: Returns the cache pointer before the last valid entry found.
395 * only_directories: Match only filenames which are a directory type.
397 static struct dircache_entry
* dircache_get_entry(const char *path
,
398 bool get_before
, bool only_directories
)
400 struct dircache_entry
*cache_entry
, *before
;
401 char namecopy
[MAX_PATH
*2];
405 strlcpy(namecopy
, path
, sizeof(namecopy
));
406 cache_entry
= dircache_root
;
409 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
410 part
= strtok_r(NULL
, "/", &end
)) {
412 /* scan dir for name */
415 if (cache_entry
== NULL
)
419 else if (cache_entry
->name_len
== 0)
421 cache_entry
= cache_entry
->next
;
425 if (!strcasecmp(part
, cache_entry
->d_name
))
427 before
= cache_entry
;
428 if (cache_entry
->down
|| only_directories
)
429 cache_entry
= cache_entry
->down
;
433 cache_entry
= cache_entry
->next
;
438 cache_entry
= before
;
443 #ifdef HAVE_EEPROM_SETTINGS
445 * Function to load the internal cache structure from disk to initialize
446 * the dircache really fast and little disk access.
448 int dircache_load(void)
450 struct dircache_maindata maindata
;
454 if (dircache_initialized
)
457 logf("Loading directory cache");
460 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
464 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
465 if (bytes_read
!= sizeof(struct dircache_maindata
)
466 || maindata
.size
<= 0)
468 logf("Dircache file header error");
470 remove(DIRCACHE_FILE
);
474 dircache_root
= buffer_alloc(0);
475 if ((long)maindata
.root_entry
!= (long)dircache_root
)
477 logf("Position missmatch");
479 remove(DIRCACHE_FILE
);
483 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
484 entry_count
= maindata
.entry_count
;
485 appflags
= maindata
.appflags
;
486 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
488 remove(DIRCACHE_FILE
);
490 if (bytes_read
!= maindata
.size
)
492 logf("Dircache read failed");
496 /* Cache successfully loaded. */
497 dircache_size
= maindata
.size
;
498 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
500 logf("Done, %ld KiB used", dircache_size
/ 1024);
501 dircache_initialized
= true;
502 memset(fd_bindings
, 0, sizeof(fd_bindings
));
508 * Function to save the internal cache stucture to disk for fast loading
511 int dircache_save(void)
513 struct dircache_maindata maindata
;
515 unsigned long bytes_written
;
517 remove(DIRCACHE_FILE
);
519 if (!dircache_initialized
)
522 logf("Saving directory cache");
523 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
525 maindata
.magic
= DIRCACHE_MAGIC
;
526 maindata
.size
= dircache_size
;
527 maindata
.root_entry
= dircache_root
;
528 maindata
.entry_count
= entry_count
;
529 maindata
.appflags
= appflags
;
531 /* Save the info structure */
532 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
533 if (bytes_written
!= sizeof(struct dircache_maindata
))
536 logf("dircache: write failed #1");
540 /* Dump whole directory cache to disk */
541 bytes_written
= write(fd
, dircache_root
, dircache_size
);
543 if (bytes_written
!= dircache_size
)
545 logf("dircache: write failed #2");
554 * Internal function which scans the disk and creates the dircache structure.
556 static int dircache_do_rebuild(void)
561 struct fat_dir dir
, *pdir
;
563 unsigned int start_tick
;
566 /* Measure how long it takes build the cache. */
567 start_tick
= current_tick
;
568 dircache_initializing
= true;
572 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
573 dircache_size
= sizeof(struct dircache_entry
);
575 #ifdef HAVE_MULTIVOLUME
576 append_position
= dircache_root
;
578 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
580 if (fat_ismounted(i
))
584 pdir
= opendir_uncached("/");
587 logf("Failed to open rootdir");
588 dircache_initializing
= false;
592 #ifdef HAVE_MULTIVOLUME
593 if ( fat_opendir(IF_MV2(i
,) &dir
, 0, NULL
) < 0 ) {
595 if ( fat_opendir(IF_MV2(0,) &dir
, 0, NULL
) < 0 ) {
596 #endif /* HAVE_MULTIVOLUME */
597 logf("Failed opening root dir");
598 dircache_initializing
= false;
604 #ifdef HAVE_MULTIVOLUME
605 if (dircache_travel(IF_MV2(i
,) pdir
, append_position
) < 0)
607 if (dircache_travel(IF_MV2(0,) pdir
, dircache_root
) < 0)
608 #endif /* HAVE_MULTIVOLUME */
610 logf("dircache_travel failed");
613 dircache_initializing
= false;
617 #ifdef HAVE_MULTIVOLUME
622 logf("Done, %ld KiB used", dircache_size
/ 1024);
624 dircache_initialized
= true;
625 dircache_initializing
= false;
626 cache_build_ticks
= current_tick
- start_tick
;
628 /* Initialized fd bindings. */
629 memset(fd_bindings
, 0, sizeof(fd_bindings
));
630 for (i
= 0; i
< fdbind_idx
; i
++)
631 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
636 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
637 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
641 /* We have to long align the audiobuf to keep the buffer access fast. */
642 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
643 audiobuf
+= DIRCACHE_RESERVE
;
644 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
652 * Internal thread that controls transparent cache building.
654 static void dircache_thread(void)
656 struct queue_event ev
;
660 queue_wait(&dircache_queue
, &ev
);
666 if (!dircache_initialized
)
668 dircache_initialized
= false;
671 thread_enabled
= true;
672 dircache_do_rebuild();
673 thread_enabled
= false;
677 logf("Stopped the rebuilding.");
678 dircache_initialized
= false;
682 case SYS_USB_CONNECTED
:
683 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
684 usb_wait_for_disconnect(&dircache_queue
);
692 * Start scanning the disk to build the dircache.
693 * Either transparent or non-transparent build method is used.
695 int dircache_build(int last_size
)
697 if (dircache_initialized
|| thread_enabled
)
700 logf("Building directory cache");
701 remove(DIRCACHE_FILE
);
703 /* Background build, dircache has been previously allocated */
704 if (dircache_size
> 0)
706 thread_enabled
= true;
707 dircache_initializing
= true;
708 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
712 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
714 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
715 dircache_root
= buffer_alloc(allocated_size
);
716 thread_enabled
= true;
718 /* Start a transparent rebuild. */
719 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
723 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
725 /* Start a non-transparent rebuild. */
726 return dircache_do_rebuild();
730 * Steal the allocated dircache buffer and disable dircache.
732 void* dircache_steal_buffer(long *size
)
735 if (dircache_size
== 0)
741 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
743 return dircache_root
;
747 * Main initialization function that must be called before any other
748 * operations within the dircache.
750 void dircache_init(void)
754 dircache_initialized
= false;
755 dircache_initializing
= false;
757 memset(opendirs
, 0, sizeof(opendirs
));
758 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
760 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
763 queue_init(&dircache_queue
, true);
764 create_thread(dircache_thread
, dircache_stack
,
765 sizeof(dircache_stack
), 0, dircache_thread_name
766 IF_PRIO(, PRIORITY_BACKGROUND
)
771 * Returns true if dircache has been initialized and is ready to be used.
773 bool dircache_is_enabled(void)
775 return dircache_initialized
;
779 * Returns true if dircache is being initialized.
781 bool dircache_is_initializing(void)
783 return dircache_initializing
|| thread_enabled
;
787 * Set application flags used to determine if dircache is still intact.
789 void dircache_set_appflag(long mask
)
795 * Get application flags used to determine if dircache is still intact.
797 bool dircache_get_appflag(long mask
)
799 return dircache_is_enabled() && (appflags
& mask
);
803 * Returns the current number of entries (directories and files) in the cache.
805 int dircache_get_entry_count(void)
811 * Returns the allocated space for dircache (without reserve space).
813 int dircache_get_cache_size(void)
815 return dircache_is_enabled() ? dircache_size
: 0;
819 * Returns how many bytes of the reserve allocation for live cache
820 * updates have been used.
822 int dircache_get_reserve_used(void)
824 return dircache_is_enabled() ? reserve_used
: 0;
828 * Returns the time in kernel ticks that took to build the cache.
830 int dircache_get_build_ticks(void)
832 return dircache_is_enabled() ? cache_build_ticks
: 0;
836 * Disables the dircache. Usually called on shutdown or when
837 * accepting a usb connection.
839 void dircache_disable(void)
845 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
847 while (thread_enabled
)
849 dircache_initialized
= false;
851 logf("Waiting for cached dirs to release");
853 cache_in_use
= false;
854 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
855 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
862 } while (cache_in_use
) ;
864 logf("Cache released");
869 * Usermode function to return dircache_entry pointer to the given path.
871 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
873 if (!dircache_initialized
|| filename
== NULL
)
876 return dircache_get_entry(filename
, false, false);
880 * Function to copy the full absolute path from dircache to the given buffer
881 * using the given dircache_entry pointer.
883 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
885 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
900 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
904 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
905 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
906 size
-= down
[depth
]->name_len
;
912 /* --- Directory cache live updating functions --- */
913 static int block_until_ready(void)
915 /* Block until dircache has been built. */
916 while (!dircache_initialized
&& dircache_is_initializing())
919 if (!dircache_initialized
)
925 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
927 struct dircache_entry
*entry
;
928 char basedir
[MAX_PATH
*2];
930 long last_cache_size
= dircache_size
;
932 strlcpy(basedir
, path
, sizeof(basedir
));
933 new = strrchr(basedir
, '/');
936 logf("error occurred");
937 dircache_initialized
= false;
944 entry
= dircache_get_entry(basedir
, false, true);
947 logf("basedir not found!");
949 dircache_initialized
= false;
953 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
956 logf("not enough space");
957 dircache_initialized
= false;
961 while (entry
->next
!= NULL
)
964 if (entry
->name_len
> 0)
965 entry
= dircache_gen_next(entry
);
969 dircache_initialized
= false;
973 entry
->attribute
= attribute
;
974 entry
->name_len
= MIN(254, strlen(new)) + 1;
975 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
976 entry
->startcluster
= 0;
980 memcpy(entry
->d_name
, new, entry
->name_len
);
981 dircache_size
+= entry
->name_len
;
983 if (attribute
& ATTR_DIRECTORY
)
986 dircache_gen_down(entry
);
989 reserve_used
+= dircache_size
- last_cache_size
;
994 void dircache_bind(int fd
, const char *path
)
996 struct dircache_entry
*entry
;
998 /* Queue requests until dircache has been built. */
999 if (!dircache_initialized
&& dircache_is_initializing())
1001 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
1003 strlcpy(fdbind_cache
[fdbind_idx
].path
, path
,
1004 sizeof(fdbind_cache
[fdbind_idx
].path
));
1005 fdbind_cache
[fdbind_idx
].fd
= fd
;
1010 if (!dircache_initialized
)
1013 logf("bind: %d/%s", fd
, path
);
1014 entry
= dircache_get_entry(path
, false, false);
1018 dircache_initialized
= false;
1022 fd_bindings
[fd
] = entry
;
1025 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1027 if (!dircache_initialized
|| fd
< 0)
1030 if (fd_bindings
[fd
] == NULL
)
1032 logf("dircache fd access error");
1033 dircache_initialized
= false;
1037 fd_bindings
[fd
]->size
= newsize
;
1038 fd_bindings
[fd
]->startcluster
= startcluster
;
1040 void dircache_update_filetime(int fd
)
1046 struct tm
*now
= get_time();
1047 if (!dircache_initialized
|| fd
< 0)
1050 if (fd_bindings
[fd
] == NULL
)
1052 logf("dircache fd access error");
1053 dircache_initialized
= false;
1056 year
= now
->tm_year
+1900-1980;
1057 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1058 (((now
->tm_mon
+1)&0xf)<<5) |
1059 (((now
->tm_mday
)&0x1f));
1060 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1061 (((now
->tm_min
)&0x3f)<<5) |
1062 (((now
->tm_sec
/2)&0x1f));
1066 void dircache_mkdir(const char *path
)
1068 if (block_until_ready())
1071 logf("mkdir: %s", path
);
1072 dircache_new_entry(path
, ATTR_DIRECTORY
);
1075 void dircache_rmdir(const char *path
)
1077 struct dircache_entry
*entry
;
1079 if (block_until_ready())
1082 logf("rmdir: %s", path
);
1083 entry
= dircache_get_entry(path
, true, true);
1087 dircache_initialized
= false;
1092 entry
->name_len
= 0;
1095 /* Remove a file from cache */
1096 void dircache_remove(const char *name
)
1098 struct dircache_entry
*entry
;
1100 if (block_until_ready())
1103 logf("remove: %s", name
);
1105 entry
= dircache_get_entry(name
, false, false);
1110 dircache_initialized
= false;
1114 entry
->name_len
= 0;
1117 void dircache_rename(const char *oldpath
, const char *newpath
)
1119 struct dircache_entry
*entry
, *newentry
;
1120 struct dircache_entry oldentry
;
1121 char absolute_path
[MAX_PATH
*2];
1124 if (block_until_ready())
1127 logf("rename: %s->%s", oldpath
, newpath
);
1129 entry
= dircache_get_entry(oldpath
, true, false);
1133 dircache_initialized
= false;
1137 /* Delete the old entry. */
1138 entry
->name_len
= 0;
1140 /** If we rename the same filename twice in a row, we need to
1141 * save the data, because the entry will be re-used. */
1144 /* Generate the absolute path for destination if necessary. */
1145 if (newpath
[0] != '/')
1147 strlcpy(absolute_path
, oldpath
, sizeof(absolute_path
));
1148 p
= strrchr(absolute_path
, '/');
1151 logf("Invalid path");
1152 dircache_initialized
= false;
1157 strlcpy(p
, absolute_path
, sizeof(absolute_path
)-strlen(p
));
1158 newpath
= absolute_path
;
1161 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1162 if (newentry
== NULL
)
1164 dircache_initialized
= false;
1168 newentry
->down
= oldentry
.down
;
1169 newentry
->size
= oldentry
.size
;
1170 newentry
->startcluster
= oldentry
.startcluster
;
1171 newentry
->wrttime
= oldentry
.wrttime
;
1172 newentry
->wrtdate
= oldentry
.wrtdate
;
1175 void dircache_add_file(const char *path
, long startcluster
)
1177 struct dircache_entry
*entry
;
1179 if (block_until_ready())
1182 logf("add file: %s", path
);
1183 entry
= dircache_new_entry(path
, 0);
1187 entry
->startcluster
= startcluster
;
1190 DIR_CACHED
* opendir_cached(const char* name
)
1192 struct dircache_entry
*cache_entry
;
1194 DIR_CACHED
* pdir
= opendirs
;
1196 if ( name
[0] != '/' )
1198 DEBUGF("Only absolute paths supported right now\n");
1202 /* find a free dir descriptor */
1203 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1207 if ( dd
== MAX_OPEN_DIRS
)
1209 DEBUGF("Too many dirs open\n");
1214 if (!dircache_initialized
)
1216 pdir
->regulardir
= opendir_uncached(name
);
1217 if (!pdir
->regulardir
)
1225 pdir
->regulardir
= NULL
;
1226 cache_entry
= dircache_get_entry(name
, false, true);
1227 pdir
->entry
= cache_entry
;
1229 if (cache_entry
== NULL
)
1238 struct dircache_entry
* readdir_cached(DIR_CACHED
* dir
)
1240 struct dirent_uncached
*regentry
;
1241 struct dircache_entry
*ce
;
1246 if (dir
->regulardir
!= NULL
)
1248 regentry
= readdir_uncached(dir
->regulardir
);
1249 if (regentry
== NULL
)
1252 strlcpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
);
1253 dir
->secondary_entry
.size
= regentry
->size
;
1254 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1255 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1256 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1257 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1258 dir
->secondary_entry
.next
= NULL
;
1260 return &dir
->secondary_entry
;
1264 if (dir
->entry
== NULL
)
1268 if (ce
->name_len
== 0)
1269 dir
->entry
= ce
->next
;
1270 } while (ce
->name_len
== 0) ;
1272 dir
->entry
= ce
->next
;
1274 strlcpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
);
1275 /* Can't do `dir->secondary_entry = *ce`
1276 because that modifies the d_name pointer. */
1277 dir
->secondary_entry
.size
= ce
->size
;
1278 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1279 dir
->secondary_entry
.attribute
= ce
->attribute
;
1280 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1281 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1282 dir
->secondary_entry
.next
= NULL
;
1283 dir
->internal_entry
= ce
;
1285 //logf("-> %s", ce->name);
1286 return &dir
->secondary_entry
;
1289 int closedir_cached(DIR_CACHED
* dir
)
1295 if (dir
->regulardir
!= NULL
)
1296 return closedir_uncached(dir
->regulardir
);
1301 int mkdir_cached(const char *name
)
1303 int rc
=mkdir_uncached(name
);
1305 dircache_mkdir(name
);
1309 int rmdir_cached(const char* name
)
1311 int rc
=rmdir_uncached(name
);
1313 dircache_rmdir(name
);