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_DEFAULT_YES | ERF_PREEN_YES, ERP_FIX},
37 {ER_DE_CHECKSUM, ERF_DEFAULT_YES | ERF_PREEN_YES, ERP_FIX},
38 {ER_FILE_VALID_SIZE, ERF_DEFAULT_YES | ERF_PREEN_YES, ERP_FIX},
39 {ER_FILE_INVALID_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
40 {ER_FILE_FIRST_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
41 {ER_FILE_SMALLER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
42 {ER_FILE_LARGER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
43 {ER_FILE_DUPLICATED_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
44 {ER_FILE_ZERO_NOFAT, ERF_DEFAULT_YES | ERF_PREEN_YES, ERP_FIX},
47 static struct exfat_repair_problem *find_problem(er_problem_code_t prcode)
51 for (i = 0; i < sizeof(problems)/sizeof(problems[0]); i++) {
52 if (problems[i].prcode == prcode) {
59 static bool ask_repair(struct exfat *exfat, struct exfat_repair_problem *pr)
64 if (exfat->options & FSCK_OPTS_REPAIR_NO ||
65 pr->flags & ERF_DEFAULT_NO)
67 else if (exfat->options & FSCK_OPTS_REPAIR_YES ||
68 pr->flags & ERF_DEFAULT_YES)
71 if (exfat->options & FSCK_OPTS_REPAIR_ASK) {
73 printf(". %s (y/N)? ",
74 prompts[pr->prompt_type]);
77 if (fgets(answer, sizeof(answer), stdin)) {
78 if (strcasecmp(answer, "Y\n") == 0)
80 else if (strcasecmp(answer, "\n") == 0
81 || strcasecmp(answer, "N\n") == 0)
85 } else if (exfat->options & FSCK_OPTS_REPAIR_AUTO &&
86 pr->flags & ERF_PREEN_YES)
90 printf(". %s (y/N)? %c\n", prompts[pr->prompt_type],
95 bool exfat_repair_ask(struct exfat *exfat, er_problem_code_t prcode,
96 const char *desc, ...)
98 struct exfat_repair_problem *pr = NULL;
101 pr = find_problem(prcode);
103 exfat_err("unknown problem code. %#x\n", prcode);
111 if (ask_repair(exfat, pr)) {
112 if (pr->prompt_type & ERP_TRUNCATE)
113 exfat->dirty_fat = true;