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