Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | /* TODO: |
| 23 | - Allow cache live updating while transparent rebuild is running. |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | #include <stdbool.h> |
Bertrik Sikken | 2843469 | 2008-04-28 16:18:04 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 33 | #include "debug.h" |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 34 | #include "system.h" |
| 35 | #include "logf.h" |
| 36 | #include "dircache.h" |
| 37 | #include "thread.h" |
| 38 | #include "kernel.h" |
| 39 | #include "usb.h" |
| 40 | #include "file.h" |
Miika Pekkarinen | 5127cfa | 2006-05-30 18:13:18 +0000 | [diff] [blame] | 41 | #include "buffer.h" |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 42 | #include "dir.h" |
Jonathan Gordon | e73f287 | 2007-09-02 13:24:51 +0000 | [diff] [blame] | 43 | #if CONFIG_RTC |
| 44 | #include "time.h" |
| 45 | #include "timefuncs.h" |
| 46 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 47 | |
| 48 | /* Queue commands. */ |
| 49 | #define DIRCACHE_BUILD 1 |
Miika Pekkarinen | 4b2aa98 | 2005-10-20 08:33:59 +0000 | [diff] [blame] | 50 | #define DIRCACHE_STOP 2 |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 51 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 52 | #define MAX_OPEN_DIRS 8 |
Bertrik Sikken | db2d61f | 2008-04-20 10:24:15 +0000 | [diff] [blame] | 53 | static DIR_CACHED opendirs[MAX_OPEN_DIRS]; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 54 | |
| 55 | static struct dircache_entry *fd_bindings[MAX_OPEN_FILES]; |
| 56 | static struct dircache_entry *dircache_root; |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 57 | #ifdef HAVE_MULTIVOLUME |
| 58 | static struct dircache_entry *append_position; |
| 59 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 60 | |
| 61 | static bool dircache_initialized = false; |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 62 | static bool dircache_initializing = false; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 63 | static bool thread_enabled = false; |
| 64 | static unsigned long allocated_size = DIRCACHE_LIMIT; |
| 65 | static unsigned long dircache_size = 0; |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 66 | static unsigned long entry_count = 0; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 67 | static unsigned long reserve_used = 0; |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 68 | static unsigned int cache_build_ticks = 0; |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 69 | static unsigned long appflags = 0; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 70 | static char dircache_cur_path[MAX_PATH*2]; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 71 | |
Michael Sevakis | 84f5c5c | 2007-10-16 22:00:51 +0000 | [diff] [blame] | 72 | static struct event_queue dircache_queue; |
Jonathan Gordon | b0d1bb8 | 2007-01-17 10:25:11 +0000 | [diff] [blame] | 73 | static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)]; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 74 | static const char dircache_thread_name[] = "dircache"; |
| 75 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 76 | static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS]; |
| 77 | static int fdbind_idx = 0; |
| 78 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 79 | /* --- Internal cache structure control functions --- */ |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Internal function to allocate a new dircache_entry from memory. |
| 83 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 84 | static struct dircache_entry* allocate_entry(void) |
| 85 | { |
| 86 | struct dircache_entry *next_entry; |
| 87 | |
| 88 | if (dircache_size > allocated_size - MAX_PATH*2) |
| 89 | { |
| 90 | logf("size limit reached"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size); |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 95 | #ifdef ROCKBOX_STRICT_ALIGN |
| 96 | /* Make sure the entry is long aligned. */ |
| 97 | if ((long)next_entry & 0x03) |
| 98 | { |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 99 | dircache_size += 4 - ((long)next_entry & 0x03); |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 100 | next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04); |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 101 | } |
| 102 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 103 | next_entry->name_len = 0; |
| 104 | next_entry->d_name = NULL; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 105 | next_entry->up = NULL; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 106 | next_entry->down = NULL; |
| 107 | next_entry->next = NULL; |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 108 | |
| 109 | dircache_size += sizeof(struct dircache_entry); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 110 | |
| 111 | return next_entry; |
| 112 | } |
| 113 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Internal function to allocate a dircache_entry and set |
| 116 | * ->next entry pointers. |
| 117 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 118 | static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce) |
| 119 | { |
| 120 | struct dircache_entry *next_entry; |
| 121 | |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 122 | if ( (next_entry = allocate_entry()) == NULL) |
| 123 | return NULL; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 124 | next_entry->up = ce->up; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 125 | ce->next = next_entry; |
| 126 | |
| 127 | return next_entry; |
| 128 | } |
| 129 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Internal function to allocate a dircache_entry and set |
| 132 | * ->down entry pointers. |
| 133 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 134 | static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce) |
| 135 | { |
| 136 | struct dircache_entry *next_entry; |
| 137 | |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 138 | if ( (next_entry = allocate_entry()) == NULL) |
| 139 | return NULL; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 140 | next_entry->up = ce; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 141 | ce->down = next_entry; |
| 142 | |
| 143 | return next_entry; |
| 144 | } |
| 145 | |
| 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]; |
| 150 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 151 | /** |
Miika Pekkarinen | e4c0ef3 | 2006-03-22 17:09:13 +0000 | [diff] [blame] | 152 | * Returns true if there is an event waiting in the queue |
| 153 | * that requires the current operation to be aborted. |
| 154 | */ |
| 155 | static bool check_event_queue(void) |
| 156 | { |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 157 | struct queue_event ev; |
Miika Pekkarinen | e4c0ef3 | 2006-03-22 17:09:13 +0000 | [diff] [blame] | 158 | |
| 159 | queue_wait_w_tmo(&dircache_queue, &ev, 0); |
| 160 | switch (ev.id) |
| 161 | { |
| 162 | case DIRCACHE_STOP: |
| 163 | case SYS_USB_CONNECTED: |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 164 | #ifdef HAVE_HOTSWAP |
| 165 | case SYS_FS_CHANGED: |
| 166 | #endif |
Miika Pekkarinen | e4c0ef3 | 2006-03-22 17:09:13 +0000 | [diff] [blame] | 167 | /* Put the event back into the queue. */ |
| 168 | queue_post(&dircache_queue, ev.id, ev.data); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /** |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 176 | * Internal function to iterate a path. |
| 177 | */ |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 178 | static int dircache_scan(IF_MV2(int volume,) struct travel_data *td) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 179 | { |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 180 | #ifdef SIMULATOR |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 181 | while ( ( td->entry = readdir_uncached(td->dir) ) ) |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 182 | #else |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 183 | while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0])) |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 184 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 185 | { |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 186 | #ifdef SIMULATOR |
| 187 | if (!strcmp(".", td->entry->d_name) || |
| 188 | !strcmp("..", td->entry->d_name)) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 189 | { |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 190 | continue; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 191 | } |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 192 | |
| 193 | td->ce->attribute = td->entry->attribute; |
Steve Bavin | 74db69e | 2007-02-01 17:15:50 +0000 | [diff] [blame] | 194 | td->ce->name_len = strlen(td->entry->d_name) + 1; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 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); |
| 200 | #else |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 201 | if (!strcmp(".", td->entry.name) || |
| 202 | !strcmp("..", td->entry.name)) |
| 203 | { |
| 204 | continue; |
| 205 | } |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 206 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 207 | td->ce->attribute = td->entry.attr; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 208 | td->ce->name_len = strlen(td->entry.name) + 1; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 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); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 215 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 216 | dircache_size += td->ce->name_len; |
Miika Pekkarinen | 7c4e0c8 | 2006-03-26 11:33:42 +0000 | [diff] [blame] | 217 | entry_count++; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 218 | |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 219 | #ifdef SIMULATOR |
| 220 | if (td->entry->attribute & ATTR_DIRECTORY) |
| 221 | #else |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 222 | if (td->entry.attr & FAT_ATTR_DIRECTORY) |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 223 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 224 | { |
| 225 | |
| 226 | td->down_entry = dircache_gen_down(td->ce); |
| 227 | if (td->down_entry == NULL) |
| 228 | return -2; |
| 229 | |
| 230 | td->pathpos = strlen(dircache_cur_path); |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 231 | strncpy(&dircache_cur_path[td->pathpos], "/", |
| 232 | sizeof(dircache_cur_path) - td->pathpos - 1); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 233 | #ifdef SIMULATOR |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 234 | strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name, |
| 235 | sizeof(dircache_cur_path) - td->pathpos - 2); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 236 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 237 | td->newdir = opendir_uncached(dircache_cur_path); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 238 | if (td->newdir == NULL) |
| 239 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 240 | logf("Failed to opendir_uncached(): %s", dircache_cur_path); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 241 | return -3; |
| 242 | } |
| 243 | #else |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 244 | strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name, |
| 245 | sizeof(dircache_cur_path) - td->pathpos - 2); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 246 | |
| 247 | td->newdir = *td->dir; |
| 248 | if (fat_opendir(IF_MV2(volume,) &td->newdir, |
| 249 | td->entry.firstcluster, td->dir) < 0 ) |
| 250 | { |
| 251 | return -3; |
| 252 | } |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 253 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 254 | |
| 255 | td->ce = dircache_gen_next(td->ce); |
| 256 | if (td->ce == NULL) |
| 257 | return -4; |
| 258 | |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | td->ce->down = NULL; |
| 263 | td->ce = dircache_gen_next(td->ce); |
| 264 | if (td->ce == NULL) |
| 265 | return -5; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 266 | |
| 267 | /* When simulator is used, it's only safe to yield here. */ |
| 268 | if (thread_enabled) |
| 269 | { |
| 270 | /* Stop if we got an external signal. */ |
| 271 | if (check_event_queue()) |
| 272 | return -6; |
| 273 | yield(); |
| 274 | } |
| 275 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 281 | /** |
| 282 | * Recursively scan the hard disk and build the cache. |
| 283 | */ |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 284 | #ifdef SIMULATOR |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 285 | static int dircache_travel(IF_MV2(int volume,) DIR_UNCACHED *dir, struct dircache_entry *ce) |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 286 | #else |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 287 | static int dircache_travel(IF_MV2(int volume,) struct fat_dir *dir, struct dircache_entry *ce) |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 288 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 289 | { |
| 290 | int depth = 0; |
| 291 | int result; |
| 292 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 293 | memset(ce, 0, sizeof(struct dircache_entry)); |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 294 | |
| 295 | #if defined(HAVE_MULTIVOLUME) && !defined(SIMULATOR) |
| 296 | if (volume > 0) |
| 297 | { |
| 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; |
| 303 | ce->size = 0; |
| 304 | append_position = dircache_gen_next(ce); |
| 305 | ce = dircache_gen_down(ce); |
| 306 | } |
| 307 | #endif |
| 308 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 309 | dir_recursion[0].dir = dir; |
| 310 | dir_recursion[0].ce = ce; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 311 | dir_recursion[0].first = ce; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 312 | |
| 313 | do { |
| 314 | //logf("=> %s", dircache_cur_path); |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 315 | result = dircache_scan(IF_MV2(volume,) &dir_recursion[depth]); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 316 | switch (result) { |
| 317 | case 0: /* Leaving the current directory. */ |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 318 | /* Add the standard . and .. entries. */ |
| 319 | ce = dir_recursion[depth].ce; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 320 | ce->d_name = "."; |
| 321 | ce->name_len = 2; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 322 | #ifdef SIMULATOR |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 323 | closedir_uncached(dir_recursion[depth].dir); |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 324 | ce->attribute = ATTR_DIRECTORY; |
| 325 | #else |
| 326 | ce->attribute = FAT_ATTR_DIRECTORY; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 327 | ce->startcluster = dir_recursion[depth].dir->file.firstcluster; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 328 | #endif |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 329 | ce->size = 0; |
| 330 | ce->down = dir_recursion[depth].first; |
| 331 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 332 | depth--; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 333 | if (depth < 0) |
| 334 | break ; |
| 335 | |
| 336 | dircache_cur_path[dir_recursion[depth].pathpos] = '\0'; |
| 337 | |
| 338 | ce = dircache_gen_next(ce); |
| 339 | if (ce == NULL) |
| 340 | { |
| 341 | logf("memory allocation error"); |
| 342 | return -3; |
| 343 | } |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 344 | #ifdef SIMULATOR |
| 345 | ce->attribute = ATTR_DIRECTORY; |
| 346 | #else |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 347 | ce->attribute = FAT_ATTR_DIRECTORY; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 348 | ce->startcluster = dir_recursion[depth].dir->file.firstcluster; |
| 349 | #endif |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 350 | ce->d_name = ".."; |
| 351 | ce->name_len = 3; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 352 | ce->size = 0; |
| 353 | ce->down = dir_recursion[depth].first; |
| 354 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 355 | break ; |
| 356 | |
| 357 | case 1: /* Going down in the directory tree. */ |
| 358 | depth++; |
| 359 | if (depth >= MAX_SCAN_DEPTH) |
| 360 | { |
| 361 | logf("Too deep directory structure"); |
| 362 | return -2; |
| 363 | } |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 364 | |
| 365 | #ifdef SIMULATOR |
| 366 | dir_recursion[depth].dir = dir_recursion[depth-1].newdir; |
| 367 | #else |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 368 | dir_recursion[depth].dir = &dir_recursion[depth-1].newdir; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 369 | #endif |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 370 | dir_recursion[depth].first = dir_recursion[depth-1].down_entry; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 371 | dir_recursion[depth].ce = dir_recursion[depth-1].down_entry; |
| 372 | break ; |
| 373 | |
| 374 | default: |
| 375 | logf("Scan failed"); |
| 376 | logf("-> %s", dircache_cur_path); |
| 377 | return -1; |
| 378 | } |
| 379 | } while (depth >= 0) ; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 384 | /** |
| 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. |
| 389 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 390 | static struct dircache_entry* dircache_get_entry(const char *path, |
| 391 | bool get_before, bool only_directories) |
| 392 | { |
| 393 | struct dircache_entry *cache_entry, *before; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 394 | char namecopy[MAX_PATH*2]; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 395 | char* part; |
| 396 | char* end; |
| 397 | |
| 398 | strncpy(namecopy, path, sizeof(namecopy) - 1); |
| 399 | cache_entry = dircache_root; |
| 400 | before = NULL; |
| 401 | |
| 402 | for ( part = strtok_r(namecopy, "/", &end); part; |
| 403 | part = strtok_r(NULL, "/", &end)) { |
| 404 | |
| 405 | /* scan dir for name */ |
| 406 | while (1) |
| 407 | { |
| 408 | if (cache_entry == NULL) |
| 409 | { |
| 410 | return NULL; |
| 411 | } |
| 412 | else if (cache_entry->name_len == 0) |
| 413 | { |
| 414 | cache_entry = cache_entry->next; |
| 415 | continue ; |
| 416 | } |
| 417 | |
| 418 | if (!strcasecmp(part, cache_entry->d_name)) |
| 419 | { |
| 420 | before = cache_entry; |
| 421 | if (cache_entry->down || only_directories) |
| 422 | cache_entry = cache_entry->down; |
| 423 | break ; |
| 424 | } |
| 425 | |
| 426 | cache_entry = cache_entry->next; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (get_before) |
| 431 | cache_entry = before; |
| 432 | |
| 433 | return cache_entry; |
| 434 | } |
| 435 | |
Miika Pekkarinen | 7b1e827 | 2007-04-07 17:48:51 +0000 | [diff] [blame] | 436 | #ifdef HAVE_EEPROM_SETTINGS |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 437 | /** |
| 438 | * Function to load the internal cache structure from disk to initialize |
| 439 | * the dircache really fast and little disk access. |
| 440 | */ |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 441 | int dircache_load(void) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 442 | { |
| 443 | struct dircache_maindata maindata; |
| 444 | int bytes_read; |
| 445 | int fd; |
| 446 | |
| 447 | if (dircache_initialized) |
| 448 | return -1; |
| 449 | |
| 450 | logf("Loading directory cache"); |
| 451 | dircache_size = 0; |
| 452 | |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 453 | fd = open(DIRCACHE_FILE, O_RDONLY); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 454 | if (fd < 0) |
| 455 | return -2; |
| 456 | |
| 457 | bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata)); |
| 458 | if (bytes_read != sizeof(struct dircache_maindata) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 459 | || maindata.size <= 0) |
| 460 | { |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 461 | logf("Dircache file header error"); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 462 | close(fd); |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 463 | remove(DIRCACHE_FILE); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 464 | return -3; |
| 465 | } |
| 466 | |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 467 | dircache_root = buffer_alloc(0); |
| 468 | if ((long)maindata.root_entry != (long)dircache_root) |
| 469 | { |
| 470 | logf("Position missmatch"); |
| 471 | close(fd); |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 472 | remove(DIRCACHE_FILE); |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 473 | return -4; |
| 474 | } |
| 475 | |
| 476 | dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE); |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 477 | entry_count = maindata.entry_count; |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 478 | appflags = maindata.appflags; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 479 | bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size)); |
| 480 | close(fd); |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 481 | remove(DIRCACHE_FILE); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 482 | |
| 483 | if (bytes_read != maindata.size) |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 484 | { |
| 485 | logf("Dircache read failed"); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 486 | return -6; |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 487 | } |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 488 | |
| 489 | /* Cache successfully loaded. */ |
| 490 | dircache_size = maindata.size; |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 491 | allocated_size = dircache_size + DIRCACHE_RESERVE; |
| 492 | reserve_used = 0; |
Magnus Holmgren | 01a010f | 2007-03-18 09:50:53 +0000 | [diff] [blame] | 493 | logf("Done, %ld KiB used", dircache_size / 1024); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 494 | dircache_initialized = true; |
| 495 | memset(fd_bindings, 0, sizeof(fd_bindings)); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 500 | /** |
| 501 | * Function to save the internal cache stucture to disk for fast loading |
| 502 | * on boot. |
| 503 | */ |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 504 | int dircache_save(void) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 505 | { |
| 506 | struct dircache_maindata maindata; |
| 507 | int fd; |
| 508 | unsigned long bytes_written; |
| 509 | |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 510 | remove(DIRCACHE_FILE); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 511 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 512 | if (!dircache_initialized) |
| 513 | return -1; |
| 514 | |
| 515 | logf("Saving directory cache"); |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 516 | fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 517 | |
| 518 | maindata.magic = DIRCACHE_MAGIC; |
| 519 | maindata.size = dircache_size; |
| 520 | maindata.root_entry = dircache_root; |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 521 | maindata.entry_count = entry_count; |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 522 | maindata.appflags = appflags; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 523 | |
| 524 | /* Save the info structure */ |
| 525 | bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata)); |
| 526 | if (bytes_written != sizeof(struct dircache_maindata)) |
| 527 | { |
| 528 | close(fd); |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 529 | logf("dircache: write failed #1"); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 530 | return -2; |
| 531 | } |
| 532 | |
| 533 | /* Dump whole directory cache to disk */ |
| 534 | bytes_written = write(fd, dircache_root, dircache_size); |
| 535 | close(fd); |
| 536 | if (bytes_written != dircache_size) |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 537 | { |
| 538 | logf("dircache: write failed #2"); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 539 | return -3; |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | #endif /* #if 0 */ |
| 545 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 546 | /** |
| 547 | * Internal function which scans the disk and creates the dircache structure. |
| 548 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 549 | static int dircache_do_rebuild(void) |
| 550 | { |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 551 | #ifdef SIMULATOR |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 552 | DIR_UNCACHED *pdir; |
Miika Pekkarinen | 3881ce9 | 2006-03-30 18:53:44 +0000 | [diff] [blame] | 553 | #else |
| 554 | struct fat_dir dir, *pdir; |
| 555 | #endif |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 556 | unsigned int start_tick; |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 557 | int i; |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 558 | |
| 559 | /* Measure how long it takes build the cache. */ |
| 560 | start_tick = current_tick; |
Steve Bavin | 12ef310 | 2007-01-12 11:10:04 +0000 | [diff] [blame] | 561 | dircache_initializing = true; |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 562 | appflags = 0; |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 563 | entry_count = 0; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 564 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 565 | memset(dircache_cur_path, 0, sizeof(dircache_cur_path)); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 566 | dircache_size = sizeof(struct dircache_entry); |
| 567 | |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 568 | #ifdef HAVE_MULTIVOLUME |
| 569 | append_position = dircache_root; |
| 570 | |
| 571 | for (i = NUM_VOLUMES; i >= 0; i--) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 572 | { |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 573 | if (fat_ismounted(i)) |
| 574 | { |
| 575 | #endif |
| 576 | #ifdef SIMULATOR |
| 577 | pdir = opendir_uncached("/"); |
| 578 | if (pdir == NULL) |
| 579 | { |
| 580 | logf("Failed to open rootdir"); |
| 581 | dircache_initializing = false; |
| 582 | return -3; |
| 583 | } |
| 584 | #else |
| 585 | #ifdef HAVE_MULTIVOLUME |
| 586 | if ( fat_opendir(IF_MV2(i,) &dir, 0, NULL) < 0 ) { |
| 587 | #else |
| 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; |
| 592 | return -3; |
| 593 | } |
| 594 | pdir = &dir; |
| 595 | #endif |
| 596 | cpu_boost(true); |
| 597 | #ifdef HAVE_MULTIVOLUME |
| 598 | if (dircache_travel(IF_MV2(i,) pdir, append_position) < 0) |
| 599 | #else |
| 600 | if (dircache_travel(IF_MV2(0,) pdir, dircache_root) < 0) |
| 601 | #endif /* HAVE_MULTIVOLUME */ |
| 602 | { |
| 603 | logf("dircache_travel failed"); |
| 604 | cpu_boost(false); |
| 605 | dircache_size = 0; |
| 606 | dircache_initializing = false; |
| 607 | return -2; |
| 608 | } |
| 609 | cpu_boost(false); |
| 610 | #ifdef HAVE_MULTIVOLUME |
| 611 | } |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 612 | } |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 613 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 614 | |
Magnus Holmgren | 01a010f | 2007-03-18 09:50:53 +0000 | [diff] [blame] | 615 | logf("Done, %ld KiB used", dircache_size / 1024); |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 616 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 617 | dircache_initialized = true; |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 618 | dircache_initializing = false; |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 619 | cache_build_ticks = current_tick - start_tick; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 620 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 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); |
Miika Pekkarinen | 6523ba4 | 2006-08-26 09:24:20 +0000 | [diff] [blame] | 625 | fdbind_idx = 0; |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 626 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 627 | if (thread_enabled) |
| 628 | { |
| 629 | if (allocated_size - dircache_size < DIRCACHE_RESERVE) |
| 630 | reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size); |
| 631 | } |
| 632 | else |
| 633 | { |
| 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; |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 638 | reserve_used = 0; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return 1; |
| 642 | } |
| 643 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 644 | /** |
| 645 | * Internal thread that controls transparent cache building. |
| 646 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 647 | static void dircache_thread(void) |
| 648 | { |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 649 | struct queue_event ev; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 650 | |
| 651 | while (1) |
| 652 | { |
| 653 | queue_wait(&dircache_queue, &ev); |
| 654 | |
| 655 | switch (ev.id) |
| 656 | { |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 657 | #ifdef HAVE_HOTSWAP |
| 658 | case SYS_FS_CHANGED: |
| 659 | if (!dircache_initialized) |
| 660 | break; |
| 661 | dircache_initialized = false; |
| 662 | #endif |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 663 | case DIRCACHE_BUILD: |
| 664 | thread_enabled = true; |
| 665 | dircache_do_rebuild(); |
| 666 | thread_enabled = false; |
| 667 | break ; |
| 668 | |
Miika Pekkarinen | 4b2aa98 | 2005-10-20 08:33:59 +0000 | [diff] [blame] | 669 | case DIRCACHE_STOP: |
| 670 | logf("Stopped the rebuilding."); |
| 671 | dircache_initialized = false; |
| 672 | break ; |
| 673 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 674 | #ifndef SIMULATOR |
| 675 | case SYS_USB_CONNECTED: |
| 676 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 677 | usb_wait_for_disconnect(&dircache_queue); |
| 678 | break ; |
| 679 | #endif |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 684 | /** |
| 685 | * Start scanning the disk to build the dircache. |
| 686 | * Either transparent or non-transparent build method is used. |
| 687 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 688 | int dircache_build(int last_size) |
| 689 | { |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 690 | if (dircache_initialized || thread_enabled) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 691 | return -3; |
| 692 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 693 | logf("Building directory cache"); |
Miika Pekkarinen | c8a9ca7 | 2006-12-22 09:11:09 +0000 | [diff] [blame] | 694 | remove(DIRCACHE_FILE); |
| 695 | |
Miika Pekkarinen | 954b732 | 2006-08-05 20:19:10 +0000 | [diff] [blame] | 696 | /* Background build, dircache has been previously allocated */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 697 | if (dircache_size > 0) |
| 698 | { |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 699 | thread_enabled = true; |
Miika Pekkarinen | 5019b32 | 2006-08-14 19:03:37 +0000 | [diff] [blame] | 700 | dircache_initializing = true; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 701 | queue_post(&dircache_queue, DIRCACHE_BUILD, 0); |
| 702 | return 2; |
| 703 | } |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 704 | |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 705 | if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT ) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 706 | { |
| 707 | allocated_size = last_size + DIRCACHE_RESERVE; |
Miika Pekkarinen | 6523ba4 | 2006-08-26 09:24:20 +0000 | [diff] [blame] | 708 | dircache_root = buffer_alloc(allocated_size); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 709 | thread_enabled = true; |
| 710 | |
| 711 | /* Start a transparent rebuild. */ |
| 712 | queue_post(&dircache_queue, DIRCACHE_BUILD, 0); |
| 713 | return 3; |
| 714 | } |
| 715 | |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 716 | dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04); |
| 717 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 718 | /* Start a non-transparent rebuild. */ |
| 719 | return dircache_do_rebuild(); |
| 720 | } |
| 721 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 722 | /** |
Miika Pekkarinen | 3b31346 | 2006-04-16 17:32:54 +0000 | [diff] [blame] | 723 | * Steal the allocated dircache buffer and disable dircache. |
| 724 | */ |
| 725 | void* dircache_steal_buffer(long *size) |
| 726 | { |
| 727 | dircache_disable(); |
| 728 | if (dircache_size == 0) |
| 729 | { |
| 730 | *size = 0; |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | *size = dircache_size + (DIRCACHE_RESERVE-reserve_used); |
| 735 | |
| 736 | return dircache_root; |
| 737 | } |
| 738 | |
| 739 | /** |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 740 | * Main initialization function that must be called before any other |
| 741 | * operations within the dircache. |
| 742 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 743 | void dircache_init(void) |
| 744 | { |
| 745 | int i; |
| 746 | |
Miika Pekkarinen | 5127cfa | 2006-05-30 18:13:18 +0000 | [diff] [blame] | 747 | dircache_initialized = false; |
Miika Pekkarinen | 5019b32 | 2006-08-14 19:03:37 +0000 | [diff] [blame] | 748 | dircache_initializing = false; |
Miika Pekkarinen | 5127cfa | 2006-05-30 18:13:18 +0000 | [diff] [blame] | 749 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 750 | memset(opendirs, 0, sizeof(opendirs)); |
| 751 | for (i = 0; i < MAX_OPEN_DIRS; i++) |
| 752 | { |
Miika Pekkarinen | d8ac607 | 2006-08-02 17:39:34 +0000 | [diff] [blame] | 753 | opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Miika Pekkarinen | a85044b | 2006-09-16 16:18:11 +0000 | [diff] [blame] | 756 | queue_init(&dircache_queue, true); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 757 | create_thread(dircache_thread, dircache_stack, |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 758 | sizeof(dircache_stack), 0, dircache_thread_name |
| 759 | IF_PRIO(, PRIORITY_BACKGROUND) |
| 760 | IF_COP(, CPU)); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 763 | /** |
| 764 | * Returns true if dircache has been initialized and is ready to be used. |
| 765 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 766 | bool dircache_is_enabled(void) |
| 767 | { |
| 768 | return dircache_initialized; |
| 769 | } |
| 770 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 771 | /** |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 772 | * Returns true if dircache is being initialized. |
| 773 | */ |
| 774 | bool dircache_is_initializing(void) |
| 775 | { |
Miika Pekkarinen | 7b1e827 | 2007-04-07 17:48:51 +0000 | [diff] [blame] | 776 | return dircache_initializing || thread_enabled; |
Miika Pekkarinen | 9cd5c3e | 2006-07-10 16:22:03 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 780 | * Set application flags used to determine if dircache is still intact. |
| 781 | */ |
| 782 | void dircache_set_appflag(long mask) |
| 783 | { |
| 784 | appflags |= mask; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Get application flags used to determine if dircache is still intact. |
| 789 | */ |
| 790 | bool dircache_get_appflag(long mask) |
| 791 | { |
Miika Pekkarinen | 7aedb02 | 2008-01-13 19:20:04 +0000 | [diff] [blame] | 792 | return dircache_is_enabled() && (appflags & mask); |
Miika Pekkarinen | 39c597b | 2008-01-13 19:13:37 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 796 | * Returns the current number of entries (directories and files) in the cache. |
| 797 | */ |
| 798 | int dircache_get_entry_count(void) |
| 799 | { |
| 800 | return entry_count; |
| 801 | } |
| 802 | |
| 803 | /** |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 804 | * Returns the allocated space for dircache (without reserve space). |
| 805 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 806 | int dircache_get_cache_size(void) |
| 807 | { |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 808 | return dircache_is_enabled() ? dircache_size : 0; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 811 | /** |
| 812 | * Returns how many bytes of the reserve allocation for live cache |
| 813 | * updates have been used. |
| 814 | */ |
Miika Pekkarinen | 8f280a3 | 2005-11-26 20:22:19 +0000 | [diff] [blame] | 815 | int dircache_get_reserve_used(void) |
| 816 | { |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 817 | return dircache_is_enabled() ? reserve_used : 0; |
Miika Pekkarinen | 8f280a3 | 2005-11-26 20:22:19 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 820 | /** |
| 821 | * Returns the time in kernel ticks that took to build the cache. |
| 822 | */ |
| 823 | int dircache_get_build_ticks(void) |
| 824 | { |
| 825 | return dircache_is_enabled() ? cache_build_ticks : 0; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Disables the dircache. Usually called on shutdown or when |
| 830 | * accepting a usb connection. |
| 831 | */ |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 832 | void dircache_disable(void) |
| 833 | { |
| 834 | int i; |
Miika Pekkarinen | 4b2aa98 | 2005-10-20 08:33:59 +0000 | [diff] [blame] | 835 | bool cache_in_use; |
| 836 | |
| 837 | if (thread_enabled) |
| 838 | queue_post(&dircache_queue, DIRCACHE_STOP, 0); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 839 | |
| 840 | while (thread_enabled) |
| 841 | sleep(1); |
| 842 | dircache_initialized = false; |
| 843 | |
Miika Pekkarinen | 4b2aa98 | 2005-10-20 08:33:59 +0000 | [diff] [blame] | 844 | logf("Waiting for cached dirs to release"); |
| 845 | do { |
| 846 | cache_in_use = false; |
| 847 | for (i = 0; i < MAX_OPEN_DIRS; i++) { |
| 848 | if (!opendirs[i].regulardir && opendirs[i].busy) |
| 849 | { |
| 850 | cache_in_use = true; |
| 851 | sleep(1); |
| 852 | break ; |
| 853 | } |
| 854 | } |
| 855 | } while (cache_in_use) ; |
Miika Pekkarinen | 5019b32 | 2006-08-14 19:03:37 +0000 | [diff] [blame] | 856 | |
Miika Pekkarinen | 4b2aa98 | 2005-10-20 08:33:59 +0000 | [diff] [blame] | 857 | logf("Cache released"); |
Miika Pekkarinen | 5019b32 | 2006-08-14 19:03:37 +0000 | [diff] [blame] | 858 | entry_count = 0; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 861 | /** |
| 862 | * Usermode function to return dircache_entry pointer to the given path. |
| 863 | */ |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 864 | const struct dircache_entry *dircache_get_entry_ptr(const char *filename) |
| 865 | { |
| 866 | if (!dircache_initialized || filename == NULL) |
| 867 | return NULL; |
| 868 | |
| 869 | return dircache_get_entry(filename, false, false); |
| 870 | } |
| 871 | |
Miika Pekkarinen | 29e04ef | 2005-11-26 20:47:10 +0000 | [diff] [blame] | 872 | /** |
| 873 | * Function to copy the full absolute path from dircache to the given buffer |
| 874 | * using the given dircache_entry pointer. |
| 875 | */ |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 876 | void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size) |
| 877 | { |
| 878 | const struct dircache_entry *down[MAX_SCAN_DEPTH]; |
| 879 | int depth = 0; |
| 880 | |
| 881 | if (size <= 0) |
| 882 | return ; |
| 883 | |
| 884 | buf[0] = '\0'; |
| 885 | |
| 886 | if (entry == NULL) |
| 887 | return ; |
| 888 | |
| 889 | do { |
| 890 | down[depth] = entry; |
| 891 | entry = entry->up; |
| 892 | depth++; |
| 893 | } while (entry != NULL && depth < MAX_SCAN_DEPTH); |
| 894 | |
| 895 | while (--depth >= 0) |
| 896 | { |
| 897 | snprintf(buf, size, "/%s", down[depth]->d_name); |
| 898 | buf += down[depth]->name_len; /* '/' + d_name */ |
| 899 | size -= down[depth]->name_len; |
| 900 | if (size <= 0) |
| 901 | break ; |
| 902 | } |
| 903 | } |
| 904 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 905 | /* --- Directory cache live updating functions --- */ |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 906 | static int block_until_ready(void) |
| 907 | { |
| 908 | /* Block until dircache has been built. */ |
Miika Pekkarinen | 7b1e827 | 2007-04-07 17:48:51 +0000 | [diff] [blame] | 909 | while (!dircache_initialized && dircache_is_initializing()) |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 910 | sleep(1); |
| 911 | |
| 912 | if (!dircache_initialized) |
| 913 | return -1; |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 918 | static struct dircache_entry* dircache_new_entry(const char *path, int attribute) |
| 919 | { |
| 920 | struct dircache_entry *entry; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 921 | char basedir[MAX_PATH*2]; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 922 | char *new; |
| 923 | long last_cache_size = dircache_size; |
| 924 | |
| 925 | strncpy(basedir, path, sizeof(basedir)-1); |
| 926 | new = strrchr(basedir, '/'); |
| 927 | if (new == NULL) |
| 928 | { |
| 929 | logf("error occurred"); |
| 930 | dircache_initialized = false; |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | *new = '\0'; |
| 935 | new++; |
| 936 | |
| 937 | entry = dircache_get_entry(basedir, false, true); |
| 938 | if (entry == NULL) |
| 939 | { |
| 940 | logf("basedir not found!"); |
Nicolas Pennequin | fa1e731 | 2007-10-02 15:35:12 +0000 | [diff] [blame] | 941 | logf("%s", basedir); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 942 | dircache_initialized = false; |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1 |
| 947 | >= DIRCACHE_RESERVE) |
| 948 | { |
| 949 | logf("not enough space"); |
| 950 | dircache_initialized = false; |
| 951 | return NULL; |
| 952 | } |
| 953 | |
| 954 | while (entry->next != NULL) |
| 955 | entry = entry->next; |
| 956 | |
| 957 | if (entry->name_len > 0) |
| 958 | entry = dircache_gen_next(entry); |
| 959 | |
| 960 | if (entry == NULL) |
| 961 | { |
| 962 | dircache_initialized = false; |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 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; |
| 970 | entry->wrtdate = 0; |
| 971 | entry->wrttime = 0; |
| 972 | entry->size = 0; |
| 973 | memcpy(entry->d_name, new, entry->name_len); |
| 974 | dircache_size += entry->name_len; |
| 975 | |
| 976 | if (attribute & ATTR_DIRECTORY) |
| 977 | { |
| 978 | logf("gen_down"); |
| 979 | dircache_gen_down(entry); |
| 980 | } |
| 981 | |
| 982 | reserve_used += dircache_size - last_cache_size; |
| 983 | |
| 984 | return entry; |
| 985 | } |
| 986 | |
| 987 | void dircache_bind(int fd, const char *path) |
| 988 | { |
| 989 | struct dircache_entry *entry; |
| 990 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 991 | /* Queue requests until dircache has been built. */ |
Miika Pekkarinen | 7b1e827 | 2007-04-07 17:48:51 +0000 | [diff] [blame] | 992 | if (!dircache_initialized && dircache_is_initializing()) |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 993 | { |
| 994 | if (fdbind_idx >= MAX_PENDING_BINDINGS) |
| 995 | return ; |
| 996 | strncpy(fdbind_cache[fdbind_idx].path, path, |
| 997 | sizeof(fdbind_cache[fdbind_idx].path)-1); |
| 998 | fdbind_cache[fdbind_idx].fd = fd; |
| 999 | fdbind_idx++; |
| 1000 | return ; |
| 1001 | } |
| 1002 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1003 | if (!dircache_initialized) |
| 1004 | return ; |
| 1005 | |
| 1006 | logf("bind: %d/%s", fd, path); |
| 1007 | entry = dircache_get_entry(path, false, false); |
| 1008 | if (entry == NULL) |
| 1009 | { |
| 1010 | logf("not found!"); |
| 1011 | dircache_initialized = false; |
| 1012 | return ; |
| 1013 | } |
| 1014 | |
| 1015 | fd_bindings[fd] = entry; |
| 1016 | } |
| 1017 | |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1018 | void dircache_update_filesize(int fd, long newsize, long startcluster) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1019 | { |
| 1020 | if (!dircache_initialized || fd < 0) |
| 1021 | return ; |
| 1022 | |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1023 | if (fd_bindings[fd] == NULL) |
| 1024 | { |
| 1025 | logf("dircache fd access error"); |
| 1026 | dircache_initialized = false; |
| 1027 | return ; |
| 1028 | } |
| 1029 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1030 | fd_bindings[fd]->size = newsize; |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1031 | fd_bindings[fd]->startcluster = startcluster; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1032 | } |
Jonathan Gordon | e73f287 | 2007-09-02 13:24:51 +0000 | [diff] [blame] | 1033 | void dircache_update_filetime(int fd) |
| 1034 | { |
| 1035 | #if CONFIG_RTC == 0 |
| 1036 | (void)fd; |
| 1037 | #else |
| 1038 | short year; |
| 1039 | struct tm *now = get_time(); |
| 1040 | if (!dircache_initialized || fd < 0) |
| 1041 | return ; |
| 1042 | |
| 1043 | if (fd_bindings[fd] == NULL) |
| 1044 | { |
| 1045 | logf("dircache fd access error"); |
| 1046 | dircache_initialized = false; |
| 1047 | return ; |
| 1048 | } |
| 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)); |
| 1056 | #endif |
| 1057 | } |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1058 | |
| 1059 | void dircache_mkdir(const char *path) |
| 1060 | { /* Test ok. */ |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1061 | if (block_until_ready()) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1062 | return ; |
| 1063 | |
| 1064 | logf("mkdir: %s", path); |
| 1065 | dircache_new_entry(path, ATTR_DIRECTORY); |
| 1066 | } |
| 1067 | |
| 1068 | void dircache_rmdir(const char *path) |
| 1069 | { /* Test ok. */ |
| 1070 | struct dircache_entry *entry; |
| 1071 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1072 | if (block_until_ready()) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1073 | return ; |
| 1074 | |
| 1075 | logf("rmdir: %s", path); |
| 1076 | entry = dircache_get_entry(path, true, true); |
| 1077 | if (entry == NULL) |
| 1078 | { |
| 1079 | logf("not found!"); |
| 1080 | dircache_initialized = false; |
| 1081 | return ; |
| 1082 | } |
| 1083 | |
| 1084 | entry->down = NULL; |
| 1085 | entry->name_len = 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* Remove a file from cache */ |
| 1089 | void dircache_remove(const char *name) |
| 1090 | { /* Test ok. */ |
| 1091 | struct dircache_entry *entry; |
| 1092 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1093 | if (block_until_ready()) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1094 | return ; |
| 1095 | |
| 1096 | logf("remove: %s", name); |
| 1097 | |
| 1098 | entry = dircache_get_entry(name, false, false); |
| 1099 | |
| 1100 | if (entry == NULL) |
| 1101 | { |
| 1102 | logf("not found!"); |
| 1103 | dircache_initialized = false; |
| 1104 | return ; |
| 1105 | } |
| 1106 | |
| 1107 | entry->name_len = 0; |
| 1108 | } |
| 1109 | |
| 1110 | void dircache_rename(const char *oldpath, const char *newpath) |
| 1111 | { /* Test ok. */ |
| 1112 | struct dircache_entry *entry, *newentry; |
Miika Pekkarinen | 10f2f89 | 2005-11-18 19:28:22 +0000 | [diff] [blame] | 1113 | struct dircache_entry oldentry; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 1114 | char absolute_path[MAX_PATH*2]; |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 1115 | char *p; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1116 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1117 | if (block_until_ready()) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1118 | return ; |
| 1119 | |
| 1120 | logf("rename: %s->%s", oldpath, newpath); |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 1121 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1122 | entry = dircache_get_entry(oldpath, true, false); |
| 1123 | if (entry == NULL) |
| 1124 | { |
| 1125 | logf("not found!"); |
| 1126 | dircache_initialized = false; |
| 1127 | return ; |
| 1128 | } |
| 1129 | |
| 1130 | /* Delete the old entry. */ |
| 1131 | entry->name_len = 0; |
| 1132 | |
Miika Pekkarinen | 10f2f89 | 2005-11-18 19:28:22 +0000 | [diff] [blame] | 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. */ |
| 1135 | oldentry = *entry; |
| 1136 | |
Miika Pekkarinen | 871575f | 2006-02-28 11:41:35 +0000 | [diff] [blame] | 1137 | /* Generate the absolute path for destination if necessary. */ |
| 1138 | if (newpath[0] != '/') |
| 1139 | { |
| 1140 | strncpy(absolute_path, oldpath, sizeof(absolute_path)-1); |
| 1141 | p = strrchr(absolute_path, '/'); |
| 1142 | if (!p) |
| 1143 | { |
| 1144 | logf("Invalid path"); |
| 1145 | dircache_initialized = false; |
| 1146 | return ; |
| 1147 | } |
| 1148 | |
| 1149 | *p = '\0'; |
| 1150 | strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p)); |
| 1151 | newpath = absolute_path; |
| 1152 | } |
| 1153 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1154 | newentry = dircache_new_entry(newpath, entry->attribute); |
| 1155 | if (newentry == NULL) |
| 1156 | { |
| 1157 | dircache_initialized = false; |
| 1158 | return ; |
| 1159 | } |
| 1160 | |
Miika Pekkarinen | 10f2f89 | 2005-11-18 19:28:22 +0000 | [diff] [blame] | 1161 | newentry->down = oldentry.down; |
Miika Pekkarinen | 10f2f89 | 2005-11-18 19:28:22 +0000 | [diff] [blame] | 1162 | newentry->size = oldentry.size; |
| 1163 | newentry->startcluster = oldentry.startcluster; |
| 1164 | newentry->wrttime = oldentry.wrttime; |
| 1165 | newentry->wrtdate = oldentry.wrtdate; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1168 | void dircache_add_file(const char *path, long startcluster) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1169 | { |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1170 | struct dircache_entry *entry; |
| 1171 | |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1172 | if (block_until_ready()) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1173 | return ; |
Miika Pekkarinen | 6ce466e | 2006-03-29 09:38:45 +0000 | [diff] [blame] | 1174 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1175 | logf("add file: %s", path); |
Miika Pekkarinen | 2d93495 | 2006-03-28 11:51:12 +0000 | [diff] [blame] | 1176 | entry = dircache_new_entry(path, 0); |
| 1177 | if (entry == NULL) |
| 1178 | return ; |
| 1179 | |
| 1180 | entry->startcluster = startcluster; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1183 | DIR_CACHED* opendir_cached(const char* name) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1184 | { |
| 1185 | struct dircache_entry *cache_entry; |
| 1186 | int dd; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1187 | DIR_CACHED* pdir = opendirs; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1188 | |
| 1189 | if ( name[0] != '/' ) |
| 1190 | { |
| 1191 | DEBUGF("Only absolute paths supported right now\n"); |
| 1192 | return NULL; |
| 1193 | } |
| 1194 | |
| 1195 | /* find a free dir descriptor */ |
| 1196 | for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++) |
| 1197 | if ( !pdir->busy ) |
| 1198 | break; |
| 1199 | |
| 1200 | if ( dd == MAX_OPEN_DIRS ) |
| 1201 | { |
| 1202 | DEBUGF("Too many dirs open\n"); |
| 1203 | errno = EMFILE; |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | |
| 1207 | if (!dircache_initialized) |
| 1208 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1209 | pdir->regulardir = opendir_uncached(name); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1210 | if (!pdir->regulardir) |
| 1211 | return NULL; |
| 1212 | |
| 1213 | pdir->busy = true; |
| 1214 | return pdir; |
| 1215 | } |
| 1216 | |
| 1217 | pdir->busy = true; |
| 1218 | pdir->regulardir = NULL; |
| 1219 | cache_entry = dircache_get_entry(name, false, true); |
| 1220 | pdir->entry = cache_entry; |
| 1221 | |
| 1222 | if (cache_entry == NULL) |
| 1223 | { |
| 1224 | pdir->busy = false; |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | |
| 1228 | return pdir; |
| 1229 | } |
| 1230 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1231 | struct dircache_entry* readdir_cached(DIR_CACHED* dir) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1232 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1233 | struct dirent_uncached *regentry; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1234 | struct dircache_entry *ce; |
| 1235 | |
| 1236 | if (!dir->busy) |
| 1237 | return NULL; |
| 1238 | |
Miika Pekkarinen | 20bc58d | 2005-10-09 07:32:23 +0000 | [diff] [blame] | 1239 | if (dir->regulardir != NULL) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1240 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1241 | regentry = readdir_uncached(dir->regulardir); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1242 | if (regentry == NULL) |
| 1243 | return NULL; |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 1244 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 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; |
| 1252 | |
| 1253 | return &dir->secondary_entry; |
| 1254 | } |
| 1255 | |
| 1256 | do { |
| 1257 | if (dir->entry == NULL) |
| 1258 | return NULL; |
| 1259 | |
| 1260 | ce = dir->entry; |
| 1261 | if (ce->name_len == 0) |
| 1262 | dir->entry = ce->next; |
| 1263 | } while (ce->name_len == 0) ; |
| 1264 | |
| 1265 | dir->entry = ce->next; |
| 1266 | |
Miika Pekkarinen | b76e8c0 | 2005-10-08 18:41:11 +0000 | [diff] [blame] | 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; |
Miika Pekkarinen | de281db | 2006-01-31 10:08:53 +0000 | [diff] [blame] | 1276 | dir->internal_entry = ce; |
Miika Pekkarinen | b76e8c0 | 2005-10-08 18:41:11 +0000 | [diff] [blame] | 1277 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1278 | //logf("-> %s", ce->name); |
Miika Pekkarinen | b76e8c0 | 2005-10-08 18:41:11 +0000 | [diff] [blame] | 1279 | return &dir->secondary_entry; |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1282 | int closedir_cached(DIR_CACHED* dir) |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1283 | { |
Miika Pekkarinen | 20bc58d | 2005-10-09 07:32:23 +0000 | [diff] [blame] | 1284 | if (!dir->busy) |
| 1285 | return -1; |
| 1286 | |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1287 | dir->busy=false; |
Miika Pekkarinen | 20bc58d | 2005-10-09 07:32:23 +0000 | [diff] [blame] | 1288 | if (dir->regulardir != NULL) |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1289 | return closedir_uncached(dir->regulardir); |
Miika Pekkarinen | ab78b04 | 2005-10-07 17:38:05 +0000 | [diff] [blame] | 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 1294 | int mkdir_cached(const char *name) |
| 1295 | { |
| 1296 | int rc=mkdir_uncached(name); |
| 1297 | if (rc >= 0) |
| 1298 | dircache_mkdir(name); |
| 1299 | return(rc); |
| 1300 | } |
| 1301 | |
| 1302 | int rmdir_cached(const char* name) |
| 1303 | { |
| 1304 | int rc=rmdir_uncached(name); |
| 1305 | if(rc>=0) |
| 1306 | dircache_rmdir(name); |
| 1307 | return(rc); |
| 1308 | } |