]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Lib/exfat/src/libexfat/repair.c
Fix a bug when install Ventoy if the USB already mounted at a path that contains...
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Lib / exfat / src / libexfat / repair.c
1 /*
2 repair.c (09.03.17)
3 exFAT file system implementation library.
4
5 Free exFAT implementation.
6 Copyright (C) 2010-2018 Andrew Nayenko
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "exfat.h"
24 #include <strings.h>
25
26 int exfat_errors_fixed;
27
28 bool exfat_ask_to_fix(const struct exfat* ef)
29 {
30 const char* question = "Fix (Y/N)?";
31 char answer[8];
32 bool yeah, nope;
33
34 switch (ef->repair)
35 {
36 case EXFAT_REPAIR_NO:
37 return false;
38 case EXFAT_REPAIR_YES:
39 printf("%s %s", question, "Y\n");
40 return true;
41 case EXFAT_REPAIR_ASK:
42 do
43 {
44 printf("%s ", question);
45 fflush(stdout);
46 if (fgets(answer, sizeof(answer), stdin))
47 {
48 yeah = strcasecmp(answer, "Y\n") == 0;
49 nope = strcasecmp(answer, "N\n") == 0;
50 }
51 else
52 {
53 yeah = false;
54 nope = true;
55 }
56 }
57 while (!yeah && !nope);
58 return yeah;
59 }
60 exfat_bug("invalid repair option value: %d", ef->repair);
61 return false;
62 }
63
64 bool exfat_fix_invalid_vbr_checksum(const struct exfat* ef, void* sector,
65 uint32_t vbr_checksum)
66 {
67 size_t i;
68 off_t sector_size = SECTOR_SIZE(*ef->sb);
69
70 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
71 ((le32_t*) sector)[i] = cpu_to_le32(vbr_checksum);
72 if (exfat_pwrite(ef->dev, sector, sector_size, 11 * sector_size) < 0)
73 {
74 exfat_error("failed to write correct VBR checksum");
75 return false;
76 }
77 exfat_errors_fixed++;
78 return true;
79 }
80
81 bool exfat_fix_invalid_node_checksum(const struct exfat* ef,
82 struct exfat_node* node)
83 {
84 /* checksum will be rewritten by exfat_flush_node() */
85 node->is_dirty = true;
86
87 exfat_errors_fixed++;
88 return true;
89 }
90
91 bool exfat_fix_unknown_entry(struct exfat* ef, struct exfat_node* dir,
92 const struct exfat_entry* entry, off_t offset)
93 {
94 struct exfat_entry deleted = *entry;
95
96 deleted.type &= ~EXFAT_ENTRY_VALID;
97 if (exfat_generic_pwrite(ef, dir, &deleted, sizeof(struct exfat_entry),
98 offset) != sizeof(struct exfat_entry))
99 return false;
100
101 exfat_errors_fixed++;
102 return true;
103 }