forked from xiaolu/philz_touch_cwm6_cn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_functions.c
3802 lines (3402 loc) · 131 KB
/
advanced_functions.c
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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/limits.h>
#include <dirent.h>
#include <sys/stat.h>
// statfs
#include <sys/vfs.h>
#include <signal.h>
#include <sys/wait.h>
#include "bootloader.h"
#include "common.h"
#include "cutils/properties.h"
#include "firmware.h"
#include "install.h"
#include "make_ext4fs.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "roots.h"
#include "recovery_ui.h"
#include "extendedcommands.h"
#include "advanced_functions.h"
#include "recovery_settings.h"
#include "nandroid.h"
#include "mounts.h"
#include "flashutils/flashutils.h"
#include "edify/expr.h"
#include <libgen.h>
#include "mtdutils/mtdutils.h"
#include "bmlutils/bmlutils.h"
#include "cutils/android_reboot.h"
#include "mmcutils/mmcutils.h"
#include "voldclient/voldclient.h"
#include "adb_install.h"
// md5 display
#include <pthread.h>
#include "digest/md5.h"
#ifdef PHILZ_TOUCH_RECOVERY
#include "libtouch_gui/gui_settings.h"
#endif
/*****************************************/
/* DO NOT REMOVE THIS CREDITS HEARDER */
/* IF YOU MODIFY ANY PART OF THIS SOURCE */
/* YOU MUST AGREE TO SHARE THE CHANGES */
/* */
/* Start PhilZ Menu settings */
/* Code written by PhilZ@xda */
/* Part of PhilZ Touch Recovery */
/*****************************************/
// ignore_android_secure = 1: this will force skipping android secure from backup/restore jobs
static int ignore_android_secure = 0;
// time using gettimeofday()
// to get time in usec, we call timenow_usec() which will link here if clock_gettime fails
static long long gettime_usec() {
struct timeval now;
long long useconds;
gettimeofday(&now, NULL);
useconds = (long long)(now.tv_sec) * 1000000LL;
useconds += (long long)now.tv_usec;
return useconds;
}
// use clock_gettime for elapsed time
// this is nsec precise + less prone to issues for elapsed time
// unsigned integers cannot be negative (overflow): set error return code to 0 (1.1.1970 00:00)
unsigned long long gettime_nsec() {
struct timespec ts;
static int err = 0;
if (err) return 0;
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
LOGI("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
++err;
return 0;
}
unsigned long long nseconds = (unsigned long long)(ts.tv_sec) * 1000000000ULL;
nseconds += (unsigned long long)(ts.tv_nsec);
return nseconds;
}
// returns the current time in usec:
long long timenow_usec() {
// try clock_gettime
unsigned long long nseconds;
nseconds = gettime_nsec();
if (nseconds == 0) {
// LOGI("dropping to gettimeofday()\n");
return gettime_usec();
}
return (long long)(nseconds / 1000ULL);
}
// returns the current time in msec:
long long timenow_msec() {
// first try using clock_gettime
unsigned long long nseconds;
nseconds = gettime_nsec();
if (nseconds == 0) {
// LOGI("dropping to gettimeofday()\n");
return (gettime_usec() / 1000LL);
}
return (long long)(nseconds / 1000000ULL);
}
static long long interval_passed_t_timer = 0;
int is_time_interval_passed(long long msec_interval) {
long long t = timenow_msec();
if (msec_interval != 0 && t - interval_passed_t_timer < msec_interval)
return 0;
interval_passed_t_timer = t;
return 1;
}
//start print tail from custom log file
void ui_print_custom_logtail(const char* filename, int nb_lines) {
char * backup_log;
char tmp[PATH_MAX];
FILE * f;
int line=0;
sprintf(tmp, "tail -n %d %s > /tmp/custom_tail.log", nb_lines, filename);
__system(tmp);
f = fopen("/tmp/custom_tail.log", "rb");
if (f != NULL) {
while (line < nb_lines) {
backup_log = fgets(tmp, PATH_MAX, f);
if (backup_log == NULL) break;
ui_print("%s", tmp);
line++;
}
fclose(f);
} else
LOGE("Cannot open /tmp/custom_tail.log\n");
}
// basename and dirname implementation that is thread safe, no free and doesn't modify argument
// it is extracted from NDK and modified dirname_r to never modify passed argument
int BaseName_r(const char* path, char* buffer, size_t bufflen) {
const char *endp, *startp;
int len, result;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
startp = ".";
len = 1;
goto Exit;
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/') {
endp--;
}
/* All slashes becomes "/" */
if (endp == path && *endp == '/') {
startp = "/";
len = 1;
goto Exit;
}
/* Find the start of the base */
startp = endp;
while (startp > path && *(startp - 1) != '/') {
startp--;
}
len = endp - startp +1;
Exit:
result = len;
if (buffer == NULL) {
return result;
}
if (len > (int)bufflen-1) {
len = (int)bufflen-1;
result = -1;
errno = ERANGE;
}
if (len >= 0) {
memcpy( buffer, startp, len );
buffer[len] = 0;
}
return result;
}
char* BaseName(const char* path) {
static char* bname = NULL;
int ret;
if (bname == NULL) {
bname = (char *)malloc(PATH_MAX);
if (bname == NULL)
return(NULL);
}
ret = BaseName_r(path, bname, PATH_MAX);
return (ret < 0) ? NULL : bname;
}
int DirName_r(const char* path, char* buffer, size_t bufflen) {
const char *endp, *startp;
int result, len;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
startp = ".";
len = 1;
goto Exit;
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/') {
endp--;
}
/* Find the start of the dir */
while (endp > path && *endp != '/') {
endp--;
}
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
startp = (*endp == '/') ? "/" : ".";
len = 1;
goto Exit;
}
do {
endp--;
} while (endp > path && *endp == '/');
startp = path;
len = endp - startp +1;
Exit:
result = len;
if (len+1 > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
if (buffer == NULL)
return result;
if (len > (int)bufflen-1) {
len = (int)bufflen-1;
result = -1;
errno = ERANGE;
}
if (len >= 0) {
memcpy( buffer, startp, len );
buffer[len] = 0;
}
return result;
}
char* DirName(const char* path) {
static char* bname = NULL;
int ret;
if (bname == NULL) {
bname = (char *)malloc(PATH_MAX);
if (bname == NULL)
return(NULL);
}
ret = DirName_r(path, bname, PATH_MAX);
return (ret < 0) ? NULL : bname;
}
// thread safe dirname (free by caller)
char* t_DirName(const char* path) {
int ret;
char* bname = (char *)malloc(PATH_MAX);
if (bname == NULL) {
LOGE("t_DirName: memory error\n");
return NULL;
}
ret = DirName_r(path, bname, PATH_MAX);
if (ret < 0) {
LOGE("t_DirName: error\n");
return NULL;
}
return bname;
}
// thread safe basename (free by caller)
char* t_BaseName(const char* path) {
int ret;
char* bname = (char *)malloc(PATH_MAX);
if (bname == NULL) {
LOGE("t_BaseName: memory error\n");
return NULL;
}
ret = BaseName_r(path, bname, PATH_MAX);
if (ret < 0) {
LOGE("t_BaseName: error\n");
return NULL;
}
return bname;
}
// case insensitive C-string compare (adapted from titanic-fanatic)
int strcmpi(const char *str1, const char *str2) {
int i = 0;
int ret = 0;
while (ret == 0 && str1[i] && str2[i]) {
ret = tolower(str1[i]) - tolower(str2[i]);
++i;
}
return ret;
}
// delete a file
void delete_a_file(const char* filename) {
ensure_path_mounted(filename);
remove(filename);
}
// check if file or folder exists
int file_found(const char* filename) {
struct stat s;
if (strncmp(filename, "/sbin/", 6) != 0 && strncmp(filename, "/res/", 5) != 0 && strncmp(filename, "/tmp/", 5) != 0) {
// do not try to mount ramdisk, else it will error "unknown volume for path..."
ensure_path_mounted(filename);
}
if (0 == stat(filename, &s))
return 1;
return 0;
}
// check directory exists
int directory_found(const char* dir) {
struct stat s;
ensure_path_mounted(dir);
lstat(dir, &s);
if (S_ISDIR(s.st_mode))
return 1;
return 0;
}
// check if path is in ramdisk since volume_for_path() will be NULL on these
int is_path_ramdisk(const char* path) {
const char *ramdisk_dirs[] = { "/sbin/", "/res/", "/tmp/", NULL };
int i = 0;
while (ramdisk_dirs[i] != NULL) {
if (strncmp(path, ramdisk_dirs[i], strlen(ramdisk_dirs[i])) == 0)
return 1;
i++;
}
return 0;
}
// copy file (ramdisk check compatible)
int copy_a_file(const char* file_in, const char* file_out) {
if (strcmp(file_in, file_out) == 0) {
LOGI("source and destination files are same, skipping copy.\n");
return 0;
}
if (!is_path_ramdisk(file_in) && ensure_path_mounted(file_in) != 0) {
LOGE("copy: cannot mount volume %s\n", file_in);
return -1;
}
if (!is_path_ramdisk(file_out) && ensure_path_mounted(file_out) != 0) {
LOGE("copy: cannot mount volume %s\n", file_out);
return -1;
}
// this will chmod folder to 775
char tmp[PATH_MAX];
sprintf(tmp, "%s", DirName(file_out));
ensure_directory(tmp);
FILE *fp = fopen(file_in, "rb");
if (fp == NULL) {
LOGE("copy: source file not found (%s)\n", file_in);
return -1;
}
FILE *fp_out = fopen(file_out, "wb");
if (fp_out == NULL) {
LOGE("copy: failed to create destination file %s\n", file_out);
fclose(fp);
return -1;
}
// start copy
size_t size;
// unsigned int size;
while ((size = fread(tmp, 1, sizeof(tmp), fp))) {
fwrite(tmp, 1, size, fp_out);
}
fclose(fp);
fclose(fp_out);
return 0;
}
// append a string to text file, create the directories and file if it doesn't exist
int append_string_to_file(const char* filename, const char* string) {
char tmp[PATH_MAX];
int ret = -1;
ensure_path_mounted(filename);
mkdir(DirName(filename), S_IRWXU | S_IRWXG | S_IRWXO);
FILE *file = fopen(filename, "a");
if (file != NULL) {
ret = fprintf(file, "%s", string);
fclose(file);
} else
LOGE("Cannot append to %s\n", filename);
return ret;
}
// get file size (by Dees_Troy - TWRP)
unsigned long Get_File_Size(const char* Path) {
struct stat st;
if (stat(Path, &st) != 0)
return 0;
return st.st_size;
}
// get partition size info (adapted from Dees_Troy - TWRP)
unsigned long long Total_Size = 0; // Overall size of the partition
unsigned long long Used_Size = 0; // Overall used space
unsigned long long Free_Size = 0; // Overall free space
int Get_Size_Via_statfs(const char* Path) {
struct statfs st;
Volume* volume;
if (is_data_media_volume_path(Path))
volume = volume_for_path("/data");
else
volume = volume_for_path(Path);
if (NULL == volume) {
LOGE("No volume found to get size from '%s'\n", Path);
return -1;
}
if (volume->mount_point == NULL || statfs(volume->mount_point, &st) != 0) {
LOGE("Unable to statfs for size '%s'\n", Path);
return -1;
}
Total_Size = (unsigned long long)(st.f_blocks) * (unsigned long long)(st.f_bsize);
Free_Size = (unsigned long long)(st.f_bfree) * (unsigned long long)(st.f_bsize);
Used_Size = Total_Size - Free_Size;
// LOGI("%s: tot=%llu, use=%llu, free=%llu\n", volume->mount_point, Total_Size, Used_Size, Free_Size); // debug
return 0;
}
// try to resolve link from blk_device to real /dev/block/mmcblk or /dev/block/mtdblock
// free by caller
char* readlink_device_blk(const char* Path)
{
char* mmcblk_from_link = NULL;
Volume* vol;
if (is_data_media_volume_path(Path))
vol = volume_for_path("/data");
else
vol = volume_for_path(Path);
if (vol == NULL)
return NULL;
char buf[1024];
ssize_t len = readlink(vol->blk_device, buf, sizeof(buf)-1);
if (len == -1) {
LOGI("failed to get device mmcblk link '%s'\n", vol->blk_device);
return NULL;
}
buf[len] = '\0';
mmcblk_from_link = strdup(buf);
LOGI("found device mmcblk link: '%s' -> '%s'\n", vol->blk_device, mmcblk_from_link);
return mmcblk_from_link;
}
// alternate method for statfs (emmc, mtd, mtk...)
int Find_Partition_Size(const char* Path) {
char line[512];
char tmpdevice[1024];
FILE* fp;
Volume* volume;
if (is_data_media_volume_path(Path))
volume = volume_for_path("/data");
else
volume = volume_for_path(Path);
if (volume == NULL) {
LOGE("Failed to find partition size '%s'\n", Path);
LOGE(" > invalid volume %s\n", Path);
return -1;
}
// In this case, we'll first get the partitions we care about (with labels)
/*
--> Start by checking if it is an MTK based device (cat /proc/dumchar_info)
Part_Name Size StartAddr Type MapTo
preloader 0x0000000000040000 0x0000000000000000 2 /dev/misc-sd
dsp_bl 0x00000000005c0000 0x0000000000040000 2 /dev/misc-sd
mbr 0x0000000000004000 0x0000000000000000 2 /dev/block/mmcblk0
ebr1 0x0000000000004000 0x0000000000004000 2 /dev/block/mmcblk0p1
pmt 0x0000000000400000 0x0000000000008000 2 /dev/block/mmcblk0
nvram 0x0000000000500000 0x0000000000408000 2 /dev/block/mmcblk0
seccfg 0x0000000000020000 0x0000000000908000 2 /dev/block/mmcblk0
uboot 0x0000000000060000 0x0000000000928000 2 /dev/block/mmcblk0
bootimg 0x0000000000600000 0x0000000000988000 2 /dev/block/mmcblk0
recovery 0x0000000000600000 0x0000000000f88000 2 /dev/block/mmcblk0
sec_ro 0x0000000000600000 0x0000000001588000 2 /dev/block/mmcblk0p2
misc 0x0000000000060000 0x0000000001b88000 2 /dev/block/mmcblk0
logo 0x0000000000300000 0x0000000001be8000 2 /dev/block/mmcblk0
expdb 0x0000000000200000 0x0000000001ee8000 2 /dev/block/mmcblk0
android 0x0000000020100000 0x00000000020e8000 2 /dev/block/mmcblk0p3
cache 0x0000000020100000 0x00000000221e8000 2 /dev/block/mmcblk0p4
usrdata 0x0000000020100000 0x00000000422e8000 2 /dev/block/mmcblk0p5
fat 0x00000000854f8000 0x00000000623e8000 2 /dev/block/mmcblk0p6
bmtpool 0x0000000001500000 0x00000000ff9f00a8 2 /dev/block/mmcblk0
Part_Name:Partition name you should open;
Size:size of partition
StartAddr:Start Address of partition;
Type:Type of partition(MTD=1,EMMC=2)
MapTo:actual device you operate
*/
fp = fopen("/proc/dumchar_info", "rt");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
char label[32], device[32];
unsigned long size = 0;
sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
// Skip header, annotation and blank lines
if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
continue;
sprintf(tmpdevice, "/dev/");
strcat(tmpdevice, label);
if (volume->blk_device != NULL && strcmp(tmpdevice, volume->blk_device) == 0) {
Total_Size = size;
fclose(fp);
return 0;
}
if (volume->blk_device2 != NULL && strcmp(tmpdevice, volume->blk_device2) == 0) {
Total_Size = size;
fclose(fp);
return 0;
}
}
fclose(fp);
}
/* It is not an MTK device entry:
--> Try mtd / emmc devices (cat /proc/partitions):
major minor #blocks name
179 0 15388672 mmcblk0
179 1 65536 mmcblk0p1
179 2 512 mmcblk0p2
179 3 512 mmcblk0p3
179 4 2048 mmcblk0p4
179 5 512 mmcblk0p5
179 6 22528 mmcblk0p6
179 7 22528 mmcblk0p7
*/
int ret = -1;
fp = fopen("/proc/partitions", "rt");
if (fp != NULL) {
// try to read blk_device link target for devices not using /dev/block/xxx in recovery.fstab
char* mmcblk_from_link = readlink_device_blk(Path);
while (fgets(line, sizeof(line), fp) != NULL) {
unsigned long major, minor, blocks;
char device[512];
if (strlen(line) < 7 || line[0] == 'm')
continue;
sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
sprintf(tmpdevice, "/dev/block/");
strcat(tmpdevice, device);
if (volume->blk_device != NULL && strcmp(tmpdevice, volume->blk_device) == 0) {
Total_Size = blocks * 1024ULL;
//LOGI("%s(%s)=%llu\n", Path, volume->blk_device, Total_Size); // debug
ret = 0;
} else if (volume->blk_device2 != NULL && strcmp(tmpdevice, volume->blk_device2) == 0) {
Total_Size = blocks * 1024ULL;
ret = 0;
} else if (mmcblk_from_link != NULL && strcmp(tmpdevice, mmcblk_from_link) == 0) {
// get size from blk_device symlink to /dev/block/xxx
free(mmcblk_from_link);
Total_Size = blocks * 1024ULL;
ret = 0;
}
}
fclose(fp);
}
if (ret != 0)
LOGE("Failed to find partition size '%s'\n", Path);
return ret;
}
//----- End partition size
// get folder size (by Dees_Troy - TWRP)
unsigned long long Get_Folder_Size(const char* Path) {
DIR* d;
struct dirent* de;
struct stat st;
char path2[PATH_MAX]; char filename[PATH_MAX];
unsigned long long dusize = 0;
unsigned long long dutemp = 0;
// Make a copy of path in case the data in the pointer gets overwritten later
strcpy(path2, Path);
d = opendir(path2);
if (d == NULL) {
LOGE("error opening '%s'\n", path2);
LOGE("error: %s\n", strerror(errno));
return 0;
}
while ((de = readdir(d)) != NULL) {
if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) {
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
dutemp = Get_Folder_Size(filename);
dusize += dutemp;
dutemp = 0;
} else if (de->d_type == DT_REG) {
strcpy(filename, path2);
strcat(filename, "/");
strcat(filename, de->d_name);
stat(filename, &st);
dusize += (unsigned long long)(st.st_size);
}
}
closedir(d);
return dusize;
}
char* read_file_to_buffer(const char* filepath, unsigned long *len) {
if (!file_found(filepath)) {
LOGE("read_file_to_buffer: '%s' not found\n", filepath);
return NULL;
}
unsigned long size = Get_File_Size(filepath);
char* buffer = (char*)malloc(size + 1);
if (buffer == NULL) {
LOGE("read_file_to_buffer: memory error\n");
return NULL;
}
FILE *file = fopen(filepath, "rb");
if (file == NULL) {
LOGE("read_file_to_buffer: can't open '%s'\n", filepath);
free(buffer);
return NULL;
}
*len = fread(buffer, 1, size, file);
if (size != *len) {
LOGE("read_file_to_buffer: read error\n");
free(buffer);
fclose(file);
return NULL;
}
fclose(file);
return buffer;
}
/**********************************/
/* Start md5sum display */
/* Original source by PhilZ */
/* MD5 code from twrpDigest */
/* by */
/* bigbiff/Dees_Troy TeamWin */
/**********************************/
static int cancel_md5digest = 0;
static int computeMD5(const char* filepath, char *md5sum) {
struct MD5Context md5c;
unsigned char md5sum_array[MD5LENGTH];
unsigned char buf[1024];
char hex[3];
unsigned long size_total;
unsigned long size_progress;
unsigned len;
int i;
if (!file_found(filepath)) {
LOGE("computeMD5: '%s' not found\n", filepath);
return -1;
}
FILE *file = fopen(filepath, "rb");
if (file == NULL) {
LOGE("computeMD5: can't open %s\n", filepath);
return -1;
}
size_total = Get_File_Size(filepath);
size_progress = 0;
is_time_interval_passed(0);
cancel_md5digest = 0;
MD5Init(&md5c);
while (!cancel_md5digest && (len = fread(buf, 1, sizeof(buf), file)) > 0) {
MD5Update(&md5c, buf, len);
size_progress += len;
if (size_total != 0 && is_time_interval_passed(300))
ui_set_progress((float)size_progress / (float)size_total);
}
if (!cancel_md5digest) {
MD5Final(md5sum_array ,&md5c);
strcpy(md5sum, "");
for (i = 0; i < 16; ++i) {
snprintf(hex, 3 ,"%02x", md5sum_array[i]);
strcat(md5sum, hex);
}
}
fclose(file);
return cancel_md5digest;
}
int write_md5digest(const char* filepath, const char* md5file, int append) {
int ret;
char md5sum[PATH_MAX];
ret = computeMD5(filepath, md5sum);
if (ret != 0)
return ret;
if (md5file == NULL) {
ui_print("%s\n", md5sum);
} else {
char* b = t_BaseName(filepath);
strcat(md5sum, " ");
strcat(md5sum, b);
strcat(md5sum, "\n");
free(b);
if (append)
ret = append_string_to_file(md5file, md5sum);
else
ret = write_string_to_file(md5file, md5sum);
}
return ret;
}
int verify_md5digest(const char* filepath, const char* md5file) {
char md5file2[PATH_MAX];
int ret = -1;
if (!file_found(filepath)) {
LOGE("verify_md5digest: '%s' not found\n", filepath);
return ret;
}
if (md5file != NULL) {
sprintf(md5file2, "%s", md5file);
} else {
sprintf(md5file2, "%s.md5", filepath);
}
// read md5 sum from md5file2
unsigned long len = 0;
char* md5read = read_file_to_buffer(md5file2, &len);
if (md5read == NULL)
return ret;
md5read[len] = '\0';
char md5sum[PATH_MAX];
if (0 == (ret = computeMD5(filepath, md5sum))) {
char* b = t_BaseName(filepath);
strcat(md5sum, " ");
strcat(md5sum, b);
strcat(md5sum, "\n");
free(b);
if (strcmp(md5read, md5sum) != 0) {
LOGE("MD5 calc: %s\n", md5sum);
LOGE("Expected: %s\n", md5read);
ret = -1;
}
}
free(md5read);
return ret;
}
pthread_t tmd5_display;
pthread_t tmd5_verify;
static void *md5_display_thread(void *arg) {
char filepath[PATH_MAX];
ui_reset_progress();
ui_show_progress(1, 0);
sprintf(filepath, "%s", (char*)arg);
write_md5digest(filepath, NULL, 0);
ui_reset_progress();
return NULL;
}
static void *md5_verify_thread(void *arg) {
int ret;
char filepath[PATH_MAX];
sprintf(filepath, "%s", (char*)arg);
ui_reset_progress();
ui_show_progress(1, 0);
ret = verify_md5digest(filepath, NULL);
ui_reset_progress();
if (ret < 0) {
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, "red");
#endif
ui_print("MD5 check: error\n");
} else if (ret == 0) {
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, "green");
#endif
ui_print("MD5 check: success\n");
}
return NULL;
}
void start_md5_display_thread(char* filepath) {
// ensure_path_mounted() is not thread safe, we must disable it when starting a thread for md5 checks
// we ensure primary storage is also mounted as it is needed by confirm_install() function
ensure_path_mounted(get_primary_storage_path());
set_ensure_mount_always_true(1);
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, NULL);
#endif
ui_print("Calculating md5sum...\n");
pthread_create(&tmd5_display, NULL, &md5_display_thread, filepath);
}
void stop_md5_display_thread() {
cancel_md5digest = 1;
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(0, NULL);
#endif
if (pthread_kill(tmd5_display, 0) != ESRCH)
ui_print("Cancelling md5sum...\n");
pthread_join(tmd5_display, NULL);
set_ensure_mount_always_true(0);
}
void start_md5_verify_thread(char* filepath) {
// ensure_path_mounted() is not thread safe, we must disable it when starting a thread for md5 checks
// we ensure primary storage is also mounted as it is needed by confirm_install() function
ensure_path_mounted(get_primary_storage_path());
set_ensure_mount_always_true(1);
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(1, NULL);
#endif
ui_print("Verifying md5sum...\n");
pthread_create(&tmd5_verify, NULL, &md5_verify_thread, filepath);
}
void stop_md5_verify_thread() {
cancel_md5digest = 1;
#ifdef PHILZ_TOUCH_RECOVERY
ui_print_preset_colors(0, NULL);
#endif
if (pthread_kill(tmd5_verify, 0) != ESRCH)
ui_print("Cancelling md5 check...\n");
pthread_join(tmd5_verify, NULL);
set_ensure_mount_always_true(0);
}
// ------- End md5sum display
/**********************************/
/* Start file parser */
/* Original source by PhilZ */
/**********************************/
// todo: parse settings file in one pass and make pairs of key:value
// get value of key from a given config file
int read_config_file(const char* config_file, const char *key, char *value, const char *value_def) {
int ret = 0;
char line[PROPERTY_VALUE_MAX];
ensure_path_mounted(config_file);
FILE *fp = fopen(config_file, "rb");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
if (strstr(line, key) != NULL && strncmp(line, key, strlen(key)) == 0 && line[strlen(key)] == '=') {
strcpy(value, strstr(line, "=") + 1);
if (value[strlen(value)-1] == '\n')
value[strlen(value)-1] = '\0';
if (value[0] != '\0') {
fclose(fp);
LOGI("%s=%s\n", key, value);
return ret;
}
}
}
ret = 1;
fclose(fp);
} else {
LOGI("Cannot open %s\n", config_file);
ret = -1;
}
strcpy(value, value_def);
LOGI("%s set to default (%s)\n", key, value_def);
return ret;
}
// set value of key in config file
int write_config_file(const char* config_file, const char* key, const char* value) {
if (ensure_path_mounted(config_file) != 0) {
LOGE("Cannot mount path for settings file: %s\n", config_file);
return -1;
}
char config_file_tmp[PATH_MAX];
char tmp[PATH_MAX];
sprintf(config_file_tmp, "%s.tmp", config_file);
sprintf(tmp, "%s", DirName(config_file_tmp));
ensure_directory(tmp);
delete_a_file(config_file_tmp);
FILE *f_tmp = fopen(config_file_tmp, "wb");
if (f_tmp == NULL) {
LOGE("failed to create temporary settings file!\n");
return -1;
}
FILE *fp = fopen(config_file, "rb");
if (fp == NULL) {
// we need to create a new settings file: write an info header
const char* header[] = {
"#PhilZ Touch Settings File\n",
"#Edit only in appropriate UNIX format (Notepad+++...)\n",
"#Entries are in the form of:\n",
"#key=value\n",
"#Do not add spaces in between!\n",
"\n",
NULL
};
int i;
for(i = 0; header[i] != NULL; i++) {
fwrite(header[i], 1, strlen(header[i]), f_tmp);
}
} else {
// parse existing config file and write new temporary file.
char line[PROPERTY_VALUE_MAX];