2002-05-03 15:36:52 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
#include <stdio.h>
|
2002-05-27 09:13:24 +00:00
|
|
|
|
#include <errno.h>
|
2002-05-03 15:36:52 +00:00
|
|
|
|
#include <string.h>
|
2002-05-13 12:29:34 +00:00
|
|
|
|
#include <stdbool.h>
|
2002-05-03 15:36:52 +00:00
|
|
|
|
#include "fat.h"
|
|
|
|
|
#include "dir.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
#define MAX_OPEN_DIRS 8
|
|
|
|
|
|
|
|
|
|
static DIR opendirs[MAX_OPEN_DIRS];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
|
|
|
|
DIR* opendir(char* name)
|
|
|
|
|
{
|
2002-07-15 22:58:28 +00:00
|
|
|
|
char namecopy[MAX_PATH];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
char* part;
|
2002-05-24 09:57:33 +00:00
|
|
|
|
char* end;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
struct fat_direntry entry;
|
2002-05-27 09:13:24 +00:00
|
|
|
|
int dd;
|
|
|
|
|
|
|
|
|
|
/* find a free dir descriptor */
|
|
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++ )
|
|
|
|
|
if ( !opendirs[dd].busy )
|
|
|
|
|
break;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ( dd == MAX_OPEN_DIRS ) {
|
|
|
|
|
DEBUGF("Too many dirs open\n");
|
|
|
|
|
errno = EMFILE;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = true;
|
|
|
|
|
|
2002-05-03 15:36:52 +00:00
|
|
|
|
if ( name[0] != '/' ) {
|
|
|
|
|
DEBUGF("Only absolute paths supported right now\n");
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ( fat_opendir(&(opendirs[dd].fatdir), 0) < 0 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
DEBUGF("Failed opening root dir\n");
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-24 09:57:33 +00:00
|
|
|
|
strncpy(namecopy,name,sizeof(namecopy));
|
|
|
|
|
namecopy[sizeof(namecopy)-1] = 0;
|
|
|
|
|
|
|
|
|
|
for ( part = strtok_r(namecopy, "/", &end); part;
|
|
|
|
|
part = strtok_r(NULL, "/", &end)) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
/* scan dir for name */
|
|
|
|
|
while (1) {
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ((fat_getnext(&(opendirs[dd].fatdir),&entry) < 0) ||
|
|
|
|
|
(!entry.name[0])) {
|
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
2002-05-27 09:13:24 +00:00
|
|
|
|
}
|
2002-05-03 15:36:52 +00:00
|
|
|
|
if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
|
2002-08-15 11:26:12 +00:00
|
|
|
|
(!strcasecmp(part, entry.name)) ) {
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ( fat_opendir(&(opendirs[dd].fatdir),
|
|
|
|
|
entry.firstcluster) < 0 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
DEBUGF("Failed opening dir '%s' (%d)\n",
|
|
|
|
|
part, entry.firstcluster);
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
return &opendirs[dd];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int closedir(DIR* dir)
|
|
|
|
|
{
|
2002-05-27 09:13:24 +00:00
|
|
|
|
dir->busy=false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* dir)
|
|
|
|
|
{
|
|
|
|
|
struct fat_direntry entry;
|
2002-05-27 09:13:24 +00:00
|
|
|
|
struct dirent* theent = &(dir->theent);
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
|
|
|
|
if (fat_getnext(&(dir->fatdir),&entry) < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if ( !entry.name[0] )
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return NULL;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) );
|
|
|
|
|
theent->attribute = entry.attr;
|
|
|
|
|
theent->size = entry.filesize;
|
|
|
|
|
theent->startcluster = entry.firstcluster;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
return theent;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
}
|