From eb68016c5442cbab4887d435ac4819be61001ff7 Mon Sep 17 00:00:00 2001 From: relan Date: Sat, 11 Mar 2017 09:33:46 +0300 Subject: [PATCH] Repairing: add repair hooks file. --- libexfat/Makefile.am | 1 + libexfat/exfat.h | 6 ++++++ libexfat/repair.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 libexfat/repair.c diff --git a/libexfat/Makefile.am b/libexfat/Makefile.am index 4ec9df5..d639e13 100644 --- a/libexfat/Makefile.am +++ b/libexfat/Makefile.am @@ -33,6 +33,7 @@ libexfat_a_SOURCES = \ mount.c \ node.c \ platform.h \ + repair.c \ time.c \ utf.c \ utils.c diff --git a/libexfat/exfat.h b/libexfat/exfat.h index e7022ca..ff68465 100644 --- a/libexfat/exfat.h +++ b/libexfat/exfat.h @@ -64,6 +64,9 @@ #define BMAP_CLR(bitmap, index) \ ((bitmap)[BMAP_BLOCK(index)] &= ~BMAP_MASK(index)) +#define EXFAT_REPAIR(hook, ef, ...) \ + (exfat_ask_to_fix(ef) && exfat_fix_ ## hook(ef, __VA_ARGS__)) + /* The size of off_t type must be 64 bits. File systems larger than 2 GB will be corrupted with 32-bit off_t. */ STATIC_ASSERT(sizeof(off_t) == 8); @@ -139,6 +142,7 @@ struct exfat_human_bytes }; extern int exfat_errors; +extern int exfat_errors_fixed; void exfat_bug(const char* format, ...) PRINTF NORETURN; void exfat_error(const char* format, ...) PRINTF; @@ -229,4 +233,6 @@ void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time, uint8_t* centisec); void exfat_tzset(void); +bool exfat_ask_to_fix(const struct exfat* ef); + #endif /* ifndef EXFAT_H_INCLUDED */ diff --git a/libexfat/repair.c b/libexfat/repair.c new file mode 100644 index 0000000..10ec865 --- /dev/null +++ b/libexfat/repair.c @@ -0,0 +1,61 @@ +/* + repair.c (09.03.17) + exFAT file system implementation library. + + Free exFAT implementation. + Copyright (C) 2010-2018 Andrew Nayenko + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "exfat.h" +#include + +int exfat_errors_fixed; + +bool exfat_ask_to_fix(const struct exfat* ef) +{ + const char* question = "Fix (Y/N)?"; + char answer[8]; + bool yeah, nope; + + switch (ef->repair) + { + case EXFAT_REPAIR_NO: + return false; + case EXFAT_REPAIR_YES: + printf("%s %s", question, "Y\n"); + return true; + case EXFAT_REPAIR_ASK: + do + { + printf("%s ", question); + fflush(stdout); + if (fgets(answer, sizeof(answer), stdin)) + { + yeah = strcasecmp(answer, "Y\n") == 0; + nope = strcasecmp(answer, "N\n") == 0; + } + else + { + yeah = false; + nope = true; + } + } + while (!yeah && !nope); + return yeah; + } + exfat_bug("invalid repair option value: %d", ef->repair); +} -- 2.11.0