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");
38 void fill_disk(char *path,int nfiles)
46 for(n = 0; n < nfiles; n++)
48 sprintf(str,"%s/%d",path,n);
50 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
52 printf("writing file %s handle %d ",str, h);
54 while ((result = yaffs_write(h,xx,600)) == 600)
58 result = yaffs_close(h);
59 printf(" close %d\n",result);
63 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
69 for(i = 0; i < ncycles; i++)
71 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
72 fill_disk(path,nfiles);
74 for(j = 0; j < nfiles; j++)
76 sprintf(str,"%s/%d",path,j);
77 result = yaffs_unlink(str);
78 printf("unlinking file %s, result %d\n",str,result);
84 void fill_files(char *path,int flags, int maxIterations,int siz)
94 sprintf(str,"%s/%d",path,i);
95 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
100 for(j = 0; j < siz; j++)
102 yaffs_write(h,str,1);
111 } while(h >= 0 && i < maxIterations);
117 sprintf(str,"%s/%d",path,i);
118 printf("unlink %s\n",str);
120 } while(yaffs_unlink(str) >= 0);
124 void leave_unlinked_file(char *path,int maxIterations,int siz)
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);
141 } while(h < 0 && i < maxIterations);
145 for(i = 0; i < siz; i++)
147 yaffs_write(h,str,1);
151 printf("Leaving file %s open\n",str);
155 void dumpDirFollow(const char *dname)
162 d = yaffs_opendir(dname);
166 printf("opendir failed\n");
170 while((de = yaffs_readdir(d)) != NULL)
172 sprintf(str,"%s/%s",dname,de->d_name);
176 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
177 switch(s.st_mode & S_IFMT)
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)
185 printf("\"%s\"",str);
187 default: printf("unknown"); break;
197 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
200 void dumpDir(const char *dname)
207 d = yaffs_opendir(dname);
211 printf("opendir failed\n");
215 while((de = yaffs_readdir(d)) != NULL)
217 sprintf(str,"%s/%s",dname,de->d_name);
221 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
222 switch(s.st_mode & S_IFMT)
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)
230 printf("\"%s\"",str);
232 default: printf("unknown"); break;
242 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
247 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
251 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
253 fd = yaffs_open(path,tflags,0);
255 if((fd >= 0) != (expectedResult > 0))
257 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
261 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
270 int long_test(int argc, char *argv[])
281 struct yaffs_stat ystat;
285 yaffs_mount("/boot");
286 yaffs_mount("/data");
287 yaffs_mount("/flash");
290 printf("\nDirectory look-up of /boot\n");
292 printf("\nDirectory look-up of /data\n");
294 printf("\nDirectory look-up of /flash\n");
297 //leave_unlinked_file("/flash",20000,0);
298 //leave_unlinked_file("/data",20000,0);
300 leave_unlinked_file("/ram",20,0);
303 f = yaffs_open("/boot/b1", O_RDONLY,0);
305 printf("open /boot/b1 readonly, f=%d\n",f);
307 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
309 printf("open /boot/b1 O_CREAT, f=%d\n",f);
312 r = yaffs_write(f,"hello",1);
313 printf("write %d attempted to write to a read-only file\n",r);
317 printf("close %d\n",r);
319 f = yaffs_open("/boot/b1", O_RDWR,0);
321 printf("open /boot/b1 O_RDWR,f=%d\n",f);
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);
329 r= yaffs_lseek(f,0,SEEK_END);
330 printf("seek end %d\n",r);
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);
337 r = yaffs_read(f,buffer,10);
338 printf("read %d \"%s\"\n",r,buffer);
340 r = yaffs_read(f,buffer,10);
341 printf("read %d \"%s\"\n",r,buffer);
343 // Check values reading at end.
344 // A read past end of file should return 0 for 0 bytes read.
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);
355 printf("close %d\n",r);
357 copy_in_a_file("/boot/yyfile","xxx");
359 // Create a file with a long name
361 copy_in_a_file("/boot/file with a long name","xxx");
364 printf("\nDirectory look-up of /boot\n");
368 r = yaffs_stat("/boot/file with a long name",&ystat);
372 r = yaffs_rename("/boot/file with a long name","/boot/r1");
374 printf("\nDirectory look-up of /boot\n");
378 r = yaffs_unlink("/boot/r1");
380 printf("\nDirectory look-up of /boot\n");
385 r = yaffs_mkdir("/boot/directory1",0);
387 printf("\nDirectory look-up of /boot\n");
389 printf("\nDirectory look-up of /boot/directory1\n");
390 dumpDir("/boot/directory1");
392 // add a file to the directory
393 copy_in_a_file("/boot/directory1/file with a long name","xxx");
395 printf("\nDirectory look-up of /boot\n");
397 printf("\nDirectory look-up of /boot/directory1\n");
398 dumpDir("/boot/directory1");
400 // Attempt to delete directory (should fail)
402 r = yaffs_rmdir("/boot/directory1");
404 printf("\nDirectory look-up of /boot\n");
406 printf("\nDirectory look-up of /boot/directory1\n");
407 dumpDir("/boot/directory1");
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");
414 printf("\nDirectory look-up of /boot\n");
416 printf("\nDirectory look-up of /boot/directory1\n");
417 dumpDir("/boot/directory1");
420 fill_disk_and_delete("/boot",20,20);
422 printf("\nDirectory look-up of /boot\n");
426 yaffs_symlink("yyfile","/boot/slink");
428 yaffs_readlink("/boot/slink",str,100);
429 printf("symlink alias is %s\n",str);
434 printf("\nDirectory look-up of /boot\n");
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");
441 h = yaffs_open("/boot/slink",O_RDWR,0);
443 printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
447 yaffs_unlink("/boot/slink");
450 printf("\nDirectory look-up of /boot\n");
455 yaffs_stat("/boot/yyfile",&ystat);
456 temp_mode = ystat.st_mode;
458 yaffs_chmod("/boot/yyfile",0x55555);
459 printf("\nDirectory look-up of /boot\n");
462 yaffs_chmod("/boot/yyfile",temp_mode);
463 printf("\nDirectory look-up of /boot\n");
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);
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);
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);
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);
483 yaffs_chmod("/boot/yyfile",temp_mode);
485 //create a zero-length file and unlink it (test for scan bug)
487 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
490 yaffs_unlink("/boot/zlf");
493 yaffs_DumpDevStruct("/boot");
495 fill_disk_and_delete("/boot",20,20);
497 yaffs_DumpDevStruct("/boot");
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);
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);
511 yaffs_DumpDevStruct("/boot");
512 yaffs_DumpDevStruct("/data");
522 int directory_rename_test(void)
528 yaffs_mkdir("/ram/a",0);
529 yaffs_mkdir("/ram/a/b",0);
530 yaffs_mkdir("/ram/c",0);
532 printf("\nDirectory look-up of /ram\n");
537 printf("Do rename (should fail)\n");
539 r = yaffs_rename("/ram/a","/ram/a/b/d");
540 printf("\nDirectory look-up of /ram\n");
545 printf("Do rename (should not fail)\n");
547 r = yaffs_rename("/ram/c","/ram/a/b/d");
548 printf("\nDirectory look-up of /ram\n");
558 int main(int argc, char *argv[])
560 //return long_test(argc,argv);
562 return directory_rename_test();