2 * Test code for the "direct" interface.
13 void copy_in_a_file(char *yaffsName,char *inName)
16 unsigned char buffer[100];
18 inh = open(inName,O_RDONLY);
19 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
21 while((ni = read(inh,buffer,100)) > 0)
23 no = yaffs_write(outh,buffer,ni);
26 printf("problem writing yaffs file\n");
39 void fill_disk(char *path,int nfiles)
47 for(n = 0; n < nfiles; n++)
49 sprintf(str,"%s/%d",path,n);
51 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
53 printf("writing file %s handle %d ",str, h);
55 while ((result = yaffs_write(h,xx,600)) == 600)
59 result = yaffs_close(h);
60 printf(" close %d\n",result);
64 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
70 for(i = 0; i < ncycles; i++)
72 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
73 fill_disk(path,nfiles);
75 for(j = 0; j < nfiles; j++)
77 sprintf(str,"%s/%d",path,j);
78 result = yaffs_unlink(str);
79 printf("unlinking file %s, result %d\n",str,result);
85 void fill_files(char *path,int flags, int maxIterations,int siz)
95 sprintf(str,"%s/%d",path,i);
96 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
101 for(j = 0; j < siz; j++)
103 yaffs_write(h,str,1);
112 } while(h >= 0 && i < maxIterations);
118 sprintf(str,"%s/%d",path,i);
119 printf("unlink %s\n",str);
121 } while(yaffs_unlink(str) >= 0);
125 void leave_unlinked_file(char *path,int maxIterations,int siz)
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);
142 } while(h < 0 && i < maxIterations);
146 for(i = 0; i < siz; i++)
148 yaffs_write(h,str,1);
152 printf("Leaving file %s open\n",str);
156 void dumpDirFollow(const char *dname)
163 d = yaffs_opendir(dname);
167 printf("opendir failed\n");
171 while((de = yaffs_readdir(d)) != NULL)
173 sprintf(str,"%s/%s",dname,de->d_name);
177 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
178 switch(s.st_mode & S_IFMT)
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)
186 printf("\"%s\"",str);
188 default: printf("unknown"); break;
198 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
201 void dumpDir(const char *dname)
208 d = yaffs_opendir(dname);
212 printf("opendir failed\n");
216 while((de = yaffs_readdir(d)) != NULL)
218 sprintf(str,"%s/%s",dname,de->d_name);
222 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
223 switch(s.st_mode & S_IFMT)
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)
231 printf("\"%s\"",str);
233 default: printf("unknown"); break;
243 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
248 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
252 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
254 fd = yaffs_open(path,tflags,0);
256 if((fd >= 0) != (expectedResult > 0))
258 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
262 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
271 int main(int argc, char *argv[])
282 struct yaffs_stat ystat;
286 yaffs_mount("/boot");
287 yaffs_mount("/data");
288 yaffs_mount("/flash");
291 printf("\nDirectory look-up of /boot\n");
293 printf("\nDirectory look-up of /data\n");
295 printf("\nDirectory look-up of /flash\n");
298 //leave_unlinked_file("/flash",20000,0);
299 //leave_unlinked_file("/data",20000,0);
301 leave_unlinked_file("/ram",20,0);
304 f = yaffs_open("/boot/b1", O_RDONLY,0);
306 printf("open /boot/b1 readonly, f=%d\n",f);
308 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
310 printf("open /boot/b1 O_CREAT, f=%d\n",f);
313 r = yaffs_write(f,"hello",1);
314 printf("write %d attempted to write to a read-only file\n",r);
318 printf("close %d\n",r);
320 f = yaffs_open("/boot/b1", O_RDWR,0);
322 printf("open /boot/b1 O_RDWR,f=%d\n",f);
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);
330 r= yaffs_lseek(f,0,SEEK_END);
331 printf("seek end %d\n",r);
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);
338 r = yaffs_read(f,buffer,10);
339 printf("read %d \"%s\"\n",r,buffer);
341 r = yaffs_read(f,buffer,10);
342 printf("read %d \"%s\"\n",r,buffer);
344 // Check values reading at end.
345 // A read past end of file should return 0 for 0 bytes read.
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);
356 printf("close %d\n",r);
358 copy_in_a_file("/boot/yyfile","xxx");
360 // Create a file with a long name
362 copy_in_a_file("/boot/file with a long name","xxx");
365 printf("\nDirectory look-up of /boot\n");
369 r = yaffs_stat("/boot/file with a long name",&ystat);
373 r = yaffs_rename("/boot/file with a long name","/boot/r1");
375 printf("\nDirectory look-up of /boot\n");
379 r = yaffs_unlink("/boot/r1");
381 printf("\nDirectory look-up of /boot\n");
386 r = yaffs_mkdir("/boot/directory1",0);
388 printf("\nDirectory look-up of /boot\n");
390 printf("\nDirectory look-up of /boot/directory1\n");
391 dumpDir("/boot/directory1");
393 // add a file to the directory
394 copy_in_a_file("/boot/directory1/file with a long name","xxx");
396 printf("\nDirectory look-up of /boot\n");
398 printf("\nDirectory look-up of /boot/directory1\n");
399 dumpDir("/boot/directory1");
401 // Attempt to delete directory (should fail)
403 r = yaffs_rmdir("/boot/directory1");
405 printf("\nDirectory look-up of /boot\n");
407 printf("\nDirectory look-up of /boot/directory1\n");
408 dumpDir("/boot/directory1");
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");
415 printf("\nDirectory look-up of /boot\n");
417 printf("\nDirectory look-up of /boot/directory1\n");
418 dumpDir("/boot/directory1");
421 fill_disk_and_delete("/boot",20,20);
423 printf("\nDirectory look-up of /boot\n");
427 yaffs_symlink("yyfile","/boot/slink");
429 yaffs_readlink("/boot/slink",str,100);
430 printf("symlink alias is %s\n",str);
435 printf("\nDirectory look-up of /boot\n");
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");
442 h = yaffs_open("/boot/slink",O_RDWR,0);
444 printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
448 yaffs_unlink("/boot/slink");
451 printf("\nDirectory look-up of /boot\n");
456 yaffs_stat("/boot/yyfile",&ystat);
457 temp_mode = ystat.st_mode;
459 yaffs_chmod("/boot/yyfile",0x55555);
460 printf("\nDirectory look-up of /boot\n");
463 yaffs_chmod("/boot/yyfile",temp_mode);
464 printf("\nDirectory look-up of /boot\n");
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);
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);
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);
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);
484 yaffs_chmod("/boot/yyfile",temp_mode);
486 //create a zero-length file and unlink it (test for scan bug)
488 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
491 yaffs_unlink("/boot/zlf");
494 yaffs_DumpDevStruct("/boot");
496 fill_disk_and_delete("/boot",20,20);
498 yaffs_DumpDevStruct("/boot");
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);
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);
512 yaffs_DumpDevStruct("/boot");
513 yaffs_DumpDevStruct("/data");