summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-05-03 15:36:52 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-05-03 15:36:52 +0000
commit4d4ec3aa0b2c0bb45e7c004885accee303f1277f (patch)
treefd6432a02c7f2c0e9e0124c51f678ee5f4c1edb1
parent924164e6a7b4a97d248a07928b8b9f22bf5aec0b (diff)
downloadrockbox-4d4ec3aa0b2c0bb45e7c004885accee303f1277f.tar.gz
rockbox-4d4ec3aa0b2c0bb45e7c004885accee303f1277f.zip
Added opendir, closedir and readdir
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@412 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/common/dir.c97
-rw-r--r--firmware/common/dir.h9
-rw-r--r--firmware/test/fat/Makefile5
-rw-r--r--firmware/test/fat/main.c49
4 files changed, 133 insertions, 27 deletions
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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Björn Stenberg
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include <stdio.h>
20#include <string.h>
21#include "fat.h"
22#include "dir.h"
23#include "debug.h"
24#include "types.h"
25
26static DIR thedir;
27static struct dirent theent;
28static bool busy=FALSE;
29
30DIR* opendir(char* name)
31{
32 char* part;
33 struct fat_direntry entry;
34 struct fat_dir* dir = &(thedir.fatdir);
35
36 if ( busy ) {
37 DEBUGF("Only one open dir at a time\n");
38 return NULL;
39 }
40
41 if ( name[0] != '/' ) {
42 DEBUGF("Only absolute paths supported right now\n");
43 return NULL;
44 }
45
46 if ( fat_opendir(dir, 0) < 0 ) {
47 DEBUGF("Failed opening root dir\n");
48 return NULL;
49 }
50
51 while ( (part = strtok(name, "/")) ) {
52 int partlen = strlen(part);
53 /* scan dir for name */
54 while (1) {
55 if (fat_getnext(dir,&entry) < 0)
56 return NULL;
57 if ( !entry.name[0] )
58 return NULL;
59 if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
60 (!strncmp(part, entry.name, partlen)) ) {
61 if ( fat_opendir(dir, entry.firstcluster) < 0 ) {
62 DEBUGF("Failed opening dir '%s' (%d)\n",
63 part, entry.firstcluster);
64 return NULL;
65 }
66 break;
67 }
68 }
69 }
70
71 busy = TRUE;
72
73 return &thedir;
74}
75
76int closedir(DIR* dir)
77{
78 busy=FALSE;
79 return 0;
80}
81
82struct dirent* readdir(DIR* dir)
83{
84 struct fat_direntry entry;
85
86 if (fat_getnext(&(dir->fatdir),&entry) < 0)
87 return NULL;
88
89 if ( !entry.name[0] )
90 return NULL;
91
92 strncpy(theent.d_name, entry.name, sizeof( theent.d_name ) );
93 theent.attribute = entry.attr;
94 theent.size = entry.filesize;
95
96 return &theent;
97}
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 @@
16 * KIND, either express or implied. 16 * KIND, either express or implied.
17 * 17 *
18 ****************************************************************************/ 18 ****************************************************************************/
19
20#ifndef _DIR_H_ 19#ifndef _DIR_H_
21#define _DIR_H_ 20#define _DIR_H_
22 21
@@ -31,10 +30,16 @@ struct dirent {
31 30
32 31
33#ifndef SIMULATOR 32#ifndef SIMULATOR
33
34#include "fat.h"
35
34typedef struct { 36typedef struct {
35 int offset; 37 int startcluster;
38 struct fat_dir fatdir;
36} DIR; 39} DIR;
40
37#else // SIMULATOR 41#else // SIMULATOR
42
38#ifdef WIN32 43#ifdef WIN32
39#include <io.h> 44#include <io.h>
40typedef struct DIRtag 45typedef 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.
5 5
6TARGET = fat 6TARGET = fat
7 7
8$(TARGET): fat.o ata-sim.o debug.o main.o disk.o 8$(TARGET): fat.o ata-sim.o main.o disk.o debug.o dir.o
9 gcc -g -o fat $+ -lfl 9 gcc -g -o fat $+ -lfl
10 10
11fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h 11fat.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
14disk.o: $(FIRMWARE)/common/disk.c 14disk.o: $(FIRMWARE)/common/disk.c
15 $(CC) $(CFLAGS) -c $< -o $@ 15 $(CC) $(CFLAGS) -c $< -o $@
16 16
17dir.o: $(FIRMWARE)/common/dir.c
18 $(CC) $(CFLAGS) -c $< -o $@
19
17debug.o: $(FIRMWARE)/debug.c 20debug.o: $(FIRMWARE)/debug.c
18 $(CC) $(CFLAGS) -c $< -o $@ 21 $(CC) $(CFLAGS) -c $< -o $@
19 22
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 @@
5#include "ata.h" 5#include "ata.h"
6#include "debug.h" 6#include "debug.h"
7#include "disk.h" 7#include "disk.h"
8#include "dir.h"
8 9
9void dbg_dump_sector(int sec); 10void dbg_dump_sector(int sec);
10void dbg_dump_buffer(unsigned char *buf); 11void dbg_dump_buffer(unsigned char *buf);
11void dbg_print_bpb(struct bpb *bpb); 12void dbg_print_bpb(struct bpb *bpb);
12void dbg_console(struct bpb *bpb); 13void dbg_console(void);
13 14
14void dbg_dump_sector(int sec) 15void dbg_dump_sector(int sec)
15{ 16{
@@ -79,34 +80,38 @@ void dbg_print_bpb(struct bpb *bpb)
79 printf("fat_type = FAT32\n"); 80 printf("fat_type = FAT32\n");
80} 81}
81 82
82void dbg_dir(struct bpb *bpb, int currdir) 83void dbg_dir(char* currdir)
83{ 84{
84 struct fat_dirent dent; 85 DIR* dir;
85 struct fat_direntry de; 86 struct dirent* entry;
86 87
87 if(fat_opendir(bpb, &dent, currdir) >= 0) 88 dir = opendir(currdir);
89 if (dir)
88 { 90 {
89 while(fat_getnext(bpb, &dent, &de) >= 0) 91 for ( entry = readdir(dir);
92 entry;
93 entry = readdir(dir) )
90 { 94 {
91 printf("%s (%d)\n", de.name,de.firstcluster); 95 printf("%s (%08x)\n", entry->d_name, entry->size);
92 } 96 }
93 } 97 }
94 else 98 else
95 { 99 {
96 fprintf(stderr, "Could not read dir on cluster %d\n", currdir); 100 fprintf(stderr, "Could not open dir %s\n", currdir);
97 } 101 }
102 closedir(dir);
98} 103}
99 104
100void dbg_type(struct bpb *bpb, int cluster) 105void dbg_type(int cluster)
101{ 106{
102 unsigned char buf[SECTOR_SIZE*5]; 107 unsigned char buf[SECTOR_SIZE*5];
103 struct fat_fileent ent; 108 struct fat_file ent;
104 int i; 109 int i;
105 110
106 fat_open(bpb,cluster,&ent); 111 fat_open(cluster,&ent);
107 112
108 for (i=0;i<5;i++) 113 for (i=0;i<5;i++)
109 if(fat_read(bpb, &ent, 1, buf) >= 0) 114 if(fat_read(&ent, 1, buf) >= 0)
110 { 115 {
111 buf[SECTOR_SIZE]=0; 116 buf[SECTOR_SIZE]=0;
112 printf("%s\n", buf); 117 printf("%s\n", buf);
@@ -125,7 +130,7 @@ void dbg_prompt(void)
125 printf("C:%s> ", current_directory); 130 printf("C:%s> ", current_directory);
126} 131}
127 132
128void dbg_console(struct bpb* bpb) 133void dbg_console(void)
129{ 134{
130 char cmd[32] = ""; 135 char cmd[32] = "";
131 char last_cmd[32] = ""; 136 char last_cmd[32] = "";
@@ -148,12 +153,10 @@ void dbg_console(struct bpb* bpb)
148 { 153 {
149 if(!strcasecmp(s, "dir")) 154 if(!strcasecmp(s, "dir"))
150 { 155 {
151 int secnum = 0; 156 s = strtok(NULL, " \n");
152 if((s = strtok(NULL, " \n"))) 157 if (!s)
153 { 158 s = "/";
154 secnum = atoi(s); 159 dbg_dir(s);
155 }
156 dbg_dir(bpb, secnum);
157 continue; 160 continue;
158 } 161 }
159 162
@@ -182,7 +185,7 @@ void dbg_console(struct bpb* bpb)
182 { 185 {
183 cluster = atoi(s); 186 cluster = atoi(s);
184 } 187 }
185 dbg_type(bpb,cluster); 188 dbg_type(cluster);
186 continue; 189 continue;
187 } 190 }
188 191
@@ -198,8 +201,6 @@ void dbg_console(struct bpb* bpb)
198 201
199int main(int argc, char *argv[]) 202int main(int argc, char *argv[])
200{ 203{
201 struct bpb bpb;
202
203 if(ata_init()) { 204 if(ata_init()) {
204 DEBUGF("*** Warning! The disk is uninitialized\n"); 205 DEBUGF("*** Warning! The disk is uninitialized\n");
205 return -1; 206 return -1;
@@ -209,11 +210,11 @@ int main(int argc, char *argv[])
209 return -1; 210 return -1;
210 } 211 }
211 212
212 if(fat_mount(&bpb,part[0].start)) { 213 if(fat_mount(part[0].start)) {
213 DEBUGF("*** Failed mounting fat\n"); 214 DEBUGF("*** Failed mounting fat\n");
214 } 215 }
215 216
216 dbg_console(&bpb); 217 dbg_console();
217 return 0; 218 return 0;
218} 219}
219 220