1 /*---------------------------------------------------------------------------*
2 Project: Sample demo program for NAND library
3 File: directory.c
4 Programmer: HIRATSU Daisuke
5
6 Copyright (C) 2006 Nintendo. All rights reserved.
7
8 These coded instructions, statements, and computer programs contain
9 proprietary information of Nintendo of America Inc. and/or Nintendo
10 Company Ltd., and are protected by Federal copyright law. They may
11 not be disclosed to third parties or copied or duplicated in any form,
12 in whole or in part, without the prior written consent of Nintendo.
13
14 $Log: directory.c,v $
15 Revision 1.9 07/19/2006 13:02:56 hiratsu
16 Now, this demo can distinguish between file and directory.
17
18 Revision 1.8 06/08/2006 11:12:05 hiratsu
19 Buffer allocation for NANDReadDir was wrong. Fixed.
20 Now printDir() prints extra information, such as permission.
21
22 Revision 1.7 06/05/2006 13:10:30 hiratsu
23 Removed NANDFinalize().
24 Now this program outputs current (home) directory.
25
26 Revision 1.6 05/03/2006 09:06:21 hiratsu
27 Revised flow.
28
29 Revision 1.5 2006/04/27 17:57:35 orest
30 Added NANDFinalize() api.
31
32 Revision 1.4 03/17/2006 04:39:05 hiratsu
33 Adapted the source to real file system module.
34
35 Revision 1.3 03/06/2006 12:05:34 hiratsu
36 Removed finalize function.
37
38 Revision 1.2 2006/03/06 09:59:08 kawaset
39 Eliminated warnings.
40
41 Revision 1.1 02/16/2006 13:01:08 hiratsu
42 Initial check in.
43 *---------------------------------------------------------------------------*/
44
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <revolution.h>
50 #include <revolution/nand.h>
51
52 #include "util.h"
53
54 #define PERM (NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE) // Owner can read/write
55 #define ATTR 0x00 // No attributes.
56
57 static void printDir (const char *path);
58 static void createDir (const char *path);
59 static void deleteDir (const char *path);
60 static void changeDir (const char *path);
61 static void printCurrentDir(void);
62 static void createFile(const char *path, u8 perm);
63
64
printDir(const char * path)65 static void printDir(const char *path)
66 {
67 u32 num = 0;
68 u32 idx = 0;
69 u32 i = 0;
70 char *buf = 0;
71
72 NANDReadDir(path, NULL, &num); // Get number of elements.
73 buf = (char*)alloc32(num*(NAND_MAX_NAME+1));
74 if(!buf)
75 {
76 OSReport("Failed to alloc memory. Cannot print directory.\n");
77 return;
78 }
79
80 NANDReadDir(path, buf, &num); // Get directory list.
81 for(i=0; i<num; ++i)
82 {
83 char name[NAND_MAX_NAME+1]="";
84 char absPath[NAND_MAX_PATH]="";
85 int n = 0;
86 NANDStatus stat;
87 u8 type = 0x00;
88 while(buf[idx] != '\0')
89 {
90 name[n] = buf[idx];
91 ++idx;
92 ++n;
93 }
94 name[n]='\0';
95 if(strcmp(path, "/")==0)
96 {
97 sprintf(absPath, "/%s", name);
98 }
99 else
100 {
101 sprintf(absPath, "%s/%s", path, name);
102 }
103 NANDGetType(absPath, &type);
104 if(type==NAND_TYPE_FILE)
105 {
106 OSReport("-");
107 }
108 else if(type==NAND_TYPE_DIR)
109 {
110 OSReport("d");
111 }
112 else
113 {
114 OSReport("?");
115 }
116 NANDGetStatus(absPath, &stat);
117 stat.permission & NAND_PERM_OWNER_READ ? OSReport("r") : OSReport("-");
118 stat.permission & NAND_PERM_OWNER_WRITE ? OSReport("w") : OSReport("-");
119 stat.permission & NAND_PERM_GROUP_READ ? OSReport("r") : OSReport("-");
120 stat.permission & NAND_PERM_GROUP_WRITE ? OSReport("w") : OSReport("-");
121 stat.permission & NAND_PERM_OTHER_READ ? OSReport("r") : OSReport("-");
122 stat.permission & NAND_PERM_OTHER_WRITE ? OSReport("w") : OSReport("-");
123 OSReport(" %02x %08x %04x %s\n", stat.attribute, stat.ownerId, stat.groupId, name);
124 ++idx;
125 }
126
127 free32(buf);
128 }
129
130
createDir(const char * path)131 static void createDir(const char *path)
132 {
133 s32 result = NANDCreateDir(path, PERM, ATTR);
134 if(result != NAND_RESULT_OK)
135 {
136 printErrMsg(result);
137 OSHalt("NANDCreateDir() failed.");
138 }
139 }
140
141
deleteDir(const char * path)142 static void deleteDir(const char *path)
143 {
144 s32 result = NANDDelete(path);
145 if(result != NAND_RESULT_OK)
146 {
147 printErrMsg(result);
148 OSHalt("NANDDelete() failed.");
149 }
150 }
151
152
changeDir(const char * path)153 static void changeDir(const char *path)
154 {
155 s32 result = NANDChangeDir(path);
156 if(result != NAND_RESULT_OK)
157 {
158 printErrMsg(result);
159 OSHalt("NANDChangeDir() failed.");
160 }
161 }
162
163
printCurrentDir(void)164 static void printCurrentDir(void)
165 {
166 char path[NAND_MAX_PATH]="";
167 s32 result = NANDGetCurrentDir(path);
168 if(result == NAND_RESULT_OK)
169 {
170 OSReport("%s\n", path);
171 }
172 else
173 {
174 printErrMsg(result);
175 OSHalt("NANDGetCurrentDir() failed.\n");
176 }
177 }
178
179
createFile(const char * path,const u8 perm)180 static void createFile(const char *path, const u8 perm)
181 {
182 s32 result = NANDCreate(path, perm, ATTR);
183 if(result != NAND_RESULT_OK)
184 {
185 printErrMsg(result);
186 OSHalt("NANDCreate() failed.");
187 }
188 }
189
190
main(void)191 int main(void)
192 {
193 s32 ret = NAND_RESULT_FATAL_ERROR;
194
195 initializeHeap();
196
197 ret = NANDInit();
198 if(ret != NAND_RESULT_OK)
199 {
200 printErrMsg(ret);
201 OSReport("NANDInit() failed.\n");
202 return EXIT_FAILURE;
203 }
204
205 OSReport("--- Current directory ---\n");
206 printCurrentDir();
207 OSReport("\n");
208
209 OSReport("--- Under / directory ---\n");
210 printDir("/");
211 OSReport("\n");
212
213 changeDir("/tmp");
214 createDir("dir");
215 OSReport("--- Under /tmp directory ---\n");
216 printDir("/tmp");
217 OSReport("\n");
218
219 changeDir("/tmp/dir");
220 createDir("subdir0");
221 createDir("subdir1");
222 createDir("subdir2");
223 createFile("file0", NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE);
224 createFile("file1", NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE | NAND_PERM_GROUP_READ);
225 createFile("file2", NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE | NAND_PERM_GROUP_READ | NAND_PERM_OTHER_READ);
226 OSReport("--- Under /tmp/dir directory ---\n");
227 printDir(".");
228 OSReport("\n");
229
230 changeDir("/tmp");
231 deleteDir("dir"); // Please note that subdirectories and files under specified directory will be deleted at once.
232
233 OSReport("--- Under /tmp directory ---\n");
234 printDir(".");
235 OSReport("\n");
236
237 finalizeHeap();
238
239 OSHalt("sample demo program for NAND library end.\n");
240 return EXIT_SUCCESS; // Never reach here
241 }
242