dtest.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. /*
  14. * Test code for the "direct" interface.
  15. */
  16. /* XXX U-BOOT XXX */
  17. #include <common.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include "yaffsfs.h"
  23. void dumpDir(const char *dname);
  24. char xx[600];
  25. void copy_in_a_file(char *yaffsName,char *inName)
  26. {
  27. int inh,outh;
  28. unsigned char buffer[100];
  29. int ni,no;
  30. inh = open(inName,O_RDONLY);
  31. outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  32. while((ni = read(inh,buffer,100)) > 0)
  33. {
  34. no = yaffs_write(outh,buffer,ni);
  35. if(ni != no)
  36. {
  37. printf("problem writing yaffs file\n");
  38. }
  39. }
  40. yaffs_close(outh);
  41. close(inh);
  42. }
  43. void make_a_file(char *yaffsName,char bval,int sizeOfFile)
  44. {
  45. int outh;
  46. int i;
  47. unsigned char buffer[100];
  48. outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  49. memset(buffer,bval,100);
  50. do{
  51. i = sizeOfFile;
  52. if(i > 100) i = 100;
  53. sizeOfFile -= i;
  54. yaffs_write(outh,buffer,i);
  55. } while (sizeOfFile > 0);
  56. yaffs_close(outh);
  57. }
  58. void make_pattern_file(char *fn,int size)
  59. {
  60. int outh;
  61. int marker;
  62. int i;
  63. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  64. yaffs_lseek(outh,size-1,SEEK_SET);
  65. yaffs_write(outh,"A",1);
  66. for(i = 0; i < size; i+=256)
  67. {
  68. marker = ~i;
  69. yaffs_lseek(outh,i,SEEK_SET);
  70. yaffs_write(outh,&marker,sizeof(marker));
  71. }
  72. yaffs_close(outh);
  73. }
  74. int check_pattern_file(char *fn)
  75. {
  76. int h;
  77. int marker;
  78. int i;
  79. int size;
  80. int ok = 1;
  81. h = yaffs_open(fn, O_RDWR,0);
  82. size = yaffs_lseek(h,0,SEEK_END);
  83. for(i = 0; i < size; i+=256)
  84. {
  85. yaffs_lseek(h,i,SEEK_SET);
  86. yaffs_read(h,&marker,sizeof(marker));
  87. ok = (marker == ~i);
  88. if(!ok)
  89. {
  90. printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
  91. fn,size,i,marker,~i);
  92. }
  93. }
  94. yaffs_close(h);
  95. return ok;
  96. }
  97. int dump_file_data(char *fn)
  98. {
  99. int h;
  100. int marker;
  101. int i = 0;
  102. int size;
  103. int ok = 1;
  104. unsigned char b;
  105. h = yaffs_open(fn, O_RDWR,0);
  106. printf("%s\n",fn);
  107. while(yaffs_read(h,&b,1)> 0)
  108. {
  109. printf("%02x",b);
  110. i++;
  111. if(i > 32)
  112. {
  113. printf("\n");
  114. i = 0;;
  115. }
  116. }
  117. printf("\n");
  118. yaffs_close(h);
  119. return ok;
  120. }
  121. void dump_file(const char *fn)
  122. {
  123. int i;
  124. int size;
  125. int h;
  126. h = yaffs_open(fn,O_RDONLY,0);
  127. if(h < 0)
  128. {
  129. printf("*****\nDump file %s does not exist\n",fn);
  130. }
  131. else
  132. {
  133. size = yaffs_lseek(h,0,SEEK_SET);
  134. printf("*****\nDump file %s size %d\n",fn,size);
  135. for(i = 0; i < size; i++)
  136. {
  137. }
  138. }
  139. }
  140. void create_file_of_size(const char *fn,int syze)
  141. {
  142. int h;
  143. int n;
  144. char xx[200];
  145. int iterations = (syze + strlen(fn) -1)/ strlen(fn);
  146. h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  147. while (iterations > 0)
  148. {
  149. sprintf(xx,"%s %8d",fn,iterations);
  150. yaffs_write(h,xx,strlen(xx));
  151. iterations--;
  152. }
  153. yaffs_close (h);
  154. }
  155. void verify_file_of_size(const char *fn,int syze)
  156. {
  157. int h;
  158. int n;
  159. char xx[200];
  160. char yy[200];
  161. int l;
  162. int iterations = (syze + strlen(fn) -1)/ strlen(fn);
  163. h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
  164. while (iterations > 0)
  165. {
  166. sprintf(xx,"%s %8d",fn,iterations);
  167. l = strlen(xx);
  168. yaffs_read(h,yy,l);
  169. yy[l] = 0;
  170. if(strcmp(xx,yy)){
  171. printf("=====>>>>> verification of file %s failed near position %d\n",fn,yaffs_lseek(h,0,SEEK_CUR));
  172. }
  173. iterations--;
  174. }
  175. yaffs_close (h);
  176. }
  177. void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
  178. {
  179. int h;
  180. int n;
  181. int iterations;
  182. h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  183. iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
  184. while (iterations > 0)
  185. {
  186. yaffs_write(h,fn,strlen(fn));
  187. iterations--;
  188. }
  189. yaffs_truncate(h,reSyze);
  190. yaffs_lseek(h,0,SEEK_SET);
  191. iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
  192. while (iterations > 0)
  193. {
  194. yaffs_write(h,fn,strlen(fn));
  195. iterations--;
  196. }
  197. yaffs_close (h);
  198. }
  199. void do_some_file_stuff(const char *path)
  200. {
  201. char fn[100];
  202. sprintf(fn,"%s/%s",path,"f1");
  203. create_file_of_size(fn,10000);
  204. sprintf(fn,"%s/%s",path,"fdel");
  205. create_file_of_size(fn,10000);
  206. yaffs_unlink(fn);
  207. sprintf(fn,"%s/%s",path,"f2");
  208. create_resized_file_of_size(fn,10000,3000,4000);
  209. }
  210. void yaffs_backward_scan_test(const char *path)
  211. {
  212. char fn[100];
  213. yaffs_StartUp();
  214. yaffs_mount(path);
  215. do_some_file_stuff(path);
  216. sprintf(fn,"%s/ddd",path);
  217. yaffs_mkdir(fn,0);
  218. do_some_file_stuff(fn);
  219. yaffs_unmount(path);
  220. yaffs_mount(path);
  221. }
  222. char xxzz[2000];
  223. void yaffs_device_flush_test(const char *path)
  224. {
  225. char fn[100];
  226. int h;
  227. int i;
  228. yaffs_StartUp();
  229. yaffs_mount(path);
  230. do_some_file_stuff(path);
  231. // Open and add some data to a few files
  232. for(i = 0; i < 10; i++) {
  233. sprintf(fn,"%s/ff%d",path,i);
  234. h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
  235. yaffs_write(h,xxzz,2000);
  236. yaffs_write(h,xxzz,2000);
  237. }
  238. yaffs_unmount(path);
  239. yaffs_mount(path);
  240. }
  241. void short_scan_test(const char *path, int fsize, int niterations)
  242. {
  243. int i;
  244. char fn[100];
  245. sprintf(fn,"%s/%s",path,"f1");
  246. yaffs_StartUp();
  247. for(i = 0; i < niterations; i++)
  248. {
  249. printf("\n*****************\nIteration %d\n",i);
  250. yaffs_mount(path);
  251. printf("\nmount: Directory look-up of %s\n",path);
  252. dumpDir(path);
  253. make_a_file(fn,1,fsize);
  254. yaffs_unmount(path);
  255. }
  256. }
  257. void scan_pattern_test(const char *path, int fsize, int niterations)
  258. {
  259. int i;
  260. int j;
  261. char fn[3][100];
  262. int result;
  263. sprintf(fn[0],"%s/%s",path,"f0");
  264. sprintf(fn[1],"%s/%s",path,"f1");
  265. sprintf(fn[2],"%s/%s",path,"f2");
  266. yaffs_StartUp();
  267. for(i = 0; i < niterations; i++)
  268. {
  269. printf("\n*****************\nIteration %d\n",i);
  270. yaffs_mount(path);
  271. printf("\nmount: Directory look-up of %s\n",path);
  272. dumpDir(path);
  273. for(j = 0; j < 3; j++)
  274. {
  275. result = dump_file_data(fn[j]);
  276. result = check_pattern_file(fn[j]);
  277. make_pattern_file(fn[j],fsize);
  278. result = dump_file_data(fn[j]);
  279. result = check_pattern_file(fn[j]);
  280. }
  281. yaffs_unmount(path);
  282. }
  283. }
  284. void fill_disk(char *path,int nfiles)
  285. {
  286. int h;
  287. int n;
  288. int result;
  289. int f;
  290. char str[50];
  291. for(n = 0; n < nfiles; n++)
  292. {
  293. sprintf(str,"%s/%d",path,n);
  294. h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  295. printf("writing file %s handle %d ",str, h);
  296. while ((result = yaffs_write(h,xx,600)) == 600)
  297. {
  298. f = yaffs_freespace(path);
  299. }
  300. result = yaffs_close(h);
  301. printf(" close %d\n",result);
  302. }
  303. }
  304. void fill_disk_and_delete(char *path, int nfiles, int ncycles)
  305. {
  306. int i,j;
  307. char str[50];
  308. int result;
  309. for(i = 0; i < ncycles; i++)
  310. {
  311. printf("@@@@@@@@@@@@@@ cycle %d\n",i);
  312. fill_disk(path,nfiles);
  313. for(j = 0; j < nfiles; j++)
  314. {
  315. sprintf(str,"%s/%d",path,j);
  316. result = yaffs_unlink(str);
  317. printf("unlinking file %s, result %d\n",str,result);
  318. }
  319. }
  320. }
  321. void fill_files(char *path,int flags, int maxIterations,int siz)
  322. {
  323. int i;
  324. int j;
  325. char str[50];
  326. int h;
  327. i = 0;
  328. do{
  329. sprintf(str,"%s/%d",path,i);
  330. h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
  331. yaffs_close(h);
  332. if(h >= 0)
  333. {
  334. for(j = 0; j < siz; j++)
  335. {
  336. yaffs_write(h,str,1);
  337. }
  338. }
  339. if( flags & 1)
  340. {
  341. yaffs_unlink(str);
  342. }
  343. i++;
  344. } while(h >= 0 && i < maxIterations);
  345. if(flags & 2)
  346. {
  347. i = 0;
  348. do{
  349. sprintf(str,"%s/%d",path,i);
  350. printf("unlink %s\n",str);
  351. i++;
  352. } while(yaffs_unlink(str) >= 0);
  353. }
  354. }
  355. void leave_unlinked_file(char *path,int maxIterations,int siz)
  356. {
  357. int i;
  358. char str[50];
  359. int h;
  360. i = 0;
  361. do{
  362. sprintf(str,"%s/%d",path,i);
  363. printf("create %s\n",str);
  364. h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
  365. if(h >= 0)
  366. {
  367. yaffs_unlink(str);
  368. }
  369. i++;
  370. } while(h < 0 && i < maxIterations);
  371. if(h >= 0)
  372. {
  373. for(i = 0; i < siz; i++)
  374. {
  375. yaffs_write(h,str,1);
  376. }
  377. }
  378. printf("Leaving file %s open\n",str);
  379. }
  380. void dumpDirFollow(const char *dname)
  381. {
  382. yaffs_DIR *d;
  383. yaffs_dirent *de;
  384. struct yaffs_stat s;
  385. char str[100];
  386. d = yaffs_opendir(dname);
  387. if(!d)
  388. {
  389. printf("opendir failed\n");
  390. }
  391. else
  392. {
  393. while((de = yaffs_readdir(d)) != NULL)
  394. {
  395. sprintf(str,"%s/%s",dname,de->d_name);
  396. yaffs_stat(str,&s);
  397. printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
  398. switch(s.st_mode & S_IFMT)
  399. {
  400. case S_IFREG: printf("data file"); break;
  401. case S_IFDIR: printf("directory"); break;
  402. case S_IFLNK: printf("symlink -->");
  403. if(yaffs_readlink(str,str,100) < 0)
  404. printf("no alias");
  405. else
  406. printf("\"%s\"",str);
  407. break;
  408. default: printf("unknown"); break;
  409. }
  410. printf("\n");
  411. }
  412. yaffs_closedir(d);
  413. }
  414. printf("\n");
  415. printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
  416. }
  417. void dump_directory_tree_worker(const char *dname,int recursive)
  418. {
  419. yaffs_DIR *d;
  420. yaffs_dirent *de;
  421. struct yaffs_stat s;
  422. char str[1000];
  423. d = yaffs_opendir(dname);
  424. if(!d)
  425. {
  426. printf("opendir failed\n");
  427. }
  428. else
  429. {
  430. while((de = yaffs_readdir(d)) != NULL)
  431. {
  432. sprintf(str,"%s/%s",dname,de->d_name);
  433. yaffs_lstat(str,&s);
  434. printf("%s inode %d obj %x length %d mode %X ",str,s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);
  435. switch(s.st_mode & S_IFMT)
  436. {
  437. case S_IFREG: printf("data file"); break;
  438. case S_IFDIR: printf("directory"); break;
  439. case S_IFLNK: printf("symlink -->");
  440. if(yaffs_readlink(str,str,100) < 0)
  441. printf("no alias");
  442. else
  443. printf("\"%s\"",str);
  444. break;
  445. default: printf("unknown"); break;
  446. }
  447. printf("\n");
  448. if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
  449. dump_directory_tree_worker(str,1);
  450. }
  451. yaffs_closedir(d);
  452. }
  453. }
  454. static void dump_directory_tree(const char *dname)
  455. {
  456. dump_directory_tree_worker(dname,1);
  457. printf("\n");
  458. printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
  459. }
  460. void dumpDir(const char *dname)
  461. { dump_directory_tree_worker(dname,0);
  462. printf("\n");
  463. printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
  464. }
  465. static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
  466. {
  467. int fd;
  468. if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
  469. fd = yaffs_open(path,tflags,0);
  470. if((fd >= 0) != (expectedResult > 0))
  471. {
  472. printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
  473. }
  474. else
  475. {
  476. printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
  477. }
  478. yaffs_close(fd);
  479. }
  480. int long_test(int argc, char *argv[])
  481. {
  482. int f;
  483. int r;
  484. char buffer[20];
  485. char str[100];
  486. int h;
  487. mode_t temp_mode;
  488. struct yaffs_stat ystat;
  489. yaffs_StartUp();
  490. yaffs_mount("/boot");
  491. yaffs_mount("/data");
  492. yaffs_mount("/flash");
  493. yaffs_mount("/ram");
  494. printf("\nDirectory look-up of /boot\n");
  495. dumpDir("/boot");
  496. printf("\nDirectory look-up of /data\n");
  497. dumpDir("/data");
  498. printf("\nDirectory look-up of /flash\n");
  499. dumpDir("/flash");
  500. //leave_unlinked_file("/flash",20000,0);
  501. //leave_unlinked_file("/data",20000,0);
  502. leave_unlinked_file("/ram",20,0);
  503. f = yaffs_open("/boot/b1", O_RDONLY,0);
  504. printf("open /boot/b1 readonly, f=%d\n",f);
  505. f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
  506. printf("open /boot/b1 O_CREAT, f=%d\n",f);
  507. r = yaffs_write(f,"hello",1);
  508. printf("write %d attempted to write to a read-only file\n",r);
  509. r = yaffs_close(f);
  510. printf("close %d\n",r);
  511. f = yaffs_open("/boot/b1", O_RDWR,0);
  512. printf("open /boot/b1 O_RDWR,f=%d\n",f);
  513. r = yaffs_write(f,"hello",2);
  514. printf("write %d attempted to write to a writeable file\n",r);
  515. r = yaffs_write(f,"world",3);
  516. printf("write %d attempted to write to a writeable file\n",r);
  517. r= yaffs_lseek(f,0,SEEK_END);
  518. printf("seek end %d\n",r);
  519. memset(buffer,0,20);
  520. r = yaffs_read(f,buffer,10);
  521. printf("read %d \"%s\"\n",r,buffer);
  522. r= yaffs_lseek(f,0,SEEK_SET);
  523. printf("seek set %d\n",r);
  524. memset(buffer,0,20);
  525. r = yaffs_read(f,buffer,10);
  526. printf("read %d \"%s\"\n",r,buffer);
  527. memset(buffer,0,20);
  528. r = yaffs_read(f,buffer,10);
  529. printf("read %d \"%s\"\n",r,buffer);
  530. // Check values reading at end.
  531. // A read past end of file should return 0 for 0 bytes read.
  532. r= yaffs_lseek(f,0,SEEK_END);
  533. r = yaffs_read(f,buffer,10);
  534. printf("read at end returned %d\n",r);
  535. r= yaffs_lseek(f,500,SEEK_END);
  536. r = yaffs_read(f,buffer,10);
  537. printf("read past end returned %d\n",r);
  538. r = yaffs_close(f);
  539. printf("close %d\n",r);
  540. copy_in_a_file("/boot/yyfile","xxx");
  541. // Create a file with a long name
  542. copy_in_a_file("/boot/file with a long name","xxx");
  543. printf("\nDirectory look-up of /boot\n");
  544. dumpDir("/boot");
  545. // Check stat
  546. r = yaffs_stat("/boot/file with a long name",&ystat);
  547. // Check rename
  548. r = yaffs_rename("/boot/file with a long name","/boot/r1");
  549. printf("\nDirectory look-up of /boot\n");
  550. dumpDir("/boot");
  551. // Check unlink
  552. r = yaffs_unlink("/boot/r1");
  553. printf("\nDirectory look-up of /boot\n");
  554. dumpDir("/boot");
  555. // Check mkdir
  556. r = yaffs_mkdir("/boot/directory1",0);
  557. printf("\nDirectory look-up of /boot\n");
  558. dumpDir("/boot");
  559. printf("\nDirectory look-up of /boot/directory1\n");
  560. dumpDir("/boot/directory1");
  561. // add a file to the directory
  562. copy_in_a_file("/boot/directory1/file with a long name","xxx");
  563. printf("\nDirectory look-up of /boot\n");
  564. dumpDir("/boot");
  565. printf("\nDirectory look-up of /boot/directory1\n");
  566. dumpDir("/boot/directory1");
  567. // Attempt to delete directory (should fail)
  568. r = yaffs_rmdir("/boot/directory1");
  569. printf("\nDirectory look-up of /boot\n");
  570. dumpDir("/boot");
  571. printf("\nDirectory look-up of /boot/directory1\n");
  572. dumpDir("/boot/directory1");
  573. // Delete file first, then rmdir should work
  574. r = yaffs_unlink("/boot/directory1/file with a long name");
  575. r = yaffs_rmdir("/boot/directory1");
  576. printf("\nDirectory look-up of /boot\n");
  577. dumpDir("/boot");
  578. printf("\nDirectory look-up of /boot/directory1\n");
  579. dumpDir("/boot/directory1");
  580. #if 0
  581. fill_disk_and_delete("/boot",20,20);
  582. printf("\nDirectory look-up of /boot\n");
  583. dumpDir("/boot");
  584. #endif
  585. yaffs_symlink("yyfile","/boot/slink");
  586. yaffs_readlink("/boot/slink",str,100);
  587. printf("symlink alias is %s\n",str);
  588. printf("\nDirectory look-up of /boot\n");
  589. dumpDir("/boot");
  590. printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
  591. dumpDirFollow("/boot");
  592. printf("\nDirectory look-up of /boot/directory1\n");
  593. dumpDir("/boot/directory1");
  594. h = yaffs_open("/boot/slink",O_RDWR,0);
  595. printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
  596. yaffs_close(h);
  597. yaffs_unlink("/boot/slink");
  598. printf("\nDirectory look-up of /boot\n");
  599. dumpDir("/boot");
  600. // Check chmod
  601. yaffs_stat("/boot/yyfile",&ystat);
  602. temp_mode = ystat.st_mode;
  603. yaffs_chmod("/boot/yyfile",0x55555);
  604. printf("\nDirectory look-up of /boot\n");
  605. dumpDir("/boot");
  606. yaffs_chmod("/boot/yyfile",temp_mode);
  607. printf("\nDirectory look-up of /boot\n");
  608. dumpDir("/boot");
  609. // Permission checks...
  610. PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
  611. PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
  612. PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
  613. PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
  614. PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
  615. PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
  616. PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
  617. PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
  618. PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
  619. PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
  620. PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
  621. PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
  622. yaffs_chmod("/boot/yyfile",temp_mode);
  623. //create a zero-length file and unlink it (test for scan bug)
  624. h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
  625. yaffs_close(h);
  626. yaffs_unlink("/boot/zlf");
  627. yaffs_DumpDevStruct("/boot");
  628. fill_disk_and_delete("/boot",20,20);
  629. yaffs_DumpDevStruct("/boot");
  630. fill_files("/boot",1,10000,0);
  631. fill_files("/boot",1,10000,5000);
  632. fill_files("/boot",2,10000,0);
  633. fill_files("/boot",2,10000,5000);
  634. leave_unlinked_file("/data",20000,0);
  635. leave_unlinked_file("/data",20000,5000);
  636. leave_unlinked_file("/data",20000,5000);
  637. leave_unlinked_file("/data",20000,5000);
  638. leave_unlinked_file("/data",20000,5000);
  639. leave_unlinked_file("/data",20000,5000);
  640. yaffs_DumpDevStruct("/boot");
  641. yaffs_DumpDevStruct("/data");
  642. return 0;
  643. }
  644. int huge_directory_test_on_path(char *path)
  645. {
  646. yaffs_DIR *d;
  647. yaffs_dirent *de;
  648. struct yaffs_stat s;
  649. int f;
  650. int i;
  651. int r;
  652. int total = 0;
  653. int lastTotal = 0;
  654. char buffer[20];
  655. char str[100];
  656. char name[100];
  657. char name2[100];
  658. int h;
  659. mode_t temp_mode;
  660. struct yaffs_stat ystat;
  661. yaffs_StartUp();
  662. yaffs_mount(path);
  663. // Create a large number of files
  664. for(i = 0; i < 2000; i++)
  665. {
  666. sprintf(str,"%s/%d",path,i);
  667. f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
  668. yaffs_close(f);
  669. }
  670. d = yaffs_opendir(path);
  671. i = 0;
  672. if (d) {
  673. while((de = yaffs_readdir(d)) != NULL) {
  674. if (total >lastTotal+100*9*1024||(i & 1023)==0){
  675. printf("files = %d, total = %d\n",i, total);
  676. lastTotal = total;
  677. }
  678. i++;
  679. sprintf(str,"%s/%s",path,de->d_name);
  680. yaffs_lstat(str,&s);
  681. switch(s.st_mode & S_IFMT){
  682. case S_IFREG:
  683. //printf("data file");
  684. total += s.st_size;
  685. break;
  686. }
  687. }
  688. yaffs_closedir(d);
  689. }
  690. return 0;
  691. }
  692. int yaffs_scan_test(const char *path)
  693. {
  694. }
  695. void rename_over_test(const char *mountpt)
  696. {
  697. int i;
  698. char a[100];
  699. char b[100];
  700. sprintf(a,"%s/a",mountpt);
  701. sprintf(b,"%s/b",mountpt);
  702. yaffs_StartUp();
  703. yaffs_mount(mountpt);
  704. i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
  705. yaffs_close(i);
  706. i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
  707. yaffs_close(i);
  708. yaffs_rename(a,b); // rename over
  709. yaffs_rename(b,a); // rename back again (not renaimng over)
  710. yaffs_rename(a,b); // rename back again (not renaimng over)
  711. yaffs_unmount(mountpt);
  712. }
  713. int resize_stress_test(const char *path)
  714. {
  715. int a,b,i,j;
  716. int x;
  717. int r;
  718. char aname[100];
  719. char bname[100];
  720. char abuffer[1000];
  721. char bbuffer[1000];
  722. yaffs_StartUp();
  723. yaffs_mount(path);
  724. sprintf(aname,"%s%s",path,"/a");
  725. sprintf(bname,"%s%s",path,"/b");
  726. memset(abuffer,'a',1000);
  727. memset(bbuffer,'b',1000);
  728. a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  729. b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  730. printf(" %s %d %s %d\n",aname,a,bname,b);
  731. x = 0;
  732. for(j = 0; j < 100; j++)
  733. {
  734. yaffs_lseek(a,0,SEEK_END);
  735. for(i = 0; i <20000; i++)
  736. {
  737. //r = yaffs_lseek(b,i,SEEK_SET);
  738. //r = yaffs_write(b,bbuffer,1000);
  739. if(x & 0x16)
  740. {
  741. // shrink
  742. int syz = yaffs_lseek(a,0,SEEK_END);
  743. syz -= 500;
  744. if(syz < 0) syz = 0;
  745. yaffs_truncate(a,syz);
  746. }
  747. else
  748. {
  749. //expand
  750. r = yaffs_lseek(a,i * 500,SEEK_SET);
  751. r = yaffs_write(a,abuffer,1000);
  752. }
  753. x++;
  754. }
  755. }
  756. return 0;
  757. }
  758. int resize_stress_test_no_grow_complex(const char *path,int iters)
  759. {
  760. int a,b,i,j;
  761. int x;
  762. int r;
  763. char aname[100];
  764. char bname[100];
  765. char abuffer[1000];
  766. char bbuffer[1000];
  767. yaffs_StartUp();
  768. yaffs_mount(path);
  769. sprintf(aname,"%s%s",path,"/a");
  770. sprintf(bname,"%s%s",path,"/b");
  771. memset(abuffer,'a',1000);
  772. memset(bbuffer,'b',1000);
  773. a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  774. b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  775. printf(" %s %d %s %d\n",aname,a,bname,b);
  776. x = 0;
  777. for(j = 0; j < iters; j++)
  778. {
  779. yaffs_lseek(a,0,SEEK_END);
  780. for(i = 0; i <20000; i++)
  781. {
  782. //r = yaffs_lseek(b,i,SEEK_SET);
  783. //r = yaffs_write(b,bbuffer,1000);
  784. if(!(x%20))
  785. {
  786. // shrink
  787. int syz = yaffs_lseek(a,0,SEEK_END);
  788. while(syz > 4000)
  789. {
  790. syz -= 2050;
  791. if(syz < 0) syz = 0;
  792. yaffs_truncate(a,syz);
  793. syz = yaffs_lseek(a,0,SEEK_END);
  794. printf("shrink to %d\n",syz);
  795. }
  796. }
  797. else
  798. {
  799. //expand
  800. r = yaffs_lseek(a,500,SEEK_END);
  801. r = yaffs_write(a,abuffer,1000);
  802. }
  803. x++;
  804. }
  805. printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
  806. }
  807. return 0;
  808. }
  809. int resize_stress_test_no_grow(const char *path,int iters)
  810. {
  811. int a,b,i,j;
  812. int x;
  813. int r;
  814. char aname[100];
  815. char bname[100];
  816. char abuffer[1000];
  817. char bbuffer[1000];
  818. yaffs_StartUp();
  819. yaffs_mount(path);
  820. sprintf(aname,"%s%s",path,"/a");
  821. sprintf(bname,"%s%s",path,"/b");
  822. memset(abuffer,'a',1000);
  823. memset(bbuffer,'b',1000);
  824. a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  825. b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  826. printf(" %s %d %s %d\n",aname,a,bname,b);
  827. x = 0;
  828. for(j = 0; j < iters; j++)
  829. {
  830. yaffs_lseek(a,0,SEEK_END);
  831. for(i = 0; i <20000; i++)
  832. {
  833. //r = yaffs_lseek(b,i,SEEK_SET);
  834. //r = yaffs_write(b,bbuffer,1000);
  835. if(!(x%20))
  836. {
  837. // shrink
  838. int syz = yaffs_lseek(a,0,SEEK_END);
  839. while(syz > 4000)
  840. {
  841. syz -= 2050;
  842. if(syz < 0) syz = 0;
  843. yaffs_truncate(a,syz);
  844. syz = yaffs_lseek(a,0,SEEK_END);
  845. printf("shrink to %d\n",syz);
  846. }
  847. }
  848. else
  849. {
  850. //expand
  851. r = yaffs_lseek(a,-500,SEEK_END);
  852. r = yaffs_write(a,abuffer,1000);
  853. }
  854. x++;
  855. }
  856. printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
  857. }
  858. return 0;
  859. }
  860. int directory_rename_test(void)
  861. {
  862. int r;
  863. yaffs_StartUp();
  864. yaffs_mount("/ram");
  865. yaffs_mkdir("/ram/a",0);
  866. yaffs_mkdir("/ram/a/b",0);
  867. yaffs_mkdir("/ram/c",0);
  868. printf("\nDirectory look-up of /ram\n");
  869. dumpDir("/ram");
  870. dumpDir("/ram/a");
  871. dumpDir("/ram/a/b");
  872. printf("Do rename (should fail)\n");
  873. r = yaffs_rename("/ram/a","/ram/a/b/d");
  874. printf("\nDirectory look-up of /ram\n");
  875. dumpDir("/ram");
  876. dumpDir("/ram/a");
  877. dumpDir("/ram/a/b");
  878. printf("Do rename (should not fail)\n");
  879. r = yaffs_rename("/ram/c","/ram/a/b/d");
  880. printf("\nDirectory look-up of /ram\n");
  881. dumpDir("/ram");
  882. dumpDir("/ram/a");
  883. dumpDir("/ram/a/b");
  884. return 1;
  885. }
  886. int cache_read_test(void)
  887. {
  888. int a,b,c;
  889. int i;
  890. int sizeOfFiles = 500000;
  891. char buffer[100];
  892. yaffs_StartUp();
  893. yaffs_mount("/boot");
  894. make_a_file("/boot/a",'a',sizeOfFiles);
  895. make_a_file("/boot/b",'b',sizeOfFiles);
  896. a = yaffs_open("/boot/a",O_RDONLY,0);
  897. b = yaffs_open("/boot/b",O_RDONLY,0);
  898. c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  899. do{
  900. i = sizeOfFiles;
  901. if (i > 100) i = 100;
  902. sizeOfFiles -= i;
  903. yaffs_read(a,buffer,i);
  904. yaffs_read(b,buffer,i);
  905. yaffs_write(c,buffer,i);
  906. } while(sizeOfFiles > 0);
  907. return 1;
  908. }
  909. int cache_bypass_bug_test(void)
  910. {
  911. // This test reporoduces a bug whereby YAFFS caching *was* buypassed
  912. // resulting in erroneous reads after writes.
  913. // This bug has been fixed.
  914. int a;
  915. int i;
  916. char buffer1[1000];
  917. char buffer2[1000];
  918. memset(buffer1,0,sizeof(buffer1));
  919. memset(buffer2,0,sizeof(buffer2));
  920. yaffs_StartUp();
  921. yaffs_mount("/boot");
  922. // Create a file of 2000 bytes.
  923. make_a_file("/boot/a",'X',2000);
  924. a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
  925. // Write a short sequence to the file.
  926. // This will go into the cache.
  927. yaffs_lseek(a,0,SEEK_SET);
  928. yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
  929. // Read a short sequence from the file.
  930. // This will come from the cache.
  931. yaffs_lseek(a,0,SEEK_SET);
  932. yaffs_read(a,buffer1,30);
  933. // Read a page size sequence from the file.
  934. yaffs_lseek(a,0,SEEK_SET);
  935. yaffs_read(a,buffer2,512);
  936. printf("buffer 1 %s\n",buffer1);
  937. printf("buffer 2 %s\n",buffer2);
  938. if(strncmp(buffer1,buffer2,20))
  939. {
  940. printf("Cache bypass bug detected!!!!!\n");
  941. }
  942. return 1;
  943. }
  944. int free_space_check(void)
  945. {
  946. int f;
  947. yaffs_StartUp();
  948. yaffs_mount("/boot");
  949. fill_disk("/boot/",2);
  950. f = yaffs_freespace("/boot");
  951. printf("%d free when disk full\n",f);
  952. return 1;
  953. }
  954. int truncate_test(void)
  955. {
  956. int a;
  957. int r;
  958. int i;
  959. int l;
  960. char y[10];
  961. yaffs_StartUp();
  962. yaffs_mount("/boot");
  963. yaffs_unlink("/boot/trunctest");
  964. a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  965. yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
  966. yaffs_truncate(a,3);
  967. l= yaffs_lseek(a,0,SEEK_END);
  968. printf("truncated length is %d\n",l);
  969. yaffs_lseek(a,5,SEEK_SET);
  970. yaffs_write(a,"1",1);
  971. yaffs_lseek(a,0,SEEK_SET);
  972. r = yaffs_read(a,y,10);
  973. printf("read %d bytes:",r);
  974. for(i = 0; i < r; i++) printf("[%02X]",y[i]);
  975. printf("\n");
  976. return 0;
  977. }
  978. void fill_disk_test(const char *mountpt)
  979. {
  980. int i;
  981. yaffs_StartUp();
  982. for(i = 0; i < 5; i++)
  983. {
  984. yaffs_mount(mountpt);
  985. fill_disk_and_delete(mountpt,100,i+1);
  986. yaffs_unmount(mountpt);
  987. }
  988. }
  989. void lookup_test(const char *mountpt)
  990. {
  991. int i;
  992. int h;
  993. char a[100];
  994. char b[100];
  995. yaffs_DIR *d;
  996. yaffs_dirent *de;
  997. struct yaffs_stat s;
  998. char str[100];
  999. yaffs_StartUp();
  1000. yaffs_mount(mountpt);
  1001. d = yaffs_opendir(mountpt);
  1002. if(!d)
  1003. {
  1004. printf("opendir failed\n");
  1005. }
  1006. else
  1007. {
  1008. for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
  1009. {
  1010. printf("unlinking %s\n",de->d_name);
  1011. yaffs_unlink(de->d_name);
  1012. }
  1013. printf("%d files deleted\n",i);
  1014. }
  1015. for(i = 0; i < 2000; i++){
  1016. sprintf(a,"%s/%d",mountpt,i);
  1017. h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
  1018. yaffs_close(h);
  1019. }
  1020. yaffs_rewinddir(d);
  1021. for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
  1022. {
  1023. printf("%d %s\n",i,de->d_name);
  1024. }
  1025. printf("%d files listed\n\n\n",i);
  1026. yaffs_rewinddir(d);
  1027. yaffs_readdir(d);
  1028. yaffs_readdir(d);
  1029. yaffs_readdir(d);
  1030. for(i = 0; i < 2000; i++){
  1031. sprintf(a,"%s/%d",mountpt,i);
  1032. yaffs_unlink(a);
  1033. }
  1034. yaffs_unmount(mountpt);
  1035. }
  1036. void link_test(const char *mountpt)
  1037. {
  1038. int i;
  1039. int h;
  1040. char a[100];
  1041. char b[100];
  1042. char c[100];
  1043. int f0;
  1044. int f1;
  1045. int f2;
  1046. int f3;
  1047. sprintf(a,"%s/aaa",mountpt);
  1048. sprintf(b,"%s/bbb",mountpt);
  1049. sprintf(c,"%s/ccc",mountpt);
  1050. yaffs_StartUp();
  1051. yaffs_mount(mountpt);
  1052. h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1053. for(i = 0; i < 100; i++)
  1054. yaffs_write(h,a,100);
  1055. yaffs_close(h);
  1056. yaffs_unlink(b);
  1057. yaffs_unlink(c);
  1058. yaffs_link(a,b);
  1059. yaffs_link(a,c);
  1060. yaffs_unlink(b);
  1061. yaffs_unlink(c);
  1062. yaffs_unlink(a);
  1063. yaffs_unmount(mountpt);
  1064. yaffs_mount(mountpt);
  1065. printf("link test done\n");
  1066. }
  1067. void freespace_test(const char *mountpt)
  1068. {
  1069. int i;
  1070. int h;
  1071. char a[100];
  1072. char b[100];
  1073. int f0;
  1074. int f1;
  1075. int f2;
  1076. int f3;
  1077. sprintf(a,"%s/aaa",mountpt);
  1078. yaffs_StartUp();
  1079. yaffs_mount(mountpt);
  1080. f0 = yaffs_freespace(mountpt);
  1081. h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1082. for(i = 0; i < 100; i++)
  1083. yaffs_write(h,a,100);
  1084. yaffs_close(h);
  1085. f1 = yaffs_freespace(mountpt);
  1086. yaffs_unlink(a);
  1087. f2 = yaffs_freespace(mountpt);
  1088. yaffs_unmount(mountpt);
  1089. yaffs_mount(mountpt);
  1090. f3 = yaffs_freespace(mountpt);
  1091. printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
  1092. }
  1093. void simple_rw_test(const char *mountpt)
  1094. {
  1095. int i;
  1096. int h;
  1097. char a[100];
  1098. int x;
  1099. int result;
  1100. sprintf(a,"%s/aaa",mountpt);
  1101. yaffs_StartUp();
  1102. yaffs_mount(mountpt);
  1103. yaffs_unlink(a);
  1104. h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1105. for(i = 100000;i < 200000; i++){
  1106. result = yaffs_write(h,&i,sizeof(i));
  1107. if(result != 4)
  1108. {
  1109. printf("write error\n");
  1110. exit(1);
  1111. }
  1112. }
  1113. //yaffs_close(h);
  1114. // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
  1115. yaffs_lseek(h,0,SEEK_SET);
  1116. for(i = 100000; i < 200000; i++){
  1117. result = yaffs_read(h,&x,sizeof(x));
  1118. if(result != 4 || x != i){
  1119. printf("read error %d %x %x\n",i,result,x);
  1120. }
  1121. }
  1122. printf("Simple rw test passed\n");
  1123. }
  1124. void scan_deleted_files_test(const char *mountpt)
  1125. {
  1126. char fn[100];
  1127. char sub[100];
  1128. const char *p;
  1129. int i;
  1130. int j;
  1131. int k;
  1132. int h;
  1133. sprintf(sub,"%s/sdir",mountpt);
  1134. yaffs_StartUp();
  1135. for(j = 0; j < 10; j++)
  1136. {
  1137. printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
  1138. yaffs_mount(mountpt);
  1139. yaffs_mkdir(sub,0);
  1140. p = (j & 0) ? mountpt: sub;
  1141. for(i = 0; i < 100; i++)
  1142. {
  1143. sprintf(fn,"%s/%d",p,i);
  1144. if(i & 1)
  1145. {
  1146. h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1147. for(k = 0; k < 1000; k++)
  1148. yaffs_write(h,fn,100);
  1149. yaffs_close(h);
  1150. }
  1151. else
  1152. yaffs_mkdir(fn,0);
  1153. }
  1154. for(i = 0; i < 10; i++)
  1155. {
  1156. sprintf(fn,"%s/%d",p,i);
  1157. if(i & 1)
  1158. yaffs_unlink(fn);
  1159. else
  1160. yaffs_rmdir(fn);
  1161. }
  1162. yaffs_unmount(mountpt);
  1163. }
  1164. }
  1165. void write_10k(int h)
  1166. {
  1167. int i;
  1168. const char *s="0123456789";
  1169. for(i = 0; i < 1000; i++)
  1170. yaffs_write(h,s,10);
  1171. }
  1172. void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
  1173. {
  1174. int h1;
  1175. int i;
  1176. int offs;
  1177. h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1178. for(i = 0; i < 100000; i+= 10000)
  1179. {
  1180. write_10k(h1);
  1181. }
  1182. offs = yaffs_lseek(h1,0,SEEK_CUR);
  1183. if( offs != 100000)
  1184. {
  1185. printf("Could not write file\n");
  1186. }
  1187. yaffs_unlink(fdel);
  1188. for(i = 0; i < 100000; i+= 10000)
  1189. {
  1190. write_10k(h1);
  1191. }
  1192. offs = yaffs_lseek(h1,0,SEEK_CUR);
  1193. if( offs != 200000)
  1194. {
  1195. printf("Could not write file\n");
  1196. }
  1197. yaffs_close(h1);
  1198. yaffs_unlink(fdel1);
  1199. }
  1200. void verify_200k_file(const char *fn)
  1201. {
  1202. int h1;
  1203. int i;
  1204. char x[11];
  1205. const char *s="0123456789";
  1206. int errCount = 0;
  1207. h1 = yaffs_open(fn, O_RDONLY, 0);
  1208. for(i = 0; i < 200000 && errCount < 10; i+= 10)
  1209. {
  1210. yaffs_read(h1,x,10);
  1211. if(strncmp(x,s,10) != 0)
  1212. {
  1213. printf("File %s verification failed at %d\n",fn,i);
  1214. errCount++;
  1215. }
  1216. }
  1217. if(errCount >= 10)
  1218. printf("Too many errors... aborted\n");
  1219. yaffs_close(h1);
  1220. }
  1221. void check_resize_gc_bug(const char *mountpt)
  1222. {
  1223. char a[30];
  1224. char b[30];
  1225. char c[30];
  1226. int i;
  1227. sprintf(a,"%s/a",mountpt);
  1228. sprintf(b,"%s/b",mountpt);
  1229. sprintf(c,"%s/c",mountpt);
  1230. yaffs_StartUp();
  1231. yaffs_mount(mountpt);
  1232. yaffs_unlink(a);
  1233. yaffs_unlink(b);
  1234. for(i = 0; i < 50; i++)
  1235. {
  1236. printf("A\n");write_200k_file(a,"",c);
  1237. printf("B\n");verify_200k_file(a);
  1238. printf("C\n");write_200k_file(b,a,c);
  1239. printf("D\n");verify_200k_file(b);
  1240. yaffs_unmount(mountpt);
  1241. yaffs_mount(mountpt);
  1242. printf("E\n");verify_200k_file(a);
  1243. printf("F\n");verify_200k_file(b);
  1244. }
  1245. }
  1246. void multi_mount_test(const char *mountpt,int nmounts)
  1247. {
  1248. char a[30];
  1249. char b[30];
  1250. char c[30];
  1251. int i;
  1252. int j;
  1253. sprintf(a,"%s/a",mountpt);
  1254. yaffs_StartUp();
  1255. for(i = 0; i < nmounts; i++){
  1256. int h0;
  1257. int h1;
  1258. int len0;
  1259. int len1;
  1260. static char xx[1000];
  1261. printf("############### Iteration %d Start\n",i);
  1262. if(1 || i == 0 || i == 5)
  1263. yaffs_mount(mountpt);
  1264. dump_directory_tree(mountpt);
  1265. yaffs_mkdir(a,0);
  1266. sprintf(xx,"%s/0",a);
  1267. h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1268. sprintf(xx,"%s/1",a);
  1269. h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1270. for(j = 0; j < 200; j++){
  1271. yaffs_write(h0,xx,1000);
  1272. yaffs_write(h1,xx,1000);
  1273. }
  1274. len0 = yaffs_lseek(h0,0,SEEK_END);
  1275. len1 = yaffs_lseek(h1,0,SEEK_END);
  1276. yaffs_lseek(h0,0,SEEK_SET);
  1277. yaffs_lseek(h1,0,SEEK_SET);
  1278. for(j = 0; j < 200; j++){
  1279. yaffs_read(h0,xx,1000);
  1280. yaffs_read(h1,xx,1000);
  1281. }
  1282. yaffs_truncate(h0,0);
  1283. yaffs_close(h0);
  1284. yaffs_close(h1);
  1285. printf("########### %d\n",i);
  1286. dump_directory_tree(mountpt);
  1287. if(1 || i == 4 || i == nmounts -1)
  1288. yaffs_unmount(mountpt);
  1289. }
  1290. }
  1291. void small_mount_test(const char *mountpt,int nmounts)
  1292. {
  1293. char a[30];
  1294. char b[30];
  1295. char c[30];
  1296. int i;
  1297. int j;
  1298. int h0;
  1299. int h1;
  1300. int len0;
  1301. int len1;
  1302. int nread;
  1303. sprintf(a,"%s/a",mountpt);
  1304. yaffs_StartUp();
  1305. for(i = 0; i < nmounts; i++){
  1306. static char xx[1000];
  1307. printf("############### Iteration %d Start\n",i);
  1308. if(1 || i == 0 || i == 5)
  1309. yaffs_mount(mountpt);
  1310. dump_directory_tree(mountpt);
  1311. yaffs_mkdir(a,0);
  1312. sprintf(xx,"%s/0",a);
  1313. if(i ==0){
  1314. h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1315. for(j = 0; j < 130; j++)
  1316. yaffs_write(h0,xx,1000);
  1317. yaffs_close(h0);
  1318. }
  1319. h0 = yaffs_open(xx,O_RDONLY,0);
  1320. sprintf(xx,"%s/1",a);
  1321. h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1322. while((nread = yaffs_read(h0,xx,1000)) > 0)
  1323. yaffs_write(h1,xx,nread);
  1324. len0 = yaffs_lseek(h0,0,SEEK_END);
  1325. len1 = yaffs_lseek(h1,0,SEEK_END);
  1326. yaffs_lseek(h0,0,SEEK_SET);
  1327. yaffs_lseek(h1,0,SEEK_SET);
  1328. for(j = 0; j < 200; j++){
  1329. yaffs_read(h0,xx,1000);
  1330. yaffs_read(h1,xx,1000);
  1331. }
  1332. yaffs_close(h0);
  1333. yaffs_close(h1);
  1334. printf("########### %d\n",i);
  1335. dump_directory_tree(mountpt);
  1336. if(1 || i == 4 || i == nmounts -1)
  1337. yaffs_unmount(mountpt);
  1338. }
  1339. }
  1340. int early_exit;
  1341. void small_overwrite_test(const char *mountpt,int nmounts)
  1342. {
  1343. char a[30];
  1344. char b[30];
  1345. char c[30];
  1346. int i;
  1347. int j;
  1348. int h0;
  1349. int h1;
  1350. int len0;
  1351. int len1;
  1352. int nread;
  1353. sprintf(a,"%s/a",mountpt);
  1354. yaffs_StartUp();
  1355. for(i = 0; i < nmounts; i++){
  1356. static char xx[8000];
  1357. printf("############### Iteration %d Start\n",i);
  1358. if(1)
  1359. yaffs_mount(mountpt);
  1360. dump_directory_tree(mountpt);
  1361. yaffs_mkdir(a,0);
  1362. sprintf(xx,"%s/0",a);
  1363. h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1364. sprintf(xx,"%s/1",a);
  1365. h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1366. for(j = 0; j < 1000000; j+=1000){
  1367. yaffs_truncate(h0,j);
  1368. yaffs_lseek(h0,j,SEEK_SET);
  1369. yaffs_write(h0,xx,7000);
  1370. yaffs_write(h1,xx,7000);
  1371. if(early_exit)
  1372. exit(0);
  1373. }
  1374. yaffs_close(h0);
  1375. printf("########### %d\n",i);
  1376. dump_directory_tree(mountpt);
  1377. if(1)
  1378. yaffs_unmount(mountpt);
  1379. }
  1380. }
  1381. void yaffs_touch(const char *fn)
  1382. {
  1383. yaffs_chmod(fn, S_IREAD | S_IWRITE);
  1384. }
  1385. void checkpoint_fill_test(const char *mountpt,int nmounts)
  1386. {
  1387. char a[50];
  1388. char b[50];
  1389. char c[50];
  1390. int i;
  1391. int j;
  1392. int h;
  1393. sprintf(a,"%s/a",mountpt);
  1394. yaffs_StartUp();
  1395. for(i = 0; i < nmounts; i++){
  1396. printf("############### Iteration %d Start\n",i);
  1397. yaffs_mount(mountpt);
  1398. dump_directory_tree(mountpt);
  1399. yaffs_mkdir(a,0);
  1400. sprintf(b,"%s/zz",a);
  1401. h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
  1402. while(yaffs_write(h,c,50) == 50){}
  1403. yaffs_close(h);
  1404. for(j = 0; j < 2; j++){
  1405. printf("touch %d\n",j);
  1406. yaffs_touch(b);
  1407. yaffs_unmount(mountpt);
  1408. yaffs_mount(mountpt);
  1409. }
  1410. dump_directory_tree(mountpt);
  1411. yaffs_unmount(mountpt);
  1412. }
  1413. }
  1414. int make_file2(const char *name1, const char *name2,int syz)
  1415. {
  1416. char xx[2500];
  1417. int i;
  1418. int h1=-1,h2=-1;
  1419. int n = 1;
  1420. if(name1)
  1421. h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1422. if(name2)
  1423. h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
  1424. while(syz > 0 && n > 0){
  1425. i = (syz > 2500) ? 2500 : syz;
  1426. n = yaffs_write(h1,xx,i);
  1427. n = yaffs_write(h2,xx,i);
  1428. syz -= 500;
  1429. }
  1430. yaffs_close(h1);
  1431. yaffs_close(h2);
  1432. }
  1433. extern void SetCheckpointReservedBlocks(int n);
  1434. void checkpoint_upgrade_test(const char *mountpt,int nmounts)
  1435. {
  1436. char a[50];
  1437. char b[50];
  1438. char c[50];
  1439. char d[50];
  1440. int i;
  1441. int j;
  1442. int h;
  1443. sprintf(a,"%s/a",mountpt);
  1444. printf("Create start condition\n");
  1445. yaffs_StartUp();
  1446. SetCheckpointReservedBlocks(0);
  1447. yaffs_mount(mountpt);
  1448. yaffs_mkdir(a,0);
  1449. sprintf(b,"%s/zz",a);
  1450. sprintf(c,"%s/xx",a);
  1451. make_file2(b,c,2000000);
  1452. sprintf(d,"%s/aa",a);
  1453. make_file2(d,NULL,500000000);
  1454. dump_directory_tree(mountpt);
  1455. printf("Umount/mount attempt full\n");
  1456. yaffs_unmount(mountpt);
  1457. SetCheckpointReservedBlocks(10);
  1458. yaffs_mount(mountpt);
  1459. printf("unlink small file\n");
  1460. yaffs_unlink(c);
  1461. dump_directory_tree(mountpt);
  1462. printf("Umount/mount attempt\n");
  1463. yaffs_unmount(mountpt);
  1464. yaffs_mount(mountpt);
  1465. for(j = 0; j < 500; j++){
  1466. printf("***** touch %d\n",j);
  1467. dump_directory_tree(mountpt);
  1468. yaffs_touch(b);
  1469. yaffs_unmount(mountpt);
  1470. yaffs_mount(mountpt);
  1471. }
  1472. for(j = 0; j < 500; j++){
  1473. printf("***** touch %d\n",j);
  1474. dump_directory_tree(mountpt);
  1475. yaffs_touch(b);
  1476. yaffs_unmount(mountpt);
  1477. yaffs_mount(mountpt);
  1478. }
  1479. }
  1480. void huge_array_test(const char *mountpt,int n)
  1481. {
  1482. char a[50];
  1483. int i;
  1484. int j;
  1485. int h;
  1486. int fnum;
  1487. sprintf(a,"mount point %s",mountpt);
  1488. yaffs_StartUp();
  1489. yaffs_mount(mountpt);
  1490. while(n>0){
  1491. n--;
  1492. fnum = 0;
  1493. printf("\n\n START run\n\n");
  1494. while(yaffs_freespace(mountpt) > 25000000){
  1495. sprintf(a,"%s/file%d",mountpt,fnum);
  1496. fnum++;
  1497. printf("create file %s\n",a);
  1498. create_file_of_size(a,10000000);
  1499. printf("verifying file %s\n",a);
  1500. verify_file_of_size(a,10000000);
  1501. }
  1502. printf("\n\n verification/deletion\n\n");
  1503. for(i = 0; i < fnum; i++){
  1504. sprintf(a,"%s/file%d",mountpt,i);
  1505. printf("verifying file %s\n",a);
  1506. verify_file_of_size(a,10000000);
  1507. printf("deleting file %s\n",a);
  1508. yaffs_unlink(a);
  1509. }
  1510. printf("\n\n done \n\n");
  1511. }
  1512. }
  1513. void random_write(int h)
  1514. {
  1515. static char buffer[12000];
  1516. int n;
  1517. n = random() & 0x1FFF;
  1518. yaffs_write(h,buffer,n);
  1519. }
  1520. void random_seek(int h)
  1521. {
  1522. int n;
  1523. n = random() & 0xFFFFF;
  1524. yaffs_lseek(h,n,SEEK_SET);
  1525. }
  1526. void random_truncate(int h, char * name)
  1527. {
  1528. int n;
  1529. int flen;
  1530. n = random() & 0xFFFFF;
  1531. flen = yaffs_lseek(h,0,SEEK_END);
  1532. if(n > flen)
  1533. n = flen / 2;
  1534. yaffs_truncate(name,n);
  1535. yaffs_lseek(h,n,SEEK_SET);
  1536. }
  1537. #define NSMALLFILES 10
  1538. void random_small_file_test(const char *mountpt,int iterations)
  1539. {
  1540. char a[NSMALLFILES][50];
  1541. int i;
  1542. int n;
  1543. int j;
  1544. int h[NSMALLFILES];
  1545. int r;
  1546. int fnum;
  1547. yaffs_StartUp();
  1548. yaffs_mount(mountpt);
  1549. for(i = 0; i < NSMALLFILES; i++){
  1550. h[i]=-1;
  1551. strcpy(a[i],"");
  1552. }
  1553. for(n = 0; n < iterations; n++){
  1554. for(i = 0; i < NSMALLFILES; i++) {
  1555. r = random();
  1556. if(strlen(a[i]) == 0){
  1557. sprintf(a[i],"%s/%dx%d",mountpt,n,i);
  1558. h[i] = yaffs_open(a,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1559. }
  1560. if(h[i] < -1)
  1561. printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
  1562. else {
  1563. r = r & 7;
  1564. switch(r){
  1565. case 0:
  1566. case 1:
  1567. case 2:
  1568. random_write(h[i]);
  1569. break;
  1570. case 3:
  1571. random_truncate(h[i],a[i]);
  1572. break;
  1573. case 4:
  1574. case 5: random_seek(h[i]);
  1575. break;
  1576. case 6:
  1577. yaffs_close(h[i]);
  1578. h[i] = -1;
  1579. break;
  1580. case 7:
  1581. yaffs_close(h[i]);
  1582. yaffs_unlink(a[i]);
  1583. strcpy(a[i],"");
  1584. h[i] = -1;
  1585. }
  1586. }
  1587. }
  1588. }
  1589. for(i = 0; i < NSMALLFILES; i++)
  1590. yaffs_close(h[i]);
  1591. yaffs_unmount(mountpt);
  1592. }
  1593. int main(int argc, char *argv[])
  1594. {
  1595. //return long_test(argc,argv);
  1596. //return cache_read_test();
  1597. resize_stress_test_no_grow("/flash/flash",20);
  1598. //huge_directory_test_on_path("/ram2k");
  1599. //yaffs_backward_scan_test("/flash/flash");
  1600. // yaffs_device_flush_test("/flash/flash");
  1601. //scan_pattern_test("/flash",10000,10);
  1602. //short_scan_test("/flash/flash",40000,200);
  1603. //small_mount_test("/flash/flash",1000);
  1604. //small_overwrite_test("/flash/flash",1000);
  1605. //checkpoint_fill_test("/flash/flash",20);
  1606. // random_small_file_test("/flash/flash",10000);
  1607. // huge_array_test("/flash/flash",10);
  1608. //long_test_on_path("/ram2k");
  1609. // long_test_on_path("/flash");
  1610. //simple_rw_test("/flash/flash");
  1611. //fill_disk_test("/flash/flash");
  1612. // rename_over_test("/flash");
  1613. //lookup_test("/flash");
  1614. //freespace_test("/flash/flash");
  1615. //link_test("/flash/flash");
  1616. // cache_bypass_bug_test();
  1617. //free_space_check();
  1618. //check_resize_gc_bug("/flash");
  1619. return 0;
  1620. }