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
;
408 #ifdef HAVE_EEPROM_SETTINGS
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 if (!dircache_initialized
)
486 logf("Saving directory cache");
487 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
489 maindata
.magic
= DIRCACHE_MAGIC
;
490 maindata
.size
= dircache_size
;
491 maindata
.root_entry
= dircache_root
;
492 maindata
.entry_count
= entry_count
;
494 /* Save the info structure */
495 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
496 if (bytes_written
!= sizeof(struct dircache_maindata
))
499 logf("dircache: write failed #1");
503 /* Dump whole directory cache to disk */
504 bytes_written
= write(fd
, dircache_root
, dircache_size
);
506 if (bytes_written
!= dircache_size
)
508 logf("dircache: write failed #2");
517 * Internal function which scans the disk and creates the dircache structure.
519 static int dircache_do_rebuild(void)
524 struct fat_dir dir
, *pdir
;
526 unsigned int start_tick
;
529 /* Measure how long it takes build the cache. */
530 start_tick
= current_tick
;
531 remove(DIRCACHE_FILE
);
532 dircache_initializing
= true;
538 logf("Failed to open rootdir");
542 if ( fat_opendir(IF_MV2(volume
,) &dir
, 0, NULL
) < 0 ) {
543 logf("Failed opening root dir");
544 dircache_initializing
= false;
550 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
551 dircache_size
= sizeof(struct dircache_entry
);
554 if (dircache_travel(pdir
, dircache_root
) < 0)
556 logf("dircache_travel failed");
559 dircache_initializing
= false;
564 logf("Done, %ld KiB used", dircache_size
/ 1024);
566 dircache_initialized
= true;
567 dircache_initializing
= false;
568 cache_build_ticks
= current_tick
- start_tick
;
570 /* Initialized fd bindings. */
571 memset(fd_bindings
, 0, sizeof(fd_bindings
));
572 for (i
= 0; i
< fdbind_idx
; i
++)
573 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
578 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
579 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
583 /* We have to long align the audiobuf to keep the buffer access fast. */
584 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
585 audiobuf
+= DIRCACHE_RESERVE
;
586 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
594 * Internal thread that controls transparent cache building.
596 static void dircache_thread(void)
602 queue_wait(&dircache_queue
, &ev
);
607 thread_enabled
= true;
608 dircache_do_rebuild();
609 thread_enabled
= false;
613 logf("Stopped the rebuilding.");
614 dircache_initialized
= false;
618 case SYS_USB_CONNECTED
:
619 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
620 usb_wait_for_disconnect(&dircache_queue
);
628 * Start scanning the disk to build the dircache.
629 * Either transparent or non-transparent build method is used.
631 int dircache_build(int last_size
)
633 if (dircache_initialized
|| thread_enabled
)
636 logf("Building directory cache");
637 remove(DIRCACHE_FILE
);
639 /* Background build, dircache has been previously allocated */
640 if (dircache_size
> 0)
642 thread_enabled
= true;
643 dircache_initializing
= true;
644 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
648 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
650 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
651 dircache_root
= buffer_alloc(allocated_size
);
652 thread_enabled
= true;
654 /* Start a transparent rebuild. */
655 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
659 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
661 /* Start a non-transparent rebuild. */
662 return dircache_do_rebuild();
666 * Steal the allocated dircache buffer and disable dircache.
668 void* dircache_steal_buffer(long *size
)
671 if (dircache_size
== 0)
677 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
679 return dircache_root
;
683 * Main initialization function that must be called before any other
684 * operations within the dircache.
686 void dircache_init(void)
690 dircache_initialized
= false;
691 dircache_initializing
= false;
693 memset(opendirs
, 0, sizeof(opendirs
));
694 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
696 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
699 queue_init(&dircache_queue
, true);
700 create_thread(dircache_thread
, dircache_stack
,
701 sizeof(dircache_stack
), dircache_thread_name
IF_PRIO(, PRIORITY_BACKGROUND
)
702 IF_COP(, CPU
, false));
706 * Returns true if dircache has been initialized and is ready to be used.
708 bool dircache_is_enabled(void)
710 return dircache_initialized
;
714 * Returns true if dircache is being initialized.
716 bool dircache_is_initializing(void)
718 return dircache_initializing
|| thread_enabled
;
722 * Returns the current number of entries (directories and files) in the cache.
724 int dircache_get_entry_count(void)
730 * Returns the allocated space for dircache (without reserve space).
732 int dircache_get_cache_size(void)
734 return dircache_is_enabled() ? dircache_size
: 0;
738 * Returns how many bytes of the reserve allocation for live cache
739 * updates have been used.
741 int dircache_get_reserve_used(void)
743 return dircache_is_enabled() ? reserve_used
: 0;
747 * Returns the time in kernel ticks that took to build the cache.
749 int dircache_get_build_ticks(void)
751 return dircache_is_enabled() ? cache_build_ticks
: 0;
755 * Disables the dircache. Usually called on shutdown or when
756 * accepting a usb connection.
758 void dircache_disable(void)
764 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
766 while (thread_enabled
)
768 dircache_initialized
= false;
770 logf("Waiting for cached dirs to release");
772 cache_in_use
= false;
773 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
774 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
781 } while (cache_in_use
) ;
783 logf("Cache released");
788 * Usermode function to return dircache_entry pointer to the given path.
790 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
792 if (!dircache_initialized
|| filename
== NULL
)
795 return dircache_get_entry(filename
, false, false);
799 * Function to copy the full absolute path from dircache to the given buffer
800 * using the given dircache_entry pointer.
802 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
804 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
819 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
823 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
824 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
825 size
-= down
[depth
]->name_len
;
831 /* --- Directory cache live updating functions --- */
832 static int block_until_ready(void)
834 /* Block until dircache has been built. */
835 while (!dircache_initialized
&& dircache_is_initializing())
838 if (!dircache_initialized
)
844 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
846 struct dircache_entry
*entry
;
847 char basedir
[MAX_PATH
*2];
849 long last_cache_size
= dircache_size
;
851 strncpy(basedir
, path
, sizeof(basedir
)-1);
852 new = strrchr(basedir
, '/');
855 logf("error occurred");
856 dircache_initialized
= false;
863 entry
= dircache_get_entry(basedir
, false, true);
866 logf("basedir not found!");
868 dircache_initialized
= false;
872 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
875 logf("not enough space");
876 dircache_initialized
= false;
880 while (entry
->next
!= NULL
)
883 if (entry
->name_len
> 0)
884 entry
= dircache_gen_next(entry
);
888 dircache_initialized
= false;
892 entry
->attribute
= attribute
;
893 entry
->name_len
= MIN(254, strlen(new)) + 1;
894 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
895 entry
->startcluster
= 0;
899 memcpy(entry
->d_name
, new, entry
->name_len
);
900 dircache_size
+= entry
->name_len
;
902 if (attribute
& ATTR_DIRECTORY
)
905 dircache_gen_down(entry
);
908 reserve_used
+= dircache_size
- last_cache_size
;
913 void dircache_bind(int fd
, const char *path
)
915 struct dircache_entry
*entry
;
917 /* Queue requests until dircache has been built. */
918 if (!dircache_initialized
&& dircache_is_initializing())
920 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
922 strncpy(fdbind_cache
[fdbind_idx
].path
, path
,
923 sizeof(fdbind_cache
[fdbind_idx
].path
)-1);
924 fdbind_cache
[fdbind_idx
].fd
= fd
;
929 if (!dircache_initialized
)
932 logf("bind: %d/%s", fd
, path
);
933 entry
= dircache_get_entry(path
, false, false);
937 dircache_initialized
= false;
941 fd_bindings
[fd
] = entry
;
944 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
946 if (!dircache_initialized
|| fd
< 0)
949 if (fd_bindings
[fd
] == NULL
)
951 logf("dircache fd access error");
952 dircache_initialized
= false;
956 fd_bindings
[fd
]->size
= newsize
;
957 fd_bindings
[fd
]->startcluster
= startcluster
;
960 void dircache_mkdir(const char *path
)
962 if (block_until_ready())
965 logf("mkdir: %s", path
);
966 dircache_new_entry(path
, ATTR_DIRECTORY
);
969 void dircache_rmdir(const char *path
)
971 struct dircache_entry
*entry
;
973 if (block_until_ready())
976 logf("rmdir: %s", path
);
977 entry
= dircache_get_entry(path
, true, true);
981 dircache_initialized
= false;
989 /* Remove a file from cache */
990 void dircache_remove(const char *name
)
992 struct dircache_entry
*entry
;
994 if (block_until_ready())
997 logf("remove: %s", name
);
999 entry
= dircache_get_entry(name
, false, false);
1004 dircache_initialized
= false;
1008 entry
->name_len
= 0;
1011 void dircache_rename(const char *oldpath
, const char *newpath
)
1013 struct dircache_entry
*entry
, *newentry
;
1014 struct dircache_entry oldentry
;
1015 char absolute_path
[MAX_PATH
*2];
1018 if (block_until_ready())
1021 logf("rename: %s->%s", oldpath
, newpath
);
1023 entry
= dircache_get_entry(oldpath
, true, false);
1027 dircache_initialized
= false;
1031 /* Delete the old entry. */
1032 entry
->name_len
= 0;
1034 /** If we rename the same filename twice in a row, we need to
1035 * save the data, because the entry will be re-used. */
1038 /* Generate the absolute path for destination if necessary. */
1039 if (newpath
[0] != '/')
1041 strncpy(absolute_path
, oldpath
, sizeof(absolute_path
)-1);
1042 p
= strrchr(absolute_path
, '/');
1045 logf("Invalid path");
1046 dircache_initialized
= false;
1051 strncpy(p
, absolute_path
, sizeof(absolute_path
)-1-strlen(p
));
1052 newpath
= absolute_path
;
1055 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1056 if (newentry
== NULL
)
1058 dircache_initialized
= false;
1062 newentry
->down
= oldentry
.down
;
1063 newentry
->size
= oldentry
.size
;
1064 newentry
->startcluster
= oldentry
.startcluster
;
1065 newentry
->wrttime
= oldentry
.wrttime
;
1066 newentry
->wrtdate
= oldentry
.wrtdate
;
1069 void dircache_add_file(const char *path
, long startcluster
)
1071 struct dircache_entry
*entry
;
1073 if (block_until_ready())
1076 logf("add file: %s", path
);
1077 entry
= dircache_new_entry(path
, 0);
1081 entry
->startcluster
= startcluster
;
1084 DIRCACHED
* opendir_cached(const char* name
)
1086 struct dircache_entry
*cache_entry
;
1088 DIRCACHED
* pdir
= opendirs
;
1090 if ( name
[0] != '/' )
1092 DEBUGF("Only absolute paths supported right now\n");
1096 /* find a free dir descriptor */
1097 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1101 if ( dd
== MAX_OPEN_DIRS
)
1103 DEBUGF("Too many dirs open\n");
1108 if (!dircache_initialized
)
1110 pdir
->regulardir
= opendir(name
);
1111 if (!pdir
->regulardir
)
1119 pdir
->regulardir
= NULL
;
1120 cache_entry
= dircache_get_entry(name
, false, true);
1121 pdir
->entry
= cache_entry
;
1123 if (cache_entry
== NULL
)
1132 struct dircache_entry
* readdir_cached(DIRCACHED
* dir
)
1134 struct dirent
*regentry
;
1135 struct dircache_entry
*ce
;
1140 if (dir
->regulardir
!= NULL
)
1142 regentry
= readdir(dir
->regulardir
);
1143 if (regentry
== NULL
)
1146 strncpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
-1);
1147 dir
->secondary_entry
.size
= regentry
->size
;
1148 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1149 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1150 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1151 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1152 dir
->secondary_entry
.next
= NULL
;
1154 return &dir
->secondary_entry
;
1158 if (dir
->entry
== NULL
)
1162 if (ce
->name_len
== 0)
1163 dir
->entry
= ce
->next
;
1164 } while (ce
->name_len
== 0) ;
1166 dir
->entry
= ce
->next
;
1168 strncpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
-1);
1169 /* Can't do `dir->secondary_entry = *ce`
1170 because that modifies the d_name pointer. */
1171 dir
->secondary_entry
.size
= ce
->size
;
1172 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1173 dir
->secondary_entry
.attribute
= ce
->attribute
;
1174 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1175 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1176 dir
->secondary_entry
.next
= NULL
;
1177 dir
->internal_entry
= ce
;
1179 //logf("-> %s", ce->name);
1180 return &dir
->secondary_entry
;
1183 int closedir_cached(DIRCACHED
* dir
)
1189 if (dir
->regulardir
!= NULL
)
1190 return closedir(dir
->regulardir
);