read_bb_file.c 2.0 KB

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