7f6963823aebe1aa1540fe8b3a809fa0d58e682f
[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         
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         f = yaffs_open("/boot/b1", O_RDONLY,0);
301         
302         printf("open /boot/b1 readonly, f=%d\n",f);
303         
304         f = yaffs_open("/boot/b1", O_CREAT,0);
305         
306         printf("open /boot/b1 O_CREAT, f=%d\n",f);
307         
308         
309         r = yaffs_write(f,"hello",1);
310         printf("write %d attempted to write to a read-only file\n",r);
311         
312         r = yaffs_close(f);
313         
314         printf("close %d\n",r);
315
316         f = yaffs_open("/boot/b1", O_RDWR,0);
317         
318         printf("open /boot/b1 O_RDWR,f=%d\n",f);
319         
320         
321         r = yaffs_write(f,"hello",2);
322         printf("write %d attempted to write to a writeable file\n",r);
323         r = yaffs_write(f,"world",3);
324         printf("write %d attempted to write to a writeable file\n",r);
325         
326         r= yaffs_lseek(f,SEEK_END,0);
327         printf("seek end %d\n",r);
328         memset(buffer,0,20);
329         r = yaffs_read(f,buffer,10);
330         printf("read %d \"%s\"\n",r,buffer);
331         r= yaffs_lseek(f,SEEK_SET,0);
332         printf("seek set %d\n",r);
333         memset(buffer,0,20);
334         r = yaffs_read(f,buffer,10);
335         printf("read %d \"%s\"\n",r,buffer);
336         memset(buffer,0,20);
337         r = yaffs_read(f,buffer,10);
338         printf("read %d \"%s\"\n",r,buffer);
339         
340         
341         r = yaffs_close(f);
342         
343         printf("close %d\n",r);
344         
345         copy_in_a_file("/boot/yyfile","xxx");
346         
347         // Create a file with a long name
348         
349         copy_in_a_file("/boot/file with a long name","xxx");
350         
351         
352         printf("\nDirectory look-up of /boot\n");
353         dumpDir("/boot");
354
355         // Check stat
356         r = yaffs_stat("/boot/file with a long name",&ystat);
357         
358         // Check rename
359         
360         r = yaffs_rename("/boot/file with a long name","/boot/r1");
361         
362         printf("\nDirectory look-up of /boot\n");
363         dumpDir("/boot");
364         
365         // Check unlink
366         r = yaffs_unlink("/boot/r1");
367         
368         printf("\nDirectory look-up of /boot\n");
369         dumpDir("/boot");
370
371         // Check mkdir
372         
373         r = yaffs_mkdir("/boot/directory1",0);
374         
375         printf("\nDirectory look-up of /boot\n");
376         dumpDir("/boot");
377         printf("\nDirectory look-up of /boot/directory1\n");
378         dumpDir("/boot/directory1");
379
380         // add a file to the directory                  
381         copy_in_a_file("/boot/directory1/file with a long name","xxx");
382         
383         printf("\nDirectory look-up of /boot\n");
384         dumpDir("/boot");
385         printf("\nDirectory look-up of /boot/directory1\n");
386         dumpDir("/boot/directory1");
387         
388         //  Attempt to delete directory (should fail)
389         
390         r = yaffs_rmdir("/boot/directory1");
391         
392         printf("\nDirectory look-up of /boot\n");
393         dumpDir("/boot");
394         printf("\nDirectory look-up of /boot/directory1\n");
395         dumpDir("/boot/directory1");
396         
397         // Delete file first, then rmdir should work
398         r = yaffs_unlink("/boot/directory1/file with a long name");
399         r = yaffs_rmdir("/boot/directory1");
400         
401         
402         printf("\nDirectory look-up of /boot\n");
403         dumpDir("/boot");
404         printf("\nDirectory look-up of /boot/directory1\n");
405         dumpDir("/boot/directory1");
406
407 #if 0
408         fill_disk_and_delete("/boot",20,20);
409                         
410         printf("\nDirectory look-up of /boot\n");
411         dumpDir("/boot");
412 #endif
413
414         yaffs_symlink("yyfile","/boot/slink");
415         
416         yaffs_readlink("/boot/slink",str,100);
417         printf("symlink alias is %s\n",str);
418         
419         
420         
421         
422         printf("\nDirectory look-up of /boot\n");
423         dumpDir("/boot");
424         printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
425         dumpDirFollow("/boot");
426         printf("\nDirectory look-up of /boot/directory1\n");
427         dumpDir("/boot/directory1");
428
429         h = yaffs_open("/boot/slink",O_RDWR,0);
430         
431         printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
432         
433         yaffs_close(h);
434         
435         yaffs_unlink("/boot/slink");
436
437         
438         printf("\nDirectory look-up of /boot\n");
439         dumpDir("/boot");
440         
441         // Check chmod
442         
443         yaffs_stat("/boot/yyfile",&ystat);
444         temp_mode = ystat.st_mode;
445         
446         yaffs_chmod("/boot/yyfile",0x55555);
447         printf("\nDirectory look-up of /boot\n");
448         dumpDir("/boot");
449         
450         yaffs_chmod("/boot/yyfile",temp_mode);
451         printf("\nDirectory look-up of /boot\n");
452         dumpDir("/boot");
453         
454         // Permission checks...
455         PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
456         PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
457         PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
458
459         PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
460         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
461         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
462
463         PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
464         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
465         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
466         
467         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
468         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
469         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
470
471         yaffs_chmod("/boot/yyfile",temp_mode);
472         
473         //create a zero-length file and unlink it (test for scan bug)
474         
475         h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
476         yaffs_close(h);
477         
478         yaffs_unlink("/boot/zlf");
479         
480         
481         yaffs_DumpDevStruct("/boot");
482         
483         fill_disk_and_delete("/boot",20,20);
484         
485         yaffs_DumpDevStruct("/boot");
486         
487         fill_files("/boot",1,10000,0);
488         fill_files("/boot",1,10000,5000);
489         fill_files("/boot",2,10000,0);
490         fill_files("/boot",2,10000,5000);
491         
492         leave_unlinked_file("/data",20000,0);
493         leave_unlinked_file("/data",20000,5000);
494         leave_unlinked_file("/data",20000,5000);
495         leave_unlinked_file("/data",20000,5000);
496         leave_unlinked_file("/data",20000,5000);
497         leave_unlinked_file("/data",20000,5000);
498         
499         yaffs_DumpDevStruct("/boot");
500         yaffs_DumpDevStruct("/data");
501         
502                 
503                 
504         return 0;
505
506 }