read_bb_file.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * read_bb_file.c --- read a list of bad blocks from a FILE *
  3. *
  4. * Copyright (C) 1994, 1995, 2000 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #if HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #if HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. /*
  27. * Reads a list of bad blocks from a FILE *
  28. */
  29. errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
  30. ext2_badblocks_list *bb_list,
  31. void *priv_data,
  32. void (*invalid)(ext2_filsys fs,
  33. blk_t blk,
  34. char *badstr,
  35. void *priv_data))
  36. {
  37. errcode_t retval;
  38. blk_t blockno;
  39. int count;
  40. char buf[128];
  41. if (fs)
  42. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  43. if (!*bb_list) {
  44. retval = ext2fs_badblocks_list_create(bb_list, 10);
  45. if (retval)
  46. return retval;
  47. }
  48. while (!feof (f)) {
  49. if (fgets(buf, sizeof(buf), f) == NULL)
  50. break;
  51. count = sscanf(buf, "%u", &blockno);
  52. if (count <= 0)
  53. continue;
  54. if (fs &&
  55. ((blockno < fs->super->s_first_data_block) ||
  56. (blockno >= fs->super->s_blocks_count))) {
  57. if (invalid)
  58. (invalid)(fs, blockno, buf, priv_data);
  59. continue;
  60. }
  61. retval = ext2fs_badblocks_list_add(*bb_list, blockno);
  62. if (retval)
  63. return retval;
  64. }
  65. return 0;
  66. }
  67. static void call_compat_invalid(ext2_filsys fs, blk_t blk,
  68. char *badstr EXT2FS_ATTR((unused)),
  69. void *priv_data)
  70. {
  71. void (*invalid)(ext2_filsys, blk_t);
  72. invalid = (void (*)(ext2_filsys, blk_t)) priv_data;
  73. if (invalid)
  74. invalid(fs, blk);
  75. }
  76. /*
  77. * Reads a list of bad blocks from a FILE *
  78. */
  79. errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
  80. ext2_badblocks_list *bb_list,
  81. void (*invalid)(ext2_filsys fs, blk_t blk))
  82. {
  83. return ext2fs_read_bb_FILE2(fs, f, bb_list, (void *) invalid,
  84. call_compat_invalid);
  85. }