lwext4_server: fix travis build
[lwext4.git] / fs_test / lwext4_server.c
1 /*
2  * Copyright (c) 2014 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 <unistd.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <getopt.h>
36 #include <time.h>
37 #include <inttypes.h>
38 #include <sys/time.h>
39
40 #ifdef WIN32
41 #include <winsock2.h>
42 #include <ws2tcpip.h>
43 #include <windows.h>
44 #else
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/types.h>
49 #endif
50
51 #include <ext4.h>
52 #include "../blockdev/linux/ext4_filedev.h"
53 #include "../blockdev/windows/io_raw.h"
54
55
56 static int winsock_init(void);
57 static void winsock_fini(void);
58 static char *entry_to_str(uint8_t type);
59
60 #define MAX_FILES 64
61 #define MAX_DIRS 64
62
63 #define MAX_RW_BUFFER (1024 * 1024)
64 #define RW_BUFFER_PATERN ('x')
65
66 /**@brief   Default connection port*/
67 static int connection_port = 1234;
68
69 /**@brief   Default filesystem filename.*/
70 static char *ext4_fname = "ext2";
71
72 /**@brief   Verbose mode*/
73 static int verbose = 0;
74
75 /**@brief   Winpart mode*/
76 static int winpart = 0;
77
78 /**@brief   Blockdev handle*/
79 static struct ext4_blockdev *bd;
80
81 static int cache_wb = 0;
82
83 static char read_buffer[MAX_RW_BUFFER];
84 static char write_buffer[MAX_RW_BUFFER];
85
86 static const char *usage = "                                    \n\
87 Welcome in lwext4_server.                                       \n\
88 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
89 Usage:                                                          \n\
90     --image     (-i) - ext2/3/4 image file                      \n\
91     --port      (-p) - server port                              \n\
92     --verbose   (-v) - verbose mode                             \n\
93     --winpart   (-w) - windows_partition mode                   \n\
94     --cache_wb  (-c) - cache writeback_mode                     \n\
95 \n";
96
97 /**@brief   Open file instance descriptor.*/
98 struct lwext4_files {
99         char name[255];
100         ext4_file fd;
101 };
102
103 /**@brief   Open directory instance descriptor.*/
104 struct lwext4_dirs {
105         char name[255];
106         ext4_dir fd;
107 };
108
109 /**@brief   Library call opcode.*/
110 struct lwext4_op_codes {
111         char *func;
112 };
113
114 /**@brief   Library call wraper.*/
115 struct lwext4_call {
116         int (*lwext4_call)(char *p);
117 };
118
119 /**@brief  */
120 static struct lwext4_files file_tab[MAX_FILES];
121
122 /**@brief  */
123 static struct lwext4_dirs dir_tab[MAX_DIRS];
124
125 /**@brief  */
126 static struct lwext4_op_codes op_codes[] = {
127     "device_register",
128     "mount",
129     "umount",
130     "mount_point_stats",
131     "cache_write_back",
132     "fremove",
133     "fopen",
134     "fclose",
135     "fread",
136     "fwrite",
137     "fseek",
138     "ftell",
139     "fsize",
140     "dir_rm",
141     "dir_mk",
142     "dir_open",
143     "dir_close",
144     "dir_entry_get",
145
146     "multi_fcreate",
147     "multi_fwrite",
148     "multi_fread",
149     "multi_fremove",
150     "multi_dcreate",
151     "multi_dremove",
152     "stats_save",
153     "stats_check",
154 };
155
156 int _device_register(char *p);
157 int _mount(char *p);
158 int _umount(char *p);
159 int _mount_point_stats(char *p);
160 int _cache_write_back(char *p);
161 int _fremove(char *p);
162 int _fopen(char *p);
163 int _fclose(char *p);
164 int _fread(char *p);
165 int _fwrite(char *p);
166 int _fseek(char *p);
167 int _ftell(char *p);
168 int _fsize(char *p);
169 int _dir_rm(char *p);
170 int _dir_mk(char *p);
171 int _dir_open(char *p);
172 int _dir_close(char *p);
173 int _dir_close(char *p);
174 int _dir_entry_get(char *p);
175
176 int _multi_fcreate(char *p);
177 int _multi_fwrite(char *p);
178 int _multi_fread(char *p);
179 int _multi_fremove(char *p);
180 int _multi_dcreate(char *p);
181 int _multi_dremove(char *p);
182 int _stats_save(char *p);
183 int _stats_check(char *p);
184
185 /**@brief  */
186 static struct lwext4_call op_call[] = {
187     _device_register,   /*PARAMS(3):   0 cache_mode dev_name   */
188     _mount,             /*PARAMS(2):   dev_name mount_point    */
189     _umount,            /*PARAMS(1):   mount_point             */
190     _mount_point_stats, /*PARAMS(2):   mount_point, 0          */
191     _cache_write_back,  /*PARAMS(2):   mount_point, en         */
192     _fremove,           /*PARAMS(1):   path                    */
193     _fopen,             /*PARAMS(2):   fid path flags          */
194     _fclose,            /*PARAMS(1):   fid                     */
195     _fread,             /*PARAMS(4):   fid 0 len 0             */
196     _fwrite,            /*PARAMS(4):   fid 0 len 0             */
197     _fseek,             /*PARAMS(2):   fid off origin          */
198     _ftell,             /*PARAMS(2):   fid exp                 */
199     _fsize,             /*PARAMS(2):   fid exp                 */
200     _dir_rm,            /*PARAMS(1):   path                    */
201     _dir_mk,            /*PARAMS(1):   path                    */
202     _dir_open,          /*PARAMS(2):   did, path               */
203     _dir_close,         /*PARAMS(1):   did                     */
204     _dir_entry_get,     /*PARAMS(2):   did, exp                */
205
206     _multi_fcreate, /*PARAMS(3):   path prefix cnt         */
207     _multi_fwrite,  /*PARAMS(4):   path prefix cnt size    */
208     _multi_fread,   /*PARAMS(4):   path prefix cnt size    */
209     _multi_fremove, /*PARAMS(2):   path prefix cnt         */
210     _multi_dcreate, /*PARAMS(3):   path prefix cnt         */
211     _multi_dremove, /*PARAMS(2):   path prefix             */
212     _stats_save,    /*PARAMS(1):   path                    */
213     _stats_check,   /*PARAMS(1):   path                    */
214 };
215
216 static clock_t get_ms(void)
217 {
218         struct timeval t;
219         gettimeofday(&t, NULL);
220         return (t.tv_sec * 1000) + (t.tv_usec / 1000);
221 }
222
223 /**@brief  */
224 static int exec_op_code(char *opcode)
225 {
226         int i;
227         int r = -1;
228
229         for (i = 0; i < sizeof(op_codes) / sizeof(op_codes[0]); ++i) {
230
231                 if (strncmp(op_codes[i].func, opcode, strlen(op_codes[i].func)))
232                         continue;
233
234                 if (opcode[strlen(op_codes[i].func)] != ' ')
235                         continue;
236
237                 printf("%s\n", opcode);
238                 opcode += strlen(op_codes[i].func);
239                 /*Call*/
240
241                 clock_t t = get_ms();
242                 r = op_call[i].lwext4_call(opcode);
243
244                 printf("rc: %d, time: %ums\n", r, (unsigned int)(get_ms() - t));
245
246                 break;
247         }
248
249         return r;
250 }
251
252 static int server_open(void)
253 {
254         int fd = 0;
255         struct sockaddr_in serv_addr;
256
257         memset(&serv_addr, 0, sizeof(serv_addr));
258
259         if (winsock_init() < 0) {
260                 printf("winsock_init() error\n");
261                 exit(-1);
262         }
263
264         fd = socket(AF_INET, SOCK_STREAM, 0);
265         if (fd < 0) {
266                 printf("socket() error: %s\n", strerror(errno));
267                 exit(-1);
268         }
269
270         int yes = 1;
271         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes,
272                        sizeof(int))) {
273                 printf("setsockopt() error: %s\n", strerror(errno));
274                 exit(-1);
275         }
276
277         serv_addr.sin_family = AF_INET;
278         serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
279         serv_addr.sin_port = htons(connection_port);
280
281         if (bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
282                 printf("bind() error: %s\n", strerror(errno));
283                 exit(-1);
284         }
285
286         if (listen(fd, 1)) {
287                 printf("listen() error: %s\n", strerror(errno));
288                 exit(-1);
289         }
290
291         return fd;
292 }
293
294 static bool parse_opt(int argc, char **argv)
295 {
296         int option_index = 0;
297         int c;
298
299         static struct option long_options[] = {
300             {"image", required_argument, 0, 'i'},
301             {"port", required_argument, 0, 'p'},
302             {"verbose", required_argument, 0, 'v'},
303             {"winpart", required_argument, 0, 'w'},
304             {"cache_wb", required_argument, 0, 'c'},
305             {0, 0, 0, 0}};
306
307         while (-1 != (c = getopt_long(argc, argv, "c:i:p:v:w:", long_options,
308                                       &option_index))) {
309
310                 switch (c) {
311                 case 'i':
312                         ext4_fname = optarg;
313                         break;
314                 case 'p':
315                         connection_port = atoi(optarg);
316                         break;
317                 case 'v':
318                         verbose = atoi(optarg);
319                         break;
320                 case 'c':
321                         cache_wb = atoi(optarg);
322                         break;
323                 case 'w':
324                         winpart = atoi(optarg);
325                         break;
326                 default:
327                         printf("%s", usage);
328                         return false;
329                 }
330         }
331         return true;
332 }
333
334 int main(int argc, char *argv[])
335 {
336         int n;
337         int listenfd;
338         int connfd;
339         char op_code[128];
340
341         if (!parse_opt(argc, argv))
342                 return -1;
343
344         listenfd = server_open();
345
346         printf("lwext4_server: listening on port: %d\n", connection_port);
347
348         memset(write_buffer, RW_BUFFER_PATERN, MAX_RW_BUFFER);
349         while (1) {
350                 connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
351
352                 n = recv(connfd, op_code, sizeof(op_code), 0);
353
354                 if (n < 0) {
355                         printf("recv() error: %s fd = %d\n", strerror(errno),
356                                connfd);
357                         break;
358                 }
359
360                 op_code[n] = 0;
361
362                 int r = exec_op_code(op_code);
363
364                 n = send(connfd, (void *)&r, sizeof(r), 0);
365                 if (n < 0) {
366                         printf("send() error: %s fd = %d\n", strerror(errno),
367                                connfd);
368                         break;
369                 }
370
371                 close(connfd);
372         }
373
374         winsock_fini();
375         return 0;
376 }
377
378 int _device_register(char *p)
379 {
380         int dev;
381         int cache_mode;
382         char dev_name[32];
383
384         if (sscanf(p, "%d %d %s", &dev, &cache_mode, dev_name) != 3) {
385                 printf("Param list error\n");
386                 return -1;
387         }
388
389 #ifdef WIN32
390         if (winpart) {
391                 ext4_io_raw_filename(ext4_fname);
392                 bd = ext4_io_raw_dev_get();
393
394         } else
395 #endif
396         {
397                 ext4_filedev_filename(ext4_fname);
398                 bd = ext4_filedev_get();
399         }
400         return ext4_device_register(bd, 0, dev_name);
401 }
402
403 int _mount(char *p)
404 {
405         char dev_name[32];
406         char mount_point[32];
407         int rc;
408
409         if (sscanf(p, "%s %s", dev_name, mount_point) != 2) {
410                 printf("Param list error\n");
411                 return -1;
412         }
413
414         rc = ext4_mount(dev_name, mount_point);
415         if (cache_wb)
416                 ext4_cache_write_back(mount_point, 1);
417         return rc;
418 }
419
420 int _umount(char *p)
421 {
422         char mount_point[32];
423
424         if (sscanf(p, "%s", mount_point) != 1) {
425                 printf("Param list error\n");
426                 return -1;
427         }
428
429         if (cache_wb)
430                 ext4_cache_write_back(mount_point, 0);
431
432         return ext4_umount(mount_point);
433 }
434
435 int _mount_point_stats(char *p)
436 {
437         char mount_point[32];
438         int d;
439         int rc;
440         struct ext4_mount_stats stats;
441
442         if (sscanf(p, "%s %d", mount_point, &d) != 2) {
443                 printf("Param list error\n");
444                 return -1;
445         }
446
447         rc = ext4_mount_point_stats(mount_point, &stats);
448
449         if (rc != EOK)
450                 return rc;
451
452         if (verbose) {
453                 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
454                 printf("\tfree_inodes_count = %" PRIu32"\n",
455                                 stats.free_inodes_count);
456                 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
457                 printf("\tfree_blocks_count = %" PRIu64"\n",
458                                 stats.free_blocks_count);
459                 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
460                 printf("\tblock_group_count = %" PRIu32"\n",
461                                 stats.block_group_count);
462                 printf("\tblocks_per_group = %" PRIu32"\n",
463                                 stats.blocks_per_group);
464                 printf("\tinodes_per_group = %" PRIu32"\n",
465                                 stats.inodes_per_group);
466                 printf("\tvolume_name = %s\n", stats.volume_name);
467         }
468
469         return rc;
470 }
471
472 int _cache_write_back(char *p)
473 {
474         char mount_point[32];
475         int en;
476
477         if (sscanf(p, "%s %d", mount_point, &en) != 2) {
478                 printf("Param list error\n");
479                 return -1;
480         }
481
482         return ext4_cache_write_back(mount_point, en);
483 }
484
485 int _fremove(char *p)
486 {
487         char path[255];
488
489         if (sscanf(p, "%s", path) != 1) {
490                 printf("Param list error\n");
491                 return -1;
492         }
493
494         return ext4_fremove(path);
495 }
496
497 int _fopen(char *p)
498 {
499         int fid = MAX_FILES;
500         char path[256];
501         char flags[8];
502         int rc;
503
504         if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
505                 printf("Param list error\n");
506                 return -1;
507         }
508
509         if (!(fid < MAX_FILES)) {
510                 printf("File id too big\n");
511                 return -1;
512         }
513
514         rc = ext4_fopen(&file_tab[fid].fd, path, flags);
515
516         if (rc == EOK)
517                 strcpy(file_tab[fid].name, path);
518
519         return rc;
520 }
521
522 int _fclose(char *p)
523 {
524         int fid = MAX_FILES;
525         int rc;
526
527         if (sscanf(p, "%d", &fid) != 1) {
528                 printf("Param list error\n");
529                 return -1;
530         }
531
532         if (!(fid < MAX_FILES)) {
533                 printf("File id too big\n");
534                 return -1;
535         }
536
537         if (file_tab[fid].name[0] == 0) {
538                 printf("File id empty\n");
539                 return -1;
540         }
541
542         rc = ext4_fclose(&file_tab[fid].fd);
543
544         if (rc == EOK)
545                 file_tab[fid].name[0] = 0;
546
547         return rc;
548 }
549
550 int _fread(char *p)
551 {
552         int fid = MAX_FILES;
553         int len;
554         int d;
555         int rc;
556         size_t rb;
557
558         if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
559                 printf("Param list error\n");
560                 return -1;
561         }
562
563         if (!(fid < MAX_FILES)) {
564                 printf("File id too big\n");
565                 return -1;
566         }
567
568         if (file_tab[fid].name[0] == 0) {
569                 printf("File id empty\n");
570                 return -1;
571         }
572
573         while (len) {
574                 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
575
576                 memset(read_buffer, 0, MAX_RW_BUFFER);
577                 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
578
579                 if (rc != EOK)
580                         break;
581
582                 if (rb != d) {
583                         printf("Read count error\n");
584                         return -1;
585                 }
586
587                 if (memcmp(read_buffer, write_buffer, d)) {
588                         printf("Read compare error\n");
589                         return -1;
590                 }
591
592                 len -= d;
593         }
594
595         return rc;
596 }
597
598 int _fwrite(char *p)
599 {
600         int fid = MAX_FILES;
601         int d;
602         int rc;
603
604         int len;
605         size_t wb;
606
607         if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
608                 printf("Param list error\n");
609                 return -1;
610         }
611
612         if (!(fid < MAX_FILES)) {
613                 printf("File id too big\n");
614                 return -1;
615         }
616
617         if (file_tab[fid].name[0] == 0) {
618                 printf("File id empty\n");
619                 return -1;
620         }
621
622         while (len) {
623                 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
624                 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
625
626                 if (rc != EOK)
627                         break;
628
629                 if (wb != d) {
630                         printf("Write count error\n");
631                         return -1;
632                 }
633
634                 len -= d;
635         }
636
637         return rc;
638 }
639
640 int _fseek(char *p)
641 {
642         int fid = MAX_FILES;
643         int off;
644         int origin;
645
646         if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
647                 printf("Param list error\n");
648                 return -1;
649         }
650
651         if (!(fid < MAX_FILES)) {
652                 printf("File id too big\n");
653                 return -1;
654         }
655
656         if (file_tab[fid].name[0] == 0) {
657                 printf("File id empty\n");
658                 return -1;
659         }
660
661         return ext4_fseek(&file_tab[fid].fd, off, origin);
662 }
663
664 int _ftell(char *p)
665 {
666         int fid = MAX_FILES;
667         uint32_t exp_pos;
668
669         if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
670                 printf("Param list error\n");
671                 return -1;
672         }
673
674         if (!(fid < MAX_FILES)) {
675                 printf("File id too big\n");
676                 return -1;
677         }
678
679         if (file_tab[fid].name[0] == 0) {
680                 printf("File id empty\n");
681                 return -1;
682         }
683
684         if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
685                 printf("Expected filepos error\n");
686                 return -1;
687         }
688
689         return EOK;
690 }
691
692 int _fsize(char *p)
693 {
694         int fid = MAX_FILES;
695         uint32_t exp_size;
696
697         if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
698                 printf("Param list error\n");
699                 return -1;
700         }
701
702         if (!(fid < MAX_FILES)) {
703                 printf("File id too big\n");
704                 return -1;
705         }
706
707         if (file_tab[fid].name[0] == 0) {
708                 printf("File id empty\n");
709                 return -1;
710         }
711
712         if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
713                 printf("Expected filesize error\n");
714                 return -1;
715         }
716
717         return EOK;
718 }
719
720 int _dir_rm(char *p)
721 {
722         char path[255];
723
724         if (sscanf(p, "%s", path) != 1) {
725                 printf("Param list error\n");
726                 return -1;
727         }
728
729         return ext4_dir_rm(path);
730 }
731
732 int _dir_mk(char *p)
733 {
734         char path[255];
735
736         if (sscanf(p, "%s", path) != 1) {
737                 printf("Param list error\n");
738                 return -1;
739         }
740
741         return ext4_dir_mk(path);
742 }
743
744 int _dir_open(char *p)
745 {
746         int did = MAX_DIRS;
747         char path[255];
748         int rc;
749
750         if (sscanf(p, "%d %s", &did, path) != 2) {
751                 printf("Param list error\n");
752                 return -1;
753         }
754
755         if (!(did < MAX_DIRS)) {
756                 printf("Dir id too big\n");
757                 return -1;
758         }
759
760         rc = ext4_dir_open(&dir_tab[did].fd, path);
761
762         if (rc == EOK)
763                 strcpy(dir_tab[did].name, path);
764
765         return rc;
766 }
767
768 int _dir_close(char *p)
769 {
770         int did = MAX_DIRS;
771         int rc;
772
773         if (sscanf(p, "%d", &did) != 1) {
774                 printf("Param list error\n");
775                 return -1;
776         }
777
778         if (!(did < MAX_DIRS)) {
779                 printf("Dir id too big\n");
780                 return -1;
781         }
782
783         if (dir_tab[did].name[0] == 0) {
784                 printf("Dir id empty\n");
785                 return -1;
786         }
787
788         rc = ext4_dir_close(&dir_tab[did].fd);
789
790         if (rc == EOK)
791                 dir_tab[did].name[0] = 0;
792
793         return rc;
794 }
795
796 int _dir_entry_get(char *p)
797 {
798         int did = MAX_DIRS;
799         int exp;
800         char name[256];
801
802         if (sscanf(p, "%d %d", &did, &exp) != 2) {
803                 printf("Param list error\n");
804                 return -1;
805         }
806
807         if (!(did < MAX_DIRS)) {
808                 printf("Dir id too big\n");
809                 return -1;
810         }
811
812         if (dir_tab[did].name[0] == 0) {
813                 printf("Dir id empty\n");
814                 return -1;
815         }
816
817         int idx = 0;
818         const ext4_direntry *d;
819
820         while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
821
822                 idx++;
823                 memcpy(name, d->name, d->name_length);
824                 name[d->name_length] = 0;
825                 if (verbose) {
826                         printf("\t%s %s\n", entry_to_str(d->inode_type), name);
827                 }
828         }
829
830         if (idx < 2) {
831                 printf("Minumum dir entry error\n");
832                 return -1;
833         }
834
835         if ((idx - 2) != exp) {
836                 printf("Expected dir entry error\n");
837                 return -1;
838         }
839
840         return EOK;
841 }
842
843 int _multi_fcreate(char *p)
844 {
845         char path[256];
846         char path1[256];
847         char prefix[32];
848         int cnt;
849         int rc;
850         int i;
851         ext4_file fd;
852
853         if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
854                 printf("Param list error\n");
855                 return -1;
856         }
857
858         for (i = 0; i < cnt; ++i) {
859                 sprintf(path1, "%s%s%d", path, prefix, i);
860                 rc = ext4_fopen(&fd, path1, "wb+");
861
862                 if (rc != EOK)
863                         break;
864         }
865
866         return rc;
867 }
868
869 int _multi_fwrite(char *p)
870 {
871         char path[256];
872         char path1[256];
873         char prefix[32];
874         int cnt, i;
875         int len, ll;
876         int rc;
877         size_t d, wb;
878         ext4_file fd;
879
880         if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
881                 printf("Param list error\n");
882                 return -1;
883         }
884
885         for (i = 0; i < cnt; ++i) {
886                 sprintf(path1, "%s%s%d", path, prefix, i);
887                 rc = ext4_fopen(&fd, path1, "rb+");
888
889                 if (rc != EOK)
890                         break;
891
892                 len = ll;
893                 while (len) {
894                         d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
895                         rc = ext4_fwrite(&fd, write_buffer, d, &wb);
896
897                         if (rc != EOK)
898                                 break;
899
900                         if (wb != d) {
901                                 printf("Write count error\n");
902                                 return -1;
903                         }
904
905                         len -= d;
906                 }
907         }
908
909         return rc;
910 }
911
912 int _multi_fread(char *p)
913 {
914         char path[256];
915         char path1[256];
916         char prefix[32];
917         int cnt;
918         int len, ll;
919         int rc ,i, d;
920         size_t rb;
921         ext4_file fd;
922
923         if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
924                 printf("Param list error\n");
925                 return -1;
926         }
927
928         for (i = 0; i < cnt; ++i) {
929                 sprintf(path1, "%s%s%d", path, prefix, i);
930                 rc = ext4_fopen(&fd, path1, "rb+");
931
932                 if (rc != EOK)
933                         break;
934
935                 len = ll;
936                 while (len) {
937                         d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
938
939                         memset(read_buffer, 0, MAX_RW_BUFFER);
940                         rc = ext4_fread(&fd, read_buffer, d, &rb);
941
942                         if (rc != EOK)
943                                 break;
944
945                         if (rb != d) {
946                                 printf("Read count error\n");
947                                 return -1;
948                         }
949
950                         if (memcmp(read_buffer, write_buffer, d)) {
951                                 printf("Read compare error\n");
952                                 return -1;
953                         }
954
955                         len -= d;
956                 }
957         }
958
959         return rc;
960 }
961
962 int _multi_fremove(char *p)
963 {
964         char path[256];
965         char path1[256];
966         char prefix[32];
967         int cnt, i, rc;
968
969         if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
970                 printf("Param list error\n");
971                 return -1;
972         }
973
974         for (i = 0; i < cnt; ++i) {
975                 sprintf(path1, "%s%s%d", path, prefix, i);
976                 rc = ext4_fremove(path1);
977                 if (rc != EOK)
978                         break;
979         }
980
981         return rc;
982 }
983
984 int _multi_dcreate(char *p)
985 {
986         char path[256];
987         char path1[256];
988         char prefix[32];
989         int cnt, i, rc;
990
991         if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
992                 printf("Param list error\n");
993                 return -1;
994         }
995
996         for (i = 0; i < cnt; ++i) {
997                 sprintf(path1, "%s%s%d", path, prefix, i);
998                 rc = ext4_dir_mk(path1);
999                 if (rc != EOK)
1000                         break;
1001         }
1002
1003         return rc;
1004 }
1005
1006 int _multi_dremove(char *p)
1007 {
1008         char path[256];
1009         char path1[256];
1010         char prefix[32];
1011         int cnt, i, rc;
1012
1013         if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1014                 printf("Param list error\n");
1015                 return -1;
1016         }
1017
1018         for (i = 0; i < cnt; ++i) {
1019                 sprintf(path1, "%s%s%d", path, prefix, i);
1020                 rc = ext4_dir_rm(path1);
1021                 if (rc != EOK)
1022                         break;
1023         }
1024
1025         return rc;
1026 }
1027
1028 struct ext4_mount_stats saved_stats;
1029
1030 int _stats_save(char *p)
1031 {
1032         char path[256];
1033
1034         if (sscanf(p, "%s", path) != 1) {
1035                 printf("Param list error\n");
1036                 return -1;
1037         }
1038
1039         return ext4_mount_point_stats(path, &saved_stats);
1040 }
1041
1042 int _stats_check(char *p)
1043 {
1044         char path[256];
1045         int rc;
1046
1047         struct ext4_mount_stats actual_stats;
1048
1049         if (sscanf(p, "%s", path) != 1) {
1050                 printf("Param list error\n");
1051                 return -1;
1052         }
1053
1054         rc = ext4_mount_point_stats(path, &actual_stats);
1055
1056         if (rc != EOK)
1057                 return rc;
1058
1059         if (memcmp(&saved_stats, &actual_stats,
1060                    sizeof(struct ext4_mount_stats))) {
1061                 if (verbose) {
1062                         printf("\tMount point stats error:\n");
1063                         printf("\tsaved_stats:\n");
1064                         printf("\tinodes_count = %" PRIu32"\n",
1065                                saved_stats.inodes_count);
1066                         printf("\tfree_inodes_count = %" PRIu32"\n",
1067                                saved_stats.free_inodes_count);
1068                         printf("\tblocks_count = %" PRIu64"\n",
1069                                saved_stats.blocks_count);
1070                         printf("\tfree_blocks_count = %" PRIu64"\n",
1071                                saved_stats.free_blocks_count);
1072                         printf("\tblock_size = %" PRIu32"\n",
1073                                         saved_stats.block_size);
1074                         printf("\tblock_group_count = %" PRIu32"\n",
1075                                saved_stats.block_group_count);
1076                         printf("\tblocks_per_group = %" PRIu32"\n",
1077                                saved_stats.blocks_per_group);
1078                         printf("\tinodes_per_group = %" PRIu32"\n",
1079                                saved_stats.inodes_per_group);
1080                         printf("\tvolume_name = %s\n", saved_stats.volume_name);
1081                         printf("\tactual_stats:\n");
1082                         printf("\tinodes_count = %" PRIu32"\n",
1083                                actual_stats.inodes_count);
1084                         printf("\tfree_inodes_count = %" PRIu32"\n",
1085                                actual_stats.free_inodes_count);
1086                         printf("\tblocks_count = %" PRIu64"\n",
1087                                actual_stats.blocks_count);
1088                         printf("\tfree_blocks_count = %" PRIu64"\n",
1089                                actual_stats.free_blocks_count);
1090                         printf("\tblock_size = %d\n", actual_stats.block_size);
1091                         printf("\tblock_group_count = %" PRIu32"\n",
1092                                actual_stats.block_group_count);
1093                         printf("\tblocks_per_group = %" PRIu32"\n",
1094                                actual_stats.blocks_per_group);
1095                         printf("\tinodes_per_group = %" PRIu32"\n",
1096                                actual_stats.inodes_per_group);
1097                         printf("\tvolume_name = %s\n",
1098                                actual_stats.volume_name);
1099                 }
1100                 return -1;
1101         }
1102
1103         return rc;
1104 }
1105
1106 static char *entry_to_str(uint8_t type)
1107 {
1108         switch (type) {
1109         case EXT4_DE_UNKNOWN:
1110                 return "[unk] ";
1111         case EXT4_DE_REG_FILE:
1112                 return "[fil] ";
1113         case EXT4_DE_DIR:
1114                 return "[dir] ";
1115         case EXT4_DE_CHRDEV:
1116                 return "[cha] ";
1117         case EXT4_DE_BLKDEV:
1118                 return "[blk] ";
1119         case EXT4_DE_FIFO:
1120                 return "[fif] ";
1121         case EXT4_DE_SOCK:
1122                 return "[soc] ";
1123         case EXT4_DE_SYMLINK:
1124                 return "[sym] ";
1125         default:
1126                 break;
1127         }
1128         return "[???]";
1129 }
1130
1131 static int winsock_init(void)
1132 {
1133 #if WIN32
1134         int rc;
1135         static WSADATA wsaData;
1136         rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1137         if (rc != 0) {
1138                 return -1;
1139         }
1140 #endif
1141         return 0;
1142 }
1143
1144 static void winsock_fini(void)
1145 {
1146 #if WIN32
1147         WSACleanup();
1148 #endif
1149 }