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