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);
84 void dumpDirFollow(const char *dname)
91 d = yaffs_opendir(dname);
95 printf("opendir failed\n");
99 while((de = yaffs_readdir(d)) != NULL)
101 sprintf(str,"%s/%s",dname,de->d_name);
105 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
106 switch(s.st_mode & S_IFMT)
108 case S_IFREG: printf("data file"); break;
109 case S_IFDIR: printf("directory"); break;
110 case S_IFLNK: printf("symlink -->");
111 if(yaffs_readlink(str,str,100) < 0)
114 printf("\"%s\"",str);
116 default: printf("unknown"); break;
126 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
129 void dumpDir(const char *dname)
136 d = yaffs_opendir(dname);
140 printf("opendir failed\n");
144 while((de = yaffs_readdir(d)) != NULL)
146 sprintf(str,"%s/%s",dname,de->d_name);
150 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
151 switch(s.st_mode & S_IFMT)
153 case S_IFREG: printf("data file"); break;
154 case S_IFDIR: printf("directory"); break;
155 case S_IFLNK: printf("symlink -->");
156 if(yaffs_readlink(str,str,100) < 0)
159 printf("\"%s\"",str);
161 default: printf("unknown"); break;
171 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
176 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
180 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
182 fd = yaffs_open(path,tflags,0);
184 if((fd >= 0) != (expectedResult > 0))
186 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
190 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
199 int main(int argc, char *argv[])
210 struct yaffs_stat ystat;
214 yaffs_mount("/boot");
216 printf("\nDirectory look-up of /boot\n");
219 f = yaffs_open("/boot/b1", O_RDONLY,0);
221 printf("open /boot/b1 readonly, f=%d\n",f);
223 f = yaffs_open("/boot/b1", O_CREAT,0);
225 printf("open /boot/b1 O_CREAT, f=%d\n",f);
228 r = yaffs_write(f,"hello",1);
229 printf("write %d attempted to write to a read-only file\n",r);
233 printf("close %d\n",r);
235 f = yaffs_open("/boot/b1", O_RDWR,0);
237 printf("open /boot/b1 O_RDWR,f=%d\n",f);
240 r = yaffs_write(f,"hello",2);
241 printf("write %d attempted to write to a writeable file\n",r);
242 r = yaffs_write(f,"world",3);
243 printf("write %d attempted to write to a writeable file\n",r);
245 r= yaffs_lseek(f,SEEK_END,0);
246 printf("seek end %d\n",r);
248 r = yaffs_read(f,buffer,10);
249 printf("read %d \"%s\"\n",r,buffer);
250 r= yaffs_lseek(f,SEEK_SET,0);
251 printf("seek set %d\n",r);
253 r = yaffs_read(f,buffer,10);
254 printf("read %d \"%s\"\n",r,buffer);
256 r = yaffs_read(f,buffer,10);
257 printf("read %d \"%s\"\n",r,buffer);
262 printf("close %d\n",r);
264 copy_in_a_file("/boot/yyfile","xxx");
266 // Create a file with a long name
268 copy_in_a_file("/boot/file with a long name","xxx");
271 printf("\nDirectory look-up of /boot\n");
275 r = yaffs_stat("/boot/file with a long name",&ystat);
279 r = yaffs_rename("/boot/file with a long name","/boot/r1");
281 printf("\nDirectory look-up of /boot\n");
285 r = yaffs_unlink("/boot/r1");
287 printf("\nDirectory look-up of /boot\n");
292 r = yaffs_mkdir("/boot/directory1",0);
294 printf("\nDirectory look-up of /boot\n");
296 printf("\nDirectory look-up of /boot/directory1\n");
297 dumpDir("/boot/directory1");
299 // add a file to the directory
300 copy_in_a_file("/boot/directory1/file with a long name","xxx");
302 printf("\nDirectory look-up of /boot\n");
304 printf("\nDirectory look-up of /boot/directory1\n");
305 dumpDir("/boot/directory1");
307 // Attempt to delete directory (should fail)
309 r = yaffs_rmdir("/boot/directory1");
311 printf("\nDirectory look-up of /boot\n");
313 printf("\nDirectory look-up of /boot/directory1\n");
314 dumpDir("/boot/directory1");
316 // Delete file first, then rmdir should work
317 r = yaffs_unlink("/boot/directory1/file with a long name");
318 r = yaffs_rmdir("/boot/directory1");
321 printf("\nDirectory look-up of /boot\n");
323 printf("\nDirectory look-up of /boot/directory1\n");
324 dumpDir("/boot/directory1");
327 fill_disk_and_delete("/boot",20,20);
329 printf("\nDirectory look-up of /boot\n");
333 yaffs_symlink("yyfile","/boot/slink");
335 yaffs_readlink("/boot/slink",str,100);
336 printf("symlink alias is %s\n",str);
341 printf("\nDirectory look-up of /boot\n");
343 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
344 dumpDirFollow("/boot");
345 printf("\nDirectory look-up of /boot/directory1\n");
346 dumpDir("/boot/directory1");
348 h = yaffs_open("/boot/slink",O_RDWR,0);
350 printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
354 yaffs_unlink("/boot/slink");
357 printf("\nDirectory look-up of /boot\n");
362 yaffs_stat("/boot/yyfile",&ystat);
363 temp_mode = ystat.st_mode;
365 yaffs_chmod("/boot/yyfile",0x55555);
366 printf("\nDirectory look-up of /boot\n");
369 yaffs_chmod("/boot/yyfile",temp_mode);
370 printf("\nDirectory look-up of /boot\n");
373 // Permission checks...
374 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
375 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
376 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
378 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
379 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
380 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
382 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
383 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
384 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
386 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
387 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
388 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
390 yaffs_chmod("/boot/yyfile",temp_mode);
392 //create a zero-length file and unlink it (test for scan bug)
394 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
397 yaffs_unlink("/boot/zlf");
400 yaffs_DumpDevStruct("/boot");
402 fill_disk_and_delete("/boot",20,20);
404 yaffs_DumpDevStruct("/boot");