实现虚拟磁盘驱动
文章目錄
- 1 實現虛擬磁盤驅動
1 實現虛擬磁盤驅動
為了方便,我們直接使用虛擬磁盤文件:
代碼實現如下:
driver.c:
/*** 本源碼配套的課程為 - 從0到1動手寫FAT32文件系統。每個例程對應一個課時,盡可能注釋。* 作者:李述銅* 課程網址:http://01ketang.cc* 版權聲明:本源碼非開源,二次開發,或其它商用前請聯系作者。*/ #include <stdio.h> #include "xdisk.h" #include "xfat.h"/*** 初始化磁盤設備* @param disk 初始化的設備* @param name 設備的名稱* @return*/ static xfat_err_t xdisk_hw_open(xdisk_t *disk, void * init_data) {const char * path = (const char *)init_data;FILE * file = fopen(path, "rb+");if (file == NULL) {printf("open disk failed:%s\n", path);return FS_ERR_IO;}disk->data = file;disk->sector_size = 512;fseek(file, 0, SEEK_END);disk->total_sector = ftell(file) / disk->sector_size;return FS_ERR_OK; }/*** 關閉存儲設備* @param disk* @return*/ static xfat_err_t xdisk_hw_close(xdisk_t * disk) {FILE * file = (FILE *)disk->data;fclose(file);return FS_ERR_OK; }/*** 從設備中讀取指定扇區數量的數據* @param disk 讀取的磁盤* @param buffer 讀取數據存儲的緩沖區* @param start_sector 讀取的起始扇區* @param count 讀取的扇區數量* @return*/ static xfat_err_t xdisk_hw_read_sector(xdisk_t *disk, u8_t *buffer, u32_t start_sector, u32_t count) {u32_t offset = start_sector * disk->sector_size;FILE * file = (FILE *)disk->data;int err = fseek(file, offset, SEEK_SET);if (err == -1) {printf("seek disk failed:0x%x\n", (int)offset);return FS_ERR_IO;}err = (int)fread(buffer, disk->sector_size, count, file);if (err == -1) {printf("read disk failed:sector:%d, count:%d\n",(int)start_sector, (int)count);return FS_ERR_IO;}return FS_ERR_OK; }/*** 向設備中寫指定的扇區數量的數據* @param disk 寫入的存儲設備* @param buffer 數據源緩沖區* @param start_sector 寫入的起始扇區* @param count 寫入的扇區數* @return*/ static xfat_err_t xdisk_hw_write_sector(xdisk_t *disk, u8_t *buffer, u32_t start_sector, u32_t count) {u32_t offset = start_sector * disk->sector_size;FILE * file = (FILE *)disk->data;int err = fseek(file, offset, SEEK_SET);if (err == -1) {printf("seek disk failed: 0x%x\n", (int)offset);return FS_ERR_IO;}err = (int)fwrite(buffer, disk->sector_size, count, file);if (err == -1) {printf("write disk failed:sector:%d, count:%d\n",(int)start_sector, (int)count);return FS_ERR_IO;}// 刷新一下,即時寫入fflush(file);return FS_ERR_OK; }/*** 虛擬磁盤驅動結構*/ xdisk_driver_t vdisk_driver = {.open = xdisk_hw_open,.close = xdisk_hw_close,.read_sector = xdisk_hw_read_sector,.write_sector = xdisk_hw_write_sector, };測試文件:
/*** 本源碼配套的課程為 - 從0到1動手寫FAT32文件系統。每個例程對應一個課時,盡可能注釋。* 作者:李述銅* 課程網址:http://01ketang.cc* 版權聲明:本源碼非開源,二次開發,或其它商用前請聯系作者。*/ #include <stdio.h> #include <string.h> #include "xdisk.h" #include "xfat.h"extern xdisk_driver_t vdisk_driver;const char * disk_path_test = "disk_test.img";static u32_t write_buffer[160*1024]; static u32_t read_buffer[160*1024];// io測試,測試通過要注意關掉 int disk_io_test (void) {int err;xdisk_t disk_test;disk_test.driver = &vdisk_driver;disk_test.sector_size = 512;memset(read_buffer, 0, sizeof(read_buffer));err = disk_test.driver->open(&disk_test, (void *)disk_path_test);if (err) {printf("open disk failed!\n");return -1;}err = disk_test.driver->write_sector(&disk_test, (u8_t *)write_buffer, 0, 2);if (err) {printf("disk write failed!\n");return -1;}err = disk_test.driver->read_sector(&disk_test, (u8_t *)read_buffer, 0, 2);if (err) {printf("disk read failed!\n");return -1;}err = memcmp((u8_t *)read_buffer, (u8_t *)write_buffer, disk_test.sector_size * 2);if (err != 0) {printf("data no equal!\n");return -1;}err = disk_test.driver->close(&disk_test);if (err) {printf("disk close failed!\n");return -1;}printf("disk io test ok!\n");return 0; }int main (void) {xfat_err_t err;int i;for (i = 0; i < sizeof(write_buffer) / sizeof(u32_t); i++) {write_buffer[i] = i;}err = disk_io_test();if (err) return err;printf("Test End!\n");return 0; }參考資料:
總結
- 上一篇: Qt中TCP服务端编程
- 下一篇: 华谊兄弟被强制执行3亿 到底发生了什么