2005-10-07 17:38:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
- Allow cache live updating while transparent rebuild is running.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "dir.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "atoi.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "dircache.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "file.h"
|
2006-05-30 18:13:18 +00:00
|
|
|
#include "buffer.h"
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
/* Queue commands. */
|
|
|
|
#define DIRCACHE_BUILD 1
|
2005-10-20 08:33:59 +00:00
|
|
|
#define DIRCACHE_STOP 2
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
#define MAX_OPEN_DIRS 8
|
|
|
|
DIRCACHED opendirs[MAX_OPEN_DIRS];
|
|
|
|
|
|
|
|
static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
|
|
|
|
static struct dircache_entry *dircache_root;
|
|
|
|
|
|
|
|
static bool dircache_initialized = false;
|
2006-03-29 09:38:45 +00:00
|
|
|
static bool dircache_initializing = false;
|
2005-10-07 17:38:05 +00:00
|
|
|
static bool thread_enabled = false;
|
|
|
|
static unsigned long allocated_size = DIRCACHE_LIMIT;
|
|
|
|
static unsigned long dircache_size = 0;
|
2006-01-31 10:08:53 +00:00
|
|
|
static unsigned long entry_count = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
static unsigned long reserve_used = 0;
|
2005-11-26 20:47:10 +00:00
|
|
|
static unsigned int cache_build_ticks = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
static char dircache_cur_path[MAX_PATH*2];
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
static struct event_queue dircache_queue;
|
2007-01-17 10:25:11 +00:00
|
|
|
static long dircache_stack[(DEFAULT_STACK_SIZE + 0x900)/sizeof(long)];
|
2005-10-07 17:38:05 +00:00
|
|
|
static const char dircache_thread_name[] = "dircache";
|
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
static struct fdbind_queue fdbind_cache[MAX_PENDING_BINDINGS];
|
|
|
|
static int fdbind_idx = 0;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
/* --- Internal cache structure control functions --- */
|
2005-11-26 20:47:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function to allocate a new dircache_entry from memory.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry* allocate_entry(void)
|
|
|
|
{
|
|
|
|
struct dircache_entry *next_entry;
|
|
|
|
|
|
|
|
if (dircache_size > allocated_size - MAX_PATH*2)
|
|
|
|
{
|
|
|
|
logf("size limit reached");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_entry = (struct dircache_entry *)((char *)dircache_root+dircache_size);
|
2006-01-31 10:08:53 +00:00
|
|
|
#ifdef ROCKBOX_STRICT_ALIGN
|
|
|
|
/* Make sure the entry is long aligned. */
|
|
|
|
if ((long)next_entry & 0x03)
|
|
|
|
{
|
|
|
|
dircache_size += 4 - ((long)next_entry & 0x03);
|
2006-03-26 11:33:42 +00:00
|
|
|
next_entry = (struct dircache_entry *)(((long)next_entry & ~0x03) + 0x04);
|
2006-01-31 10:08:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
next_entry->name_len = 0;
|
|
|
|
next_entry->d_name = NULL;
|
2005-11-17 19:31:29 +00:00
|
|
|
next_entry->up = NULL;
|
2005-10-07 17:38:05 +00:00
|
|
|
next_entry->down = NULL;
|
|
|
|
next_entry->next = NULL;
|
2006-01-31 10:08:53 +00:00
|
|
|
|
|
|
|
dircache_size += sizeof(struct dircache_entry);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
return next_entry;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal function to allocate a dircache_entry and set
|
|
|
|
* ->next entry pointers.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry* dircache_gen_next(struct dircache_entry *ce)
|
|
|
|
{
|
|
|
|
struct dircache_entry *next_entry;
|
|
|
|
|
2006-03-30 18:53:44 +00:00
|
|
|
if ( (next_entry = allocate_entry()) == NULL)
|
|
|
|
return NULL;
|
2005-11-17 19:31:29 +00:00
|
|
|
next_entry->up = ce->up;
|
2005-10-07 17:38:05 +00:00
|
|
|
ce->next = next_entry;
|
|
|
|
|
|
|
|
return next_entry;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/*
|
|
|
|
* Internal function to allocate a dircache_entry and set
|
|
|
|
* ->down entry pointers.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry* dircache_gen_down(struct dircache_entry *ce)
|
|
|
|
{
|
|
|
|
struct dircache_entry *next_entry;
|
|
|
|
|
2006-03-30 18:53:44 +00:00
|
|
|
if ( (next_entry = allocate_entry()) == NULL)
|
|
|
|
return NULL;
|
2005-11-17 19:31:29 +00:00
|
|
|
next_entry->up = ce;
|
2005-10-07 17:38:05 +00:00
|
|
|
ce->down = next_entry;
|
|
|
|
|
|
|
|
return next_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This will eat ~30 KiB of memory!
|
|
|
|
* We should probably use that as additional reserve buffer in future. */
|
|
|
|
#define MAX_SCAN_DEPTH 16
|
|
|
|
static struct travel_data dir_recursion[MAX_SCAN_DEPTH];
|
|
|
|
|
2006-03-22 17:09:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if there is an event waiting in the queue
|
|
|
|
* that requires the current operation to be aborted.
|
|
|
|
*/
|
|
|
|
static bool check_event_queue(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
queue_wait_w_tmo(&dircache_queue, &ev, 0);
|
|
|
|
switch (ev.id)
|
|
|
|
{
|
|
|
|
case DIRCACHE_STOP:
|
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
/* Put the event back into the queue. */
|
|
|
|
queue_post(&dircache_queue, ev.id, ev.data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal function to iterate a path.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static int dircache_scan(struct travel_data *td)
|
|
|
|
{
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
while ( ( td->entry = readdir(td->dir) ) )
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
while ( (fat_getnext(td->dir, &td->entry) >= 0) && (td->entry.name[0]))
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
if (!strcmp(".", td->entry->d_name) ||
|
|
|
|
!strcmp("..", td->entry->d_name))
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2006-03-30 18:53:44 +00:00
|
|
|
continue;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2006-03-30 18:53:44 +00:00
|
|
|
|
|
|
|
td->ce->attribute = td->entry->attribute;
|
2006-11-10 08:03:33 +00:00
|
|
|
td->ce->name_len = strlen(td->entry->d_name);
|
2006-03-30 18:53:44 +00:00
|
|
|
td->ce->d_name = ((char *)dircache_root+dircache_size);
|
|
|
|
td->ce->size = td->entry->size;
|
|
|
|
td->ce->wrtdate = td->entry->wrtdate;
|
|
|
|
td->ce->wrttime = td->entry->wrttime;
|
|
|
|
memcpy(td->ce->d_name, td->entry->d_name, td->ce->name_len);
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
if (!strcmp(".", td->entry.name) ||
|
|
|
|
!strcmp("..", td->entry.name))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2006-02-28 11:41:35 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
td->ce->attribute = td->entry.attr;
|
2006-11-10 08:03:33 +00:00
|
|
|
td->ce->name_len = strlen(td->entry.name) + 1;
|
2005-10-07 17:38:05 +00:00
|
|
|
td->ce->d_name = ((char *)dircache_root+dircache_size);
|
|
|
|
td->ce->startcluster = td->entry.firstcluster;
|
|
|
|
td->ce->size = td->entry.filesize;
|
|
|
|
td->ce->wrtdate = td->entry.wrtdate;
|
|
|
|
td->ce->wrttime = td->entry.wrttime;
|
|
|
|
memcpy(td->ce->d_name, td->entry.name, td->ce->name_len);
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_size += td->ce->name_len;
|
2006-03-26 11:33:42 +00:00
|
|
|
entry_count++;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
if (td->entry->attribute & ATTR_DIRECTORY)
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
if (td->entry.attr & FAT_ATTR_DIRECTORY)
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
td->down_entry = dircache_gen_down(td->ce);
|
|
|
|
if (td->down_entry == NULL)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
td->pathpos = strlen(dircache_cur_path);
|
2006-11-10 08:03:33 +00:00
|
|
|
strncpy(&dircache_cur_path[td->pathpos], "/",
|
|
|
|
sizeof(dircache_cur_path) - td->pathpos - 1);
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
2006-11-10 08:03:33 +00:00
|
|
|
strncpy(&dircache_cur_path[td->pathpos+1], td->entry->d_name,
|
|
|
|
sizeof(dircache_cur_path) - td->pathpos - 2);
|
2006-03-30 18:53:44 +00:00
|
|
|
|
|
|
|
td->newdir = opendir(dircache_cur_path);
|
|
|
|
if (td->newdir == NULL)
|
|
|
|
{
|
|
|
|
logf("Failed to opendir(): %s", dircache_cur_path);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
#else
|
2006-11-10 08:03:33 +00:00
|
|
|
strncpy(&dircache_cur_path[td->pathpos+1], td->entry.name,
|
|
|
|
sizeof(dircache_cur_path) - td->pathpos - 2);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
td->newdir = *td->dir;
|
|
|
|
if (fat_opendir(IF_MV2(volume,) &td->newdir,
|
|
|
|
td->entry.firstcluster, td->dir) < 0 )
|
|
|
|
{
|
|
|
|
return -3;
|
|
|
|
}
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
td->ce = dircache_gen_next(td->ce);
|
|
|
|
if (td->ce == NULL)
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
td->ce->down = NULL;
|
|
|
|
td->ce = dircache_gen_next(td->ce);
|
|
|
|
if (td->ce == NULL)
|
|
|
|
return -5;
|
2006-03-30 18:53:44 +00:00
|
|
|
|
|
|
|
/* When simulator is used, it's only safe to yield here. */
|
|
|
|
if (thread_enabled)
|
|
|
|
{
|
|
|
|
/* Stop if we got an external signal. */
|
|
|
|
if (check_event_queue())
|
|
|
|
return -6;
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Recursively scan the hard disk and build the cache.
|
|
|
|
*/
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
static int dircache_travel(DIR *dir, struct dircache_entry *ce)
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
static int dircache_travel(struct fat_dir *dir, struct dircache_entry *ce)
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
int result;
|
|
|
|
|
2005-11-17 19:31:29 +00:00
|
|
|
memset(ce, 0, sizeof(struct dircache_entry));
|
2005-10-07 17:38:05 +00:00
|
|
|
dir_recursion[0].dir = dir;
|
|
|
|
dir_recursion[0].ce = ce;
|
2006-02-28 11:41:35 +00:00
|
|
|
dir_recursion[0].first = ce;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
//logf("=> %s", dircache_cur_path);
|
|
|
|
result = dircache_scan(&dir_recursion[depth]);
|
|
|
|
switch (result) {
|
|
|
|
case 0: /* Leaving the current directory. */
|
2006-02-28 11:41:35 +00:00
|
|
|
/* Add the standard . and .. entries. */
|
|
|
|
ce = dir_recursion[depth].ce;
|
|
|
|
ce->d_name = ".";
|
|
|
|
ce->name_len = 2;
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
closedir(dir_recursion[depth].dir);
|
|
|
|
ce->attribute = ATTR_DIRECTORY;
|
|
|
|
#else
|
|
|
|
ce->attribute = FAT_ATTR_DIRECTORY;
|
2006-02-28 11:41:35 +00:00
|
|
|
ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2006-02-28 11:41:35 +00:00
|
|
|
ce->size = 0;
|
|
|
|
ce->down = dir_recursion[depth].first;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
depth--;
|
2006-02-28 11:41:35 +00:00
|
|
|
if (depth < 0)
|
|
|
|
break ;
|
|
|
|
|
|
|
|
dircache_cur_path[dir_recursion[depth].pathpos] = '\0';
|
|
|
|
|
|
|
|
ce = dircache_gen_next(ce);
|
|
|
|
if (ce == NULL)
|
|
|
|
{
|
|
|
|
logf("memory allocation error");
|
|
|
|
return -3;
|
|
|
|
}
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
ce->attribute = ATTR_DIRECTORY;
|
|
|
|
#else
|
2006-02-28 11:41:35 +00:00
|
|
|
ce->attribute = FAT_ATTR_DIRECTORY;
|
2006-03-30 18:53:44 +00:00
|
|
|
ce->startcluster = dir_recursion[depth].dir->file.firstcluster;
|
|
|
|
#endif
|
2006-02-28 11:41:35 +00:00
|
|
|
ce->d_name = "..";
|
|
|
|
ce->name_len = 3;
|
|
|
|
ce->size = 0;
|
|
|
|
ce->down = dir_recursion[depth].first;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
break ;
|
|
|
|
|
|
|
|
case 1: /* Going down in the directory tree. */
|
|
|
|
depth++;
|
|
|
|
if (depth >= MAX_SCAN_DEPTH)
|
|
|
|
{
|
|
|
|
logf("Too deep directory structure");
|
|
|
|
return -2;
|
|
|
|
}
|
2006-03-30 18:53:44 +00:00
|
|
|
|
|
|
|
#ifdef SIMULATOR
|
|
|
|
dir_recursion[depth].dir = dir_recursion[depth-1].newdir;
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
dir_recursion[depth].dir = &dir_recursion[depth-1].newdir;
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2006-02-28 11:41:35 +00:00
|
|
|
dir_recursion[depth].first = dir_recursion[depth-1].down_entry;
|
2005-10-07 17:38:05 +00:00
|
|
|
dir_recursion[depth].ce = dir_recursion[depth-1].down_entry;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
default:
|
|
|
|
logf("Scan failed");
|
|
|
|
logf("-> %s", dircache_cur_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (depth >= 0) ;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal function to get a pointer to dircache_entry for a given filename.
|
|
|
|
* path: Absolute path to a file or directory.
|
|
|
|
* get_before: Returns the cache pointer before the last valid entry found.
|
|
|
|
* only_directories: Match only filenames which are a directory type.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry* dircache_get_entry(const char *path,
|
|
|
|
bool get_before, bool only_directories)
|
|
|
|
{
|
|
|
|
struct dircache_entry *cache_entry, *before;
|
2006-11-10 08:03:33 +00:00
|
|
|
char namecopy[MAX_PATH*2];
|
2005-10-07 17:38:05 +00:00
|
|
|
char* part;
|
|
|
|
char* end;
|
|
|
|
|
|
|
|
strncpy(namecopy, path, sizeof(namecopy) - 1);
|
|
|
|
cache_entry = dircache_root;
|
|
|
|
before = NULL;
|
|
|
|
|
|
|
|
for ( part = strtok_r(namecopy, "/", &end); part;
|
|
|
|
part = strtok_r(NULL, "/", &end)) {
|
|
|
|
|
|
|
|
/* scan dir for name */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (cache_entry == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (cache_entry->name_len == 0)
|
|
|
|
{
|
|
|
|
cache_entry = cache_entry->next;
|
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(part, cache_entry->d_name))
|
|
|
|
{
|
|
|
|
before = cache_entry;
|
|
|
|
if (cache_entry->down || only_directories)
|
|
|
|
cache_entry = cache_entry->down;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_entry = cache_entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_before)
|
|
|
|
cache_entry = before;
|
|
|
|
|
|
|
|
return cache_entry;
|
|
|
|
}
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
#if 1
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Function to load the internal cache structure from disk to initialize
|
|
|
|
* the dircache really fast and little disk access.
|
|
|
|
*/
|
2006-12-22 09:11:09 +00:00
|
|
|
int dircache_load(void)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
struct dircache_maindata maindata;
|
|
|
|
int bytes_read;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (dircache_initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
logf("Loading directory cache");
|
|
|
|
dircache_size = 0;
|
|
|
|
|
2006-12-22 09:11:09 +00:00
|
|
|
fd = open(DIRCACHE_FILE, O_RDONLY);
|
2005-10-07 17:38:05 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
bytes_read = read(fd, &maindata, sizeof(struct dircache_maindata));
|
|
|
|
if (bytes_read != sizeof(struct dircache_maindata)
|
|
|
|
|| maindata.size <= 0)
|
|
|
|
{
|
2006-08-05 20:19:10 +00:00
|
|
|
logf("Dircache file header error");
|
2005-10-07 17:38:05 +00:00
|
|
|
close(fd);
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
2005-10-07 17:38:05 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
dircache_root = buffer_alloc(0);
|
|
|
|
if ((long)maindata.root_entry != (long)dircache_root)
|
|
|
|
{
|
|
|
|
logf("Position missmatch");
|
|
|
|
close(fd);
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
2006-08-05 20:19:10 +00:00
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
dircache_root = buffer_alloc(maindata.size + DIRCACHE_RESERVE);
|
2006-01-31 10:08:53 +00:00
|
|
|
entry_count = maindata.entry_count;
|
2005-10-07 17:38:05 +00:00
|
|
|
bytes_read = read(fd, dircache_root, MIN(DIRCACHE_LIMIT, maindata.size));
|
|
|
|
close(fd);
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
if (bytes_read != maindata.size)
|
2006-08-05 20:19:10 +00:00
|
|
|
{
|
|
|
|
logf("Dircache read failed");
|
2005-10-07 17:38:05 +00:00
|
|
|
return -6;
|
2006-08-05 20:19:10 +00:00
|
|
|
}
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
/* Cache successfully loaded. */
|
|
|
|
dircache_size = maindata.size;
|
2006-08-05 20:19:10 +00:00
|
|
|
allocated_size = dircache_size + DIRCACHE_RESERVE;
|
|
|
|
reserve_used = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
logf("Done, %d KiB used", dircache_size / 1024);
|
|
|
|
dircache_initialized = true;
|
|
|
|
memset(fd_bindings, 0, sizeof(fd_bindings));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Function to save the internal cache stucture to disk for fast loading
|
|
|
|
* on boot.
|
|
|
|
*/
|
2006-12-22 09:11:09 +00:00
|
|
|
int dircache_save(void)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
struct dircache_maindata maindata;
|
|
|
|
int fd;
|
|
|
|
unsigned long bytes_written;
|
|
|
|
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
while (thread_enabled)
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
if (!dircache_initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
logf("Saving directory cache");
|
2006-12-22 09:11:09 +00:00
|
|
|
fd = open(DIRCACHE_FILE, O_WRONLY | O_CREAT | O_TRUNC);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
maindata.magic = DIRCACHE_MAGIC;
|
|
|
|
maindata.size = dircache_size;
|
|
|
|
maindata.root_entry = dircache_root;
|
2006-01-31 10:08:53 +00:00
|
|
|
maindata.entry_count = entry_count;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
/* Save the info structure */
|
|
|
|
bytes_written = write(fd, &maindata, sizeof(struct dircache_maindata));
|
|
|
|
if (bytes_written != sizeof(struct dircache_maindata))
|
|
|
|
{
|
|
|
|
close(fd);
|
2006-08-05 20:19:10 +00:00
|
|
|
logf("dircache: write failed #1");
|
2005-10-07 17:38:05 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump whole directory cache to disk */
|
|
|
|
bytes_written = write(fd, dircache_root, dircache_size);
|
|
|
|
close(fd);
|
|
|
|
if (bytes_written != dircache_size)
|
2006-08-05 20:19:10 +00:00
|
|
|
{
|
|
|
|
logf("dircache: write failed #2");
|
2005-10-07 17:38:05 +00:00
|
|
|
return -3;
|
2006-08-05 20:19:10 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* #if 0 */
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal function which scans the disk and creates the dircache structure.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static int dircache_do_rebuild(void)
|
|
|
|
{
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
DIR *pdir;
|
|
|
|
#else
|
|
|
|
struct fat_dir dir, *pdir;
|
|
|
|
#endif
|
2005-11-26 20:47:10 +00:00
|
|
|
unsigned int start_tick;
|
2006-03-29 09:38:45 +00:00
|
|
|
int i;
|
2005-11-26 20:47:10 +00:00
|
|
|
|
|
|
|
/* Measure how long it takes build the cache. */
|
|
|
|
start_tick = current_tick;
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
2007-01-12 11:10:04 +00:00
|
|
|
dircache_initializing = true;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2006-03-30 18:53:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
pdir = opendir("/");
|
|
|
|
if (pdir == NULL)
|
|
|
|
{
|
|
|
|
logf("Failed to open rootdir");
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
if ( fat_opendir(IF_MV2(volume,) &dir, 0, NULL) < 0 ) {
|
|
|
|
logf("Failed opening root dir");
|
2006-03-29 09:38:45 +00:00
|
|
|
dircache_initializing = false;
|
2005-10-07 17:38:05 +00:00
|
|
|
return -3;
|
|
|
|
}
|
2006-03-30 18:53:44 +00:00
|
|
|
pdir = &dir;
|
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2006-11-10 08:03:33 +00:00
|
|
|
memset(dircache_cur_path, 0, sizeof(dircache_cur_path));
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_size = sizeof(struct dircache_entry);
|
|
|
|
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(true);
|
2006-03-30 18:53:44 +00:00
|
|
|
if (dircache_travel(pdir, dircache_root) < 0)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
logf("dircache_travel failed");
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_size = 0;
|
2006-03-29 09:38:45 +00:00
|
|
|
dircache_initializing = false;
|
2005-10-07 17:38:05 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2006-12-05 20:01:48 +00:00
|
|
|
cpu_boost(false);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
logf("Done, %d KiB used", dircache_size / 1024);
|
2006-03-29 09:38:45 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_initialized = true;
|
2006-03-29 09:38:45 +00:00
|
|
|
dircache_initializing = false;
|
2005-11-26 20:47:10 +00:00
|
|
|
cache_build_ticks = current_tick - start_tick;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
/* Initialized fd bindings. */
|
|
|
|
memset(fd_bindings, 0, sizeof(fd_bindings));
|
|
|
|
for (i = 0; i < fdbind_idx; i++)
|
|
|
|
dircache_bind(fdbind_cache[i].fd, fdbind_cache[i].path);
|
2006-08-26 09:24:20 +00:00
|
|
|
fdbind_idx = 0;
|
2006-03-29 09:38:45 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
if (thread_enabled)
|
|
|
|
{
|
|
|
|
if (allocated_size - dircache_size < DIRCACHE_RESERVE)
|
|
|
|
reserve_used = DIRCACHE_RESERVE - (allocated_size - dircache_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We have to long align the audiobuf to keep the buffer access fast. */
|
|
|
|
audiobuf += (long)((dircache_size & ~0x03) + 0x04);
|
|
|
|
audiobuf += DIRCACHE_RESERVE;
|
|
|
|
allocated_size = dircache_size + DIRCACHE_RESERVE;
|
2006-04-16 17:32:54 +00:00
|
|
|
reserve_used = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal thread that controls transparent cache building.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
static void dircache_thread(void)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
queue_wait(&dircache_queue, &ev);
|
|
|
|
|
|
|
|
switch (ev.id)
|
|
|
|
{
|
|
|
|
case DIRCACHE_BUILD:
|
|
|
|
thread_enabled = true;
|
|
|
|
dircache_do_rebuild();
|
|
|
|
thread_enabled = false;
|
|
|
|
break ;
|
|
|
|
|
2005-10-20 08:33:59 +00:00
|
|
|
case DIRCACHE_STOP:
|
|
|
|
logf("Stopped the rebuilding.");
|
|
|
|
dircache_initialized = false;
|
|
|
|
break ;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
#ifndef SIMULATOR
|
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&dircache_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Start scanning the disk to build the dircache.
|
|
|
|
* Either transparent or non-transparent build method is used.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
int dircache_build(int last_size)
|
|
|
|
{
|
2006-04-16 17:32:54 +00:00
|
|
|
if (dircache_initialized || thread_enabled)
|
2005-10-07 17:38:05 +00:00
|
|
|
return -3;
|
|
|
|
|
|
|
|
logf("Building directory cache");
|
2006-12-22 09:11:09 +00:00
|
|
|
remove(DIRCACHE_FILE);
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Background build, dircache has been previously allocated */
|
2005-10-07 17:38:05 +00:00
|
|
|
if (dircache_size > 0)
|
|
|
|
{
|
|
|
|
thread_enabled = true;
|
2006-08-14 19:03:37 +00:00
|
|
|
dircache_initializing = true;
|
2005-10-07 17:38:05 +00:00
|
|
|
queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
|
|
|
|
return 2;
|
|
|
|
}
|
2006-04-16 17:32:54 +00:00
|
|
|
|
|
|
|
if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
allocated_size = last_size + DIRCACHE_RESERVE;
|
2006-08-26 09:24:20 +00:00
|
|
|
dircache_root = buffer_alloc(allocated_size);
|
2005-10-07 17:38:05 +00:00
|
|
|
thread_enabled = true;
|
|
|
|
|
|
|
|
/* Start a transparent rebuild. */
|
|
|
|
queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2006-08-02 17:39:34 +00:00
|
|
|
dircache_root = (struct dircache_entry *)(((long)audiobuf & ~0x03) + 0x04);
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
/* Start a non-transparent rebuild. */
|
|
|
|
return dircache_do_rebuild();
|
|
|
|
}
|
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
/**
|
|
|
|
* Steal the allocated dircache buffer and disable dircache.
|
|
|
|
*/
|
|
|
|
void* dircache_steal_buffer(long *size)
|
|
|
|
{
|
|
|
|
dircache_disable();
|
|
|
|
if (dircache_size == 0)
|
|
|
|
{
|
|
|
|
*size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = dircache_size + (DIRCACHE_RESERVE-reserve_used);
|
|
|
|
|
|
|
|
return dircache_root;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Main initialization function that must be called before any other
|
|
|
|
* operations within the dircache.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
void dircache_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-05-30 18:13:18 +00:00
|
|
|
dircache_initialized = false;
|
2006-08-14 19:03:37 +00:00
|
|
|
dircache_initializing = false;
|
2006-05-30 18:13:18 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
memset(opendirs, 0, sizeof(opendirs));
|
|
|
|
for (i = 0; i < MAX_OPEN_DIRS; i++)
|
|
|
|
{
|
2006-08-02 17:39:34 +00:00
|
|
|
opendirs[i].secondary_entry.d_name = buffer_alloc(MAX_PATH);
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&dircache_queue, true);
|
2005-10-07 17:38:05 +00:00
|
|
|
create_thread(dircache_thread, dircache_stack,
|
2006-09-16 16:18:11 +00:00
|
|
|
sizeof(dircache_stack), dircache_thread_name IF_PRIO(, PRIORITY_BACKGROUND));
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Returns true if dircache has been initialized and is ready to be used.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
bool dircache_is_enabled(void)
|
|
|
|
{
|
|
|
|
return dircache_initialized;
|
|
|
|
}
|
|
|
|
|
2006-07-10 16:22:03 +00:00
|
|
|
/**
|
|
|
|
* Returns true if dircache is being initialized.
|
|
|
|
*/
|
|
|
|
bool dircache_is_initializing(void)
|
|
|
|
{
|
|
|
|
return dircache_initializing;
|
|
|
|
}
|
|
|
|
|
2006-01-31 10:08:53 +00:00
|
|
|
/**
|
|
|
|
* Returns the current number of entries (directories and files) in the cache.
|
|
|
|
*/
|
|
|
|
int dircache_get_entry_count(void)
|
|
|
|
{
|
|
|
|
return entry_count;
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the allocated space for dircache (without reserve space).
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
int dircache_get_cache_size(void)
|
|
|
|
{
|
2005-11-26 20:47:10 +00:00
|
|
|
return dircache_is_enabled() ? dircache_size : 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Returns how many bytes of the reserve allocation for live cache
|
|
|
|
* updates have been used.
|
|
|
|
*/
|
2005-11-26 20:22:19 +00:00
|
|
|
int dircache_get_reserve_used(void)
|
|
|
|
{
|
2005-11-26 20:47:10 +00:00
|
|
|
return dircache_is_enabled() ? reserve_used : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the time in kernel ticks that took to build the cache.
|
|
|
|
*/
|
|
|
|
int dircache_get_build_ticks(void)
|
|
|
|
{
|
|
|
|
return dircache_is_enabled() ? cache_build_ticks : 0;
|
2005-11-26 20:22:19 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Disables the dircache. Usually called on shutdown or when
|
|
|
|
* accepting a usb connection.
|
|
|
|
*/
|
2005-10-07 17:38:05 +00:00
|
|
|
void dircache_disable(void)
|
|
|
|
{
|
|
|
|
int i;
|
2005-10-20 08:33:59 +00:00
|
|
|
bool cache_in_use;
|
|
|
|
|
|
|
|
if (thread_enabled)
|
|
|
|
queue_post(&dircache_queue, DIRCACHE_STOP, 0);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
while (thread_enabled)
|
|
|
|
sleep(1);
|
|
|
|
dircache_initialized = false;
|
|
|
|
|
2005-10-20 08:33:59 +00:00
|
|
|
logf("Waiting for cached dirs to release");
|
|
|
|
do {
|
|
|
|
cache_in_use = false;
|
|
|
|
for (i = 0; i < MAX_OPEN_DIRS; i++) {
|
|
|
|
if (!opendirs[i].regulardir && opendirs[i].busy)
|
|
|
|
{
|
|
|
|
cache_in_use = true;
|
|
|
|
sleep(1);
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (cache_in_use) ;
|
2006-08-14 19:03:37 +00:00
|
|
|
|
2005-10-20 08:33:59 +00:00
|
|
|
logf("Cache released");
|
2006-08-14 19:03:37 +00:00
|
|
|
entry_count = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Usermode function to return dircache_entry pointer to the given path.
|
|
|
|
*/
|
2005-11-17 19:31:29 +00:00
|
|
|
const struct dircache_entry *dircache_get_entry_ptr(const char *filename)
|
|
|
|
{
|
|
|
|
if (!dircache_initialized || filename == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return dircache_get_entry(filename, false, false);
|
|
|
|
}
|
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Function to copy the full absolute path from dircache to the given buffer
|
|
|
|
* using the given dircache_entry pointer.
|
|
|
|
*/
|
2005-11-17 19:31:29 +00:00
|
|
|
void dircache_copy_path(const struct dircache_entry *entry, char *buf, int size)
|
|
|
|
{
|
|
|
|
const struct dircache_entry *down[MAX_SCAN_DEPTH];
|
|
|
|
int depth = 0;
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
do {
|
|
|
|
down[depth] = entry;
|
|
|
|
entry = entry->up;
|
|
|
|
depth++;
|
|
|
|
} while (entry != NULL && depth < MAX_SCAN_DEPTH);
|
|
|
|
|
|
|
|
while (--depth >= 0)
|
|
|
|
{
|
|
|
|
snprintf(buf, size, "/%s", down[depth]->d_name);
|
|
|
|
buf += down[depth]->name_len; /* '/' + d_name */
|
|
|
|
size -= down[depth]->name_len;
|
|
|
|
if (size <= 0)
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
/* --- Directory cache live updating functions --- */
|
2006-03-29 09:38:45 +00:00
|
|
|
static int block_until_ready(void)
|
|
|
|
{
|
|
|
|
/* Block until dircache has been built. */
|
|
|
|
while (!dircache_initialized && dircache_initializing)
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
if (!dircache_initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry* dircache_new_entry(const char *path, int attribute)
|
|
|
|
{
|
|
|
|
struct dircache_entry *entry;
|
2006-11-10 08:03:33 +00:00
|
|
|
char basedir[MAX_PATH*2];
|
2005-10-07 17:38:05 +00:00
|
|
|
char *new;
|
|
|
|
long last_cache_size = dircache_size;
|
|
|
|
|
|
|
|
strncpy(basedir, path, sizeof(basedir)-1);
|
|
|
|
new = strrchr(basedir, '/');
|
|
|
|
if (new == NULL)
|
|
|
|
{
|
|
|
|
logf("error occurred");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*new = '\0';
|
|
|
|
new++;
|
|
|
|
|
|
|
|
entry = dircache_get_entry(basedir, false, true);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("basedir not found!");
|
2006-02-28 11:41:35 +00:00
|
|
|
logf(basedir);
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reserve_used + 2*sizeof(struct dircache_entry) + strlen(new)+1
|
|
|
|
>= DIRCACHE_RESERVE)
|
|
|
|
{
|
|
|
|
logf("not enough space");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (entry->next != NULL)
|
|
|
|
entry = entry->next;
|
|
|
|
|
|
|
|
if (entry->name_len > 0)
|
|
|
|
entry = dircache_gen_next(entry);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->attribute = attribute;
|
|
|
|
entry->name_len = MIN(254, strlen(new)) + 1;
|
|
|
|
entry->d_name = ((char *)dircache_root+dircache_size);
|
|
|
|
entry->startcluster = 0;
|
|
|
|
entry->wrtdate = 0;
|
|
|
|
entry->wrttime = 0;
|
|
|
|
entry->size = 0;
|
|
|
|
memcpy(entry->d_name, new, entry->name_len);
|
|
|
|
dircache_size += entry->name_len;
|
|
|
|
|
|
|
|
if (attribute & ATTR_DIRECTORY)
|
|
|
|
{
|
|
|
|
logf("gen_down");
|
|
|
|
dircache_gen_down(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
reserve_used += dircache_size - last_cache_size;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dircache_bind(int fd, const char *path)
|
|
|
|
{
|
|
|
|
struct dircache_entry *entry;
|
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
/* Queue requests until dircache has been built. */
|
|
|
|
if (!dircache_initialized && dircache_initializing)
|
|
|
|
{
|
|
|
|
if (fdbind_idx >= MAX_PENDING_BINDINGS)
|
|
|
|
return ;
|
|
|
|
strncpy(fdbind_cache[fdbind_idx].path, path,
|
|
|
|
sizeof(fdbind_cache[fdbind_idx].path)-1);
|
|
|
|
fdbind_cache[fdbind_idx].fd = fd;
|
|
|
|
fdbind_idx++;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
if (!dircache_initialized)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("bind: %d/%s", fd, path);
|
|
|
|
entry = dircache_get_entry(path, false, false);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_bindings[fd] = entry;
|
|
|
|
}
|
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
void dircache_update_filesize(int fd, long newsize, long startcluster)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
if (!dircache_initialized || fd < 0)
|
|
|
|
return ;
|
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
if (fd_bindings[fd] == NULL)
|
|
|
|
{
|
|
|
|
logf("dircache fd access error");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
fd_bindings[fd]->size = newsize;
|
2006-03-28 11:51:12 +00:00
|
|
|
fd_bindings[fd]->startcluster = startcluster;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dircache_mkdir(const char *path)
|
|
|
|
{ /* Test ok. */
|
2006-03-29 09:38:45 +00:00
|
|
|
if (block_until_ready())
|
2005-10-07 17:38:05 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("mkdir: %s", path);
|
|
|
|
dircache_new_entry(path, ATTR_DIRECTORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dircache_rmdir(const char *path)
|
|
|
|
{ /* Test ok. */
|
|
|
|
struct dircache_entry *entry;
|
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
if (block_until_ready())
|
2005-10-07 17:38:05 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("rmdir: %s", path);
|
|
|
|
entry = dircache_get_entry(path, true, true);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->down = NULL;
|
|
|
|
entry->name_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a file from cache */
|
|
|
|
void dircache_remove(const char *name)
|
|
|
|
{ /* Test ok. */
|
|
|
|
struct dircache_entry *entry;
|
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
if (block_until_ready())
|
2005-10-07 17:38:05 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("remove: %s", name);
|
|
|
|
|
|
|
|
entry = dircache_get_entry(name, false, false);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->name_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dircache_rename(const char *oldpath, const char *newpath)
|
|
|
|
{ /* Test ok. */
|
|
|
|
struct dircache_entry *entry, *newentry;
|
2005-11-18 19:28:22 +00:00
|
|
|
struct dircache_entry oldentry;
|
2006-11-10 08:03:33 +00:00
|
|
|
char absolute_path[MAX_PATH*2];
|
2006-02-28 11:41:35 +00:00
|
|
|
char *p;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
if (block_until_ready())
|
2005-10-07 17:38:05 +00:00
|
|
|
return ;
|
|
|
|
|
|
|
|
logf("rename: %s->%s", oldpath, newpath);
|
2006-02-28 11:41:35 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
entry = dircache_get_entry(oldpath, true, false);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the old entry. */
|
|
|
|
entry->name_len = 0;
|
|
|
|
|
2005-11-18 19:28:22 +00:00
|
|
|
/** If we rename the same filename twice in a row, we need to
|
|
|
|
* save the data, because the entry will be re-used. */
|
|
|
|
oldentry = *entry;
|
|
|
|
|
2006-02-28 11:41:35 +00:00
|
|
|
/* Generate the absolute path for destination if necessary. */
|
|
|
|
if (newpath[0] != '/')
|
|
|
|
{
|
|
|
|
strncpy(absolute_path, oldpath, sizeof(absolute_path)-1);
|
|
|
|
p = strrchr(absolute_path, '/');
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
logf("Invalid path");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
strncpy(p, absolute_path, sizeof(absolute_path)-1-strlen(p));
|
|
|
|
newpath = absolute_path;
|
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
newentry = dircache_new_entry(newpath, entry->attribute);
|
|
|
|
if (newentry == NULL)
|
|
|
|
{
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-11-18 19:28:22 +00:00
|
|
|
newentry->down = oldentry.down;
|
|
|
|
newentry->size = oldentry.size;
|
|
|
|
newentry->startcluster = oldentry.startcluster;
|
|
|
|
newentry->wrttime = oldentry.wrttime;
|
|
|
|
newentry->wrtdate = oldentry.wrtdate;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
void dircache_add_file(const char *path, long startcluster)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2006-03-28 11:51:12 +00:00
|
|
|
struct dircache_entry *entry;
|
|
|
|
|
2006-03-29 09:38:45 +00:00
|
|
|
if (block_until_ready())
|
2005-10-07 17:38:05 +00:00
|
|
|
return ;
|
2006-03-29 09:38:45 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
logf("add file: %s", path);
|
2006-03-28 11:51:12 +00:00
|
|
|
entry = dircache_new_entry(path, 0);
|
|
|
|
if (entry == NULL)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
entry->startcluster = startcluster;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DIRCACHED* opendir_cached(const char* name)
|
|
|
|
{
|
|
|
|
struct dircache_entry *cache_entry;
|
|
|
|
int dd;
|
|
|
|
DIRCACHED* pdir = opendirs;
|
|
|
|
|
|
|
|
if ( name[0] != '/' )
|
|
|
|
{
|
|
|
|
DEBUGF("Only absolute paths supported right now\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a free dir descriptor */
|
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
|
if ( !pdir->busy )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( dd == MAX_OPEN_DIRS )
|
|
|
|
{
|
|
|
|
DEBUGF("Too many dirs open\n");
|
|
|
|
errno = EMFILE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dircache_initialized)
|
|
|
|
{
|
|
|
|
pdir->regulardir = opendir(name);
|
|
|
|
if (!pdir->regulardir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pdir->busy = true;
|
|
|
|
return pdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdir->busy = true;
|
|
|
|
pdir->regulardir = NULL;
|
|
|
|
cache_entry = dircache_get_entry(name, false, true);
|
|
|
|
pdir->entry = cache_entry;
|
|
|
|
|
|
|
|
if (cache_entry == NULL)
|
|
|
|
{
|
|
|
|
pdir->busy = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dircache_entry* readdir_cached(DIRCACHED* dir)
|
|
|
|
{
|
|
|
|
struct dirent *regentry;
|
|
|
|
struct dircache_entry *ce;
|
|
|
|
|
|
|
|
if (!dir->busy)
|
|
|
|
return NULL;
|
|
|
|
|
2005-10-09 07:32:23 +00:00
|
|
|
if (dir->regulardir != NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
regentry = readdir(dir->regulardir);
|
|
|
|
if (regentry == NULL)
|
|
|
|
return NULL;
|
2006-01-31 10:08:53 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
strncpy(dir->secondary_entry.d_name, regentry->d_name, MAX_PATH-1);
|
|
|
|
dir->secondary_entry.size = regentry->size;
|
|
|
|
dir->secondary_entry.startcluster = regentry->startcluster;
|
|
|
|
dir->secondary_entry.attribute = regentry->attribute;
|
|
|
|
dir->secondary_entry.wrttime = regentry->wrttime;
|
|
|
|
dir->secondary_entry.wrtdate = regentry->wrtdate;
|
|
|
|
dir->secondary_entry.next = NULL;
|
|
|
|
|
|
|
|
return &dir->secondary_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (dir->entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ce = dir->entry;
|
|
|
|
if (ce->name_len == 0)
|
|
|
|
dir->entry = ce->next;
|
|
|
|
} while (ce->name_len == 0) ;
|
|
|
|
|
|
|
|
dir->entry = ce->next;
|
|
|
|
|
2005-10-08 18:41:11 +00:00
|
|
|
strncpy(dir->secondary_entry.d_name, ce->d_name, MAX_PATH-1);
|
|
|
|
/* Can't do `dir->secondary_entry = *ce`
|
|
|
|
because that modifies the d_name pointer. */
|
|
|
|
dir->secondary_entry.size = ce->size;
|
|
|
|
dir->secondary_entry.startcluster = ce->startcluster;
|
|
|
|
dir->secondary_entry.attribute = ce->attribute;
|
|
|
|
dir->secondary_entry.wrttime = ce->wrttime;
|
|
|
|
dir->secondary_entry.wrtdate = ce->wrtdate;
|
|
|
|
dir->secondary_entry.next = NULL;
|
2006-01-31 10:08:53 +00:00
|
|
|
dir->internal_entry = ce;
|
2005-10-08 18:41:11 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
//logf("-> %s", ce->name);
|
2005-10-08 18:41:11 +00:00
|
|
|
return &dir->secondary_entry;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int closedir_cached(DIRCACHED* dir)
|
|
|
|
{
|
2005-10-09 07:32:23 +00:00
|
|
|
if (!dir->busy)
|
|
|
|
return -1;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
dir->busy=false;
|
2005-10-09 07:32:23 +00:00
|
|
|
if (dir->regulardir != NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
return closedir(dir->regulardir);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|