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