2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Timothy Manning <timothy@yaffs.net>
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.
15 * yaffs_tester.c designed to stress test yaffs2 direct.
19 #include "yaffs_tester.h"
24 int simulate_power_failure = 0;
27 buffer message_buffer; /*create message_buffer */
30 struct handle_regster{
31 int handle[MAX_NUMBER_OF_OPENED_HANDLES];
32 char path[MAX_NUMBER_OF_OPENED_HANDLES][100];
33 int number_of_open_handles;
37 int main(int argc, char *argv[]){
38 char yaffs_test_dir[] ="/yaffs2/test_dir"; /*the path to the directory where all of the testing will take place*/
39 char yaffs_mount_dir[]="/yaffs2/"; /*the path to the mount point which yaffs will mount*/
41 init(yaffs_test_dir,yaffs_mount_dir,argc,argv);
43 yaffs_unmount(yaffs_mount_dir);
49 void init(char *yaffs_test_dir,char *yaffs_mount_dir,int argc, char *argv[]){
54 /*these variables are already set to zero, but it is better not to take chances*/
55 message_buffer.head=0;
56 message_buffer.tail=0;
59 log_handle=fopen(LOG_FILE,"w");
60 if (log_handle!=NULL){
61 fputs("log file for yaffs tester\n",log_handle);
64 add_to_buffer(&message_buffer,"welcome to the yaffs tester",MESSAGE_LEVEL_BASIC_TASKS,PRINT);/* print boot up message*/
66 yaffs_mount(yaffs_mount_dir);
68 // add_to_buffer(&message_buffer,"argv ",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
69 // add_to_buffer(&message_buffer,argv[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
70 if (strcmp("-seed",argv[x])==0){ /*warning only compares the length of the strings, quick fix*/
71 seed= atoi(argv[x+1]);
72 /*add_to_buffer(&message_buffer,"setting seed to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
73 append_int_to_buffer(&message_buffer,seed,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
74 append_to_buffer(&message_buffer,"\n",MESSAGE_LEVEL_BASIC_TASKS,PRINT);*/
84 add_to_buffer(&message_buffer,"setting seed to ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
85 append_int_to_buffer(&message_buffer,seed,MESSAGE_LEVEL_BASIC_TASKS,PRINT);/* print boot up message*/
87 if (yaffs_access(yaffs_test_dir,0)) /* if the test folder does not exist then create it */
89 add_to_buffer(&message_buffer,"creating dir: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
90 append_to_buffer(&message_buffer,yaffs_test_dir,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
91 output=yaffs_mkdir(yaffs_test_dir,S_IREAD | S_IWRITE);
92 yaffs_check_for_errors(output, &message_buffer,"could not create dir","created dir\n\n");
96 void join_paths(char *path1,char *path2,char *new_path ){
98 /* printf("strlen path1:%d\n",strlen(path1));
99 printf("strlen path2:%d\n",strlen(path2));
100 printf("path1; %s\n",path1);
102 add_to_buffer(&message_buffer, "joining paths:",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
103 append_to_buffer(&message_buffer,path1,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
104 append_to_buffer(&message_buffer, " and ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
105 append_to_buffer(&message_buffer, path2,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
106 if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
107 /*paths are compatiable. concatanate them. note -2 is because of \0*/
108 strcat(new_path,path1);
109 strcat(new_path,path2);
110 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
111 //strcpy(new_path,strcat(path1,path2));
114 else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
115 /*paths are compatiable. concatanate them*/
116 strcat(new_path,path1);
117 strcat(new_path,path2);
118 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
119 //strcpy(new_path,strcat(path1,path2));
122 else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
123 /*need to add a "/". */
124 strcat(new_path,path1);
125 strcat(new_path,"/");
126 strcat(new_path,path2);
127 //strcpy(new_path,strcat(path1,strcat("/\0",path2)));
130 copy_array(path1,new_path,0,0);
131 copy_array('\0',new_path,0,(sizeof(path1)/sizeof(char)));
132 copy_array(path2,new_path,0,(sizeof(path1)/sizeof(char))+1);
133 old method now trying to use copy_array
134 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))+1];
135 for (x=0;x<=(sizeof(path1)/sizeof(char))-1;x++){
136 new_path[x]=path1[x];
139 for (x=(sizeof(path1)/sizeof(char)) ,i=0 ;i<=(sizeof(path2)/sizeof(char));x++,i++){
140 new_path[x]=path2[i];
146 else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
147 /*need to remove a "/". */
148 /*yaffs does not mind the extra slash. */
149 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))-1];
151 strcat(new_path,path1);
152 strcat(new_path,path2);
153 //strcpy(new_path,strcat(path1,strncat("",path2,(sizeof(path1)/sizeof(char))-1)));
163 void stat_file(char *path){
165 struct yaffs_stat stat;
166 if (yaffs_access(path,0)==0){
167 add_to_buffer(&message_buffer,"file exists, trying to stat: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
168 add_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
169 output=yaffs_lstat(path,&stat);
170 yaffs_check_for_errors(output, &message_buffer,"failed to stat file","statted file");
171 //stat.st_ino,(int)stat.st_size,stat.st_mode
172 add_to_buffer(&message_buffer,"yaffs inode: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
173 append_int_to_buffer(&message_buffer,stat.st_ino,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
174 append_to_buffer(&message_buffer," file size: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
175 append_int_to_buffer(&message_buffer,(int)stat.st_size,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
176 append_to_buffer(&message_buffer," file mode: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
177 append_int_to_buffer(&message_buffer,stat.st_mode,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
180 add_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
181 append_to_buffer(&message_buffer," does not exist,could not stat",MESSAGE_LEVEL_BASIC_TASKS,PRINT);
185 void test(char*yaffs_test_dir){
186 struct yaffs_stat stat;
188 char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
189 char path[MAX_FILE_NAME_SIZE];
190 struct handle_regster open_handles_array;
191 //int handle_pointers[MAX_NUMBER_OF_OPENED_HANDLES];
192 //int number_of_opened_handles=0;
194 join_paths(yaffs_test_dir,name,path);
196 open_handles_array.number_of_open_handles=0;
197 for (x=0;x<MAX_NUMBER_OF_OPENED_HANDLES;x++){
198 open_handles_array.handle[x]=-3;
199 open_handles_array.path[x][0]='\0';
204 path[0]='\0';// this should clear the path
205 add_to_buffer(&message_buffer,"number of opened handles: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
206 append_int_to_buffer(&message_buffer,open_handles_array.number_of_open_handles,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
207 if (open_handles_array.number_of_open_handles<MAX_NUMBER_OF_OPENED_HANDLES)
209 generate_random_string(name);
210 join_paths(yaffs_test_dir,name,path);
211 add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
212 append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
213 if (yaffs_access(path,0)==0){
217 add_to_buffer(&message_buffer,"file does not exists, creating file",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
220 output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
222 for (x=0;open_handles_array.handle[x]!=-3 && x<MAX_NUMBER_OF_OPENED_HANDLES;x++){
223 //x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
226 add_to_buffer(&message_buffer,"handle array id ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
227 append_int_to_buffer(&message_buffer,x,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
229 //for (x=0;handle_pointers[x]!=NULL;x++){}
230 open_handles_array.handle[x]=output;
231 open_handles_array.path[x][0]='\0';
232 strcat(open_handles_array.path[x],path);
233 add_to_buffer(&message_buffer,"yaffs handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
234 append_int_to_buffer(&message_buffer,output,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
235 add_to_buffer(&message_buffer,"stored handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
237 //yaffs_open will return a null pointer if it cannot open a file. check for errors will not work.
238 yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
240 open_handles_array.number_of_open_handles++;
242 else{ /*run out of space on the handle pointer array*/
244 while (open_handles_array.handle[x]==-3){
245 x=rand() % (MAX_NUMBER_OF_OPENED_HANDLES-1);
247 add_to_buffer(&message_buffer,"trying to close file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
248 append_to_buffer(&message_buffer,open_handles_array.path[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
249 add_to_buffer(&message_buffer,"file handle: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
250 append_int_to_buffer(&message_buffer,open_handles_array.handle[x],MESSAGE_LEVEL_BASIC_TASKS,PRINT);
252 stat_file(open_handles_array.path[x]);
254 output=yaffs_close(open_handles_array.handle[x]);
256 if (output==-1) yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
258 yaffs_check_for_errors(output, &message_buffer,"failed to close file","closed file");
259 open_handles_array.handle[x]=-3;
260 open_handles_array.path[x][0]='\0';
261 open_handles_array.number_of_open_handles--;
266 void generate_random_string(char *ptr){
268 unsigned int length=((rand() %MAX_FILE_NAME_SIZE)+1); /*creates a int with the number of charecters been between 1 and 51*/
271 //printf("generating string\n");
272 //printf("string length is %d\n",length);
273 for (x=0; x <= (length-2) &&length>2 ; x++)
275 //printf("x=%d\n",x);
276 /* keep generating a charecter until the charecter is legal*/
277 while((letter=='\0' )||(letter=='/')||(letter=='\\')){
278 letter=(rand() % 126-32)+32; /*generate a number between 32 and 126 and uses it as a charecter (letter) */
281 //printf("charecter generated is %c\n",ptr[x]);
283 ptr[x+1]='\0'; /*adds NULL charecter to turn it into a string*/