1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 - Allow cache live updating while transparent rebuild is running.
43 #define DIRCACHE_BUILD 1
44 #define DIRCACHE_STOP 2
46 #define MAX_OPEN_DIRS 8
47 DIRCACHED opendirs
[MAX_OPEN_DIRS
];
49 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
50 static struct dircache_entry
*dircache_root
;
52 static bool dircache_initialized
= false;
53 static bool dircache_initializing
= false;
54 static bool thread_enabled
= false;
55 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
56 static unsigned long dircache_size
= 0;
57 static unsigned long entry_count
= 0;
58 static unsigned long reserve_used
= 0;
59 static unsigned int cache_build_ticks
= 0;
60 static char dircache_cur_path
[MAX_PATH
*2];
62 static struct event_queue dircache_queue
;
63 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x900)/sizeof(long)];
64 static const char dircache_thread_name
[] = "dircache";
66 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
67 static int fdbind_idx
= 0;
69 /* --- Internal cache structure control functions --- */
72 * Internal function to allocate a new dircache_entry from memory.
74 static struct dircache_entry
* allocate_entry(void)
76 struct dircache_entry
*next_entry
;
78 if (dircache_size
> allocated_size
- MAX_PATH
*2)
80 logf("size limit reached");
84 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
85 #ifdef ROCKBOX_STRICT_ALIGN
86 /* Make sure the entry is long aligned. */
87 if ((long)next_entry
& 0x03)
89 dircache_size
+= 4 - ((long)next_entry
& 0x03);
90 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
93 next_entry
->name_len
= 0;
94 next_entry
->d_name
= NULL
;
95 next_entry
->up
= NULL
;
96 next_entry
->down
= NULL
;
97 next_entry
->next
= NULL
;
99 dircache_size
+= sizeof(struct dircache_entry
);
105 * Internal function to allocate a dircache_entry and set
106 * ->next entry pointers.
108 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
110 struct dircache_entry
*next_entry
;
112 if ( (next_entry
= allocate_entry()) == NULL
)
114 next_entry
->up
= ce
->up
;
115 ce
->next
= next_entry
;
121 * Internal function to allocate a dircache_entry and set
122 * ->down entry pointers.
124 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
126 struct dircache_entry
*next_entry
;
128 if ( (next_entry
= allocate_entry()) == NULL
)
131 ce
->down
= next_entry
;
136 /* This will eat ~30 KiB of memory!
137 * We should probably use that as additional reserve buffer in future. */
138 #define MAX_SCAN_DEPTH 16
139 static struct travel_data dir_recursion
[MAX_SCAN_DEPTH
];
142 * Returns true if there is an event waiting in the queue
143 * that requires the current operation to be aborted.
145 static bool check_event_queue(void)
149 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
153 case SYS_USB_CONNECTED
:
154 /* Put the event back into the queue. */
155 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
163 * Internal function to iterate a path.
165 static int dircache_scan(struct travel_data
*td
)
168 while ( ( td
->entry
= readdir(td
->dir
) ) )
170 while ( (fat_getnext(td
->dir
, &td
->entry
) >= 0) && (td
->entry
.name
[0]))
174 if (!strcmp(".", td
->entry
->d_name
) ||
175 !strcmp("..", td
->entry
->d_name
))
180 td
->ce
->attribute
= td
->entry
->attribute
;
181 td
->ce
->name_len
= strlen(td
->entry
->d_name
) + 1;
182 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
183 td
->ce
->size
= td
->entry
->size
;
184 td
->ce
->wrtdate
= td
->entry
->wrtdate
;
185 td
->ce
->wrttime
= td
->entry
->wrttime
;
186 memcpy(td
->ce
->d_name
, td
->entry
->d_name
, td
->ce
->name_len
);
188 if (!strcmp(".", td
->entry
.name
) ||
189 !strcmp("..", td
->entry
.name
))
194 td
->ce
->attribute
= td
->entry
.attr
;
195 td
->ce
->name_len
= strlen(td
->entry
.name
) + 1;
196 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
197 td
->ce
->startcluster
= td
->entry
.firstcluster
;
198 td
->ce
->size
= td
->entry
.filesize
;
199 td
->ce
->wrtdate
= td
->entry
.wrtdate
;
200 td
->ce
->wrttime
= td
->entry
.wrttime
;
201 memcpy(td
->ce
->d_name
, td
->entry
.name
, td
->ce
->name_len
);
203 dircache_size
+= td
->ce
->name_len
;
207 if (td
->entry
->attribute
& ATTR_DIRECTORY
)
209 if (td
->entry
.attr
& FAT_ATTR_DIRECTORY
)
213 td
->down_entry
= dircache_gen_down(td
->ce
);
214 if (td
->down_entry
== NULL
)
217 td
->pathpos
= strlen(dircache_cur_path
);
218 strncpy(&dircache_cur_path
[td
->pathpos
], "/",
219 sizeof(dircache_cur_path
) - td
->pathpos
- 1);
221 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
->d_name
,
222 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
224 td
->newdir
= opendir(dircache_cur_path
);
225 if (td
->newdir
== NULL
)
227 logf("Failed to opendir(): %s", dircache_cur_path
);
231 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
.name
,
232 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
234 td
->newdir
= *td
->dir
;
235 if (fat_opendir(IF_MV2(volume
,) &td
->newdir
,
236 td
->entry
.firstcluster
, td
->dir
) < 0 )
242 td
->ce
= dircache_gen_next(td
->ce
);
250 td
->ce
= dircache_gen_next(td
->ce
);
254 /* When simulator is used, it's only safe to yield here. */
257 /* Stop if we got an external signal. */
258 if (check_event_queue())
269 * Recursively scan the hard disk and build the cache.
272 static int dircache_travel(DIR *dir
, struct dircache_entry
*ce
)
274 static int dircache_travel(struct fat_dir
*dir
, struct dircache_entry
*ce
)
280 memset(ce
, 0, sizeof(struct dircache_entry
));
281 dir_recursion
[0].dir
= dir
;
282 dir_recursion
[0].ce
= ce
;
283 dir_recursion
[0].first
= ce
;
286 //logf("=> %s", dircache_cur_path);
287 result
= dircache_scan(&dir_recursion
[depth
]);
289 case 0: /* Leaving the current directory. */
290 /* Add the standard . and .. entries. */
291 ce
= dir_recursion
[depth
].ce
;
295 closedir(dir_recursion
[depth
].dir
);
296 ce
->attribute
= ATTR_DIRECTORY
;
298 ce
->attribute
= FAT_ATTR_DIRECTORY
;
299 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
302 ce
->down
= dir_recursion
[depth
].first
;
308 dircache_cur_path
[dir_recursion
[depth
].pathpos
] = '\0';
310 ce
= dircache_gen_next(ce
);
313 logf("memory allocation error");
317 ce
->attribute
= ATTR_DIRECTORY
;
319 ce
->attribute
= FAT_ATTR_DIRECTORY
;
320 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
325 ce
->down
= dir_recursion
[depth
].first
;
329 case 1: /* Going down in the directory tree. */
331 if (depth
>= MAX_SCAN_DEPTH
)
333 logf("Too deep directory structure");
338 dir_recursion
[depth
].dir
= dir_recursion
[depth
-1].newdir
;
340 dir_recursion
[depth
].dir
= &dir_recursion
[depth
-1].newdir
;
342 dir_recursion
[depth
].first
= dir_recursion
[depth
-1].down_entry
;
343 dir_recursion
[depth
].ce
= dir_recursion
[depth
-1].down_entry
;
348 logf("-> %s", dircache_cur_path
);
351 } while (depth
>= 0) ;
357 * Internal function to get a pointer to dircache_entry for a given filename.
358 * path: Absolute path to a file or directory.
359 * get_before: Returns the cache pointer before the last valid entry found.
360 * only_directories: Match only filenames which are a directory type.
362 static struct dircache_entry
* dircache_get_entry(const char *path
,
363 bool get_before
, bool only_directories
)
365 struct dircache_entry
*cache_entry
, *before
;
366 char namecopy
[MAX_PATH
*2];
370 strncpy(namecopy
, path
, sizeof(namecopy
) - 1);
371 cache_entry
= dircache_root
;
374 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
375 part
= strtok_r(NULL
, "/", &end
)) {
377 /* scan dir for name */
380 if (cache_entry
== NULL
)
384 else if (cache_entry
->name_len
== 0)
386 cache_entry
= cache_entry
->next
;
390 if (!strcasecmp(part
, cache_entry
->d_name
))
392 before
= cache_entry
;
393 if (cache_entry
->down
|| only_directories
)
394 cache_entry
= cache_entry
->down
;
398 cache_entry
= cache_entry
->next
;
403 cache_entry
= before
;
410 * Function to load the internal cache structure from disk to initialize
411 * the dircache really fast and little disk access.
413 int dircache_load(void)
415 struct dircache_maindata maindata
;
419 if (dircache_initialized
)
422 logf("Loading directory cache");
425 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
429 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
430 if (bytes_read
!= sizeof(struct dircache_maindata
)
431 || maindata
.size
<= 0)
433 logf("Dircache file header error");
435 remove(DIRCACHE_FILE
);
439 dircache_root
= buffer_alloc(0);
440 if ((long)maindata
.root_entry
!= (long)dircache_root
)
442 logf("Position missmatch");
444 remove(DIRCACHE_FILE
);
448 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
449 entry_count
= maindata
.entry_count
;
450 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
452 remove(DIRCACHE_FILE
);
454 if (bytes_read
!= maindata
.size
)
456 logf("Dircache read failed");
460 /* Cache successfully loaded. */
461 dircache_size
= maindata
.size
;
462 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
464 logf("Done, %ld KiB used", dircache_size
/ 1024);
465 dircache_initialized
= true;
466 memset(fd_bindings
, 0, sizeof(fd_bindings
));
472 * Function to save the internal cache stucture to disk for fast loading
475 int dircache_save(void)
477 struct dircache_maindata maindata
;
479 unsigned long bytes_written
;
481 remove(DIRCACHE_FILE
);
483 while (thread_enabled
)
486 if (!dircache_initialized
)
489 logf("Saving directory cache");
490 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
492 maindata
.magic
= DIRCACHE_MAGIC
;
493 maindata
.size
= dircache_size
;
494 maindata
.root_entry
= dircache_root
;
495 maindata
.entry_count
= entry_count
;
497 /* Save the info structure */
498 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
499 if (bytes_written
!= sizeof(struct dircache_maindata
))
502 logf("dircache: write failed #1");
506 /* Dump whole directory cache to disk */
507 bytes_written
= write(fd
, dircache_root
, dircache_size
);
509 if (bytes_written
!= dircache_size
)
511 logf("dircache: write failed #2");
520 * Internal function which scans the disk and creates the dircache structure.
522 static int dircache_do_rebuild(void)
527 struct fat_dir dir
, *pdir
;
529 unsigned int start_tick
;
532 /* Measure how long it takes build the cache. */
533 start_tick
= current_tick
;
534 remove(DIRCACHE_FILE
);
535 dircache_initializing
= true;
541 logf("Failed to open rootdir");
545 if ( fat_opendir(IF_MV2(volume
,) &dir
, 0, NULL
) < 0 ) {
546 logf("Failed opening root dir");
547 dircache_initializing
= false;
553 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
554 dircache_size
= sizeof(struct dircache_entry
);
557 if (dircache_travel(pdir
, dircache_root
) < 0)
559 logf("dircache_travel failed");
562 dircache_initializing
= false;
567 logf("Done, %ld KiB used", dircache_size
/ 1024);
569 dircache_initialized
= true;
570 dircache_initializing
= false;
571 cache_build_ticks
= current_tick
- start_tick
;
573 /* Initialized fd bindings. */
574 memset(fd_bindings
, 0, sizeof(fd_bindings
));
575 for (i
= 0; i
< fdbind_idx
; i
++)
576 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
581 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
582 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
586 /* We have to long align the audiobuf to keep the buffer access fast. */
587 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
588 audiobuf
+= DIRCACHE_RESERVE
;
589 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
597 * Internal thread that controls transparent cache building.
599 static void dircache_thread(void)
605 queue_wait(&dircache_queue
, &ev
);
610 thread_enabled
= true;
611 dircache_do_rebuild();
612 thread_enabled
= false;
616 logf("Stopped the rebuilding.");
617 dircache_initialized
= false;
621 case SYS_USB_CONNECTED
:
622 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
623 usb_wait_for_disconnect(&dircache_queue
);
631 * Start scanning the disk to build the dircache.
632 * Either transparent or non-transparent build method is used.
634 int dircache_build(int last_size
)
636 if (dircache_initialized
|| thread_enabled
)
639 logf("Building directory cache");
640 remove(DIRCACHE_FILE
);
642 /* Background build, dircache has been previously allocated */
643 if (dircache_size
> 0)
645 thread_enabled
= true;
646 dircache_initializing
= true;
647 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
651 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
653 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
654 dircache_root
= buffer_alloc(allocated_size
);
655 thread_enabled
= true;
657 /* Start a transparent rebuild. */
658 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
662 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
664 /* Start a non-transparent rebuild. */
665 return dircache_do_rebuild();
669 * Steal the allocated dircache buffer and disable dircache.
671 void* dircache_steal_buffer(long *size
)
674 if (dircache_size
== 0)
680 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
682 return dircache_root
;
686 * Main initialization function that must be called before any other
687 * operations within the dircache.
689 void dircache_init(void)
693 dircache_initialized
= false;
694 dircache_initializing
= false;
696 memset(opendirs
, 0, sizeof(opendirs
));
697 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
699 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
702 queue_init(&dircache_queue
, true);
703 create_thread(dircache_thread
, dircache_stack
,
704 sizeof(dircache_stack
), dircache_thread_name
IF_PRIO(, PRIORITY_BACKGROUND
)
705 IF_COP(, CPU
, false));
709 * Returns true if dircache has been initialized and is ready to be used.
711 bool dircache_is_enabled(void)
713 return dircache_initialized
;
717 * Returns true if dircache is being initialized.
719 bool dircache_is_initializing(void)
721 return dircache_initializing
;
725 * Returns the current number of entries (directories and files) in the cache.
727 int dircache_get_entry_count(void)
733 * Returns the allocated space for dircache (without reserve space).
735 int dircache_get_cache_size(void)
737 return dircache_is_enabled() ? dircache_size
: 0;
741 * Returns how many bytes of the reserve allocation for live cache
742 * updates have been used.
744 int dircache_get_reserve_used(void)
746 return dircache_is_enabled() ? reserve_used
: 0;
750 * Returns the time in kernel ticks that took to build the cache.
752 int dircache_get_build_ticks(void)
754 return dircache_is_enabled() ? cache_build_ticks
: 0;
758 * Disables the dircache. Usually called on shutdown or when
759 * accepting a usb connection.
761 void dircache_disable(void)
767 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
769 while (thread_enabled
)
771 dircache_initialized
= false;
773 logf("Waiting for cached dirs to release");
775 cache_in_use
= false;
776 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
777 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
784 } while (cache_in_use
) ;
786 logf("Cache released");
791 * Usermode function to return dircache_entry pointer to the given path.
793 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
795 if (!dircache_initialized
|| filename
== NULL
)
798 return dircache_get_entry(filename
, false, false);
802 * Function to copy the full absolute path from dircache to the given buffer
803 * using the given dircache_entry pointer.
805 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
807 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
822 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
826 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
827 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
828 size
-= down
[depth
]->name_len
;
834 /* --- Directory cache live updating functions --- */
835 static int block_until_ready(void)
837 /* Block until dircache has been built. */
838 while (!dircache_initialized
&& dircache_initializing
)
841 if (!dircache_initialized
)
847 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
849 struct dircache_entry
*entry
;
850 char basedir
[MAX_PATH
*2];
852 long last_cache_size
= dircache_size
;
854 strncpy(basedir
, path
, sizeof(basedir
)-1);
855 new = strrchr(basedir
, '/');
858 logf("error occurred");
859 dircache_initialized
= false;
866 entry
= dircache_get_entry(basedir
, false, true);
869 logf("basedir not found!");
871 dircache_initialized
= false;
875 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
878 logf("not enough space");
879 dircache_initialized
= false;
883 while (entry
->next
!= NULL
)
886 if (entry
->name_len
> 0)
887 entry
= dircache_gen_next(entry
);
891 dircache_initialized
= false;
895 entry
->attribute
= attribute
;
896 entry
->name_len
= MIN(254, strlen(new)) + 1;
897 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
898 entry
->startcluster
= 0;
902 memcpy(entry
->d_name
, new, entry
->name_len
);
903 dircache_size
+= entry
->name_len
;
905 if (attribute
& ATTR_DIRECTORY
)
908 dircache_gen_down(entry
);
911 reserve_used
+= dircache_size
- last_cache_size
;
916 void dircache_bind(int fd
, const char *path
)
918 struct dircache_entry
*entry
;
920 /* Queue requests until dircache has been built. */
921 if (!dircache_initialized
&& dircache_initializing
)
923 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
925 strncpy(fdbind_cache
[fdbind_idx
].path
, path
,
926 sizeof(fdbind_cache
[fdbind_idx
].path
)-1);
927 fdbind_cache
[fdbind_idx
].fd
= fd
;
932 if (!dircache_initialized
)
935 logf("bind: %d/%s", fd
, path
);
936 entry
= dircache_get_entry(path
, false, false);
940 dircache_initialized
= false;
944 fd_bindings
[fd
] = entry
;
947 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
949 if (!dircache_initialized
|| fd
< 0)
952 if (fd_bindings
[fd
] == NULL
)
954 logf("dircache fd access error");
955 dircache_initialized
= false;
959 fd_bindings
[fd
]->size
= newsize
;
960 fd_bindings
[fd
]->startcluster
= startcluster
;
963 void dircache_mkdir(const char *path
)
965 if (block_until_ready())
968 logf("mkdir: %s", path
);
969 dircache_new_entry(path
, ATTR_DIRECTORY
);
972 void dircache_rmdir(const char *path
)
974 struct dircache_entry
*entry
;
976 if (block_until_ready())
979 logf("rmdir: %s", path
);
980 entry
= dircache_get_entry(path
, true, true);
984 dircache_initialized
= false;
992 /* Remove a file from cache */
993 void dircache_remove(const char *name
)
995 struct dircache_entry
*entry
;
997 if (block_until_ready())
1000 logf("remove: %s", name
);
1002 entry
= dircache_get_entry(name
, false, false);
1007 dircache_initialized
= false;
1011 entry
->name_len
= 0;
1014 void dircache_rename(const char *oldpath
, const char *newpath
)
1016 struct dircache_entry
*entry
, *newentry
;
1017 struct dircache_entry oldentry
;
1018 char absolute_path
[MAX_PATH
*2];
1021 if (block_until_ready())
1024 logf("rename: %s->%s", oldpath
, newpath
);
1026 entry
= dircache_get_entry(oldpath
, true, false);
1030 dircache_initialized
= false;
1034 /* Delete the old entry. */
1035 entry
->name_len
= 0;
1037 /** If we rename the same filename twice in a row, we need to
1038 * save the data, because the entry will be re-used. */
1041 /* Generate the absolute path for destination if necessary. */
1042 if (newpath
[0] != '/')
1044 strncpy(absolute_path
, oldpath
, sizeof(absolute_path
)-1);
1045 p
= strrchr(absolute_path
, '/');
1048 logf("Invalid path");
1049 dircache_initialized
= false;
1054 strncpy(p
, absolute_path
, sizeof(absolute_path
)-1-strlen(p
));
1055 newpath
= absolute_path
;
1058 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1059 if (newentry
== NULL
)
1061 dircache_initialized
= false;
1065 newentry
->down
= oldentry
.down
;
1066 newentry
->size
= oldentry
.size
;
1067 newentry
->startcluster
= oldentry
.startcluster
;
1068 newentry
->wrttime
= oldentry
.wrttime
;
1069 newentry
->wrtdate
= oldentry
.wrtdate
;
1072 void dircache_add_file(const char *path
, long startcluster
)
1074 struct dircache_entry
*entry
;
1076 if (block_until_ready())
1079 logf("add file: %s", path
);
1080 entry
= dircache_new_entry(path
, 0);
1084 entry
->startcluster
= startcluster
;
1087 DIRCACHED
* opendir_cached(const char* name
)
1089 struct dircache_entry
*cache_entry
;
1091 DIRCACHED
* pdir
= opendirs
;
1093 if ( name
[0] != '/' )
1095 DEBUGF("Only absolute paths supported right now\n");
1099 /* find a free dir descriptor */
1100 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1104 if ( dd
== MAX_OPEN_DIRS
)
1106 DEBUGF("Too many dirs open\n");
1111 if (!dircache_initialized
)
1113 pdir
->regulardir
= opendir(name
);
1114 if (!pdir
->regulardir
)
1122 pdir
->regulardir
= NULL
;
1123 cache_entry
= dircache_get_entry(name
, false, true);
1124 pdir
->entry
= cache_entry
;
1126 if (cache_entry
== NULL
)
1135 struct dircache_entry
* readdir_cached(DIRCACHED
* dir
)
1137 struct dirent
*regentry
;
1138 struct dircache_entry
*ce
;
1143 if (dir
->regulardir
!= NULL
)
1145 regentry
= readdir(dir
->regulardir
);
1146 if (regentry
== NULL
)
1149 strncpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
-1);
1150 dir
->secondary_entry
.size
= regentry
->size
;
1151 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1152 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1153 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1154 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1155 dir
->secondary_entry
.next
= NULL
;
1157 return &dir
->secondary_entry
;
1161 if (dir
->entry
== NULL
)
1165 if (ce
->name_len
== 0)
1166 dir
->entry
= ce
->next
;
1167 } while (ce
->name_len
== 0) ;
1169 dir
->entry
= ce
->next
;
1171 strncpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
-1);
1172 /* Can't do `dir->secondary_entry = *ce`
1173 because that modifies the d_name pointer. */
1174 dir
->secondary_entry
.size
= ce
->size
;
1175 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1176 dir
->secondary_entry
.attribute
= ce
->attribute
;
1177 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1178 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1179 dir
->secondary_entry
.next
= NULL
;
1180 dir
->internal_entry
= ce
;
1182 //logf("-> %s", ce->name);
1183 return &dir
->secondary_entry
;
1186 int closedir_cached(DIRCACHED
* dir
)
1192 if (dir
->regulardir
!= NULL
)
1193 return closedir(dir
->regulardir
);