*** 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
85 void fill_files(char *path,int flags, int maxIterations,int siz)
86 {
87         int i;
88         int j;
89         char str[50];
90         int h;
91         
92         i = 0;
93         
94         do{
95                 sprintf(str,"%s/%d",path,i);
96                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
97                 yaffs_close(h);
98
99                 if(h >= 0)
100                 {
101                         for(j = 0; j < siz; j++)
102                         {
103                                 yaffs_write(h,str,1);
104                         }
105                 }
106                 
107                 if( flags & 1)
108                 {
109                         yaffs_unlink(str);
110                 }
111                 i++;
112         } while(h >= 0 && i < maxIterations);
113         
114         if(flags & 2)
115         {
116                 i = 0;
117                 do{
118                         sprintf(str,"%s/%d",path,i);
119                         printf("unlink %s\n",str);
120                         i++;
121                 } while(yaffs_unlink(str) >= 0);
122         }
123 }
124
125 void leave_unlinked_file(char *path,int maxIterations,int siz)
126 {
127         int i;
128         char str[50];
129         int h;
130         
131         i = 0;
132         
133         do{
134                 sprintf(str,"%s/%d",path,i);
135                 printf("create %s\n",str);
136                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
137                 if(h >= 0)
138                 {
139                         yaffs_unlink(str);
140                 }
141                 i++;
142         } while(h < 0 && i < maxIterations);
143         
144         if(h >= 0)
145         {
146                 for(i = 0; i < siz; i++)
147                 {
148                         yaffs_write(h,str,1);
149                 }
150         }
151         
152         printf("Leaving file %s open\n",str);
153
154 }
155
156 void dumpDirFollow(const char *dname)
157 {
158         yaffs_DIR *d;
159         yaffs_dirent *de;
160         struct yaffs_stat s;
161         char str[100];
162                         
163         d = yaffs_opendir(dname);
164         
165         if(!d)
166         {
167                 printf("opendir failed\n");
168         }
169         else
170         {
171                 while((de = yaffs_readdir(d)) != NULL)
172                 {
173                         sprintf(str,"%s/%s",dname,de->d_name);
174                         
175                         yaffs_stat(str,&s);
176                         
177                         printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
178                         switch(s.st_mode & S_IFMT)
179                         {
180                                 case S_IFREG: printf("data file"); break;
181                                 case S_IFDIR: printf("directory"); break;
182                                 case S_IFLNK: printf("symlink -->");
183                                                           if(yaffs_readlink(str,str,100) < 0)
184                                                                 printf("no alias");
185                                                           else
186                                                                 printf("\"%s\"",str);    
187                                                           break;
188                                 default: printf("unknown"); break;
189                         }
190                         
191                         printf("\n");           
192                 }
193                 
194                 yaffs_closedir(d);
195         }
196         printf("\n");
197         
198         printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
199
200 }
201 void dumpDir(const char *dname)
202 {
203         yaffs_DIR *d;
204         yaffs_dirent *de;
205         struct yaffs_stat s;
206         char str[100];
207                         
208         d = yaffs_opendir(dname);
209         
210         if(!d)
211         {
212                 printf("opendir failed\n");
213         }
214         else
215         {
216                 while((de = yaffs_readdir(d)) != NULL)
217                 {
218                         sprintf(str,"%s/%s",dname,de->d_name);
219                         
220                         yaffs_lstat(str,&s);
221                         
222                         printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
223                         switch(s.st_mode & S_IFMT)
224                         {
225                                 case S_IFREG: printf("data file"); break;
226                                 case S_IFDIR: printf("directory"); break;
227                                 case S_IFLNK: printf("symlink -->");
228                                                           if(yaffs_readlink(str,str,100) < 0)
229                                                                 printf("no alias");
230                                                           else
231                                                                 printf("\"%s\"",str);    
232                                                           break;
233                                 default: printf("unknown"); break;
234                         }
235                         
236                         printf("\n");           
237                 }
238                 
239                 yaffs_closedir(d);
240         }
241         printf("\n");
242         
243         printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
244
245 }
246
247
248 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
249 {
250         int fd;
251         
252         if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
253         
254         fd = yaffs_open(path,tflags,0);
255         
256         if((fd >= 0) != (expectedResult > 0))
257         {
258                 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
259         }
260         else
261         {
262                 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
263         }
264         
265         
266         yaffs_close(fd);
267         
268         
269 }
270
271 int main(int argc, char *argv[])
272 {
273
274         int f;
275         int r;
276         char buffer[20];
277         
278         char str[100];
279         
280         int h;
281         mode_t temp_mode;
282         struct yaffs_stat ystat;
283         
284         yaffs_StartUp();
285         
286         yaffs_mount("/boot");
287         yaffs_mount("/data");
288         yaffs_mount("/flash");
289         yaffs_mount("/ram");
290         
291         printf("\nDirectory look-up of /boot\n");
292         dumpDir("/boot");
293         printf("\nDirectory look-up of /data\n");
294         dumpDir("/data");
295         printf("\nDirectory look-up of /flash\n");
296         dumpDir("/flash");
297
298         //leave_unlinked_file("/flash",20000,0);
299         //leave_unlinked_file("/data",20000,0);
300         
301         leave_unlinked_file("/ram",20,0);
302         
303
304         f = yaffs_open("/boot/b1", O_RDONLY,0);
305         
306         printf("open /boot/b1 readonly, f=%d\n",f);
307         
308         f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
309         
310         printf("open /boot/b1 O_CREAT, f=%d\n",f);
311         
312         
313         r = yaffs_write(f,"hello",1);
314         printf("write %d attempted to write to a read-only file\n",r);
315         
316         r = yaffs_close(f);
317         
318         printf("close %d\n",r);
319
320         f = yaffs_open("/boot/b1", O_RDWR,0);
321         
322         printf("open /boot/b1 O_RDWR,f=%d\n",f);
323         
324         
325         r = yaffs_write(f,"hello",2);
326         printf("write %d attempted to write to a writeable file\n",r);
327         r = yaffs_write(f,"world",3);
328         printf("write %d attempted to write to a writeable file\n",r);
329         
330         r= yaffs_lseek(f,0,SEEK_END);
331         printf("seek end %d\n",r);
332         memset(buffer,0,20);
333         r = yaffs_read(f,buffer,10);
334         printf("read %d \"%s\"\n",r,buffer);
335         r= yaffs_lseek(f,0,SEEK_SET);
336         printf("seek set %d\n",r);
337         memset(buffer,0,20);
338         r = yaffs_read(f,buffer,10);
339         printf("read %d \"%s\"\n",r,buffer);
340         memset(buffer,0,20);
341         r = yaffs_read(f,buffer,10);
342         printf("read %d \"%s\"\n",r,buffer);
343
344         // Check values reading at end.
345         // A read past end of file should return 0 for 0 bytes read.
346                 
347         r= yaffs_lseek(f,0,SEEK_END);
348         r = yaffs_read(f,buffer,10);
349         printf("read at end returned  %d\n",r); 
350         r= yaffs_lseek(f,500,SEEK_END);
351         r = yaffs_read(f,buffer,10);
352         printf("read past end returned  %d\n",r);       
353         
354         r = yaffs_close(f);
355         
356         printf("close %d\n",r);
357         
358         copy_in_a_file("/boot/yyfile","xxx");
359         
360         // Create a file with a long name
361         
362         copy_in_a_file("/boot/file with a long name","xxx");
363         
364         
365         printf("\nDirectory look-up of /boot\n");
366         dumpDir("/boot");
367
368         // Check stat
369         r = yaffs_stat("/boot/file with a long name",&ystat);
370         
371         // Check rename
372         
373         r = yaffs_rename("/boot/file with a long name","/boot/r1");
374         
375         printf("\nDirectory look-up of /boot\n");
376         dumpDir("/boot");
377         
378         // Check unlink
379         r = yaffs_unlink("/boot/r1");
380         
381         printf("\nDirectory look-up of /boot\n");
382         dumpDir("/boot");
383
384         // Check mkdir
385         
386         r = yaffs_mkdir("/boot/directory1",0);
387         
388         printf("\nDirectory look-up of /boot\n");
389         dumpDir("/boot");
390         printf("\nDirectory look-up of /boot/directory1\n");
391         dumpDir("/boot/directory1");
392
393         // add a file to the directory                  
394         copy_in_a_file("/boot/directory1/file with a long name","xxx");
395         
396         printf("\nDirectory look-up of /boot\n");
397         dumpDir("/boot");
398         printf("\nDirectory look-up of /boot/directory1\n");
399         dumpDir("/boot/directory1");
400         
401         //  Attempt to delete directory (should fail)
402         
403         r = yaffs_rmdir("/boot/directory1");
404         
405         printf("\nDirectory look-up of /boot\n");
406         dumpDir("/boot");
407         printf("\nDirectory look-up of /boot/directory1\n");
408         dumpDir("/boot/directory1");
409         
410         // Delete file first, then rmdir should work
411         r = yaffs_unlink("/boot/directory1/file with a long name");
412         r = yaffs_rmdir("/boot/directory1");
413         
414         
415         printf("\nDirectory look-up of /boot\n");
416         dumpDir("/boot");
417         printf("\nDirectory look-up of /boot/directory1\n");
418         dumpDir("/boot/directory1");
419
420 #if 0
421         fill_disk_and_delete("/boot",20,20);
422                         
423         printf("\nDirectory look-up of /boot\n");
424         dumpDir("/boot");
425 #endif
426
427         yaffs_symlink("yyfile","/boot/slink");
428         
429         yaffs_readlink("/boot/slink",str,100);
430         printf("symlink alias is %s\n",str);
431         
432         
433         
434         
435         printf("\nDirectory look-up of /boot\n");
436         dumpDir("/boot");
437         printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
438         dumpDirFollow("/boot");
439         printf("\nDirectory look-up of /boot/directory1\n");
440         dumpDir("/boot/directory1");
441
442         h = yaffs_open("/boot/slink",O_RDWR,0);
443         
444         printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
445         
446         yaffs_close(h);
447         
448         yaffs_unlink("/boot/slink");
449
450         
451         printf("\nDirectory look-up of /boot\n");
452         dumpDir("/boot");
453         
454         // Check chmod
455         
456         yaffs_stat("/boot/yyfile",&ystat);
457         temp_mode = ystat.st_mode;
458         
459         yaffs_chmod("/boot/yyfile",0x55555);
460         printf("\nDirectory look-up of /boot\n");
461         dumpDir("/boot");
462         
463         yaffs_chmod("/boot/yyfile",temp_mode);
464         printf("\nDirectory look-up of /boot\n");
465         dumpDir("/boot");
466         
467         // Permission checks...
468         PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
469         PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
470         PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
471
472         PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
473         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
474         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
475
476         PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
477         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
478         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
479         
480         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
481         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
482         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
483
484         yaffs_chmod("/boot/yyfile",temp_mode);
485         
486         //create a zero-length file and unlink it (test for scan bug)
487         
488         h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
489         yaffs_close(h);
490         
491         yaffs_unlink("/boot/zlf");
492         
493         
494         yaffs_DumpDevStruct("/boot");
495         
496         fill_disk_and_delete("/boot",20,20);
497         
498         yaffs_DumpDevStruct("/boot");
499         
500         fill_files("/boot",1,10000,0);
501         fill_files("/boot",1,10000,5000);
502         fill_files("/boot",2,10000,0);
503         fill_files("/boot",2,10000,5000);
504         
505         leave_unlinked_file("/data",20000,0);
506         leave_unlinked_file("/data",20000,5000);
507         leave_unlinked_file("/data",20000,5000);
508         leave_unlinked_file("/data",20000,5000);
509         leave_unlinked_file("/data",20000,5000);
510         leave_unlinked_file("/data",20000,5000);
511         
512         yaffs_DumpDevStruct("/boot");
513         yaffs_DumpDevStruct("/data");
514         
515                 
516                 
517         return 0;
518
519 }