• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2020 Hyunchul Lee <hyc.lee@gmail.com>
4  */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9 
10 #include "exfat_ondisk.h"
11 #include "libexfat.h"
12 #include "repair.h"
13 #include "exfat_fs.h"
14 #include "exfat_dir.h"
15 #include "fsck.h"
16 
17 struct exfat_repair_problem {
18 	er_problem_code_t	prcode;
19 	unsigned int		flags;
20 	unsigned int		prompt_type;
21 	unsigned int		default_number;
22 	unsigned int		bypass_number;
23 	unsigned int		max_number;
24 };
25 
26 /* Problem flags */
27 #define ERF_PREEN_YES		0x00000001
28 #define ERF_DEFAULT_YES		0x00000002
29 #define ERF_DEFAULT_NO		0x00000004
30 
31 /* Prompt types */
32 #define ERP_FIX			0x00000001
33 #define ERP_TRUNCATE		0x00000002
34 #define ERP_DELETE		0x00000003
35 #define ERP_RENAME		0x00000004
36 
37 static const char *prompts[] = {
38 	"Repair",
39 	"Fix",
40 	"Truncate",
41 	"Delete",
42 	"Select",
43 };
44 
45 static struct exfat_repair_problem problems[] = {
46 	{ER_BS_CHECKSUM, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
47 	{ER_BS_BOOT_REGION, 0, ERP_FIX, 0, 0, 0},
48 	{ER_DE_CHECKSUM, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
49 	{ER_DE_UNKNOWN, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
50 	{ER_DE_FILE, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
51 	{ER_DE_SECONDARY_COUNT, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
52 	{ER_DE_STREAM, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
53 	{ER_DE_NAME, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
54 	{ER_DE_NAME_HASH, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
55 	{ER_DE_NAME_LEN, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
56 	{ER_DE_DOT_NAME, ERF_PREEN_YES, ERP_RENAME, 2, 3, 4},
57 	{ER_FILE_VALID_SIZE, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
58 	{ER_FILE_INVALID_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
59 	{ER_FILE_FIRST_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
60 	{ER_FILE_SMALLER_SIZE, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
61 	{ER_FILE_LARGER_SIZE, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
62 	{ER_FILE_DUPLICATED_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
63 	{ER_FILE_ZERO_NOFAT, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
64 };
65 
find_problem(er_problem_code_t prcode)66 static struct exfat_repair_problem *find_problem(er_problem_code_t prcode)
67 {
68 	unsigned int i;
69 
70 	for (i = 0; i < sizeof(problems)/sizeof(problems[0]); i++) {
71 		if (problems[i].prcode == prcode) {
72 			return &problems[i];
73 		}
74 	}
75 	return NULL;
76 }
77 
ask_repair(struct exfat_fsck * fsck,struct exfat_repair_problem * pr)78 static int ask_repair(struct exfat_fsck *fsck, struct exfat_repair_problem *pr)
79 {
80 	int repair = 0;
81 	char answer[8];
82 
83 	if (fsck->options & FSCK_OPTS_REPAIR_NO ||
84 	    pr->flags & ERF_DEFAULT_NO)
85 		repair = 0;
86 	else if (fsck->options & FSCK_OPTS_REPAIR_YES ||
87 		 pr->flags & ERF_DEFAULT_YES)
88 		repair = 1;
89 	else {
90 		if (fsck->options & FSCK_OPTS_REPAIR_ASK) {
91 			do {
92 				if (pr->prompt_type & ERP_RENAME) {
93 					printf("%s (Number: ?) ",
94 					       prompts[pr->prompt_type]);
95 				} else {
96 					printf(". %s (y/N)? ",
97 					       prompts[pr->prompt_type]);
98 				}
99 				fflush(stdout);
100 
101 				if (!fgets(answer, sizeof(answer), stdin))
102 					continue;
103 
104 				if (pr->prompt_type & ERP_RENAME) {
105 					unsigned int number = atoi(answer);
106 
107 					if (number > 0 && number < pr->max_number)
108 						return number;
109 				} else {
110 					if (strcasecmp(answer, "Y\n") == 0)
111 						return 1;
112 					else if (strcasecmp(answer, "\n") == 0 ||
113 						 strcasecmp(answer, "N\n") == 0)
114 						return 0;
115 				}
116 			} while (1);
117 		} else if (fsck->options & FSCK_OPTS_REPAIR_AUTO &&
118 			   pr->flags & ERF_PREEN_YES)
119 			repair = 1;
120 	}
121 
122 	if (pr->prompt_type & ERP_RENAME) {
123 		int print_num = repair ? pr->default_number : pr->bypass_number;
124 
125 		printf("%s (Number : %d)\n", prompts[pr->prompt_type],
126 		       print_num);
127 		repair = print_num;
128 	} else {
129 		printf(". %s (y/N)? %c\n", prompts[pr->prompt_type],
130 		       repair ? 'y' : 'n');
131 	}
132 	return repair;
133 }
134 
exfat_repair_ask(struct exfat_fsck * fsck,er_problem_code_t prcode,const char * desc,...)135 int exfat_repair_ask(struct exfat_fsck *fsck, er_problem_code_t prcode,
136 		     const char *desc, ...)
137 {
138 	struct exfat_repair_problem *pr = NULL;
139 	va_list ap;
140 	int repair;
141 
142 	pr = find_problem(prcode);
143 	if (!pr) {
144 		exfat_err("unknown problem code. %#x\n", prcode);
145 		return 0;
146 	}
147 
148 	va_start(ap, desc);
149 	vprintf(desc, ap);
150 	va_end(ap);
151 
152 	repair = ask_repair(fsck, pr);
153 	if (repair) {
154 		if (pr->prompt_type & ERP_TRUNCATE)
155 			fsck->dirty_fat = true;
156 		fsck->dirty = true;
157 	}
158 	return repair;
159 }
160