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 #define MAX_OPEN_DIRS 8
53 static DIR_CACHED opendirs
[MAX_OPEN_DIRS
];
55 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
56 static struct dircache_entry
*dircache_root
;
57 #ifdef HAVE_MULTIVOLUME
58 static struct dircache_entry
*append_position
;
61 static bool dircache_initialized
= false;
62 static bool dircache_initializing
= false;
63 static bool thread_enabled
= false;
64 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
65 static unsigned long dircache_size
= 0;
66 static unsigned long entry_count
= 0;
67 static unsigned long reserve_used
= 0;
68 static unsigned int cache_build_ticks
= 0;
69 static unsigned long appflags
= 0;
70 static char dircache_cur_path
[MAX_PATH
*2];
72 static struct event_queue dircache_queue
;
73 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x900)/sizeof(long)];
74 static const char dircache_thread_name
[] = "dircache";
76 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
77 static int fdbind_idx
= 0;
79 /* --- Internal cache structure control functions --- */
82 * Internal function to allocate a new dircache_entry from memory.
84 static struct dircache_entry
* allocate_entry(void)
86 struct dircache_entry
*next_entry
;
88 if (dircache_size
> allocated_size
- MAX_PATH
*2)
90 logf("size limit reached");
94 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
95 #ifdef ROCKBOX_STRICT_ALIGN
96 /* Make sure the entry is long aligned. */
97 if ((long)next_entry
& 0x03)
99 dircache_size
+= 4 - ((long)next_entry
& 0x03);
100 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
103 next_entry
->name_len
= 0;
104 next_entry
->d_name
= NULL
;
105 next_entry
->up
= NULL
;
106 next_entry
->down
= NULL
;
107 next_entry
->next
= NULL
;
109 dircache_size
+= sizeof(struct dircache_entry
);
115 * Internal function to allocate a dircache_entry and set
116 * ->next entry pointers.
118 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
120 struct dircache_entry
*next_entry
;
122 if ( (next_entry
= allocate_entry()) == NULL
)
124 next_entry
->up
= ce
->up
;
125 ce
->next
= next_entry
;
131 * Internal function to allocate a dircache_entry and set
132 * ->down entry pointers.
134 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
136 struct dircache_entry
*next_entry
;
138 if ( (next_entry
= allocate_entry()) == NULL
)
141 ce
->down
= next_entry
;
146 /* This will eat ~30 KiB of memory!
147 * We should probably use that as additional reserve buffer in future. */
148 #define MAX_SCAN_DEPTH 16
149 static struct travel_data dir_recursion
[MAX_SCAN_DEPTH
];
152 * Returns true if there is an event waiting in the queue
153 * that requires the current operation to be aborted.
155 static bool check_event_queue(void)
157 struct queue_event ev
;
159 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
163 case SYS_USB_CONNECTED
:
167 /* Put the event back into the queue. */
168 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
176 * Internal function to iterate a path.
178 static int dircache_scan(IF_MV2(int volume
,) struct travel_data
*td
)
181 while ( ( td
->entry
= readdir_uncached(td
->dir
) ) )
183 while ( (fat_getnext(td
->dir
, &td
->entry
) >= 0) && (td
->entry
.name
[0]))
187 if (!strcmp(".", td
->entry
->d_name
) ||
188 !strcmp("..", td
->entry
->d_name
))
193 td
->ce
->attribute
= td
->entry
->attribute
;
194 td
->ce
->name_len
= strlen(td
->entry
->d_name
) + 1;
195 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
196 td
->ce
->size
= td
->entry
->size
;
197 td
->ce
->wrtdate
= td
->entry
->wrtdate
;
198 td
->ce
->wrttime
= td
->entry
->wrttime
;
199 memcpy(td
->ce
->d_name
, td
->entry
->d_name
, td
->ce
->name_len
);
201 if (!strcmp(".", td
->entry
.name
) ||
202 !strcmp("..", td
->entry
.name
))
207 td
->ce
->attribute
= td
->entry
.attr
;
208 td
->ce
->name_len
= strlen(td
->entry
.name
) + 1;
209 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
210 td
->ce
->startcluster
= td
->entry
.firstcluster
;
211 td
->ce
->size
= td
->entry
.filesize
;
212 td
->ce
->wrtdate
= td
->entry
.wrtdate
;
213 td
->ce
->wrttime
= td
->entry
.wrttime
;
214 memcpy(td
->ce
->d_name
, td
->entry
.name
, td
->ce
->name_len
);
216 dircache_size
+= td
->ce
->name_len
;
220 if (td
->entry
->attribute
& ATTR_DIRECTORY
)
222 if (td
->entry
.attr
& FAT_ATTR_DIRECTORY
)
226 td
->down_entry
= dircache_gen_down(td
->ce
);
227 if (td
->down_entry
== NULL
)
230 td
->pathpos
= strlen(dircache_cur_path
);
231 strncpy(&dircache_cur_path
[td
->pathpos
], "/",
232 sizeof(dircache_cur_path
) - td
->pathpos
- 1);
234 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
->d_name
,
235 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
237 td
->newdir
= opendir_uncached(dircache_cur_path
);
238 if (td
->newdir
== NULL
)
240 logf("Failed to opendir_uncached(): %s", dircache_cur_path
);
244 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
.name
,
245 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
247 td
->newdir
= *td
->dir
;
248 if (fat_opendir(IF_MV2(volume
,) &td
->newdir
,
249 td
->entry
.firstcluster
, td
->dir
) < 0 )
255 td
->ce
= dircache_gen_next(td
->ce
);
263 td
->ce
= dircache_gen_next(td
->ce
);
267 /* When simulator is used, it's only safe to yield here. */
270 /* Stop if we got an external signal. */
271 if (check_event_queue())
282 * Recursively scan the hard disk and build the cache.
285 static int dircache_travel(IF_MV2(int volume
,) DIR_UNCACHED
*dir
, struct dircache_entry
*ce
)
287 static int dircache_travel(IF_MV2(int volume
,) struct fat_dir
*dir
, struct dircache_entry
*ce
)
293 memset(ce
, 0, sizeof(struct dircache_entry
));
295 #if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR)
298 ce
->d_name
= ((char *)dircache_root
+dircache_size
);
299 snprintf(ce
->d_name
, VOL_ENUM_POS
+ 3, VOL_NAMES
, volume
);
300 ce
->name_len
= VOL_ENUM_POS
+ 3;
301 dircache_size
+= ce
->name_len
;
302 ce
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
304 append_position
= dircache_gen_next(ce
);
305 ce
= dircache_gen_down(ce
);
309 dir_recursion
[0].dir
= dir
;
310 dir_recursion
[0].ce
= ce
;
311 dir_recursion
[0].first
= ce
;
314 //logf("=> %s", dircache_cur_path);
315 result
= dircache_scan(IF_MV2(volume
,) &dir_recursion
[depth
]);
317 case 0: /* Leaving the current directory. */
318 /* Add the standard . and .. entries. */
319 ce
= dir_recursion
[depth
].ce
;
323 closedir_uncached(dir_recursion
[depth
].dir
);
324 ce
->attribute
= ATTR_DIRECTORY
;
326 ce
->attribute
= FAT_ATTR_DIRECTORY
;
327 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
330 ce
->down
= dir_recursion
[depth
].first
;
336 dircache_cur_path
[dir_recursion
[depth
].pathpos
] = '\0';
338 ce
= dircache_gen_next(ce
);
341 logf("memory allocation error");
345 ce
->attribute
= ATTR_DIRECTORY
;
347 ce
->attribute
= FAT_ATTR_DIRECTORY
;
348 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
353 ce
->down
= dir_recursion
[depth
].first
;
357 case 1: /* Going down in the directory tree. */
359 if (depth
>= MAX_SCAN_DEPTH
)
361 logf("Too deep directory structure");
366 dir_recursion
[depth
].dir
= dir_recursion
[depth
-1].newdir
;
368 dir_recursion
[depth
].dir
= &dir_recursion
[depth
-1].newdir
;
370 dir_recursion
[depth
].first
= dir_recursion
[depth
-1].down_entry
;
371 dir_recursion
[depth
].ce
= dir_recursion
[depth
-1].down_entry
;
376 logf("-> %s", dircache_cur_path
);
379 } while (depth
>= 0) ;
385 * Internal function to get a pointer to dircache_entry for a given filename.
386 * path: Absolute path to a file or directory.
387 * get_before: Returns the cache pointer before the last valid entry found.
388 * only_directories: Match only filenames which are a directory type.
390 static struct dircache_entry
* dircache_get_entry(const char *path
,
391 bool get_before
, bool only_directories
)
393 struct dircache_entry
*cache_entry
, *before
;
394 char namecopy
[MAX_PATH
*2];
398 strncpy(namecopy
, path
, sizeof(namecopy
) - 1);
399 cache_entry
= dircache_root
;
402 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
403 part
= strtok_r(NULL
, "/", &end
)) {
405 /* scan dir for name */
408 if (cache_entry
== NULL
)
412 else if (cache_entry
->name_len
== 0)
414 cache_entry
= cache_entry
->next
;
418 if (!strcasecmp(part
, cache_entry
->d_name
))
420 before
= cache_entry
;
421 if (cache_entry
->down
|| only_directories
)
422 cache_entry
= cache_entry
->down
;
426 cache_entry
= cache_entry
->next
;
431 cache_entry
= before
;
436 #ifdef HAVE_EEPROM_SETTINGS
438 * Function to load the internal cache structure from disk to initialize
439 * the dircache really fast and little disk access.
441 int dircache_load(void)
443 struct dircache_maindata maindata
;
447 if (dircache_initialized
)
450 logf("Loading directory cache");
453 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
457 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
458 if (bytes_read
!= sizeof(struct dircache_maindata
)
459 || maindata
.size
<= 0)
461 logf("Dircache file header error");
463 remove(DIRCACHE_FILE
);
467 dircache_root
= buffer_alloc(0);
468 if ((long)maindata
.root_entry
!= (long)dircache_root
)
470 logf("Position missmatch");
472 remove(DIRCACHE_FILE
);
476 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
477 entry_count
= maindata
.entry_count
;
478 appflags
= maindata
.appflags
;
479 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
481 remove(DIRCACHE_FILE
);
483 if (bytes_read
!= maindata
.size
)
485 logf("Dircache read failed");
489 /* Cache successfully loaded. */
490 dircache_size
= maindata
.size
;
491 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
493 logf("Done, %ld KiB used", dircache_size
/ 1024);
494 dircache_initialized
= true;
495 memset(fd_bindings
, 0, sizeof(fd_bindings
));
501 * Function to save the internal cache stucture to disk for fast loading
504 int dircache_save(void)
506 struct dircache_maindata maindata
;
508 unsigned long bytes_written
;
510 remove(DIRCACHE_FILE
);
512 if (!dircache_initialized
)
515 logf("Saving directory cache");
516 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
518 maindata
.magic
= DIRCACHE_MAGIC
;
519 maindata
.size
= dircache_size
;
520 maindata
.root_entry
= dircache_root
;
521 maindata
.entry_count
= entry_count
;
522 maindata
.appflags
= appflags
;
524 /* Save the info structure */
525 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
526 if (bytes_written
!= sizeof(struct dircache_maindata
))
529 logf("dircache: write failed #1");
533 /* Dump whole directory cache to disk */
534 bytes_written
= write(fd
, dircache_root
, dircache_size
);
536 if (bytes_written
!= dircache_size
)
538 logf("dircache: write failed #2");
547 * Internal function which scans the disk and creates the dircache structure.
549 static int dircache_do_rebuild(void)
554 struct fat_dir dir
, *pdir
;
556 unsigned int start_tick
;
559 /* Measure how long it takes build the cache. */
560 start_tick
= current_tick
;
561 dircache_initializing
= true;
565 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
566 dircache_size
= sizeof(struct dircache_entry
);
568 #ifdef HAVE_MULTIVOLUME
569 append_position
= dircache_root
;
571 for (i
= NUM_VOLUMES
; i
>= 0; i
--)
573 if (fat_ismounted(i
))
577 pdir
= opendir_uncached("/");
580 logf("Failed to open rootdir");
581 dircache_initializing
= false;
585 #ifdef HAVE_MULTIVOLUME
586 if ( fat_opendir(IF_MV2(i
,) &dir
, 0, NULL
) < 0 ) {
588 if ( fat_opendir(IF_MV2(0,) &dir
, 0, NULL
) < 0 ) {
589 #endif /* HAVE_MULTIVOLUME */
590 logf("Failed opening root dir");
591 dircache_initializing
= false;
597 #ifdef HAVE_MULTIVOLUME
598 if (dircache_travel(IF_MV2(i
,) pdir
, append_position
) < 0)
600 if (dircache_travel(IF_MV2(0,) pdir
, dircache_root
) < 0)
601 #endif /* HAVE_MULTIVOLUME */
603 logf("dircache_travel failed");
606 dircache_initializing
= false;
610 #ifdef HAVE_MULTIVOLUME
615 logf("Done, %ld KiB used", dircache_size
/ 1024);
617 dircache_initialized
= true;
618 dircache_initializing
= false;
619 cache_build_ticks
= current_tick
- start_tick
;
621 /* Initialized fd bindings. */
622 memset(fd_bindings
, 0, sizeof(fd_bindings
));
623 for (i
= 0; i
< fdbind_idx
; i
++)
624 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
629 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
630 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
634 /* We have to long align the audiobuf to keep the buffer access fast. */
635 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
636 audiobuf
+= DIRCACHE_RESERVE
;
637 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
645 * Internal thread that controls transparent cache building.
647 static void dircache_thread(void)
649 struct queue_event ev
;
653 queue_wait(&dircache_queue
, &ev
);
659 if (!dircache_initialized
)
661 dircache_initialized
= false;
664 thread_enabled
= true;
665 dircache_do_rebuild();
666 thread_enabled
= false;
670 logf("Stopped the rebuilding.");
671 dircache_initialized
= false;
675 case SYS_USB_CONNECTED
:
676 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
677 usb_wait_for_disconnect(&dircache_queue
);
685 * Start scanning the disk to build the dircache.
686 * Either transparent or non-transparent build method is used.
688 int dircache_build(int last_size
)
690 if (dircache_initialized
|| thread_enabled
)
693 logf("Building directory cache");
694 remove(DIRCACHE_FILE
);
696 /* Background build, dircache has been previously allocated */
697 if (dircache_size
> 0)
699 thread_enabled
= true;
700 dircache_initializing
= true;
701 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
705 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
707 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
708 dircache_root
= buffer_alloc(allocated_size
);
709 thread_enabled
= true;
711 /* Start a transparent rebuild. */
712 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
716 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
718 /* Start a non-transparent rebuild. */
719 return dircache_do_rebuild();
723 * Steal the allocated dircache buffer and disable dircache.
725 void* dircache_steal_buffer(long *size
)
728 if (dircache_size
== 0)
734 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
736 return dircache_root
;
740 * Main initialization function that must be called before any other
741 * operations within the dircache.
743 void dircache_init(void)
747 dircache_initialized
= false;
748 dircache_initializing
= false;
750 memset(opendirs
, 0, sizeof(opendirs
));
751 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
753 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
756 queue_init(&dircache_queue
, true);
757 create_thread(dircache_thread
, dircache_stack
,
758 sizeof(dircache_stack
), 0, dircache_thread_name
759 IF_PRIO(, PRIORITY_BACKGROUND
)
764 * Returns true if dircache has been initialized and is ready to be used.
766 bool dircache_is_enabled(void)
768 return dircache_initialized
;
772 * Returns true if dircache is being initialized.
774 bool dircache_is_initializing(void)
776 return dircache_initializing
|| thread_enabled
;
780 * Set application flags used to determine if dircache is still intact.
782 void dircache_set_appflag(long mask
)
788 * Get application flags used to determine if dircache is still intact.
790 bool dircache_get_appflag(long mask
)
792 return dircache_is_enabled() && (appflags
& mask
);
796 * Returns the current number of entries (directories and files) in the cache.
798 int dircache_get_entry_count(void)
804 * Returns the allocated space for dircache (without reserve space).
806 int dircache_get_cache_size(void)
808 return dircache_is_enabled() ? dircache_size
: 0;
812 * Returns how many bytes of the reserve allocation for live cache
813 * updates have been used.
815 int dircache_get_reserve_used(void)
817 return dircache_is_enabled() ? reserve_used
: 0;
821 * Returns the time in kernel ticks that took to build the cache.
823 int dircache_get_build_ticks(void)
825 return dircache_is_enabled() ? cache_build_ticks
: 0;
829 * Disables the dircache. Usually called on shutdown or when
830 * accepting a usb connection.
832 void dircache_disable(void)
838 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
840 while (thread_enabled
)
842 dircache_initialized
= false;
844 logf("Waiting for cached dirs to release");
846 cache_in_use
= false;
847 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
848 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
855 } while (cache_in_use
) ;
857 logf("Cache released");
862 * Usermode function to return dircache_entry pointer to the given path.
864 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
866 if (!dircache_initialized
|| filename
== NULL
)
869 return dircache_get_entry(filename
, false, false);
873 * Function to copy the full absolute path from dircache to the given buffer
874 * using the given dircache_entry pointer.
876 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
878 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
893 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
897 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
898 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
899 size
-= down
[depth
]->name_len
;
905 /* --- Directory cache live updating functions --- */
906 static int block_until_ready(void)
908 /* Block until dircache has been built. */
909 while (!dircache_initialized
&& dircache_is_initializing())
912 if (!dircache_initialized
)
918 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
920 struct dircache_entry
*entry
;
921 char basedir
[MAX_PATH
*2];
923 long last_cache_size
= dircache_size
;
925 strncpy(basedir
, path
, sizeof(basedir
)-1);
926 new = strrchr(basedir
, '/');
929 logf("error occurred");
930 dircache_initialized
= false;
937 entry
= dircache_get_entry(basedir
, false, true);
940 logf("basedir not found!");
942 dircache_initialized
= false;
946 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
949 logf("not enough space");
950 dircache_initialized
= false;
954 while (entry
->next
!= NULL
)
957 if (entry
->name_len
> 0)
958 entry
= dircache_gen_next(entry
);
962 dircache_initialized
= false;
966 entry
->attribute
= attribute
;
967 entry
->name_len
= MIN(254, strlen(new)) + 1;
968 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
969 entry
->startcluster
= 0;
973 memcpy(entry
->d_name
, new, entry
->name_len
);
974 dircache_size
+= entry
->name_len
;
976 if (attribute
& ATTR_DIRECTORY
)
979 dircache_gen_down(entry
);
982 reserve_used
+= dircache_size
- last_cache_size
;
987 void dircache_bind(int fd
, const char *path
)
989 struct dircache_entry
*entry
;
991 /* Queue requests until dircache has been built. */
992 if (!dircache_initialized
&& dircache_is_initializing())
994 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
996 strncpy(fdbind_cache
[fdbind_idx
].path
, path
,
997 sizeof(fdbind_cache
[fdbind_idx
].path
)-1);
998 fdbind_cache
[fdbind_idx
].fd
= fd
;
1003 if (!dircache_initialized
)
1006 logf("bind: %d/%s", fd
, path
);
1007 entry
= dircache_get_entry(path
, false, false);
1011 dircache_initialized
= false;
1015 fd_bindings
[fd
] = entry
;
1018 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
1020 if (!dircache_initialized
|| fd
< 0)
1023 if (fd_bindings
[fd
] == NULL
)
1025 logf("dircache fd access error");
1026 dircache_initialized
= false;
1030 fd_bindings
[fd
]->size
= newsize
;
1031 fd_bindings
[fd
]->startcluster
= startcluster
;
1033 void dircache_update_filetime(int fd
)
1039 struct tm
*now
= get_time();
1040 if (!dircache_initialized
|| fd
< 0)
1043 if (fd_bindings
[fd
] == NULL
)
1045 logf("dircache fd access error");
1046 dircache_initialized
= false;
1049 year
= now
->tm_year
+1900-1980;
1050 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1051 (((now
->tm_mon
+1)&0xf)<<5) |
1052 (((now
->tm_mday
)&0x1f));
1053 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1054 (((now
->tm_min
)&0x3f)<<5) |
1055 (((now
->tm_sec
/2)&0x1f));
1059 void dircache_mkdir(const char *path
)
1061 if (block_until_ready())
1064 logf("mkdir: %s", path
);
1065 dircache_new_entry(path
, ATTR_DIRECTORY
);
1068 void dircache_rmdir(const char *path
)
1070 struct dircache_entry
*entry
;
1072 if (block_until_ready())
1075 logf("rmdir: %s", path
);
1076 entry
= dircache_get_entry(path
, true, true);
1080 dircache_initialized
= false;
1085 entry
->name_len
= 0;
1088 /* Remove a file from cache */
1089 void dircache_remove(const char *name
)
1091 struct dircache_entry
*entry
;
1093 if (block_until_ready())
1096 logf("remove: %s", name
);
1098 entry
= dircache_get_entry(name
, false, false);
1103 dircache_initialized
= false;
1107 entry
->name_len
= 0;
1110 void dircache_rename(const char *oldpath
, const char *newpath
)
1112 struct dircache_entry
*entry
, *newentry
;
1113 struct dircache_entry oldentry
;
1114 char absolute_path
[MAX_PATH
*2];
1117 if (block_until_ready())
1120 logf("rename: %s->%s", oldpath
, newpath
);
1122 entry
= dircache_get_entry(oldpath
, true, false);
1126 dircache_initialized
= false;
1130 /* Delete the old entry. */
1131 entry
->name_len
= 0;
1133 /** If we rename the same filename twice in a row, we need to
1134 * save the data, because the entry will be re-used. */
1137 /* Generate the absolute path for destination if necessary. */
1138 if (newpath
[0] != '/')
1140 strncpy(absolute_path
, oldpath
, sizeof(absolute_path
)-1);
1141 p
= strrchr(absolute_path
, '/');
1144 logf("Invalid path");
1145 dircache_initialized
= false;
1150 strncpy(p
, absolute_path
, sizeof(absolute_path
)-1-strlen(p
));
1151 newpath
= absolute_path
;
1154 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1155 if (newentry
== NULL
)
1157 dircache_initialized
= false;
1161 newentry
->down
= oldentry
.down
;
1162 newentry
->size
= oldentry
.size
;
1163 newentry
->startcluster
= oldentry
.startcluster
;
1164 newentry
->wrttime
= oldentry
.wrttime
;
1165 newentry
->wrtdate
= oldentry
.wrtdate
;
1168 void dircache_add_file(const char *path
, long startcluster
)
1170 struct dircache_entry
*entry
;
1172 if (block_until_ready())
1175 logf("add file: %s", path
);
1176 entry
= dircache_new_entry(path
, 0);
1180 entry
->startcluster
= startcluster
;
1183 DIR_CACHED
* opendir_cached(const char* name
)
1185 struct dircache_entry
*cache_entry
;
1187 DIR_CACHED
* pdir
= opendirs
;
1189 if ( name
[0] != '/' )
1191 DEBUGF("Only absolute paths supported right now\n");
1195 /* find a free dir descriptor */
1196 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1200 if ( dd
== MAX_OPEN_DIRS
)
1202 DEBUGF("Too many dirs open\n");
1207 if (!dircache_initialized
)
1209 pdir
->regulardir
= opendir_uncached(name
);
1210 if (!pdir
->regulardir
)
1218 pdir
->regulardir
= NULL
;
1219 cache_entry
= dircache_get_entry(name
, false, true);
1220 pdir
->entry
= cache_entry
;
1222 if (cache_entry
== NULL
)
1231 struct dircache_entry
* readdir_cached(DIR_CACHED
* dir
)
1233 struct dirent_uncached
*regentry
;
1234 struct dircache_entry
*ce
;
1239 if (dir
->regulardir
!= NULL
)
1241 regentry
= readdir_uncached(dir
->regulardir
);
1242 if (regentry
== NULL
)
1245 strncpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
-1);
1246 dir
->secondary_entry
.size
= regentry
->size
;
1247 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1248 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1249 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1250 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1251 dir
->secondary_entry
.next
= NULL
;
1253 return &dir
->secondary_entry
;
1257 if (dir
->entry
== NULL
)
1261 if (ce
->name_len
== 0)
1262 dir
->entry
= ce
->next
;
1263 } while (ce
->name_len
== 0) ;
1265 dir
->entry
= ce
->next
;
1267 strncpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
-1);
1268 /* Can't do `dir->secondary_entry = *ce`
1269 because that modifies the d_name pointer. */
1270 dir
->secondary_entry
.size
= ce
->size
;
1271 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1272 dir
->secondary_entry
.attribute
= ce
->attribute
;
1273 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1274 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1275 dir
->secondary_entry
.next
= NULL
;
1276 dir
->internal_entry
= ce
;
1278 //logf("-> %s", ce->name);
1279 return &dir
->secondary_entry
;
1282 int closedir_cached(DIR_CACHED
* dir
)
1288 if (dir
->regulardir
!= NULL
)
1289 return closedir_uncached(dir
->regulardir
);
1294 int mkdir_cached(const char *name
)
1296 int rc
=mkdir_uncached(name
);
1298 dircache_mkdir(name
);
1302 int rmdir_cached(const char* name
)
1304 int rc
=rmdir_uncached(name
);
1306 dircache_rmdir(name
);