0
我有這個程序從LBA(邏輯塊地址)讀取數據,但每次無論我提供的LBA號碼,它都會給出相同的輸出。如何驗證從LBA中讀取的數據是正確的?
我如何驗證它?
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/fs.h>
//#include "common.h"
typedef unsigned long long int var64;
int getSectorSize(int handle)
{
int sectorSize = 0;
//get the physical sector size of the disk
if (ioctl(handle, BLKSSZGET, §orSize)) {
printf("getSectorSize: Reading physical sector size failed.\n");
sectorSize = 512;
}
return sectorSize;
}
var64 readLBA(int handle, var64 lba, void* buf, var64 bytes)
{
int ret = 0;
int sectorSize = getSectorSize(handle);
var64 offset = lba * sectorSize;
printf("readFromLBA: entered.\n");
lseek64(handle, offset, SEEK_SET);
ret = read(handle, buf, bytes);
if(ret != bytes) {
printf("read LBA: read failed.\n");
return -1;
}
printf("read LBA: retval: %lld.\n", ret);
return ret;
}
int main()
{
int sectorSize, fd;
char buff[100];
printf("Calling getSectorSize\n");
fd = open("/dev/sda1", O_RDONLY);
if(fd == -1)
{
printf("open /dev/sda1 failed");
exit(1);
}
sectorSize = getSectorSize(fd);
printf("Sector size = %u\n", sectorSize);
memset(buff, 0, sizeof(buff));
readLBA(fd, 1, buff, 2); // if i put the 2nd arg as -12378 gives same answer
}
這裏是輸出:
sles10-sp3:~ # gcc getSectorSizeMain.c
getSectorSizeMain.c: In function ‘main’:
getSectorSizeMain.c:75: warning: incompatible implicit declaration of built-in function ‘memset’
sles10-sp3:~ # ./a.out
Calling getSectorSize
Sector size = 512
read LBA: entered.
read LBA: retval: 8589934594. // This is always constant, how to validate? If i tell to read an invalid LBA number like -123456 the answer remains same. How to validate?
謝謝,它打印一些有意義的十六進制(buff),但如果我把number_of_bytes讀(第三個參數)從LBA讀到2或12,爲什麼buff參數不會增加?我的意思是提到第三個參數,因爲13應該讀取來自LBA的13個參數,但它打印的是十六進制的相同數字位數(數據讀取:bfb31868在13情況下)數據讀取:bfb61098在3情況下。 – kingsmasher1 2012-07-06 10:53:02
@ kingsmasher1:您必須自己指定要打印的字節數 - printf()說明符全部爲下一個要打印的值假設特定的類型**和**大小(例如,%x將接下來的4個字節並將它們解釋爲一個int值)。 – 2012-07-06 11:01:53
你的意思是打印爲buff [0],buff [1],就像那樣?我只是打印爲:'printf(「%x」,buff);'它打印類似bfb61098的東西,但是如果我打印buff [0],buff [1]就打印1,0。我猜這兩個都不正確。 – kingsmasher1 2012-07-06 11:18:28