2005-10-07 17:38:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Miika Pekkarinen
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-10-07 17:38:05 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2010-05-06 21:04:40 +00:00
|
|
|
#include "string-extra.h"
|
2005-10-07 17:38:05 +00:00
|
|
|
#include <stdbool.h>
|
2008-04-28 16:18:04 +00:00
|
|
|
#include <stdlib.h>
|
2005-10-07 17:38:05 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "dircache.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "file.h"
|
2011-08-30 14:01:33 +00:00
|
|
|
#include "core_alloc.h"
|
2008-03-11 19:39:26 +00:00
|
|
|
#include "dir.h"
|
2010-04-03 22:02:09 +00:00
|
|
|
#include "storage.h"
|
2007-09-02 13:24:51 +00:00
|
|
|
#if CONFIG_RTC
|
|
|
|
#include "time.h"
|
|
|
|
#include "timefuncs.h"
|
|
|
|
#endif
|
2010-08-01 16:22:48 +00:00
|
|
|
#include "rbpaths.h"
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
|
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
|
|
|
|
2011-02-02 17:43:32 +00:00
|
|
|
#if (MEMORYSIZE > 8)
|
2008-08-10 13:22:31 +00:00
|
|
|
#define MAX_OPEN_DIRS 12
|
2008-08-10 12:46:15 +00:00
|
|
|
#else
|
2005-10-07 17:38:05 +00:00
|
|
|
#define MAX_OPEN_DIRS 8
|
2008-08-10 12:46:15 +00:00
|
|
|
#endif
|
2011-08-30 14:01:33 +00:00
|
|
|
static DIR_CACHED opendirs[MAX_OPEN_DIRS];
|
|
|
|
static char opendir_dnames[MAX_OPEN_DIRS][MAX_PATH];
|
2011-06-20 20:12:42 +00:00
|
|
|
|
|
|
|
#define MAX_PENDING_BINDINGS 2
|
|
|
|
struct fdbind_queue {
|
|
|
|
char path[MAX_PATH];
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2011-08-30 14:01:45 +00:00
|
|
|
/* Unions with char to make pointer arithmetic simpler and avoid casting */
|
2011-06-20 20:12:42 +00:00
|
|
|
struct dircache_entry {
|
|
|
|
struct dirinfo info;
|
2011-08-30 14:01:45 +00:00
|
|
|
union {
|
|
|
|
struct dircache_entry *next;
|
|
|
|
char* next_char;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
struct dircache_entry *up;
|
|
|
|
char* up_char;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
struct dircache_entry *down;
|
|
|
|
char* down_char;
|
|
|
|
};
|
2011-06-20 20:12:42 +00:00
|
|
|
long startcluster;
|
|
|
|
char *d_name;
|
|
|
|
};
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
/* Cache Layout:
|
|
|
|
*
|
|
|
|
* x - array of struct dircache_entry
|
|
|
|
* r - reserved buffer
|
|
|
|
* d - name buffer for the d_name entry of the struct dircache_entry
|
|
|
|
* |xxxxxx|rrrrrrrrr|dddddd|
|
|
|
|
*
|
|
|
|
* subsequent x are allocated from the front, d are allocated from the back,
|
|
|
|
* using the reserve buffer for entries added after initial scan
|
|
|
|
*
|
|
|
|
* after a while the cache may look like:
|
|
|
|
* |xxxxxxxx|rrrrr|dddddddd|
|
|
|
|
*
|
|
|
|
* after a reboot, the reserve buffer is restored in it's size, so that the
|
|
|
|
* total allocation size grows
|
|
|
|
* |xxxxxxxx|rrrrrrrrr|dddddddd|
|
|
|
|
*/
|
|
|
|
/* this points to the beginnging of the buffer and the first entry */
|
2005-10-07 17:38:05 +00:00
|
|
|
static struct dircache_entry *dircache_root;
|
2011-06-20 20:12:30 +00:00
|
|
|
/* these point to the start and end of the name buffer (d above) */
|
|
|
|
static char *d_names_start, *d_names_end;
|
|
|
|
/* put "." and ".." into the d_names buffer to enable easy pointer logic */
|
|
|
|
static char *dot, *dotdot;
|
2008-03-11 19:39:26 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
static struct dircache_entry *append_position;
|
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2011-06-20 20:12:42 +00:00
|
|
|
static DIR_CACHED opendirs[MAX_OPEN_DIRS];
|
|
|
|
static struct dircache_entry *fd_bindings[MAX_OPEN_FILES];
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
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;
|
2011-08-16 13:01:03 +00:00
|
|
|
static unsigned long allocated_size = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
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;
|
2008-01-13 19:13:37 +00:00
|
|
|
static unsigned long appflags = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2011-02-14 11:27:45 +00:00
|
|
|
static struct event_queue dircache_queue SHAREDBSS_ATTR;
|
2010-04-22 08:31:10 +00:00
|
|
|
static long dircache_stack[(DEFAULT_STACK_SIZE + 0x400)/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
|
|
|
|
2011-06-20 20:12:42 +00:00
|
|
|
static inline struct dircache_entry* get_entry(int id)
|
|
|
|
{
|
|
|
|
return &dircache_root[id];
|
|
|
|
}
|
|
|
|
|
2011-08-30 14:01:45 +00:00
|
|
|
/* flag to make sure buffer doesn't move due to other allocs.
|
|
|
|
* this is set to true completely during dircache build */
|
|
|
|
static bool dont_move = false;
|
|
|
|
static int dircache_handle;
|
|
|
|
static int move_callback(int handle, void* current, void* new)
|
|
|
|
{
|
|
|
|
(void)handle;
|
|
|
|
if (dont_move)
|
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
|
|
|
|
|
|
|
/* relocate the cache */
|
|
|
|
ptrdiff_t diff = new - current;
|
|
|
|
for(unsigned i = 0; i < entry_count; i++)
|
|
|
|
{
|
|
|
|
if (dircache_root[i].d_name)
|
|
|
|
dircache_root[i].d_name += diff;
|
|
|
|
if (dircache_root[i].next_char)
|
|
|
|
dircache_root[i].next_char += diff;
|
|
|
|
if (dircache_root[i].up_char)
|
|
|
|
dircache_root[i].up_char += diff;
|
|
|
|
if (dircache_root[i].down_char)
|
|
|
|
dircache_root[i].down_char += diff;
|
|
|
|
}
|
|
|
|
dircache_root = new;
|
|
|
|
|
|
|
|
d_names_start -= diff;
|
|
|
|
d_names_end -= diff;
|
|
|
|
dot -= diff;
|
|
|
|
dotdot -= diff;
|
|
|
|
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buflib_callbacks ops = {
|
|
|
|
.move_callback = move_callback,
|
|
|
|
.shrink_callback = NULL,
|
|
|
|
};
|
|
|
|
|
2010-07-26 13:41:47 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
|
|
|
/**
|
|
|
|
* Open the dircache file to save a snapshot on disk
|
|
|
|
*/
|
|
|
|
static int open_dircache_file(unsigned flags, int permissions)
|
|
|
|
{
|
|
|
|
if (permissions != 0)
|
2010-12-06 22:26:31 +00:00
|
|
|
return open(DIRCACHE_FILE, flags, permissions);
|
2010-07-26 13:41:47 +00:00
|
|
|
|
2010-12-06 22:26:31 +00:00
|
|
|
return open(DIRCACHE_FILE, flags);
|
2010-07-26 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the snapshot file
|
|
|
|
*/
|
|
|
|
static int remove_dircache_file(void)
|
|
|
|
{
|
2010-12-06 22:26:31 +00:00
|
|
|
return remove(DIRCACHE_FILE);
|
2010-07-26 13:41:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
next_entry = &dircache_root[entry_count++];
|
2005-10-07 17:38:05 +00:00
|
|
|
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;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2006-03-22 17:09:13 +00:00
|
|
|
|
2010-05-21 12:50:50 +00:00
|
|
|
if(!queue_peek(&dircache_queue, &ev))
|
|
|
|
return false;
|
|
|
|
|
2006-03-22 17:09:13 +00:00
|
|
|
switch (ev.id)
|
|
|
|
{
|
|
|
|
case DIRCACHE_STOP:
|
|
|
|
case SYS_USB_CONNECTED:
|
2008-03-11 19:39:26 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
case SYS_FS_CHANGED:
|
|
|
|
#endif
|
2006-03-22 17:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-21 16:53:00 +00:00
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
|
2010-02-14 19:09:52 +00:00
|
|
|
/* scan and build static data (avoid redundancy on stack) */
|
|
|
|
static struct
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2009-11-26 17:39:30 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2010-02-14 19:09:52 +00:00
|
|
|
int volume;
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2010-02-14 19:09:52 +00:00
|
|
|
struct fat_dir *dir;
|
|
|
|
struct fat_direntry *direntry;
|
|
|
|
}sab;
|
|
|
|
|
|
|
|
static int sab_process_dir(unsigned long startcluster, struct dircache_entry *ce)
|
|
|
|
{
|
|
|
|
/* normally, opendir expects a full fat_dir as parent but in our case,
|
|
|
|
* it's completely useless because we don't modify anything
|
|
|
|
* WARNING: this heavily relies on current FAT implementation ! */
|
|
|
|
|
|
|
|
/* those field are necessary to update the FAT entry in case of modification
|
|
|
|
here we don't touch anything so we put dummy values */
|
|
|
|
sab.dir->entry = 0;
|
|
|
|
sab.dir->entrycount = 0;
|
|
|
|
sab.dir->file.firstcluster = 0;
|
|
|
|
/* open directory */
|
|
|
|
int rc = fat_opendir(IF_MV2(sab.volume,) sab.dir, startcluster, sab.dir);
|
|
|
|
if(rc < 0)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2010-02-14 19:09:52 +00:00
|
|
|
logf("fat_opendir failed: %d", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first pass : read dir */
|
|
|
|
struct dircache_entry *first_ce = ce;
|
|
|
|
|
|
|
|
/* read through directory */
|
|
|
|
while((rc = fat_getnext(sab.dir, sab.direntry)) >= 0 && sab.direntry->name[0])
|
|
|
|
{
|
|
|
|
if(!strcmp(".", sab.direntry->name) ||
|
|
|
|
!strcmp("..", sab.direntry->name))
|
2006-03-30 18:53:44 +00:00
|
|
|
continue;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
size_t size = strlen(sab.direntry->name) + 1;
|
|
|
|
ce->d_name = (d_names_start -= size);
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->startcluster = sab.direntry->firstcluster;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.size = sab.direntry->filesize;
|
|
|
|
ce->info.attribute = sab.direntry->attr;
|
|
|
|
ce->info.wrtdate = sab.direntry->wrtdate;
|
|
|
|
ce->info.wrttime = sab.direntry->wrttime;
|
2011-06-20 20:07:35 +00:00
|
|
|
|
|
|
|
strcpy(ce->d_name, sab.direntry->name);
|
2011-06-20 20:12:30 +00:00
|
|
|
dircache_size += size;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2010-09-01 21:29:34 +00:00
|
|
|
if(ce->info.attribute & FAT_ATTR_DIRECTORY)
|
2010-02-14 19:09:52 +00:00
|
|
|
dircache_gen_down(ce);
|
|
|
|
|
|
|
|
ce = dircache_gen_next(ce);
|
|
|
|
if(ce == NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
return -5;
|
2006-03-30 18:53:44 +00:00
|
|
|
|
|
|
|
/* When simulator is used, it's only safe to yield here. */
|
2010-02-14 19:09:52 +00:00
|
|
|
if(thread_enabled)
|
2006-03-30 18:53:44 +00:00
|
|
|
{
|
|
|
|
/* Stop if we got an external signal. */
|
2010-02-14 19:09:52 +00:00
|
|
|
if(check_event_queue())
|
2006-03-30 18:53:44 +00:00
|
|
|
return -6;
|
|
|
|
yield();
|
|
|
|
}
|
2010-02-14 19:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add "." and ".." */
|
2011-06-20 20:12:30 +00:00
|
|
|
ce->d_name = dot;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.attribute = FAT_ATTR_DIRECTORY;
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->startcluster = startcluster;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.size = 0;
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->down = first_ce;
|
|
|
|
|
|
|
|
ce = dircache_gen_next(ce);
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
ce->d_name = dotdot;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.attribute = FAT_ATTR_DIRECTORY;
|
2010-02-21 22:48:54 +00:00
|
|
|
ce->startcluster = (first_ce->up ? first_ce->up->startcluster : 0);
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.size = 0;
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->down = first_ce->up;
|
|
|
|
|
|
|
|
/* second pass: recurse ! */
|
|
|
|
ce = first_ce;
|
|
|
|
|
2010-02-17 19:31:07 +00:00
|
|
|
while(rc >= 0 && ce)
|
2010-02-14 19:09:52 +00:00
|
|
|
{
|
2011-06-20 20:07:35 +00:00
|
|
|
if(ce->d_name != NULL && ce->down != NULL && strcmp(ce->d_name, ".")
|
|
|
|
&& strcmp(ce->d_name, ".."))
|
2010-02-17 19:31:07 +00:00
|
|
|
rc = sab_process_dir(ce->startcluster, ce->down);
|
2006-03-30 18:53:44 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
ce = ce->next;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2010-02-14 19:09:52 +00:00
|
|
|
|
|
|
|
return rc;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 22:49:11 +00:00
|
|
|
/* used during the generation */
|
|
|
|
static struct fat_dir sab_fat_dir;
|
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2005-11-17 19:31:29 +00:00
|
|
|
memset(ce, 0, sizeof(struct dircache_entry));
|
2008-03-11 19:39:26 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2008-03-11 19:39:26 +00:00
|
|
|
if (volume > 0)
|
|
|
|
{
|
2011-07-30 23:21:58 +00:00
|
|
|
/* broken for 100+ volumes because the format string is too small
|
|
|
|
* and we use that for size calculation */
|
|
|
|
const size_t max_len = VOL_ENUM_POS + 3;
|
|
|
|
ce->d_name = (d_names_start -= max_len);
|
|
|
|
snprintf(ce->d_name, max_len, VOL_NAMES, volume);
|
|
|
|
dircache_size += max_len;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
|
|
|
|
ce->info.size = 0;
|
2008-03-11 19:39:26 +00:00
|
|
|
append_position = dircache_gen_next(ce);
|
|
|
|
ce = dircache_gen_down(ce);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
struct fat_direntry direntry; /* ditto */
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
sab.volume = volume;
|
2006-03-30 18:53:44 +00:00
|
|
|
#endif
|
2010-02-16 22:49:11 +00:00
|
|
|
sab.dir = &sab_fat_dir;
|
2010-02-14 19:09:52 +00:00
|
|
|
sab.direntry = &direntry;
|
2006-02-28 11:41:35 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
return sab_process_dir(0, ce);
|
|
|
|
}
|
2010-06-21 16:53:00 +00:00
|
|
|
#elif (CONFIG_PLATFORM & PLATFORM_HOSTED) /* PLATFORM_HOSTED */
|
2010-02-14 19:09:52 +00:00
|
|
|
static char sab_path[MAX_PATH];
|
2006-02-28 11:41:35 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
static int sab_process_dir(struct dircache_entry *ce)
|
|
|
|
{
|
|
|
|
struct dirent_uncached *entry;
|
|
|
|
struct dircache_entry *first_ce = ce;
|
|
|
|
DIR_UNCACHED *dir = opendir_uncached(sab_path);
|
|
|
|
if(dir == NULL)
|
|
|
|
{
|
|
|
|
logf("Failed to opendir_uncached(%s)", sab_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((entry = readdir_uncached(dir)))
|
|
|
|
{
|
|
|
|
if(!strcmp(".", entry->d_name) ||
|
|
|
|
!strcmp("..", entry->d_name))
|
|
|
|
continue;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
size_t size = strlen(entry->d_name) + 1;
|
|
|
|
ce->d_name = (d_names_start -= size);
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info = entry->info;
|
2011-06-20 20:07:35 +00:00
|
|
|
|
|
|
|
strcpy(ce->d_name, entry->d_name);
|
2011-06-20 20:12:30 +00:00
|
|
|
dircache_size += size;
|
2010-02-14 19:09:52 +00:00
|
|
|
|
2010-09-01 21:29:34 +00:00
|
|
|
if(entry->info.attribute & ATTR_DIRECTORY)
|
2010-02-14 19:09:52 +00:00
|
|
|
{
|
|
|
|
dircache_gen_down(ce);
|
|
|
|
if(ce->down == NULL)
|
|
|
|
{
|
|
|
|
closedir_uncached(dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* save current paths size */
|
|
|
|
int pathpos = strlen(sab_path);
|
|
|
|
/* append entry */
|
|
|
|
strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
|
|
|
|
strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
|
2006-03-30 18:53:44 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
int rc = sab_process_dir(ce->down);
|
|
|
|
/* restore path */
|
|
|
|
sab_path[pathpos] = '\0';
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
if(rc < 0)
|
|
|
|
{
|
|
|
|
closedir_uncached(dir);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ce = dircache_gen_next(ce);
|
|
|
|
if(ce == NULL)
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* 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())
|
2005-10-07 17:38:05 +00:00
|
|
|
return -1;
|
2010-02-14 19:09:52 +00:00
|
|
|
yield();
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2010-02-14 19:09:52 +00:00
|
|
|
}
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
/* add "." and ".." */
|
2011-06-20 20:12:30 +00:00
|
|
|
ce->d_name = dot;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.attribute = ATTR_DIRECTORY;
|
|
|
|
ce->info.size = 0;
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->down = first_ce;
|
|
|
|
|
|
|
|
ce = dircache_gen_next(ce);
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
ce->d_name = dotdot;
|
2010-09-01 21:29:34 +00:00
|
|
|
ce->info.attribute = ATTR_DIRECTORY;
|
|
|
|
ce->info.size = 0;
|
2010-02-14 19:09:52 +00:00
|
|
|
ce->down = first_ce->up;
|
|
|
|
|
|
|
|
closedir_uncached(dir);
|
2005-10-07 17:38:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-14 19:09:52 +00:00
|
|
|
static int dircache_scan_and_build(IF_MV2(int volume,) struct dircache_entry *ce)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
(void) volume;
|
|
|
|
#endif
|
|
|
|
memset(ce, 0, sizeof(struct dircache_entry));
|
|
|
|
|
|
|
|
strlcpy(sab_path, "/", sizeof sab_path);
|
|
|
|
return sab_process_dir(ce);
|
|
|
|
}
|
2010-06-21 16:53:00 +00:00
|
|
|
#endif /* PLATFORM_NATIVE */
|
2010-02-14 19:09:52 +00:00
|
|
|
|
2005-11-26 20:47:10 +00:00
|
|
|
/**
|
|
|
|
* Internal function to get a pointer to dircache_entry for a given filename.
|
2010-01-31 17:02:56 +00:00
|
|
|
* path: Absolute path to a file or directory (see comment)
|
|
|
|
* go_down: Returns the first entry of the directory given by the path (see comment)
|
|
|
|
*
|
2010-02-01 23:00:35 +00:00
|
|
|
* As a a special case, accept path="" as an alias for "/".
|
|
|
|
* Also if the path omits the first '/', it will be accepted.
|
2010-01-31 17:02:56 +00:00
|
|
|
*
|
|
|
|
* * If get_down=true:
|
|
|
|
* If path="/", the returned entry is the first of root directory (ie dircache_root)
|
|
|
|
* Otherwise, if 'entry' is the returned value when get_down=false,
|
|
|
|
* the functions returns entry->down (which can be NULL)
|
|
|
|
*
|
|
|
|
* * If get_down=false:
|
|
|
|
* If path="/chunk_1/chunk_2/.../chunk_n" then this functions returns the entry
|
|
|
|
* root_entry()->chunk_1->chunk_2->...->chunk_(n-1)
|
|
|
|
* Which means that
|
|
|
|
* dircache_get_entry(path)->d_name == chunk_n
|
|
|
|
*
|
|
|
|
* If path="/", the returned entry is NULL.
|
|
|
|
* If the entry doesn't exist, return NULL
|
|
|
|
*
|
|
|
|
* NOTE: this functions silently handles double '/'
|
2005-11-26 20:47:10 +00:00
|
|
|
*/
|
2010-01-31 17:02:56 +00:00
|
|
|
static struct dircache_entry* dircache_get_entry(const char *path, bool go_down)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2010-01-31 17:02:56 +00:00
|
|
|
char namecopy[MAX_PATH];
|
2005-10-07 17:38:05 +00:00
|
|
|
char* part;
|
|
|
|
char* end;
|
|
|
|
|
2010-01-31 17:02:56 +00:00
|
|
|
bool at_root = true;
|
|
|
|
struct dircache_entry *cache_entry = dircache_root;
|
|
|
|
|
2010-02-01 23:00:35 +00:00
|
|
|
strlcpy(namecopy, path, sizeof(namecopy));
|
2010-01-31 17:02:56 +00:00
|
|
|
|
|
|
|
for(part = strtok_r(namecopy, "/", &end); part; part = strtok_r(NULL, "/", &end))
|
|
|
|
{
|
|
|
|
/* If request another chunk, the current entry has to be directory
|
|
|
|
* and so cache_entry->down has to be non-NULL/
|
|
|
|
* Special case of root because it's already the first entry of the root directory
|
|
|
|
*
|
|
|
|
* NOTE: this is safe even if cache_entry->down is NULL */
|
|
|
|
if(!at_root)
|
|
|
|
cache_entry = cache_entry->down;
|
|
|
|
else
|
|
|
|
at_root = false;
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
/* scan dir for name */
|
2010-01-31 17:02:56 +00:00
|
|
|
while(cache_entry != NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2010-01-31 17:02:56 +00:00
|
|
|
/* skip unused entries */
|
2011-06-20 20:07:35 +00:00
|
|
|
if(cache_entry->d_name == NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
cache_entry = cache_entry->next;
|
2010-01-31 17:02:56 +00:00
|
|
|
continue;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2010-01-31 17:02:56 +00:00
|
|
|
/* compare names */
|
|
|
|
if(!strcasecmp(part, cache_entry->d_name))
|
|
|
|
break;
|
|
|
|
/* go to next entry */
|
2005-10-07 17:38:05 +00:00
|
|
|
cache_entry = cache_entry->next;
|
|
|
|
}
|
2010-01-31 17:02:56 +00:00
|
|
|
|
|
|
|
/* handle not found case */
|
|
|
|
if(cache_entry == NULL)
|
|
|
|
return NULL;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 17:02:56 +00:00
|
|
|
/* NOTE: here cache_entry!=NULL so taking ->down is safe */
|
|
|
|
if(go_down)
|
|
|
|
return at_root ? cache_entry : cache_entry->down;
|
|
|
|
else
|
|
|
|
return at_root ? NULL : cache_entry;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 17:48:51 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
2011-06-20 20:12:35 +00:00
|
|
|
|
|
|
|
#define DIRCACHE_MAGIC 0x00d0c0a1
|
|
|
|
struct dircache_maindata {
|
|
|
|
long magic;
|
|
|
|
long size;
|
|
|
|
long entry_count;
|
|
|
|
long appflags;
|
|
|
|
struct dircache_entry *root_entry;
|
|
|
|
char *d_names_start;
|
|
|
|
};
|
|
|
|
|
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;
|
2011-06-20 20:12:30 +00:00
|
|
|
ssize_t bytes_read;
|
2005-10-07 17:38:05 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (dircache_initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
logf("Loading directory cache");
|
|
|
|
dircache_size = 0;
|
|
|
|
|
2010-07-26 13:41:47 +00:00
|
|
|
fd = open_dircache_file(O_RDONLY, 0);
|
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)
|
2011-06-20 20:12:35 +00:00
|
|
|
|| maindata.magic != DIRCACHE_MAGIC || maindata.size <= 0)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2006-08-05 20:19:10 +00:00
|
|
|
logf("Dircache file header error");
|
2005-10-07 17:38:05 +00:00
|
|
|
close(fd);
|
2010-07-26 13:41:47 +00:00
|
|
|
remove_dircache_file();
|
2005-10-07 17:38:05 +00:00
|
|
|
return -3;
|
|
|
|
}
|
2006-08-05 20:19:10 +00:00
|
|
|
|
2011-06-22 14:18:22 +00:00
|
|
|
allocated_size = maindata.size + DIRCACHE_RESERVE;
|
2011-08-30 14:01:45 +00:00
|
|
|
dircache_handle = core_alloc_ex("dircache", allocated_size, &ops);
|
|
|
|
/* block movement during upcoming I/O */
|
|
|
|
dont_move = true;
|
|
|
|
dircache_root = core_get_data(dircache_handle);
|
|
|
|
ALIGN_BUFFER(dircache_root, allocated_size, sizeof(struct dircache_entry*));
|
2006-01-31 10:08:53 +00:00
|
|
|
entry_count = maindata.entry_count;
|
2008-01-13 19:13:37 +00:00
|
|
|
appflags = maindata.appflags;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
/* read the dircache file into memory,
|
|
|
|
* start with the struct dircache_entries */
|
|
|
|
ssize_t bytes_to_read = entry_count*sizeof(struct dircache_entry);
|
|
|
|
bytes_read = read(fd, dircache_root, bytes_to_read);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
if (bytes_read != bytes_to_read)
|
2006-08-05 20:19:10 +00:00
|
|
|
{
|
2011-06-20 20:12:30 +00:00
|
|
|
logf("Dircache read failed #1");
|
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
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
/* continue with the d_names. Fix up pointers to them if needed */
|
|
|
|
bytes_to_read = maindata.size - bytes_to_read;
|
2011-06-22 14:18:22 +00:00
|
|
|
d_names_start = (char*)dircache_root + allocated_size - bytes_to_read;
|
2011-06-20 20:12:30 +00:00
|
|
|
bytes_read = read(fd, d_names_start, bytes_to_read);
|
|
|
|
close(fd);
|
|
|
|
remove_dircache_file();
|
|
|
|
if (bytes_read != bytes_to_read)
|
|
|
|
{
|
|
|
|
logf("Dircache read failed #2");
|
|
|
|
return -7;
|
|
|
|
}
|
|
|
|
|
2011-06-22 14:18:22 +00:00
|
|
|
d_names_end = d_names_start + bytes_read;
|
2011-06-20 20:12:30 +00:00
|
|
|
dot = d_names_end - sizeof(".");
|
|
|
|
dotdot = dot - sizeof("..");
|
|
|
|
|
|
|
|
/* d_names are in reverse order, so the last entry points to the first string */
|
2011-08-30 14:01:45 +00:00
|
|
|
ptrdiff_t offset_d_names = maindata.d_names_start - d_names_start;
|
|
|
|
ptrdiff_t offset_entries = maindata.root_entry - dircache_root;
|
|
|
|
offset_entries *= sizeof(struct dircache_entry); /* make it bytes */
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
/* offset_entries is less likely to differ, so check if it's 0 in the loop
|
|
|
|
* offset_d_names however is almost always non-zero, since dircache_save()
|
|
|
|
* creates a file which causes the reserve buffer to be used. since
|
|
|
|
* we allocate a new, empty DIRCACHE_RESERVE here, the strings are
|
|
|
|
* farther behind */
|
|
|
|
if (offset_entries != 0 || offset_d_names != 0)
|
|
|
|
{
|
|
|
|
for(unsigned i = 0; i < entry_count; i++)
|
|
|
|
{
|
|
|
|
if (dircache_root[i].d_name)
|
|
|
|
dircache_root[i].d_name -= offset_d_names;
|
|
|
|
|
|
|
|
if (offset_entries == 0)
|
|
|
|
continue;
|
2011-08-30 14:01:45 +00:00
|
|
|
if (dircache_root[i].next_char)
|
|
|
|
dircache_root[i].next_char -= offset_entries;
|
|
|
|
if (dircache_root[i].up_char)
|
|
|
|
dircache_root[i].up_char -= offset_entries;
|
|
|
|
if (dircache_root[i].down_char)
|
|
|
|
dircache_root[i].down_char -= offset_entries;
|
2011-06-20 20:12:30 +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
|
|
|
reserve_used = 0;
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("Done, %ld KiB used", dircache_size / 1024);
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_initialized = true;
|
|
|
|
memset(fd_bindings, 0, sizeof(fd_bindings));
|
2011-08-30 14:01:45 +00:00
|
|
|
dont_move = false;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-07-26 13:41:47 +00:00
|
|
|
remove_dircache_file();
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
if (!dircache_initialized)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
logf("Saving directory cache");
|
2011-08-30 14:01:45 +00:00
|
|
|
dont_move = true;
|
2010-07-26 13:41:47 +00:00
|
|
|
fd = open_dircache_file(O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
maindata.magic = DIRCACHE_MAGIC;
|
|
|
|
maindata.size = dircache_size;
|
|
|
|
maindata.root_entry = dircache_root;
|
2011-06-20 20:12:30 +00:00
|
|
|
maindata.d_names_start = d_names_start;
|
2006-01-31 10:08:53 +00:00
|
|
|
maindata.entry_count = entry_count;
|
2008-01-13 19:13:37 +00:00
|
|
|
maindata.appflags = appflags;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
/* Dump whole directory cache to disk
|
|
|
|
* start by writing the dircache_entries */
|
|
|
|
size_t bytes_to_write = entry_count*sizeof(struct dircache_entry);
|
|
|
|
bytes_written = write(fd, dircache_root, bytes_to_write);
|
|
|
|
if (bytes_written != bytes_to_write)
|
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
|
|
|
}
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
/* continue with the d_names */
|
|
|
|
bytes_to_write = d_names_end - d_names_start;
|
|
|
|
bytes_written = write(fd, d_names_start, bytes_to_write);
|
|
|
|
close(fd);
|
|
|
|
if (bytes_written != bytes_to_write)
|
|
|
|
{
|
|
|
|
logf("dircache: write failed #3");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2011-08-30 14:01:45 +00:00
|
|
|
dont_move = false;
|
2005-10-07 17:38:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-07-26 13:41:47 +00:00
|
|
|
#endif /* HAVE_EEPROM_SETTINGS */
|
2005-10-07 17:38:05 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2011-06-20 20:12:30 +00:00
|
|
|
struct dircache_entry* root_entry;
|
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;
|
2007-01-12 11:10:04 +00:00
|
|
|
dircache_initializing = true;
|
2008-01-13 19:13:37 +00:00
|
|
|
appflags = 0;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
/* reset dircache and alloc root entry */
|
2008-03-11 19:39:26 +00:00
|
|
|
entry_count = 0;
|
2011-06-20 20:12:30 +00:00
|
|
|
root_entry = allocate_entry();
|
2011-08-30 14:01:45 +00:00
|
|
|
dont_move = true;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2008-03-11 19:39:26 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2011-06-20 20:12:30 +00:00
|
|
|
append_position = root_entry;
|
2008-03-11 19:39:26 +00:00
|
|
|
|
|
|
|
for (i = NUM_VOLUMES; i >= 0; i--)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2008-03-11 19:39:26 +00:00
|
|
|
if (fat_ismounted(i))
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
cpu_boost(true);
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2010-02-14 19:09:52 +00:00
|
|
|
if (dircache_scan_and_build(IF_MV2(i,) append_position) < 0)
|
2008-03-11 19:39:26 +00:00
|
|
|
#else
|
2011-06-20 20:12:30 +00:00
|
|
|
if (dircache_scan_and_build(IF_MV2(0,) root_entry) < 0)
|
2008-03-11 19:39:26 +00:00
|
|
|
#endif /* HAVE_MULTIVOLUME */
|
|
|
|
{
|
2010-02-16 22:49:11 +00:00
|
|
|
logf("dircache_scan_and_build failed");
|
2008-03-11 19:39:26 +00:00
|
|
|
cpu_boost(false);
|
|
|
|
dircache_size = 0;
|
|
|
|
dircache_initializing = false;
|
2011-08-30 14:01:45 +00:00
|
|
|
dont_move = false;
|
2008-03-11 19:39:26 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
cpu_boost(false);
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
}
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2008-03-11 19:39:26 +00:00
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2007-03-18 09:50:53 +00:00
|
|
|
logf("Done, %ld 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);
|
|
|
|
}
|
2011-08-30 14:01:45 +00:00
|
|
|
|
|
|
|
dont_move = false;
|
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)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
queue_wait(&dircache_queue, &ev);
|
|
|
|
|
|
|
|
switch (ev.id)
|
|
|
|
{
|
2008-03-11 19:39:26 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
case SYS_FS_CHANGED:
|
|
|
|
if (!dircache_initialized)
|
|
|
|
break;
|
|
|
|
dircache_initialized = false;
|
|
|
|
#endif
|
2005-10-07 17:38:05 +00:00
|
|
|
case DIRCACHE_BUILD:
|
|
|
|
thread_enabled = true;
|
2011-08-30 14:01:45 +00:00
|
|
|
if (dircache_do_rebuild() < 0)
|
|
|
|
core_free(dircache_handle);
|
2005-10-07 17:38:05 +00:00
|
|
|
thread_enabled = false;
|
|
|
|
break ;
|
|
|
|
|
2005-10-20 08:33:59 +00:00
|
|
|
case DIRCACHE_STOP:
|
|
|
|
logf("Stopped the rebuilding.");
|
|
|
|
dircache_initialized = false;
|
|
|
|
break ;
|
|
|
|
|
2010-06-21 16:53:00 +00:00
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
|
2005-10-07 17:38:05 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
usb_wait_for_disconnect(&dircache_queue);
|
|
|
|
break ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:30 +00:00
|
|
|
static void generate_dot_d_names(void)
|
|
|
|
{
|
|
|
|
dot = (d_names_start -= sizeof("."));
|
|
|
|
dotdot = (d_names_start -= sizeof(".."));
|
|
|
|
dircache_size += sizeof(".") + sizeof("..");
|
|
|
|
strcpy(dot, ".");
|
|
|
|
strcpy(dotdot, "..");
|
|
|
|
}
|
2011-08-30 14:01:33 +00:00
|
|
|
|
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");
|
2010-07-26 13:41:47 +00:00
|
|
|
#ifdef HAVE_EEPROM_SETTINGS
|
|
|
|
remove_dircache_file();
|
|
|
|
#endif
|
|
|
|
|
2006-08-05 20:19:10 +00:00
|
|
|
/* Background build, dircache has been previously allocated */
|
2011-08-16 13:01:03 +00:00
|
|
|
if (allocated_size > 0)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2011-07-01 13:38:20 +00:00
|
|
|
d_names_start = d_names_end;
|
|
|
|
dircache_size = 0;
|
|
|
|
reserve_used = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
thread_enabled = true;
|
2006-08-14 19:03:37 +00:00
|
|
|
dircache_initializing = true;
|
2011-07-01 13:38:20 +00:00
|
|
|
generate_dot_d_names();
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
|
|
|
|
return 2;
|
|
|
|
}
|
2011-08-30 14:01:33 +00:00
|
|
|
|
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;
|
2011-08-30 14:01:45 +00:00
|
|
|
dircache_handle = core_alloc_ex("dircache", allocated_size, &ops);
|
|
|
|
dircache_root = core_get_data(dircache_handle);
|
|
|
|
ALIGN_BUFFER(dircache_root, allocated_size, sizeof(struct dircache_entry*));
|
2011-07-30 23:21:58 +00:00
|
|
|
d_names_start = d_names_end = ((char*)dircache_root)+allocated_size-1;
|
2011-06-20 20:12:30 +00:00
|
|
|
dircache_size = 0;
|
2005-10-07 17:38:05 +00:00
|
|
|
thread_enabled = true;
|
2011-06-20 20:12:30 +00:00
|
|
|
generate_dot_d_names();
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
/* Start a transparent rebuild. */
|
|
|
|
queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:52 +00:00
|
|
|
/* We'll use the entire audiobuf to allocate the dircache
|
|
|
|
* struct dircache_entrys are allocated from the beginning
|
|
|
|
* and their corresponding d_name from the end
|
2011-06-20 20:12:30 +00:00
|
|
|
* after generation the buffer will be compacted with DIRCACHE_RESERVE
|
2011-06-20 20:12:52 +00:00
|
|
|
* free bytes inbetween */
|
2011-08-14 15:13:00 +00:00
|
|
|
size_t got_size;
|
2011-08-30 14:01:45 +00:00
|
|
|
dircache_handle = core_alloc_maximum("dircache", &got_size, &ops);
|
|
|
|
char* buf = core_get_data(dircache_handle);
|
2011-08-15 15:13:17 +00:00
|
|
|
dircache_root = (struct dircache_entry*)ALIGN_UP(buf,
|
2011-08-30 14:01:45 +00:00
|
|
|
sizeof(struct dircache_entry*));
|
2011-08-15 15:13:17 +00:00
|
|
|
d_names_start = d_names_end = buf + got_size - 1;
|
2011-06-20 20:12:30 +00:00
|
|
|
dircache_size = 0;
|
|
|
|
generate_dot_d_names();
|
2006-08-02 17:39:34 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
/* Start a non-transparent rebuild. */
|
2011-06-20 20:12:30 +00:00
|
|
|
int res = dircache_do_rebuild();
|
2011-06-20 20:12:52 +00:00
|
|
|
if (res < 0)
|
2011-08-14 15:13:00 +00:00
|
|
|
goto fail;
|
2011-06-20 20:12:52 +00:00
|
|
|
|
|
|
|
/* now compact the dircache buffer */
|
|
|
|
char* dst = ((char*)&dircache_root[entry_count] + DIRCACHE_RESERVE);
|
2011-07-30 23:21:58 +00:00
|
|
|
ptrdiff_t offset = d_names_start - dst;
|
2011-06-20 20:12:52 +00:00
|
|
|
if (offset <= 0) /* something went wrong */
|
2011-08-14 15:13:00 +00:00
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-06-20 20:12:52 +00:00
|
|
|
|
2011-07-30 23:21:58 +00:00
|
|
|
/* memmove d_names down, there's a possibility of overlap
|
|
|
|
* equivaent to dircache_size - entry_count*sizeof(struct dircache_entry) */
|
|
|
|
ptrdiff_t size_to_move = d_names_end - d_names_start;
|
2011-06-20 20:12:52 +00:00
|
|
|
memmove(dst, d_names_start, size_to_move);
|
|
|
|
|
|
|
|
/* fix up pointers to the d_names */
|
|
|
|
for(unsigned i = 0; i < entry_count; i++)
|
|
|
|
dircache_root[i].d_name -= offset;
|
|
|
|
|
2011-07-04 17:13:48 +00:00
|
|
|
d_names_start -= offset;
|
2011-06-20 20:12:52 +00:00
|
|
|
d_names_end -= offset;
|
2011-07-30 23:21:58 +00:00
|
|
|
dot -= offset;
|
|
|
|
dotdot -= offset;
|
2011-07-04 17:13:48 +00:00
|
|
|
|
2011-08-15 15:13:17 +00:00
|
|
|
/* equivalent to dircache_size + DIRCACHE_RESERVE + align */
|
|
|
|
allocated_size = (d_names_end - buf);
|
2011-06-20 20:12:52 +00:00
|
|
|
reserve_used = 0;
|
2011-06-20 20:12:30 +00:00
|
|
|
|
2011-08-30 14:01:45 +00:00
|
|
|
core_shrink(dircache_handle, dircache_root, allocated_size);
|
2011-08-14 15:13:00 +00:00
|
|
|
return res;
|
|
|
|
fail:
|
|
|
|
dircache_disable();
|
2011-08-30 14:01:45 +00:00
|
|
|
core_free(dircache_handle);
|
2011-06-20 20:12:30 +00:00
|
|
|
return res;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
/**
|
|
|
|
* Steal the allocated dircache buffer and disable dircache.
|
|
|
|
*/
|
2011-08-14 15:13:00 +00:00
|
|
|
void* dircache_steal_buffer(size_t *size)
|
2006-04-16 17:32:54 +00:00
|
|
|
{
|
|
|
|
dircache_disable();
|
|
|
|
if (dircache_size == 0)
|
|
|
|
{
|
|
|
|
*size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-30 14:01:45 +00:00
|
|
|
|
|
|
|
/* since we give up the buffer (without freeing), it must not move anymore */
|
|
|
|
dont_move = true;
|
2006-04-16 17:32:54 +00:00
|
|
|
*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;
|
2011-05-08 21:06:38 +00:00
|
|
|
int thread_id __attribute__((unused));
|
2005-10-07 17:38:05 +00:00
|
|
|
|
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++)
|
|
|
|
{
|
2011-08-30 14:01:33 +00:00
|
|
|
opendirs[i].theent.d_name = opendir_dnames[i];
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&dircache_queue, true);
|
2010-04-03 22:02:09 +00:00
|
|
|
thread_id = create_thread(dircache_thread, dircache_stack,
|
2007-10-16 01:25:17 +00:00
|
|
|
sizeof(dircache_stack), 0, dircache_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_BACKGROUND)
|
|
|
|
IF_COP(, CPU));
|
2010-04-03 22:02:09 +00:00
|
|
|
#ifdef HAVE_IO_PRIORITY
|
|
|
|
thread_set_io_priority(thread_id,IO_PRIORITY_BACKGROUND);
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
{
|
2007-04-07 17:48:51 +00:00
|
|
|
return dircache_initializing || thread_enabled;
|
2006-07-10 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 19:13:37 +00:00
|
|
|
/**
|
|
|
|
* Set application flags used to determine if dircache is still intact.
|
|
|
|
*/
|
|
|
|
void dircache_set_appflag(long mask)
|
|
|
|
{
|
|
|
|
appflags |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get application flags used to determine if dircache is still intact.
|
|
|
|
*/
|
|
|
|
bool dircache_get_appflag(long mask)
|
|
|
|
{
|
2008-01-13 19:20:04 +00:00
|
|
|
return dircache_is_enabled() && (appflags & mask);
|
2008-01-13 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2011-06-20 20:12:42 +00:00
|
|
|
* Usermode function to return dircache_entry index to the given path.
|
2005-11-26 20:47:10 +00:00
|
|
|
*/
|
2011-06-20 20:12:42 +00:00
|
|
|
static int dircache_get_entry_id_ex(const char *filename, bool go_down)
|
2005-11-17 19:31:29 +00:00
|
|
|
{
|
|
|
|
if (!dircache_initialized || filename == NULL)
|
2011-06-20 20:12:42 +00:00
|
|
|
return -1;
|
2005-11-17 19:31:29 +00:00
|
|
|
|
2011-06-20 20:12:42 +00:00
|
|
|
struct dircache_entry* res = dircache_get_entry(filename, go_down);
|
|
|
|
return res ? res - dircache_root : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dircache_get_entry_id(const char* filename)
|
|
|
|
{
|
|
|
|
return dircache_get_entry_id_ex(filename, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal: Get the startcluster for the index
|
|
|
|
*/
|
|
|
|
long _dircache_get_entry_startcluster(int id)
|
|
|
|
{
|
|
|
|
return get_entry(id)->startcluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal: Get the struct dirinfo for the index
|
|
|
|
*/
|
|
|
|
struct dirinfo* _dircache_get_entry_dirinfo(int id)
|
|
|
|
{
|
|
|
|
return &get_entry(id)->info;
|
2005-11-17 19:31:29 +00:00
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:10 +00:00
|
|
|
/*
|
|
|
|
* build a path from an entry upto the root using recursion
|
|
|
|
*
|
|
|
|
* it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
|
|
|
|
* and it will leave a trailing '/'
|
|
|
|
*
|
|
|
|
* returns the position of that trailing '/' so it can be deleted afterwards
|
|
|
|
* (or, in case of truncation, the position of the nul byte */
|
|
|
|
static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size)
|
|
|
|
{
|
2011-06-20 20:12:47 +00:00
|
|
|
int offset = 1;
|
2011-06-20 20:12:10 +00:00
|
|
|
/* has parent? */
|
|
|
|
if (entry->up)
|
2011-06-20 20:12:47 +00:00
|
|
|
offset += copy_path_helper(entry->up, buf, size);
|
2011-06-20 20:12:10 +00:00
|
|
|
|
2011-06-20 20:12:47 +00:00
|
|
|
size_t len = strlcpy(buf+offset, entry->d_name, size - offset) + offset;
|
2011-06-20 20:12:10 +00:00
|
|
|
if (len < size)
|
|
|
|
{
|
|
|
|
buf[len++] = '/';
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
return len-1;
|
|
|
|
}
|
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.
|
2011-06-20 20:12:15 +00:00
|
|
|
*
|
|
|
|
* Returns the size of the resulting string, or 0 if an error occured
|
2005-11-26 20:47:10 +00:00
|
|
|
*/
|
2011-06-20 20:12:42 +00:00
|
|
|
size_t dircache_copy_path(int index, char *buf, size_t size)
|
2005-11-17 19:31:29 +00:00
|
|
|
{
|
2011-06-20 20:12:42 +00:00
|
|
|
if (!size || !buf || index < 0)
|
2011-06-20 20:12:15 +00:00
|
|
|
return 0;
|
2011-06-20 20:12:10 +00:00
|
|
|
|
|
|
|
buf[0] = '/';
|
2011-06-20 20:12:47 +00:00
|
|
|
size_t res = copy_path_helper(&dircache_root[index], buf, size - 1);
|
2011-06-20 20:12:10 +00:00
|
|
|
/* fixup trailing '/' */
|
|
|
|
buf[res] = '\0';
|
2011-06-20 20:12:15 +00:00
|
|
|
return res;
|
2005-11-17 19:31:29 +00:00
|
|
|
}
|
|
|
|
|
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. */
|
2007-04-07 17:48:51 +00:00
|
|
|
while (!dircache_initialized && dircache_is_initializing())
|
2006-03-29 09:38:45 +00:00
|
|
|
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;
|
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(basedir, path, sizeof(basedir));
|
2005-10-07 17:38:05 +00:00
|
|
|
new = strrchr(basedir, '/');
|
|
|
|
if (new == NULL)
|
|
|
|
{
|
|
|
|
logf("error occurred");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*new = '\0';
|
|
|
|
new++;
|
|
|
|
|
2010-01-31 17:02:56 +00:00
|
|
|
entry = dircache_get_entry(basedir, true);
|
2005-10-07 17:38:05 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("basedir not found!");
|
2007-10-02 15:35:12 +00:00
|
|
|
logf("%s", 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;
|
|
|
|
|
2011-06-20 20:07:35 +00:00
|
|
|
if (entry->d_name != NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2011-06-20 20:07:35 +00:00
|
|
|
entry = dircache_gen_next(entry);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
dircache_initialized = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2011-06-20 20:12:30 +00:00
|
|
|
|
|
|
|
size_t size = strlen(new) + 1;
|
|
|
|
entry->d_name = (d_names_start -= size);
|
2005-10-07 17:38:05 +00:00
|
|
|
entry->startcluster = 0;
|
2010-09-01 21:29:34 +00:00
|
|
|
memset(&entry->info, 0, sizeof(entry->info));
|
|
|
|
entry->info.attribute = attribute;
|
2011-06-20 20:07:35 +00:00
|
|
|
|
|
|
|
strcpy(entry->d_name, new);
|
2011-06-20 20:12:30 +00:00
|
|
|
dircache_size += size;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
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. */
|
2007-04-07 17:48:51 +00:00
|
|
|
if (!dircache_initialized && dircache_is_initializing())
|
2006-03-29 09:38:45 +00:00
|
|
|
{
|
|
|
|
if (fdbind_idx >= MAX_PENDING_BINDINGS)
|
|
|
|
return ;
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(fdbind_cache[fdbind_idx].path, path,
|
|
|
|
sizeof(fdbind_cache[fdbind_idx].path));
|
2006-03-29 09:38:45 +00:00
|
|
|
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);
|
2010-01-31 17:02:56 +00:00
|
|
|
entry = dircache_get_entry(path, false);
|
2005-10-07 17:38:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-02-01 23:00:35 +00:00
|
|
|
logf("dircache fd(%d) access error", fd);
|
2006-03-28 11:51:12 +00:00
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2010-09-01 21:29:34 +00:00
|
|
|
fd_bindings[fd]->info.size = newsize;
|
2006-03-28 11:51:12 +00:00
|
|
|
fd_bindings[fd]->startcluster = startcluster;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2007-09-02 13:24:51 +00:00
|
|
|
void dircache_update_filetime(int fd)
|
|
|
|
{
|
|
|
|
#if CONFIG_RTC == 0
|
|
|
|
(void)fd;
|
|
|
|
#else
|
|
|
|
short year;
|
|
|
|
struct tm *now = get_time();
|
|
|
|
if (!dircache_initialized || fd < 0)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
if (fd_bindings[fd] == NULL)
|
|
|
|
{
|
|
|
|
logf("dircache fd access error");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
year = now->tm_year+1900-1980;
|
2010-09-01 21:29:34 +00:00
|
|
|
fd_bindings[fd]->info.wrtdate = (((year)&0x7f)<<9) |
|
|
|
|
(((now->tm_mon+1)&0xf)<<5) |
|
|
|
|
(((now->tm_mday)&0x1f));
|
|
|
|
fd_bindings[fd]->info.wrttime = (((now->tm_hour)&0x1f)<<11) |
|
|
|
|
(((now->tm_min)&0x3f)<<5) |
|
|
|
|
(((now->tm_sec/2)&0x1f));
|
2007-09-02 13:24:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
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 ;
|
|
|
|
|
2010-05-21 12:50:50 +00:00
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
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);
|
2010-01-31 17:02:56 +00:00
|
|
|
entry = dircache_get_entry(path, false);
|
|
|
|
if (entry == NULL || entry->down == NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2010-01-31 17:02:56 +00:00
|
|
|
logf("not found or not a directory!");
|
2005-10-07 17:38:05 +00:00
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->down = NULL;
|
2011-06-20 20:07:35 +00:00
|
|
|
entry->d_name = NULL;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2010-01-31 17:02:56 +00:00
|
|
|
entry = dircache_get_entry(name, false);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2011-06-20 20:07:35 +00:00
|
|
|
entry->d_name = NULL;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-01-31 17:02:56 +00:00
|
|
|
entry = dircache_get_entry(oldpath, false);
|
2005-10-07 17:38:05 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
logf("not found!");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the old entry. */
|
2011-06-20 20:07:35 +00:00
|
|
|
entry->d_name = NULL;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
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] != '/')
|
|
|
|
{
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(absolute_path, oldpath, sizeof(absolute_path));
|
2006-02-28 11:41:35 +00:00
|
|
|
p = strrchr(absolute_path, '/');
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
logf("Invalid path");
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(p, absolute_path, sizeof(absolute_path)-strlen(p));
|
2006-02-28 11:41:35 +00:00
|
|
|
newpath = absolute_path;
|
|
|
|
}
|
|
|
|
|
2010-09-01 21:29:34 +00:00
|
|
|
newentry = dircache_new_entry(newpath, entry->info.attribute);
|
2005-10-07 17:38:05 +00:00
|
|
|
if (newentry == NULL)
|
|
|
|
{
|
|
|
|
dircache_initialized = false;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2005-11-18 19:28:22 +00:00
|
|
|
newentry->down = oldentry.down;
|
|
|
|
newentry->startcluster = oldentry.startcluster;
|
2010-09-01 21:29:34 +00:00
|
|
|
newentry->info.size = oldentry.info.size;
|
|
|
|
newentry->info.wrtdate = oldentry.info.wrtdate;
|
|
|
|
newentry->info.wrttime = oldentry.info.wrttime;
|
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
|
|
|
}
|
|
|
|
|
2010-05-21 12:50:50 +00:00
|
|
|
static bool is_disable_msg_pending(void)
|
2010-05-21 08:31:11 +00:00
|
|
|
{
|
2010-05-21 12:50:50 +00:00
|
|
|
return check_event_queue();
|
2010-05-21 08:31:11 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_CACHED* opendir_cached(const char* name)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
int dd;
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_CACHED* pdir = opendirs;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-02-16 22:49:11 +00:00
|
|
|
|
|
|
|
pdir->busy = true;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2010-05-21 12:50:50 +00:00
|
|
|
if (!dircache_initialized || is_disable_msg_pending())
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2011-06-20 20:12:42 +00:00
|
|
|
pdir->internal_entry = -1;
|
2010-02-16 22:49:11 +00:00
|
|
|
pdir->regulardir = opendir_uncached(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pdir->regulardir = NULL;
|
2011-06-20 20:12:42 +00:00
|
|
|
pdir->internal_entry = dircache_get_entry_id_ex(name, true);
|
2010-09-01 21:29:34 +00:00
|
|
|
pdir->theent.info.attribute = -1; /* used to make readdir_cached aware of the first call */
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2011-06-20 20:12:42 +00:00
|
|
|
if (pdir->internal_entry == -1 && pdir->regulardir == NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
|
|
|
pdir->busy = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pdir;
|
|
|
|
}
|
|
|
|
|
2010-02-16 22:49:11 +00:00
|
|
|
struct dirent_cached* readdir_cached(DIR_CACHED* dir)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2011-06-20 20:12:42 +00:00
|
|
|
struct dircache_entry *ce = get_entry(dir->internal_entry);
|
2007-07-20 17:06:55 +00:00
|
|
|
struct dirent_uncached *regentry;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2007-07-20 17:06:55 +00:00
|
|
|
regentry = readdir_uncached(dir->regulardir);
|
2005-10-07 17:38:05 +00:00
|
|
|
if (regentry == NULL)
|
|
|
|
return NULL;
|
2006-01-31 10:08:53 +00:00
|
|
|
|
2010-02-16 22:49:11 +00:00
|
|
|
strlcpy(dir->theent.d_name, regentry->d_name, MAX_PATH);
|
|
|
|
dir->theent.startcluster = regentry->startcluster;
|
2010-09-01 21:29:34 +00:00
|
|
|
dir->theent.info = regentry->info;
|
2005-10-07 17:38:05 +00:00
|
|
|
|
2010-02-16 22:49:11 +00:00
|
|
|
return &dir->theent;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
2010-02-16 22:49:11 +00:00
|
|
|
|
|
|
|
/* if theent.attribute=-1 then this is the first call */
|
|
|
|
/* otherwise, this is is not so we first take the entry's ->next */
|
|
|
|
/* NOTE: normal file can't have attribute=-1 */
|
2010-09-01 21:29:34 +00:00
|
|
|
if(dir->theent.info.attribute != -1)
|
2010-02-16 22:49:11 +00:00
|
|
|
ce = ce->next;
|
|
|
|
/* skip unused entries */
|
2011-06-20 20:07:35 +00:00
|
|
|
while(ce != NULL && ce->d_name == NULL)
|
2010-02-16 22:49:11 +00:00
|
|
|
ce = ce->next;
|
|
|
|
|
|
|
|
if (ce == NULL)
|
2005-10-07 17:38:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
2010-02-16 22:49:11 +00:00
|
|
|
strlcpy(dir->theent.d_name, ce->d_name, MAX_PATH);
|
|
|
|
/* Can't do `dir->theent = *ce`
|
2005-10-08 18:41:11 +00:00
|
|
|
because that modifies the d_name pointer. */
|
2010-02-16 22:49:11 +00:00
|
|
|
dir->theent.startcluster = ce->startcluster;
|
2010-09-01 21:29:34 +00:00
|
|
|
dir->theent.info = ce->info;
|
2011-06-20 20:12:42 +00:00
|
|
|
dir->internal_entry = ce - dircache_root;
|
2005-10-08 18:41:11 +00:00
|
|
|
|
2011-06-20 20:07:35 +00:00
|
|
|
//logf("-> %s", ce->d_name);
|
2010-02-16 22:49:11 +00:00
|
|
|
return &dir->theent;
|
2005-10-07 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
int closedir_cached(DIR_CACHED* dir)
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
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)
|
2007-07-20 17:06:55 +00:00
|
|
|
return closedir_uncached(dir->regulardir);
|
2005-10-07 17:38:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
int mkdir_cached(const char *name)
|
|
|
|
{
|
|
|
|
int rc=mkdir_uncached(name);
|
|
|
|
if (rc >= 0)
|
|
|
|
dircache_mkdir(name);
|
|
|
|
return(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rmdir_cached(const char* name)
|
|
|
|
{
|
|
|
|
int rc=rmdir_uncached(name);
|
2010-02-16 22:49:11 +00:00
|
|
|
if(rc >= 0)
|
2007-07-20 17:06:55 +00:00
|
|
|
dircache_rmdir(name);
|
|
|
|
return(rc);
|
|
|
|
}
|