summaryrefslogtreecommitdiffstats
path: root/lib/ext2fs/read_bb_file.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 09:25:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 09:25:10 +0000
commit5dced3d1b3deca80e01415a2e35dc7972dcbfae7 (patch)
tree6a403684e0978f0287d7f0ec0e5aab1fd31a59e1 /lib/ext2fs/read_bb_file.c
parentInitial commit. (diff)
downloade2fsprogs-5dced3d1b3deca80e01415a2e35dc7972dcbfae7.tar.xz
e2fsprogs-5dced3d1b3deca80e01415a2e35dc7972dcbfae7.zip
Adding upstream version 1.47.0.upstream/1.47.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/ext2fs/read_bb_file.c')
-rw-r--r--lib/ext2fs/read_bb_file.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/lib/ext2fs/read_bb_file.c b/lib/ext2fs/read_bb_file.c
new file mode 100644
index 0000000..a6d3beb
--- /dev/null
+++ b/lib/ext2fs/read_bb_file.c
@@ -0,0 +1,109 @@
+/*
+ * read_bb_file.c --- read a list of bad blocks from a FILE *
+ *
+ * Copyright (C) 1994, 1995, 2000 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
+ * %End-Header%
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <fcntl.h>
+#include <time.h>
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#include "ext2_fs.h"
+#include "ext2fs.h"
+
+/*
+ * Reads a list of bad blocks from a FILE *
+ */
+errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
+ ext2_badblocks_list *bb_list,
+ void *priv_data,
+ void (*invalid)(ext2_filsys fs,
+ blk_t blk,
+ char *badstr,
+ void *priv_data))
+{
+ errcode_t retval;
+ unsigned long long blockno;
+ int count;
+ char buf[128];
+
+ if (fs)
+ EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
+
+ if (!*bb_list) {
+ retval = ext2fs_badblocks_list_create(bb_list, 10);
+ if (retval)
+ return retval;
+ }
+
+ while (!feof (f)) {
+ if (fgets(buf, sizeof(buf), f) == NULL)
+ break;
+ count = sscanf(buf, "%llu", &blockno);
+ if (count <= 0)
+ continue;
+ /* Badblocks isn't going to be updated for 64bit */
+ if (blockno >> 32)
+ return EOVERFLOW;
+ if (fs &&
+ ((blockno < fs->super->s_first_data_block) ||
+ (blockno >= ext2fs_blocks_count(fs->super)))) {
+ if (invalid)
+ (invalid)(fs, (blk64_t) blockno, buf, priv_data);
+ continue;
+ }
+ retval = ext2fs_badblocks_list_add(*bb_list, (blk64_t) blockno);
+ if (retval)
+ return retval;
+ }
+ return 0;
+}
+
+struct compat_struct {
+ void (*invalid)(ext2_filsys, blk_t);
+};
+
+static void call_compat_invalid(ext2_filsys fs, blk_t blk,
+ char *badstr EXT2FS_ATTR((unused)),
+ void *priv_data)
+{
+ struct compat_struct *st;
+
+ st = (struct compat_struct *) priv_data;
+ if (st->invalid)
+ (st->invalid)(fs, blk);
+}
+
+
+/*
+ * Reads a list of bad blocks from a FILE *
+ */
+errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
+ ext2_badblocks_list *bb_list,
+ void (*invalid)(ext2_filsys fs, blk_t blk))
+{
+ struct compat_struct st;
+
+ st.invalid = invalid;
+
+ return ext2fs_read_bb_FILE2(fs, f, bb_list, &st,
+ call_compat_invalid);
+}
+
+