Fix "const const" warning
[lwext4.git] / fs_test / lwext4_generic.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <stdbool.h>
35 #include <inttypes.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sys/time.h>
39
40 #include <ext4.h>
41 #include "../blockdev/linux/file_dev.h"
42 #include "../blockdev/windows/file_windows.h"
43 #include "common/test_lwext4.h"
44
45 #ifdef WIN32
46 #include <windows.h>
47 #endif
48
49 /**@brief   Input stream name.*/
50 char input_name[128] = "ext_images/ext2";
51
52 /**@brief   Read-write size*/
53 static int rw_szie = 1024 * 1024;
54
55 /**@brief   Read-write size*/
56 static int rw_count = 10;
57
58 /**@brief   Directory test count*/
59 static int dir_cnt = 0;
60
61 /**@brief   Cleanup after test.*/
62 static bool cleanup_flag = false;
63
64 /**@brief   Block device stats.*/
65 static bool bstat = false;
66
67 /**@brief   Superblock stats.*/
68 static bool sbstat = false;
69
70 /**@brief   Indicates that input is windows partition.*/
71 static bool winpart = false;
72
73 /**@brief   Verbose mode*/
74 static bool verbose = 0;
75
76 /**@brief   Block device handle.*/
77 static struct ext4_blockdev *bd;
78
79 /**@brief   Block cache handle.*/
80 static struct ext4_bcache *bc;
81
82 static const char *usage = "                                    \n\
83 Welcome in ext4 generic demo.                                   \n\
84 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
85 Usage:                                                          \n\
86 [-i] --input    - input file         (default = ext2)           \n\
87 [-w] --rw_size  - single R/W size    (default = 1024 * 1024)    \n\
88 [-c] --rw_count - R/W count          (default = 10)             \n\
89 [-d] --dirs   - directory test count (default = 0)              \n\
90 [-l] --clean  - clean up after test                             \n\
91 [-b] --bstat  - block device stats                              \n\
92 [-t] --sbstat - superblock stats                                \n\
93 [-w] --wpart  - windows partition mode                          \n\
94 \n";
95
96 void io_timings_clear(void)
97 {
98 }
99
100 const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
101 {
102         return NULL;
103 }
104
105 uint32_t tim_get_ms(void)
106 {
107         struct timeval t;
108         gettimeofday(&t, NULL);
109         return (t.tv_sec * 1000) + (t.tv_usec / 1000);
110 }
111
112 uint64_t tim_get_us(void)
113 {
114         struct timeval t;
115         gettimeofday(&t, NULL);
116         return (t.tv_sec * 1000000) + (t.tv_usec);
117 }
118
119 static bool open_linux(void)
120 {
121         file_dev_name_set(input_name);
122         bd = file_dev_get();
123         if (!bd) {
124                 printf("open_filedev: fail\n");
125                 return false;
126         }
127         return true;
128 }
129
130 static bool open_windows(void)
131 {
132 #ifdef WIN32
133         file_windows_name_set(input_name);
134         bd = file_windows_dev_get();
135         if (!bd) {
136                 printf("open_winpartition: fail\n");
137                 return false;
138         }
139         return true;
140 #else
141         printf("open_winpartition: this mode should be used only under windows "
142                "!\n");
143         return false;
144 #endif
145 }
146
147 static bool open_filedev(void)
148 {
149         return winpart ? open_windows() : open_linux();
150 }
151
152 static bool parse_opt(int argc, char **argv)
153 {
154         int option_index = 0;
155         int c;
156
157         static struct option long_options[] = {
158             {"input", required_argument, 0, 'i'},
159             {"rw_size", required_argument, 0, 's'},
160             {"rw_count", required_argument, 0, 'c'},
161             {"dirs", required_argument, 0, 'd'},
162             {"clean", no_argument, 0, 'l'},
163             {"bstat", no_argument, 0, 'b'},
164             {"sbstat", no_argument, 0, 't'},
165             {"wpart", no_argument, 0, 'w'},
166             {"verbose", no_argument, 0, 'v'},
167             {"version", no_argument, 0, 'x'},
168             {0, 0, 0, 0}};
169
170         while (-1 != (c = getopt_long(argc, argv, "i:s:c:q:d:lbtwvx",
171                                       long_options, &option_index))) {
172
173                 switch (c) {
174                 case 'i':
175                         strcpy(input_name, optarg);
176                         break;
177                 case 's':
178                         rw_szie = atoi(optarg);
179                         break;
180                 case 'c':
181                         rw_count = atoi(optarg);
182                         break;
183                 case 'd':
184                         dir_cnt = atoi(optarg);
185                         break;
186                 case 'l':
187                         cleanup_flag = true;
188                         break;
189                 case 'b':
190                         bstat = true;
191                         break;
192                 case 't':
193                         sbstat = true;
194                         break;
195                 case 'w':
196                         winpart = true;
197                         break;
198                 case 'v':
199                         verbose = true;
200                         break;
201                 case 'x':
202                         puts(VERSION);
203                         exit(0);
204                         break;
205                 default:
206                         printf("%s", usage);
207                         return false;
208                 }
209         }
210         return true;
211 }
212
213 int main(int argc, char **argv)
214 {
215         if (!parse_opt(argc, argv))
216                 return EXIT_FAILURE;
217
218         printf("ext4_generic\n");
219         printf("test conditions:\n");
220         printf("\timput name: %s\n", input_name);
221         printf("\trw size: %d\n", rw_szie);
222         printf("\trw count: %d\n", rw_count);
223
224         if (!open_filedev()) {
225                 printf("open_filedev error\n");
226                 return EXIT_FAILURE;
227         }
228
229         if (verbose)
230                 ext4_dmask_set(DEBUG_ALL);
231
232         if (!test_lwext4_mount(bd, bc))
233                 return EXIT_FAILURE;
234
235         test_lwext4_cleanup();
236
237         if (sbstat)
238                 test_lwext4_mp_stats();
239
240         test_lwext4_dir_ls("/mp/");
241         fflush(stdout);
242         if (!test_lwext4_dir_test(dir_cnt))
243                 return EXIT_FAILURE;
244
245         fflush(stdout);
246         uint8_t *rw_buff = malloc(rw_szie);
247         if (!rw_buff) {
248                 free(rw_buff);
249                 return EXIT_FAILURE;
250         }
251         if (!test_lwext4_file_test(rw_buff, rw_szie, rw_count)) {
252                 free(rw_buff);
253                 return EXIT_FAILURE;
254         }
255
256         free(rw_buff);
257
258         fflush(stdout);
259         test_lwext4_dir_ls("/mp/");
260
261         if (sbstat)
262                 test_lwext4_mp_stats();
263
264         if (cleanup_flag)
265                 test_lwext4_cleanup();
266
267         if (bstat)
268                 test_lwext4_block_stats();
269
270         if (!test_lwext4_umount())
271                 return EXIT_FAILURE;
272
273         printf("\ntest finished\n");
274         return EXIT_SUCCESS;
275 }