removed - more testing
[yaffs/.git] / yaffsdev.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffsdev.c
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15  
16 #include "yaffsinterface.h"
17 #include "yaffs_nandemul.h" 
18 #include "yaffs_guts.h"
19 #include <stdlib.h>
20
21 #include <stdio.h>
22 #include <string.h>
23
24  
25
26
27
28 yaffs_Device device;
29
30
31
32
33
34
35
36 char *testStr = "this is a test string";
37
38 char *testStr2 = "abcdefghijklmnopqrstuvwxyz1234567890";
39
40 void TestTimexxx(yaffs_Device *dev)
41 {
42         yaffs_Object *f;
43         int x;
44         
45         
46         printf("Start\n");
47         
48
49         f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1");
50         if(f)
51         {
52                 printf("Found\n");
53         }
54         else
55         {
56                 f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0);
57                 printf("Created\n");
58         }
59         
60         
61         x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename");
62                 
63 }
64
65
66 void TestTimeasasas(yaffs_Device *dev)
67 {
68         yaffs_Object *f; 
69         int x;
70         int i;
71         int b;
72         char data[200];
73         int written;
74         
75         
76         printf("Start\n");
77         
78
79         f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1");
80         if(f)
81         {
82                 printf("Found\n");
83         }
84         else
85         {
86                 f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0);
87                 printf("Created\n");
88         }
89         
90         
91         x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename");
92         
93         
94         
95         for(i = 0; i < 10000; i+=20)
96         {
97         
98                 b++;
99                 if(b & 1)
100                         written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr));
101                 else
102                         written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2));
103         }
104         
105         
106         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
107         
108         printf("Flush\n");
109         
110         yaffs_FlushFile(f);
111
112         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
113         
114         yaffs_ReadDataFromFile(f,data,1000,50);
115         data[50] = 0;
116         
117         printf("Read data is \"%s\"\n",data);
118         
119         yaffs_DumpObject(f);
120
121         printf("Resize\n");     
122         yaffs_ResizeFile(f,2000);
123         
124         yaffs_DumpObject(f);
125
126         yaffs_FlushFile(f);
127         
128         
129                 
130
131 }
132
133 void TestTime(yaffs_Device *dev)
134 {
135         yaffs_Object *f;
136         yaffs_Object *sl;
137         yaffs_Object *lnf;
138         
139         yaffs_Object *hl1;
140         yaffs_Object *hl2;
141         yaffs_Object *hl3;
142         yaffs_Object *d, *df;
143         
144         int x;
145         int i;
146         int b;
147         char data[200];
148         
149         char * alias;
150         int written;
151         
152         
153         printf("Start\n");
154         
155
156         f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1");
157         if(f)
158         {
159                 printf("Found\n");
160         }
161         else
162         {
163                 f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0);
164                 printf("Created\n");
165         }
166         
167         
168         x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename");
169         
170         for(i = 0; i < 100000; i+=20)
171         {
172         
173                 b++;
174                 if(b & 1)
175                         written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr));
176                 else
177                         written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2));
178         }
179         
180         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
181         
182         printf("Flush\n");
183         
184         yaffs_FlushFile(f);
185         
186         printf("File length is %d\n",yaffs_GetObjectFileLength(f));
187         
188         sl = yaffs_MknodSymLink(yaffs_Root(dev),"sym-link",0,0,0,"/tmp/alias");
189
190
191         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
192         
193         printf("\n\nsymlink alias is \"%s\"\n",alias = yaffs_GetSymlinkAlias(sl));
194         
195         free(alias);
196         
197         printf("Unlink symlink %d\n",yaffs_Unlink(yaffs_Root(dev),"sym-link"));
198         
199         
200         yaffs_ReadDataFromFile(f,data,1000,50);
201         data[50] = 0;
202         
203         printf("Read data is \"%s\"\n",data);
204         
205         yaffs_DumpObject(f);
206
207         printf("Resize\n");     
208         yaffs_ResizeFile(f,2000);
209         
210         yaffs_DumpObject(f);
211         
212         lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME);
213         
214         
215
216         yaffs_FlushFile(f);
217                 
218
219         printf("Unlink file: %d\n",yaffs_Unlink(yaffs_Root(dev),"Rename"));
220         
221         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
222         
223         // Create a directory and play with it
224         
225
226         printf("Create directory and play with it\n");
227                 
228         d = yaffs_MknodDirectory(yaffs_Root(dev),"direct",0,0,0);
229         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
230         yaffs_ApplyToDirectoryChildren(d,yaffs_DumpObject);
231         
232         printf("Make file in directory\n");
233         
234         df = yaffs_MknodFile(d,"file-in-directory",0,0,0);
235         yaffs_ApplyToDirectoryChildren(d,yaffs_DumpObject);
236         
237         
238         // Do some stuff with hardlinks
239         //
240         // NB Deleting hardlinked objects can mess up pointers to hardlinks.
241         // The mechanism is as follows:
242         // * If you unlink a file,softlink or directory that has one or more hardlinks,
243         // then the object is renamed to one of the hardlinks and that hardlink is unlinked.
244         // This means that a pointer to a hardlink so deleted will point to an invalid address.
245         // Thus, make sure that pointers to hardlinks are immediately dereferenced.
246
247
248         printf("Hard link tests\n");
249                 
250         f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0);
251         hl1 = yaffs_Link(yaffs_Root(dev),"HardLink 1",f);
252         hl2 = yaffs_Link(yaffs_Root(dev),"HardLink 2",f);
253         hl3 = yaffs_Link(yaffs_Root(dev),"HardLink 3",hl2);
254
255         printf("\n\nHard links created\n");
256         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
257         
258         yaffs_Unlink(yaffs_Root(dev),"HardLink 1");
259         printf("\n\nHard link deleted\n");
260         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
261         
262         yaffs_Unlink(yaffs_Root(dev),"Name1");
263         printf("\n\nHard linked file deleted\n");
264         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
265         
266         yaffs_Unlink(yaffs_Root(dev),"HardLink 2");
267         printf("\n\nHard link 2 deleted\n");
268         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
269         
270         yaffs_Unlink(yaffs_Root(dev),"HardLink 3");
271
272         printf("\n\nHard link 3 deleted\n");
273         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
274         
275         // NB We don't allow unlinking or rename of the root or lost+found
276         // We allow setting attributes, but these must not be written to
277         // NAND since they are not real objects.
278         
279         printf("Attempt to rename lost+found - should have failed\n");
280         x = yaffs_RenameObject(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME,NULL,"Renamed");
281         yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject);
282
283 }
284
285 int main(int argc,char *argv[])
286 {
287         device.nBlocks = (2 * 1024 * 1024) / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
288         device.startBlock = 1;  // Don't use block 0
289         device.endBlock = device.nBlocks - 1;
290         
291         device.writeChunkToNAND = nandemul_WriteChunkToNAND;
292         device.readChunkFromNAND = nandemul_ReadChunkFromNAND;
293         device.eraseBlockInNAND = nandemul_EraseBlockInNAND;
294         device.initialiseNAND = nandemul_InitialiseNAND;
295         
296         yaffs_GutsInitialise(&device);
297         
298         // yaffs_GutsTest();
299         
300         TestTime(&device);
301         
302         exit(0);
303 }