Просмотр исходного кода

Better error handling on disk read

Johannes Zellner 8 месяцев назад
Родитель
Сommit
1fc4783910
4 измененных файлов с 19 добавлено и 6 удалено
  1. 7 2
      cat.c
  2. 4 1
      fat.c
  3. 1 1
      fat.h
  4. 7 2
      ls.c

+ 7 - 2
cat.c

@@ -44,8 +44,13 @@ void program_cat_main(const char *filename)
 		return;
 	}
 	to_8_3(filename, 64, name_8_3);
-	if (fat_init() != 0) {
-		io_putstr("cat: fat init failed\n");
+	n = fat_init();
+	if (n == -2) {
+		io_putstr("cat: disk read error\n");
+		return;
+	}
+	if (n != 0) {
+		io_putstr("cat: no FAT filesystem found\n");
 		return;
 	}
 	n = fat_read_file(name_8_3, buf, DUMP_BUF_SIZE);

+ 4 - 1
fat.c

@@ -14,6 +14,7 @@
 #define BPB_NUM_FATS        0x10
 #define BPB_ROOT_ENTRIES    0x11
 #define BPB_SEC_PER_FAT     0x16
+#define BPB_SIGNATURE       0x1FE   /* 0x55 0xAA at end of boot sector */
 
 static unsigned int fat_reserved;
 static unsigned int fat_root_start;
@@ -49,7 +50,9 @@ int fat_init(void)
 	unsigned int num_fats, root_entries, sec_per_fat;
 
 	if (fat_read_sector(0) != 0)
-		return -1;
+		return -2;  /* disk I/O error */
+	if (sector_buf[BPB_SIGNATURE] != 0x55 || sector_buf[BPB_SIGNATURE + 1] != 0xAA)
+		return -1;  /* no valid FAT filesystem */
 	fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
 	num_fats = sector_buf[BPB_NUM_FATS];
 	root_entries = (unsigned int)sector_buf[BPB_ROOT_ENTRIES] | ((unsigned int)sector_buf[BPB_ROOT_ENTRIES + 1] << 8);

+ 1 - 1
fat.h

@@ -7,7 +7,7 @@
 typedef void (*fat_root_callback_t)(const char *name, int is_dir, unsigned int size);
 
 /* Parse BPB from sector 0. Call once before list/read.
- * Returns 0 on success, -1 on error. */
+ * Returns 0 on success, -1 if no valid FAT filesystem, -2 on disk I/O error. */
 int fat_init(void);
 
 /* Iterate root directory, call callback for each file/dir.

+ 7 - 2
ls.c

@@ -38,8 +38,13 @@ void program_ls_main(void)
 {
 	int r;
 
-	if (fat_init() != 0) {
-		io_putstr("ls: fat init failed\n");
+	r = fat_init();
+	if (r == -2) {
+		io_putstr("ls: disk read error\n");
+		return;
+	}
+	if (r != 0) {
+		io_putstr("ls: no FAT filesystem found\n");
 		return;
 	}
 	r = fat_list_root(list_cb);