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