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.
42 #include "timefuncs.h"
46 #define DIRCACHE_BUILD 1
47 #define DIRCACHE_STOP 2
49 #define MAX_OPEN_DIRS 8
50 DIR_CACHED opendirs
[MAX_OPEN_DIRS
];
52 static struct dircache_entry
*fd_bindings
[MAX_OPEN_FILES
];
53 static struct dircache_entry
*dircache_root
;
55 static bool dircache_initialized
= false;
56 static bool dircache_initializing
= false;
57 static bool thread_enabled
= false;
58 static unsigned long allocated_size
= DIRCACHE_LIMIT
;
59 static unsigned long dircache_size
= 0;
60 static unsigned long entry_count
= 0;
61 static unsigned long reserve_used
= 0;
62 static unsigned int cache_build_ticks
= 0;
63 static unsigned long appflags
= 0;
64 static char dircache_cur_path
[MAX_PATH
*2];
66 static struct event_queue dircache_queue
;
67 static long dircache_stack
[(DEFAULT_STACK_SIZE
+ 0x900)/sizeof(long)];
68 static const char dircache_thread_name
[] = "dircache";
70 static struct fdbind_queue fdbind_cache
[MAX_PENDING_BINDINGS
];
71 static int fdbind_idx
= 0;
73 /* --- Internal cache structure control functions --- */
76 * Internal function to allocate a new dircache_entry from memory.
78 static struct dircache_entry
* allocate_entry(void)
80 struct dircache_entry
*next_entry
;
82 if (dircache_size
> allocated_size
- MAX_PATH
*2)
84 logf("size limit reached");
88 next_entry
= (struct dircache_entry
*)((char *)dircache_root
+dircache_size
);
89 #ifdef ROCKBOX_STRICT_ALIGN
90 /* Make sure the entry is long aligned. */
91 if ((long)next_entry
& 0x03)
93 dircache_size
+= 4 - ((long)next_entry
& 0x03);
94 next_entry
= (struct dircache_entry
*)(((long)next_entry
& ~0x03) + 0x04);
97 next_entry
->name_len
= 0;
98 next_entry
->d_name
= NULL
;
99 next_entry
->up
= NULL
;
100 next_entry
->down
= NULL
;
101 next_entry
->next
= NULL
;
103 dircache_size
+= sizeof(struct dircache_entry
);
109 * Internal function to allocate a dircache_entry and set
110 * ->next entry pointers.
112 static struct dircache_entry
* dircache_gen_next(struct dircache_entry
*ce
)
114 struct dircache_entry
*next_entry
;
116 if ( (next_entry
= allocate_entry()) == NULL
)
118 next_entry
->up
= ce
->up
;
119 ce
->next
= next_entry
;
125 * Internal function to allocate a dircache_entry and set
126 * ->down entry pointers.
128 static struct dircache_entry
* dircache_gen_down(struct dircache_entry
*ce
)
130 struct dircache_entry
*next_entry
;
132 if ( (next_entry
= allocate_entry()) == NULL
)
135 ce
->down
= next_entry
;
140 /* This will eat ~30 KiB of memory!
141 * We should probably use that as additional reserve buffer in future. */
142 #define MAX_SCAN_DEPTH 16
143 static struct travel_data dir_recursion
[MAX_SCAN_DEPTH
];
146 * Returns true if there is an event waiting in the queue
147 * that requires the current operation to be aborted.
149 static bool check_event_queue(void)
151 struct queue_event ev
;
153 queue_wait_w_tmo(&dircache_queue
, &ev
, 0);
157 case SYS_USB_CONNECTED
:
158 /* Put the event back into the queue. */
159 queue_post(&dircache_queue
, ev
.id
, ev
.data
);
167 * Internal function to iterate a path.
169 static int dircache_scan(struct travel_data
*td
)
172 while ( ( td
->entry
= readdir_uncached(td
->dir
) ) )
174 while ( (fat_getnext(td
->dir
, &td
->entry
) >= 0) && (td
->entry
.name
[0]))
178 if (!strcmp(".", td
->entry
->d_name
) ||
179 !strcmp("..", td
->entry
->d_name
))
184 td
->ce
->attribute
= td
->entry
->attribute
;
185 td
->ce
->name_len
= strlen(td
->entry
->d_name
) + 1;
186 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
187 td
->ce
->size
= td
->entry
->size
;
188 td
->ce
->wrtdate
= td
->entry
->wrtdate
;
189 td
->ce
->wrttime
= td
->entry
->wrttime
;
190 memcpy(td
->ce
->d_name
, td
->entry
->d_name
, td
->ce
->name_len
);
192 if (!strcmp(".", td
->entry
.name
) ||
193 !strcmp("..", td
->entry
.name
))
198 td
->ce
->attribute
= td
->entry
.attr
;
199 td
->ce
->name_len
= strlen(td
->entry
.name
) + 1;
200 td
->ce
->d_name
= ((char *)dircache_root
+dircache_size
);
201 td
->ce
->startcluster
= td
->entry
.firstcluster
;
202 td
->ce
->size
= td
->entry
.filesize
;
203 td
->ce
->wrtdate
= td
->entry
.wrtdate
;
204 td
->ce
->wrttime
= td
->entry
.wrttime
;
205 memcpy(td
->ce
->d_name
, td
->entry
.name
, td
->ce
->name_len
);
207 dircache_size
+= td
->ce
->name_len
;
211 if (td
->entry
->attribute
& ATTR_DIRECTORY
)
213 if (td
->entry
.attr
& FAT_ATTR_DIRECTORY
)
217 td
->down_entry
= dircache_gen_down(td
->ce
);
218 if (td
->down_entry
== NULL
)
221 td
->pathpos
= strlen(dircache_cur_path
);
222 strncpy(&dircache_cur_path
[td
->pathpos
], "/",
223 sizeof(dircache_cur_path
) - td
->pathpos
- 1);
225 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
->d_name
,
226 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
228 td
->newdir
= opendir_uncached(dircache_cur_path
);
229 if (td
->newdir
== NULL
)
231 logf("Failed to opendir_uncached(): %s", dircache_cur_path
);
235 strncpy(&dircache_cur_path
[td
->pathpos
+1], td
->entry
.name
,
236 sizeof(dircache_cur_path
) - td
->pathpos
- 2);
238 td
->newdir
= *td
->dir
;
239 if (fat_opendir(IF_MV2(volume
,) &td
->newdir
,
240 td
->entry
.firstcluster
, td
->dir
) < 0 )
246 td
->ce
= dircache_gen_next(td
->ce
);
254 td
->ce
= dircache_gen_next(td
->ce
);
258 /* When simulator is used, it's only safe to yield here. */
261 /* Stop if we got an external signal. */
262 if (check_event_queue())
273 * Recursively scan the hard disk and build the cache.
276 static int dircache_travel(DIR_UNCACHED
*dir
, struct dircache_entry
*ce
)
278 static int dircache_travel(struct fat_dir
*dir
, struct dircache_entry
*ce
)
284 memset(ce
, 0, sizeof(struct dircache_entry
));
285 dir_recursion
[0].dir
= dir
;
286 dir_recursion
[0].ce
= ce
;
287 dir_recursion
[0].first
= ce
;
290 //logf("=> %s", dircache_cur_path);
291 result
= dircache_scan(&dir_recursion
[depth
]);
293 case 0: /* Leaving the current directory. */
294 /* Add the standard . and .. entries. */
295 ce
= dir_recursion
[depth
].ce
;
299 closedir_uncached(dir_recursion
[depth
].dir
);
300 ce
->attribute
= ATTR_DIRECTORY
;
302 ce
->attribute
= FAT_ATTR_DIRECTORY
;
303 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
306 ce
->down
= dir_recursion
[depth
].first
;
312 dircache_cur_path
[dir_recursion
[depth
].pathpos
] = '\0';
314 ce
= dircache_gen_next(ce
);
317 logf("memory allocation error");
321 ce
->attribute
= ATTR_DIRECTORY
;
323 ce
->attribute
= FAT_ATTR_DIRECTORY
;
324 ce
->startcluster
= dir_recursion
[depth
].dir
->file
.firstcluster
;
329 ce
->down
= dir_recursion
[depth
].first
;
333 case 1: /* Going down in the directory tree. */
335 if (depth
>= MAX_SCAN_DEPTH
)
337 logf("Too deep directory structure");
342 dir_recursion
[depth
].dir
= dir_recursion
[depth
-1].newdir
;
344 dir_recursion
[depth
].dir
= &dir_recursion
[depth
-1].newdir
;
346 dir_recursion
[depth
].first
= dir_recursion
[depth
-1].down_entry
;
347 dir_recursion
[depth
].ce
= dir_recursion
[depth
-1].down_entry
;
352 logf("-> %s", dircache_cur_path
);
355 } while (depth
>= 0) ;
361 * Internal function to get a pointer to dircache_entry for a given filename.
362 * path: Absolute path to a file or directory.
363 * get_before: Returns the cache pointer before the last valid entry found.
364 * only_directories: Match only filenames which are a directory type.
366 static struct dircache_entry
* dircache_get_entry(const char *path
,
367 bool get_before
, bool only_directories
)
369 struct dircache_entry
*cache_entry
, *before
;
370 char namecopy
[MAX_PATH
*2];
374 strncpy(namecopy
, path
, sizeof(namecopy
) - 1);
375 cache_entry
= dircache_root
;
378 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
379 part
= strtok_r(NULL
, "/", &end
)) {
381 /* scan dir for name */
384 if (cache_entry
== NULL
)
388 else if (cache_entry
->name_len
== 0)
390 cache_entry
= cache_entry
->next
;
394 if (!strcasecmp(part
, cache_entry
->d_name
))
396 before
= cache_entry
;
397 if (cache_entry
->down
|| only_directories
)
398 cache_entry
= cache_entry
->down
;
402 cache_entry
= cache_entry
->next
;
407 cache_entry
= before
;
412 #ifdef HAVE_EEPROM_SETTINGS
414 * Function to load the internal cache structure from disk to initialize
415 * the dircache really fast and little disk access.
417 int dircache_load(void)
419 struct dircache_maindata maindata
;
423 if (dircache_initialized
)
426 logf("Loading directory cache");
429 fd
= open(DIRCACHE_FILE
, O_RDONLY
);
433 bytes_read
= read(fd
, &maindata
, sizeof(struct dircache_maindata
));
434 if (bytes_read
!= sizeof(struct dircache_maindata
)
435 || maindata
.size
<= 0)
437 logf("Dircache file header error");
439 remove(DIRCACHE_FILE
);
443 dircache_root
= buffer_alloc(0);
444 if ((long)maindata
.root_entry
!= (long)dircache_root
)
446 logf("Position missmatch");
448 remove(DIRCACHE_FILE
);
452 dircache_root
= buffer_alloc(maindata
.size
+ DIRCACHE_RESERVE
);
453 entry_count
= maindata
.entry_count
;
454 appflags
= maindata
.appflags
;
455 bytes_read
= read(fd
, dircache_root
, MIN(DIRCACHE_LIMIT
, maindata
.size
));
457 remove(DIRCACHE_FILE
);
459 if (bytes_read
!= maindata
.size
)
461 logf("Dircache read failed");
465 /* Cache successfully loaded. */
466 dircache_size
= maindata
.size
;
467 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
469 logf("Done, %ld KiB used", dircache_size
/ 1024);
470 dircache_initialized
= true;
471 memset(fd_bindings
, 0, sizeof(fd_bindings
));
477 * Function to save the internal cache stucture to disk for fast loading
480 int dircache_save(void)
482 struct dircache_maindata maindata
;
484 unsigned long bytes_written
;
486 remove(DIRCACHE_FILE
);
488 if (!dircache_initialized
)
491 logf("Saving directory cache");
492 fd
= open(DIRCACHE_FILE
, O_WRONLY
| O_CREAT
| O_TRUNC
);
494 maindata
.magic
= DIRCACHE_MAGIC
;
495 maindata
.size
= dircache_size
;
496 maindata
.root_entry
= dircache_root
;
497 maindata
.entry_count
= entry_count
;
498 maindata
.appflags
= appflags
;
500 /* Save the info structure */
501 bytes_written
= write(fd
, &maindata
, sizeof(struct dircache_maindata
));
502 if (bytes_written
!= sizeof(struct dircache_maindata
))
505 logf("dircache: write failed #1");
509 /* Dump whole directory cache to disk */
510 bytes_written
= write(fd
, dircache_root
, dircache_size
);
512 if (bytes_written
!= dircache_size
)
514 logf("dircache: write failed #2");
523 * Internal function which scans the disk and creates the dircache structure.
525 static int dircache_do_rebuild(void)
530 struct fat_dir dir
, *pdir
;
532 unsigned int start_tick
;
535 /* Measure how long it takes build the cache. */
536 start_tick
= current_tick
;
537 dircache_initializing
= true;
541 pdir
= opendir_uncached("/");
544 logf("Failed to open rootdir");
545 dircache_initializing
= false;
549 if ( fat_opendir(IF_MV2(volume
,) &dir
, 0, NULL
) < 0 ) {
550 logf("Failed opening root dir");
551 dircache_initializing
= false;
557 memset(dircache_cur_path
, 0, sizeof(dircache_cur_path
));
558 dircache_size
= sizeof(struct dircache_entry
);
561 if (dircache_travel(pdir
, dircache_root
) < 0)
563 logf("dircache_travel failed");
566 dircache_initializing
= false;
571 logf("Done, %ld KiB used", dircache_size
/ 1024);
573 dircache_initialized
= true;
574 dircache_initializing
= false;
575 cache_build_ticks
= current_tick
- start_tick
;
577 /* Initialized fd bindings. */
578 memset(fd_bindings
, 0, sizeof(fd_bindings
));
579 for (i
= 0; i
< fdbind_idx
; i
++)
580 dircache_bind(fdbind_cache
[i
].fd
, fdbind_cache
[i
].path
);
585 if (allocated_size
- dircache_size
< DIRCACHE_RESERVE
)
586 reserve_used
= DIRCACHE_RESERVE
- (allocated_size
- dircache_size
);
590 /* We have to long align the audiobuf to keep the buffer access fast. */
591 audiobuf
+= (long)((dircache_size
& ~0x03) + 0x04);
592 audiobuf
+= DIRCACHE_RESERVE
;
593 allocated_size
= dircache_size
+ DIRCACHE_RESERVE
;
601 * Internal thread that controls transparent cache building.
603 static void dircache_thread(void)
605 struct queue_event ev
;
609 queue_wait(&dircache_queue
, &ev
);
614 thread_enabled
= true;
615 dircache_do_rebuild();
616 thread_enabled
= false;
620 logf("Stopped the rebuilding.");
621 dircache_initialized
= false;
625 case SYS_USB_CONNECTED
:
626 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
627 usb_wait_for_disconnect(&dircache_queue
);
635 * Start scanning the disk to build the dircache.
636 * Either transparent or non-transparent build method is used.
638 int dircache_build(int last_size
)
640 if (dircache_initialized
|| thread_enabled
)
643 logf("Building directory cache");
644 remove(DIRCACHE_FILE
);
646 /* Background build, dircache has been previously allocated */
647 if (dircache_size
> 0)
649 thread_enabled
= true;
650 dircache_initializing
= true;
651 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
655 if (last_size
> DIRCACHE_RESERVE
&& last_size
< DIRCACHE_LIMIT
)
657 allocated_size
= last_size
+ DIRCACHE_RESERVE
;
658 dircache_root
= buffer_alloc(allocated_size
);
659 thread_enabled
= true;
661 /* Start a transparent rebuild. */
662 queue_post(&dircache_queue
, DIRCACHE_BUILD
, 0);
666 dircache_root
= (struct dircache_entry
*)(((long)audiobuf
& ~0x03) + 0x04);
668 /* Start a non-transparent rebuild. */
669 return dircache_do_rebuild();
673 * Steal the allocated dircache buffer and disable dircache.
675 void* dircache_steal_buffer(long *size
)
678 if (dircache_size
== 0)
684 *size
= dircache_size
+ (DIRCACHE_RESERVE
-reserve_used
);
686 return dircache_root
;
690 * Main initialization function that must be called before any other
691 * operations within the dircache.
693 void dircache_init(void)
697 dircache_initialized
= false;
698 dircache_initializing
= false;
700 memset(opendirs
, 0, sizeof(opendirs
));
701 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++)
703 opendirs
[i
].secondary_entry
.d_name
= buffer_alloc(MAX_PATH
);
706 queue_init(&dircache_queue
, true);
707 create_thread(dircache_thread
, dircache_stack
,
708 sizeof(dircache_stack
), 0, dircache_thread_name
709 IF_PRIO(, PRIORITY_BACKGROUND
)
714 * Returns true if dircache has been initialized and is ready to be used.
716 bool dircache_is_enabled(void)
718 return dircache_initialized
;
722 * Returns true if dircache is being initialized.
724 bool dircache_is_initializing(void)
726 return dircache_initializing
|| thread_enabled
;
730 * Set application flags used to determine if dircache is still intact.
732 void dircache_set_appflag(long mask
)
738 * Get application flags used to determine if dircache is still intact.
740 bool dircache_get_appflag(long mask
)
742 return dircache_is_enabled() && (appflags
& mask
);
746 * Returns the current number of entries (directories and files) in the cache.
748 int dircache_get_entry_count(void)
754 * Returns the allocated space for dircache (without reserve space).
756 int dircache_get_cache_size(void)
758 return dircache_is_enabled() ? dircache_size
: 0;
762 * Returns how many bytes of the reserve allocation for live cache
763 * updates have been used.
765 int dircache_get_reserve_used(void)
767 return dircache_is_enabled() ? reserve_used
: 0;
771 * Returns the time in kernel ticks that took to build the cache.
773 int dircache_get_build_ticks(void)
775 return dircache_is_enabled() ? cache_build_ticks
: 0;
779 * Disables the dircache. Usually called on shutdown or when
780 * accepting a usb connection.
782 void dircache_disable(void)
788 queue_post(&dircache_queue
, DIRCACHE_STOP
, 0);
790 while (thread_enabled
)
792 dircache_initialized
= false;
794 logf("Waiting for cached dirs to release");
796 cache_in_use
= false;
797 for (i
= 0; i
< MAX_OPEN_DIRS
; i
++) {
798 if (!opendirs
[i
].regulardir
&& opendirs
[i
].busy
)
805 } while (cache_in_use
) ;
807 logf("Cache released");
812 * Usermode function to return dircache_entry pointer to the given path.
814 const struct dircache_entry
*dircache_get_entry_ptr(const char *filename
)
816 if (!dircache_initialized
|| filename
== NULL
)
819 return dircache_get_entry(filename
, false, false);
823 * Function to copy the full absolute path from dircache to the given buffer
824 * using the given dircache_entry pointer.
826 void dircache_copy_path(const struct dircache_entry
*entry
, char *buf
, int size
)
828 const struct dircache_entry
*down
[MAX_SCAN_DEPTH
];
843 } while (entry
!= NULL
&& depth
< MAX_SCAN_DEPTH
);
847 snprintf(buf
, size
, "/%s", down
[depth
]->d_name
);
848 buf
+= down
[depth
]->name_len
; /* '/' + d_name */
849 size
-= down
[depth
]->name_len
;
855 /* --- Directory cache live updating functions --- */
856 static int block_until_ready(void)
858 /* Block until dircache has been built. */
859 while (!dircache_initialized
&& dircache_is_initializing())
862 if (!dircache_initialized
)
868 static struct dircache_entry
* dircache_new_entry(const char *path
, int attribute
)
870 struct dircache_entry
*entry
;
871 char basedir
[MAX_PATH
*2];
873 long last_cache_size
= dircache_size
;
875 strncpy(basedir
, path
, sizeof(basedir
)-1);
876 new = strrchr(basedir
, '/');
879 logf("error occurred");
880 dircache_initialized
= false;
887 entry
= dircache_get_entry(basedir
, false, true);
890 logf("basedir not found!");
892 dircache_initialized
= false;
896 if (reserve_used
+ 2*sizeof(struct dircache_entry
) + strlen(new)+1
899 logf("not enough space");
900 dircache_initialized
= false;
904 while (entry
->next
!= NULL
)
907 if (entry
->name_len
> 0)
908 entry
= dircache_gen_next(entry
);
912 dircache_initialized
= false;
916 entry
->attribute
= attribute
;
917 entry
->name_len
= MIN(254, strlen(new)) + 1;
918 entry
->d_name
= ((char *)dircache_root
+dircache_size
);
919 entry
->startcluster
= 0;
923 memcpy(entry
->d_name
, new, entry
->name_len
);
924 dircache_size
+= entry
->name_len
;
926 if (attribute
& ATTR_DIRECTORY
)
929 dircache_gen_down(entry
);
932 reserve_used
+= dircache_size
- last_cache_size
;
937 void dircache_bind(int fd
, const char *path
)
939 struct dircache_entry
*entry
;
941 /* Queue requests until dircache has been built. */
942 if (!dircache_initialized
&& dircache_is_initializing())
944 if (fdbind_idx
>= MAX_PENDING_BINDINGS
)
946 strncpy(fdbind_cache
[fdbind_idx
].path
, path
,
947 sizeof(fdbind_cache
[fdbind_idx
].path
)-1);
948 fdbind_cache
[fdbind_idx
].fd
= fd
;
953 if (!dircache_initialized
)
956 logf("bind: %d/%s", fd
, path
);
957 entry
= dircache_get_entry(path
, false, false);
961 dircache_initialized
= false;
965 fd_bindings
[fd
] = entry
;
968 void dircache_update_filesize(int fd
, long newsize
, long startcluster
)
970 if (!dircache_initialized
|| fd
< 0)
973 if (fd_bindings
[fd
] == NULL
)
975 logf("dircache fd access error");
976 dircache_initialized
= false;
980 fd_bindings
[fd
]->size
= newsize
;
981 fd_bindings
[fd
]->startcluster
= startcluster
;
983 void dircache_update_filetime(int fd
)
989 struct tm
*now
= get_time();
990 if (!dircache_initialized
|| fd
< 0)
993 if (fd_bindings
[fd
] == NULL
)
995 logf("dircache fd access error");
996 dircache_initialized
= false;
999 year
= now
->tm_year
+1900-1980;
1000 fd_bindings
[fd
]->wrtdate
= (((year
)&0x7f)<<9) |
1001 (((now
->tm_mon
+1)&0xf)<<5) |
1002 (((now
->tm_mday
)&0x1f));
1003 fd_bindings
[fd
]->wrttime
= (((now
->tm_hour
)&0x1f)<<11) |
1004 (((now
->tm_min
)&0x3f)<<5) |
1005 (((now
->tm_sec
/2)&0x1f));
1009 void dircache_mkdir(const char *path
)
1011 if (block_until_ready())
1014 logf("mkdir: %s", path
);
1015 dircache_new_entry(path
, ATTR_DIRECTORY
);
1018 void dircache_rmdir(const char *path
)
1020 struct dircache_entry
*entry
;
1022 if (block_until_ready())
1025 logf("rmdir: %s", path
);
1026 entry
= dircache_get_entry(path
, true, true);
1030 dircache_initialized
= false;
1035 entry
->name_len
= 0;
1038 /* Remove a file from cache */
1039 void dircache_remove(const char *name
)
1041 struct dircache_entry
*entry
;
1043 if (block_until_ready())
1046 logf("remove: %s", name
);
1048 entry
= dircache_get_entry(name
, false, false);
1053 dircache_initialized
= false;
1057 entry
->name_len
= 0;
1060 void dircache_rename(const char *oldpath
, const char *newpath
)
1062 struct dircache_entry
*entry
, *newentry
;
1063 struct dircache_entry oldentry
;
1064 char absolute_path
[MAX_PATH
*2];
1067 if (block_until_ready())
1070 logf("rename: %s->%s", oldpath
, newpath
);
1072 entry
= dircache_get_entry(oldpath
, true, false);
1076 dircache_initialized
= false;
1080 /* Delete the old entry. */
1081 entry
->name_len
= 0;
1083 /** If we rename the same filename twice in a row, we need to
1084 * save the data, because the entry will be re-used. */
1087 /* Generate the absolute path for destination if necessary. */
1088 if (newpath
[0] != '/')
1090 strncpy(absolute_path
, oldpath
, sizeof(absolute_path
)-1);
1091 p
= strrchr(absolute_path
, '/');
1094 logf("Invalid path");
1095 dircache_initialized
= false;
1100 strncpy(p
, absolute_path
, sizeof(absolute_path
)-1-strlen(p
));
1101 newpath
= absolute_path
;
1104 newentry
= dircache_new_entry(newpath
, entry
->attribute
);
1105 if (newentry
== NULL
)
1107 dircache_initialized
= false;
1111 newentry
->down
= oldentry
.down
;
1112 newentry
->size
= oldentry
.size
;
1113 newentry
->startcluster
= oldentry
.startcluster
;
1114 newentry
->wrttime
= oldentry
.wrttime
;
1115 newentry
->wrtdate
= oldentry
.wrtdate
;
1118 void dircache_add_file(const char *path
, long startcluster
)
1120 struct dircache_entry
*entry
;
1122 if (block_until_ready())
1125 logf("add file: %s", path
);
1126 entry
= dircache_new_entry(path
, 0);
1130 entry
->startcluster
= startcluster
;
1133 DIR_CACHED
* opendir_cached(const char* name
)
1135 struct dircache_entry
*cache_entry
;
1137 DIR_CACHED
* pdir
= opendirs
;
1139 if ( name
[0] != '/' )
1141 DEBUGF("Only absolute paths supported right now\n");
1145 /* find a free dir descriptor */
1146 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
1150 if ( dd
== MAX_OPEN_DIRS
)
1152 DEBUGF("Too many dirs open\n");
1157 if (!dircache_initialized
)
1159 pdir
->regulardir
= opendir_uncached(name
);
1160 if (!pdir
->regulardir
)
1168 pdir
->regulardir
= NULL
;
1169 cache_entry
= dircache_get_entry(name
, false, true);
1170 pdir
->entry
= cache_entry
;
1172 if (cache_entry
== NULL
)
1181 struct dircache_entry
* readdir_cached(DIR_CACHED
* dir
)
1183 struct dirent_uncached
*regentry
;
1184 struct dircache_entry
*ce
;
1189 if (dir
->regulardir
!= NULL
)
1191 regentry
= readdir_uncached(dir
->regulardir
);
1192 if (regentry
== NULL
)
1195 strncpy(dir
->secondary_entry
.d_name
, regentry
->d_name
, MAX_PATH
-1);
1196 dir
->secondary_entry
.size
= regentry
->size
;
1197 dir
->secondary_entry
.startcluster
= regentry
->startcluster
;
1198 dir
->secondary_entry
.attribute
= regentry
->attribute
;
1199 dir
->secondary_entry
.wrttime
= regentry
->wrttime
;
1200 dir
->secondary_entry
.wrtdate
= regentry
->wrtdate
;
1201 dir
->secondary_entry
.next
= NULL
;
1203 return &dir
->secondary_entry
;
1207 if (dir
->entry
== NULL
)
1211 if (ce
->name_len
== 0)
1212 dir
->entry
= ce
->next
;
1213 } while (ce
->name_len
== 0) ;
1215 dir
->entry
= ce
->next
;
1217 strncpy(dir
->secondary_entry
.d_name
, ce
->d_name
, MAX_PATH
-1);
1218 /* Can't do `dir->secondary_entry = *ce`
1219 because that modifies the d_name pointer. */
1220 dir
->secondary_entry
.size
= ce
->size
;
1221 dir
->secondary_entry
.startcluster
= ce
->startcluster
;
1222 dir
->secondary_entry
.attribute
= ce
->attribute
;
1223 dir
->secondary_entry
.wrttime
= ce
->wrttime
;
1224 dir
->secondary_entry
.wrtdate
= ce
->wrtdate
;
1225 dir
->secondary_entry
.next
= NULL
;
1226 dir
->internal_entry
= ce
;
1228 //logf("-> %s", ce->name);
1229 return &dir
->secondary_entry
;
1232 int closedir_cached(DIR_CACHED
* dir
)
1238 if (dir
->regulardir
!= NULL
)
1239 return closedir_uncached(dir
->regulardir
);
1244 int mkdir_cached(const char *name
)
1246 int rc
=mkdir_uncached(name
);
1248 dircache_mkdir(name
);
1252 int rmdir_cached(const char* name
)
1254 int rc
=rmdir_uncached(name
);
1256 dircache_rmdir(name
);