*** empty log message ***
[yaffs/.git] / direct / dtest.c
1 /*
2 * Test code for the "direct" interface. 
3 */
4
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "yaffsfs.h"
10
11 char xx[600];
12
13 void copy_in_a_file(char *yaffsName,char *inName)
14 {
15         int inh,outh;
16         unsigned char buffer[100];
17         int ni,no;
18         inh = open(inName,O_RDONLY);
19         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
20         
21         while((ni = read(inh,buffer,100)) > 0)
22         {
23                 no = yaffs_write(outh,buffer,ni);
24                 if(ni != no)
25                 {
26                         printf("problem writing yaffs file\n");
27                 }
28                 
29         }
30         
31         yaffs_close(outh);
32         close(inh);
33 }
34
35
36
37
38
39 void fill_disk(char *path,int nfiles)
40 {
41         int h;
42         int n;
43         int result;
44         
45         char str[50];
46         
47         for(n = 0; n < nfiles; n++)
48         {
49                 sprintf(str,"%s/%d",path,n);
50                 
51                 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
52                 
53                 printf("writing file %s handle %d ",str, h);
54                 
55                 while ((result = yaffs_write(h,xx,600)) == 600)
56                 {
57                         //printf(".");
58                 }
59                 result = yaffs_close(h);
60                 printf(" close %d\n",result);
61         }
62 }
63
64 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
65 {
66         int i,j;
67         char str[50];
68         int result;
69         
70         for(i = 0; i < ncycles; i++)
71         {
72                 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
73                 fill_disk(path,nfiles);
74                 
75                 for(j = 0; j < nfiles; j++)
76                 {
77                         sprintf(str,"%s/%d",path,j);
78                         result = yaffs_unlink(str);
79                         printf("unlinking file %s, result %d\n",str,result);
80                 }
81         }
82 }
83
84 void dumpDirFollow(const char *dname)
85 {
86         yaffs_DIR *d;
87         yaffs_dirent *de;
88         struct yaffs_stat s;
89         char str[100];
90                         
91         d = yaffs_opendir(dname);
92         
93         if(!d)
94         {
95                 printf("opendir failed\n");
96         }
97         else
98         {
99                 while((de = yaffs_readdir(d)) != NULL)
100                 {
101                         sprintf(str,"%s/%s",dname,de->d_name);
102                         
103                         yaffs_stat(str,&s);
104                         
105                         printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
106                         switch(s.st_mode & S_IFMT)
107                         {
108                                 case S_IFREG: printf("data file"); break;
109                                 case S_IFDIR: printf("directory"); break;
110                                 case S_IFLNK: printf("symlink -->");
111                                                           if(yaffs_readlink(str,str,100) < 0)
112                                                                 printf("no alias");
113                                                           else
114                                                                 printf("\"%s\"",str);    
115                                                           break;
116                                 default: printf("unknown"); break;
117                         }
118                         
119                         printf("\n");           
120                 }
121                 
122                 yaffs_closedir(d);
123         }
124         printf("\n");
125         
126         printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
127
128 }
129 void dumpDir(const char *dname)
130 {
131         yaffs_DIR *d;
132         yaffs_dirent *de;
133         struct yaffs_stat s;
134         char str[100];
135                         
136         d = yaffs_opendir(dname);
137         
138         if(!d)
139         {
140                 printf("opendir failed\n");
141         }
142         else
143         {
144                 while((de = yaffs_readdir(d)) != NULL)
145                 {
146                         sprintf(str,"%s/%s",dname,de->d_name);
147                         
148                         yaffs_lstat(str,&s);
149                         
150                         printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
151                         switch(s.st_mode & S_IFMT)
152                         {
153                                 case S_IFREG: printf("data file"); break;
154                                 case S_IFDIR: printf("directory"); break;
155                                 case S_IFLNK: printf("symlink -->");
156                                                           if(yaffs_readlink(str,str,100) < 0)
157                                                                 printf("no alias");
158                                                           else
159                                                                 printf("\"%s\"",str);    
160                                                           break;
161                                 default: printf("unknown"); break;
162                         }
163                         
164                         printf("\n");           
165                 }
166                 
167                 yaffs_closedir(d);
168         }
169         printf("\n");
170         
171         printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
172
173 }
174
175
176 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
177 {
178         int fd;
179         
180         if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
181         
182         fd = yaffs_open(path,tflags,0);
183         
184         if((fd >= 0) != (expectedResult > 0))
185         {
186                 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
187         }
188         else
189         {
190                 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
191         }
192         
193         
194         yaffs_close(fd);
195         
196         
197 }
198
199 int main(int argc, char *argv[])
200 {
201
202         int f;
203         int r;
204         char buffer[20];
205         
206         char str[100];
207         
208         int h;
209         mode_t temp_mode;
210         struct yaffs_stat ystat;
211         
212         yaffs_StartUp();
213         
214         yaffs_mount("/boot");
215         
216         printf("\nDirectory look-up of /boot\n");
217         dumpDir("/boot");
218
219         f = yaffs_open("/boot/b1", O_RDONLY,0);
220         
221         printf("open /boot/b1 readonly, f=%d\n",f);
222         
223         f = yaffs_open("/boot/b1", O_CREAT,0);
224         
225         printf("open /boot/b1 O_CREAT, f=%d\n",f);
226         
227         
228         r = yaffs_write(f,"hello",1);
229         printf("write %d attempted to write to a read-only file\n",r);
230         
231         r = yaffs_close(f);
232         
233         printf("close %d\n",r);
234
235         f = yaffs_open("/boot/b1", O_RDWR,0);
236         
237         printf("open /boot/b1 O_RDWR,f=%d\n",f);
238         
239         
240         r = yaffs_write(f,"hello",2);
241         printf("write %d attempted to write to a writeable file\n",r);
242         r = yaffs_write(f,"world",3);
243         printf("write %d attempted to write to a writeable file\n",r);
244         
245         r= yaffs_lseek(f,SEEK_END,0);
246         printf("seek end %d\n",r);
247         memset(buffer,0,20);
248         r = yaffs_read(f,buffer,10);
249         printf("read %d \"%s\"\n",r,buffer);
250         r= yaffs_lseek(f,SEEK_SET,0);
251         printf("seek set %d\n",r);
252         memset(buffer,0,20);
253         r = yaffs_read(f,buffer,10);
254         printf("read %d \"%s\"\n",r,buffer);
255         memset(buffer,0,20);
256         r = yaffs_read(f,buffer,10);
257         printf("read %d \"%s\"\n",r,buffer);
258         
259         
260         r = yaffs_close(f);
261         
262         printf("close %d\n",r);
263         
264         copy_in_a_file("/boot/yyfile","xxx");
265         
266         // Create a file with a long name
267         
268         copy_in_a_file("/boot/file with a long name","xxx");
269         
270         
271         printf("\nDirectory look-up of /boot\n");
272         dumpDir("/boot");
273
274         // Check stat
275         r = yaffs_stat("/boot/file with a long name",&ystat);
276         
277         // Check rename
278         
279         r = yaffs_rename("/boot/file with a long name","/boot/r1");
280         
281         printf("\nDirectory look-up of /boot\n");
282         dumpDir("/boot");
283         
284         // Check unlink
285         r = yaffs_unlink("/boot/r1");
286         
287         printf("\nDirectory look-up of /boot\n");
288         dumpDir("/boot");
289
290         // Check mkdir
291         
292         r = yaffs_mkdir("/boot/directory1",0);
293         
294         printf("\nDirectory look-up of /boot\n");
295         dumpDir("/boot");
296         printf("\nDirectory look-up of /boot/directory1\n");
297         dumpDir("/boot/directory1");
298
299         // add a file to the directory                  
300         copy_in_a_file("/boot/directory1/file with a long name","xxx");
301         
302         printf("\nDirectory look-up of /boot\n");
303         dumpDir("/boot");
304         printf("\nDirectory look-up of /boot/directory1\n");
305         dumpDir("/boot/directory1");
306         
307         //  Attempt to delete directory (should fail)
308         
309         r = yaffs_rmdir("/boot/directory1");
310         
311         printf("\nDirectory look-up of /boot\n");
312         dumpDir("/boot");
313         printf("\nDirectory look-up of /boot/directory1\n");
314         dumpDir("/boot/directory1");
315         
316         // Delete file first, then rmdir should work
317         r = yaffs_unlink("/boot/directory1/file with a long name");
318         r = yaffs_rmdir("/boot/directory1");
319         
320         
321         printf("\nDirectory look-up of /boot\n");
322         dumpDir("/boot");
323         printf("\nDirectory look-up of /boot/directory1\n");
324         dumpDir("/boot/directory1");
325
326 #if 0
327         fill_disk_and_delete("/boot",20,20);
328                         
329         printf("\nDirectory look-up of /boot\n");
330         dumpDir("/boot");
331 #endif
332
333         yaffs_symlink("yyfile","/boot/slink");
334         
335         yaffs_readlink("/boot/slink",str,100);
336         printf("symlink alias is %s\n",str);
337         
338         
339         
340         
341         printf("\nDirectory look-up of /boot\n");
342         dumpDir("/boot");
343         printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
344         dumpDirFollow("/boot");
345         printf("\nDirectory look-up of /boot/directory1\n");
346         dumpDir("/boot/directory1");
347
348         h = yaffs_open("/boot/slink",O_RDWR,0);
349         
350         printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
351         
352         yaffs_close(h);
353         
354         yaffs_unlink("/boot/slink");
355
356         
357         printf("\nDirectory look-up of /boot\n");
358         dumpDir("/boot");
359         
360         // Check chmod
361         
362         yaffs_stat("/boot/yyfile",&ystat);
363         temp_mode = ystat.st_mode;
364         
365         yaffs_chmod("/boot/yyfile",0x55555);
366         printf("\nDirectory look-up of /boot\n");
367         dumpDir("/boot");
368         
369         yaffs_chmod("/boot/yyfile",temp_mode);
370         printf("\nDirectory look-up of /boot\n");
371         dumpDir("/boot");
372         
373         // Permission checks...
374         PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
375         PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
376         PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
377
378         PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
379         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
380         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
381
382         PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
383         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
384         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
385         
386         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
387         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
388         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
389
390         yaffs_chmod("/boot/yyfile",temp_mode);
391         
392         //create a zero-length file and unlink it (test for scan bug)
393         
394         h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
395         yaffs_close(h);
396         
397         yaffs_unlink("/boot/zlf");
398         
399         
400         yaffs_DumpDevStruct("/boot");
401         
402         fill_disk_and_delete("/boot",20,20);
403         
404         yaffs_DumpDevStruct("/boot");
405                 
406                 
407         return 0;
408
409 }