1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2020 Hyunchul Lee <hyc.lee@gmail.com>
9 #include "exfat_ondisk.h"
14 struct exfat_repair_problem {
15 er_problem_code_t prcode;
17 unsigned int prompt_type;
21 #define ERF_PREEN_YES 0x00000001
22 #define ERF_DEFAULT_YES 0x00000002
23 #define ERF_DEFAULT_NO 0x00000004
26 #define ERP_FIX 0x00000001
27 #define ERP_TRUNCATE 0x00000002
29 static const char *prompts[] = {
35 static struct exfat_repair_problem problems[] = {
36 {ER_BS_CHECKSUM, ERF_PREEN_YES, ERP_FIX},
37 {ER_BS_BOOT_REGION, 0, ERP_FIX},
38 {ER_DE_CHECKSUM, ERF_PREEN_YES, ERP_FIX},
39 {ER_FILE_VALID_SIZE, ERF_PREEN_YES, ERP_FIX},
40 {ER_FILE_INVALID_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
41 {ER_FILE_FIRST_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
42 {ER_FILE_SMALLER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
43 {ER_FILE_LARGER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
44 {ER_FILE_DUPLICATED_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
45 {ER_FILE_ZERO_NOFAT, ERF_PREEN_YES, ERP_FIX},
48 static struct exfat_repair_problem *find_problem(er_problem_code_t prcode)
52 for (i = 0; i < sizeof(problems)/sizeof(problems[0]); i++) {
53 if (problems[i].prcode == prcode) {
60 static bool ask_repair(struct exfat *exfat, struct exfat_repair_problem *pr)
65 if (exfat->options & FSCK_OPTS_REPAIR_NO ||
66 pr->flags & ERF_DEFAULT_NO)
68 else if (exfat->options & FSCK_OPTS_REPAIR_YES ||
69 pr->flags & ERF_DEFAULT_YES)
72 if (exfat->options & FSCK_OPTS_REPAIR_ASK) {
74 printf(". %s (y/N)? ",
75 prompts[pr->prompt_type]);
78 if (fgets(answer, sizeof(answer), stdin)) {
79 if (strcasecmp(answer, "Y\n") == 0)
81 else if (strcasecmp(answer, "\n") == 0
82 || strcasecmp(answer, "N\n") == 0)
86 } else if (exfat->options & FSCK_OPTS_REPAIR_AUTO &&
87 pr->flags & ERF_PREEN_YES)
91 printf(". %s (y/N)? %c\n", prompts[pr->prompt_type],
96 bool exfat_repair_ask(struct exfat *exfat, er_problem_code_t prcode,
97 const char *desc, ...)
99 struct exfat_repair_problem *pr = NULL;
102 pr = find_problem(prcode);
104 exfat_err("unknown problem code. %#x\n", prcode);
112 if (ask_repair(exfat, pr)) {
113 if (pr->prompt_type & ERP_TRUNCATE)
114 exfat->dirty_fat = true;