fixed up all the quick tests
[yaffs2.git] / direct / test-framework / timothy_tests / quick_tests / lib.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Timothy Manning <timothy@yaffs.net>
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 #include "lib.h"
15
16 static int EXIT_ON_ERROR = 1;
17 static int  PRINT_LEVEL = 2;    //This sets the level of detail which is printed. There are 3 levels 0,1,2 and 3  
18                         //0 just prints the number of tests passed and failed.
19                         //1 is the basic print level. it will print the details of a failed test.
20                         //2 will print if a test passes and cleans. 
21 void set_print_level(int new_level)
22 {
23         PRINT_LEVEL=new_level;
24 }
25
26 void set_exit_on_error(int num)
27 {
28         EXIT_ON_ERROR=num;
29 }
30
31 int get_exit_on_error(void)
32 {
33         return EXIT_ON_ERROR;
34 }
35
36
37 int EROFS_setup(void)
38 {
39         int output= -1;
40         
41         output=yaffs_remount(YAFFS_MOUNT_POINT,1,1);
42         if (output<0){
43                 print_message("failed to remount yaffs\n",2);
44                 return -1;
45         }
46         return 1;
47 }
48
49 int EROFS_clean(void)
50 {
51         int output=-1;
52         output= yaffs_remount(YAFFS_MOUNT_POINT,1,0);
53         if (output<0){
54                 print_message("failed to remount yaffs\n",2);
55                 return -1;
56         }
57         return 1;
58 }
59
60
61 int set_up_ELOOP(void){
62         int output1=1;
63         int output2=1;
64         int error =0;
65         
66         output1=yaffs_symlink(ELOOP_PATH,ELOOP2_PATH);
67         if (output1 <0){
68                 error=yaffs_get_error();
69                 if (abs(error)==EEXIST){
70                         output1= 1;
71                 } else {
72                         output1=-1;
73                 }
74         }
75         
76         output2=yaffs_symlink(ELOOP2_PATH,ELOOP_PATH);
77
78         if (output2 <0){
79                 error=yaffs_get_error();
80                 if (abs(error)==EEXIST){
81                         output2= 1;
82                 } else {
83                         output2=-1;
84                 }
85         }
86         
87
88         yaffs_set_error(0);     /*reset the last error to 0 */
89         return (output1|output2);
90 }
91
92 void join_paths(char *path1,char *path2,char *new_path )
93 {
94
95         //strcat(new_path,path1);       /*since all functions have this then pull it out*/
96         if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
97                 /*paths are compatiable. concatanate them. note -2 is because of \0*/  
98                 strcat(new_path,path1);
99                 strcat(new_path,path2);         
100                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
101                 //strcpy(new_path,strcat(path1,path2)); 
102                 //return new_path;
103         } else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
104                 /*paths are compatiable. concatanate them*/  
105                 strcat(new_path,path1);
106                 strcat(new_path,path2);         
107
108         } else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
109                         /*need to add a "/". */  
110                 strcat(new_path,path1);
111                 strcat(new_path,"/");
112                 strcat(new_path,path2);
113
114         } else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
115                 /*need to remove a "/". */
116                 /*yaffs does not mind the extra slash. */               
117                 strcat(new_path,path1);
118                 strcat(new_path,path2);
119
120         } else {
121                 //error 
122                 //return -1;
123         }
124 }
125
126 void print_message(char *message,char print_level)
127 {
128         if (print_level <= PRINT_LEVEL){
129                 printf("%s",message);
130         }
131 }
132
133 /*same as forcing the rmdir of a directory*/
134 int delete_dir(char *dir_name)
135 {
136         char message[200];
137         yaffs_DIR *d;
138         struct yaffs_dirent *de;
139         struct yaffs_stat s;
140         char str[100];
141         d = yaffs_opendir(dir_name);
142         //printf("%s\n",dir_name);
143         if(!d)
144         {
145                 sprintf(message,"\nfailed to open dir %s \n was trying to delete this",dir_name);
146                 print_message(message,1);
147
148                 
149                 get_error();
150                 return -1;
151     }
152     
153     while((de = yaffs_readdir(d)) != NULL) {
154                 //stats the file
155                 sprintf(str,"%s/%s",dir_name,de->d_name);
156                 yaffs_lstat(str,&s);
157                 
158                 if ((s.st_mode & S_IFMT) == S_IFDIR){
159                         //it is a directory. call the function recursivly.
160                         delete_dir(str);
161                         yaffs_rmdir(str);
162                 }else{
163                         yaffs_unlink(str);
164                 }
165         }
166         yaffs_rmdir(dir_name);
167         yaffs_closedir(d);
168         return 1;
169 }
170
171 void get_error(void)
172 {
173         int error_code=0;
174         char message[30];
175         message[0]='\0';
176
177         error_code=yaffs_get_error();
178         sprintf(message,"yaffs_error code %d\n",error_code);
179         print_message(message,1);
180         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
181         print_message(message,1);
182 }
183