From 4d4ec3aa0b2c0bb45e7c004885accee303f1277f Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Fri, 3 May 2002 15:36:52 +0000 Subject: Added opendir, closedir and readdir git-svn-id: svn://svn.rockbox.org/rockbox/trunk@412 a1c6a512-1295-4272-9138-f99709370657 --- firmware/common/dir.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++ firmware/common/dir.h | 9 ++++- firmware/test/fat/Makefile | 5 ++- firmware/test/fat/main.c | 49 +++++++++++------------ 4 files changed, 133 insertions(+), 27 deletions(-) create mode 100644 firmware/common/dir.c diff --git a/firmware/common/dir.c b/firmware/common/dir.c new file mode 100644 index 0000000000..9388fb243a --- /dev/null +++ b/firmware/common/dir.c @@ -0,0 +1,97 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Bjö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 +#include +#include "fat.h" +#include "dir.h" +#include "debug.h" +#include "types.h" + +static DIR thedir; +static struct dirent theent; +static bool busy=FALSE; + +DIR* opendir(char* name) +{ + char* part; + struct fat_direntry entry; + struct fat_dir* dir = &(thedir.fatdir); + + if ( busy ) { + DEBUGF("Only one open dir at a time\n"); + return NULL; + } + + if ( name[0] != '/' ) { + DEBUGF("Only absolute paths supported right now\n"); + return NULL; + } + + if ( fat_opendir(dir, 0) < 0 ) { + DEBUGF("Failed opening root dir\n"); + return NULL; + } + + while ( (part = strtok(name, "/")) ) { + int partlen = strlen(part); + /* scan dir for name */ + while (1) { + if (fat_getnext(dir,&entry) < 0) + return NULL; + if ( !entry.name[0] ) + return NULL; + if ( (entry.attr & FAT_ATTR_DIRECTORY) && + (!strncmp(part, entry.name, partlen)) ) { + if ( fat_opendir(dir, entry.firstcluster) < 0 ) { + DEBUGF("Failed opening dir '%s' (%d)\n", + part, entry.firstcluster); + return NULL; + } + break; + } + } + } + + busy = TRUE; + + return &thedir; +} + +int closedir(DIR* dir) +{ + busy=FALSE; + return 0; +} + +struct dirent* readdir(DIR* dir) +{ + struct fat_direntry entry; + + if (fat_getnext(&(dir->fatdir),&entry) < 0) + return NULL; + + if ( !entry.name[0] ) + return NULL; + + strncpy(theent.d_name, entry.name, sizeof( theent.d_name ) ); + theent.attribute = entry.attr; + theent.size = entry.filesize; + + return &theent; +} diff --git a/firmware/common/dir.h b/firmware/common/dir.h index 078ede2d73..2aa248579e 100644 --- a/firmware/common/dir.h +++ b/firmware/common/dir.h @@ -16,7 +16,6 @@ * KIND, either express or implied. * ****************************************************************************/ - #ifndef _DIR_H_ #define _DIR_H_ @@ -31,10 +30,16 @@ struct dirent { #ifndef SIMULATOR + +#include "fat.h" + typedef struct { - int offset; + int startcluster; + struct fat_dir fatdir; } DIR; + #else // SIMULATOR + #ifdef WIN32 #include typedef struct DIRtag diff --git a/firmware/test/fat/Makefile b/firmware/test/fat/Makefile index 6b253fe16c..48bddb9630 100644 --- a/firmware/test/fat/Makefile +++ b/firmware/test/fat/Makefile @@ -5,7 +5,7 @@ CFLAGS = -g -Wall -DTEST_FAT -I$(DRIVERS) -I$(FIRMWARE)/common -I$(FIRMWARE) -I. TARGET = fat -$(TARGET): fat.o ata-sim.o debug.o main.o disk.o +$(TARGET): fat.o ata-sim.o main.o disk.o debug.o dir.o gcc -g -o fat $+ -lfl fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h @@ -14,6 +14,9 @@ fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h disk.o: $(FIRMWARE)/common/disk.c $(CC) $(CFLAGS) -c $< -o $@ +dir.o: $(FIRMWARE)/common/dir.c + $(CC) $(CFLAGS) -c $< -o $@ + debug.o: $(FIRMWARE)/debug.c $(CC) $(CFLAGS) -c $< -o $@ diff --git a/firmware/test/fat/main.c b/firmware/test/fat/main.c index ffe3dd8764..13c30b2ed5 100644 --- a/firmware/test/fat/main.c +++ b/firmware/test/fat/main.c @@ -5,11 +5,12 @@ #include "ata.h" #include "debug.h" #include "disk.h" +#include "dir.h" void dbg_dump_sector(int sec); void dbg_dump_buffer(unsigned char *buf); void dbg_print_bpb(struct bpb *bpb); -void dbg_console(struct bpb *bpb); +void dbg_console(void); void dbg_dump_sector(int sec) { @@ -79,34 +80,38 @@ void dbg_print_bpb(struct bpb *bpb) printf("fat_type = FAT32\n"); } -void dbg_dir(struct bpb *bpb, int currdir) +void dbg_dir(char* currdir) { - struct fat_dirent dent; - struct fat_direntry de; + DIR* dir; + struct dirent* entry; - if(fat_opendir(bpb, &dent, currdir) >= 0) + dir = opendir(currdir); + if (dir) { - while(fat_getnext(bpb, &dent, &de) >= 0) + for ( entry = readdir(dir); + entry; + entry = readdir(dir) ) { - printf("%s (%d)\n", de.name,de.firstcluster); + printf("%s (%08x)\n", entry->d_name, entry->size); } } else { - fprintf(stderr, "Could not read dir on cluster %d\n", currdir); + fprintf(stderr, "Could not open dir %s\n", currdir); } + closedir(dir); } -void dbg_type(struct bpb *bpb, int cluster) +void dbg_type(int cluster) { unsigned char buf[SECTOR_SIZE*5]; - struct fat_fileent ent; + struct fat_file ent; int i; - fat_open(bpb,cluster,&ent); + fat_open(cluster,&ent); for (i=0;i<5;i++) - if(fat_read(bpb, &ent, 1, buf) >= 0) + if(fat_read(&ent, 1, buf) >= 0) { buf[SECTOR_SIZE]=0; printf("%s\n", buf); @@ -125,7 +130,7 @@ void dbg_prompt(void) printf("C:%s> ", current_directory); } -void dbg_console(struct bpb* bpb) +void dbg_console(void) { char cmd[32] = ""; char last_cmd[32] = ""; @@ -148,12 +153,10 @@ void dbg_console(struct bpb* bpb) { if(!strcasecmp(s, "dir")) { - int secnum = 0; - if((s = strtok(NULL, " \n"))) - { - secnum = atoi(s); - } - dbg_dir(bpb, secnum); + s = strtok(NULL, " \n"); + if (!s) + s = "/"; + dbg_dir(s); continue; } @@ -182,7 +185,7 @@ void dbg_console(struct bpb* bpb) { cluster = atoi(s); } - dbg_type(bpb,cluster); + dbg_type(cluster); continue; } @@ -198,8 +201,6 @@ void dbg_console(struct bpb* bpb) int main(int argc, char *argv[]) { - struct bpb bpb; - if(ata_init()) { DEBUGF("*** Warning! The disk is uninitialized\n"); return -1; @@ -209,11 +210,11 @@ int main(int argc, char *argv[]) return -1; } - if(fat_mount(&bpb,part[0].start)) { + if(fat_mount(part[0].start)) { DEBUGF("*** Failed mounting fat\n"); } - dbg_console(&bpb); + dbg_console(); return 0; } -- cgit v1.2.3